[LIB-13] Tournament and game formats for JSON parsing
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / json / format / AbstractPGNFormat.java
1 /*
2  * Copyright (c) 2019-2020. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.scanner.json.format;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.hedgecode.chess.scanner.ChessHogScannerConstants;
23 import org.hedgecode.chess.scanner.entity.PGNEntity;
24 import org.hedgecode.chess.scanner.entity.PGNTag;
25
26 /**
27  * AbstractPGNFormat
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public abstract class AbstractPGNFormat extends AbstractBaseFormat implements PGNEntity {
32
33     private static final String EMPTY = "";
34
35     private static final String MOVES_FORMAT = "%s %s";
36
37     private final Map<PGNTag, String> pgnTags = new HashMap<>();
38     private String pgnMoves;
39
40     protected abstract void assignPGN();
41
42     protected void addTag(PGNTag tag, String value) {
43         pgnTags.put(tag, value);
44     }
45
46     protected void addMoves(String moves) {
47         pgnMoves = moves;
48     }
49
50     private String moves() {
51         return pgnMoves != null ? pgnMoves : EMPTY;
52     }
53
54     private String result() {
55         String result = pgnTags.get(PGNTag.RESULT);
56         return result != null ? result : PGNTag.RESULT.defaultValue();
57     }
58
59     @Override
60     public String pgn() {
61         assignPGN();
62         StringBuilder sb = new StringBuilder();
63         for (PGNTag tag : PGNTag.values()) {
64             String tagValue = pgnTags.get(tag);
65             if (tag.isRequired() || tagValue != null) {
66                 sb.append(
67                         String.format(
68                                 PGNTag.TAG_FORMAT,
69                                 tag.getName(),
70                                 tagValue != null ? tagValue : tag.defaultValue()
71                         )
72                 ).append(
73                         ChessHogScannerConstants.CRLF
74                 );
75             }
76         }
77         sb.append(
78                 ChessHogScannerConstants.CRLF
79         ).append(
80                 String.format(MOVES_FORMAT, moves(), result())
81         ).append(
82                 ChessHogScannerConstants.CRLF
83         );
84         return sb.toString();
85     }
86
87 }