2 * Copyright (c) 2019-2020. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.scanner.json.format;
19 import java.util.HashMap;
22 import org.hedgecode.chess.scanner.entity.PGNEntity;
23 import org.hedgecode.chess.scanner.entity.PGNTag;
25 import static org.hedgecode.chess.scanner.ChessHogScannerConstants.*;
30 * @author Dmitry Samoshin aka gotty
32 public abstract class AbstractPGNFormat extends AbstractBaseFormat implements PGNEntity {
34 private static final String EMPTY = "";
36 private static final String MOVES_FORMAT = "%s %s";
38 private final Map<PGNTag, String> pgnTags = new HashMap<>();
39 private String pgnMoves;
41 protected abstract void assignPGN();
43 protected void addTag(PGNTag tag, String value) {
44 pgnTags.put(tag, value);
47 protected void addMoves(String moves) {
51 private String moves() {
52 return pgnMoves != null ? pgnMoves : EMPTY;
55 private String result() {
56 String result = pgnTags.get(PGNTag.RESULT);
57 return result != null ? result : PGNTag.RESULT.defaultValue();
63 StringBuilder sb = new StringBuilder();
64 for (PGNTag tag : PGNTag.values()) {
65 String tagValue = pgnTags.get(tag);
66 if (tag.isRequired() || tagValue != null) {
71 tagValue != null ? tagValue : tag.defaultValue()
76 sb.append(CRLF).append(
77 String.format(MOVES_FORMAT, moves(), result())