2 * Copyright (c) 2018-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.pgn.format;
19 import java.text.DateFormat;
20 import java.text.SimpleDateFormat;
21 import java.util.Arrays;
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.List;
27 import org.hedgecode.chess.pgn.PGNUtils;
28 import org.hedgecode.chess.pgn.entity.Move;
29 import org.hedgecode.chess.pgn.entity.Tag;
30 import org.hedgecode.chess.pgn.format.wrap.ExportWrapper;
31 import org.hedgecode.chess.pgn.format.wrap.MovesWrapper;
33 import static org.hedgecode.chess.pgn.PGNConstants.*;
38 * @author Dmitry Samoshin aka gotty
40 public abstract class AbstractPGNFormat<Format extends MovesFormat> implements PGNFormat<Format> {
42 private final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
43 private final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
45 private final Map<Tag, String> pgnTags = new HashMap<>();
47 private String pgnMoves;
48 private List<Move> moves;
50 private MovesWrapper wrapper;
52 public AbstractPGNFormat() {
53 this.wrapper = new ExportWrapper();
56 public AbstractPGNFormat(MovesWrapper wrapper) {
57 this.wrapper = wrapper;
61 public void addTag(Tag tag, String value) {
62 pgnTags.put(tag, value);
66 public void addTags(Map<String, String> tags) {
67 for (Tag tag : Tag.tags()) {
68 String value = tags.get(tag.getName());
70 pgnTags.put(tag, value);
76 public String getTag(Tag tag) {
77 return pgnTags.get(tag);
81 public void addMoves(String moves) {
86 public void addMoves(Move[] moves) {
87 this.moves = Arrays.asList(moves);
91 public void addMoves(List<Move> moves) {
96 public String formatDate(Date date) {
97 return dateFormat.format(date);
101 public String formatTime(Date time) {
102 return timeFormat.format(time);
105 protected String formatMoves() {
106 if (pgnMoves == null) {
107 pgnMoves = movesFormat().format(moves);
109 pgnMoves = PGNUtils.stripCrlf(pgnMoves);
120 private String result() {
121 String result = pgnTags.get(Tag.RESULT);
122 return result != null ? result : Tag.RESULT.defaultValue();