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.ChessHogScannerConstants;
23 import org.hedgecode.chess.scanner.entity.PGNEntity;
24 import org.hedgecode.chess.scanner.entity.PGNTag;
29 * @author Dmitry Samoshin aka gotty
31 public abstract class AbstractPGNFormat extends AbstractBaseFormat implements PGNEntity {
33 private static final String EMPTY = "";
35 private static final String MOVES_FORMAT = "%s %s";
37 private final Map<PGNTag, String> pgnTags = new HashMap<>();
38 private String pgnMoves;
40 protected abstract void assignPGN();
42 protected void addTag(PGNTag tag, String value) {
43 pgnTags.put(tag, value);
46 protected void addMoves(String moves) {
50 private String moves() {
51 return pgnMoves != null ? pgnMoves : EMPTY;
54 private String result() {
55 String result = pgnTags.get(PGNTag.RESULT);
56 return result != null ? result : PGNTag.RESULT.defaultValue();
62 StringBuilder sb = new StringBuilder();
63 for (PGNTag tag : PGNTag.values()) {
64 String tagValue = pgnTags.get(tag);
65 if (tag.isRequired() || tagValue != null) {
70 tagValue != null ? tagValue : tag.defaultValue()
73 ChessHogScannerConstants.CRLF
78 ChessHogScannerConstants.CRLF
80 String.format(MOVES_FORMAT, moves(), result())
82 ChessHogScannerConstants.CRLF