--- /dev/null
+/*
+ * Copyright (c) 2018-2020. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.pgn.format;
+
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.hedgecode.chess.pgn.PGNUtils;
+import org.hedgecode.chess.pgn.entity.Move;
+import org.hedgecode.chess.pgn.entity.Tag;
+import org.hedgecode.chess.pgn.format.wrap.ExportWrapper;
+import org.hedgecode.chess.pgn.format.wrap.MovesWrapper;
+
+import static org.hedgecode.chess.pgn.PGNConstants.*;
+
+/**
+ * AbstractPGNFormat
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public abstract class AbstractPGNFormat<Format extends MovesFormat> implements PGNFormat<Format> {
+
+ private final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
+ private final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
+
+ private final Map<Tag, String> pgnTags = new HashMap<>();
+
+ private String pgnMoves;
+ private List<Move> moves;
+
+ private MovesWrapper wrapper;
+
+ public AbstractPGNFormat() {
+ this.wrapper = new ExportWrapper();
+ }
+
+ public AbstractPGNFormat(MovesWrapper wrapper) {
+ this.wrapper = wrapper;
+ }
+
+ @Override
+ public void addTag(Tag tag, String value) {
+ pgnTags.put(tag, value);
+ }
+
+ @Override
+ public void addTags(Map<String, String> tags) {
+ for (Tag tag : Tag.tags()) {
+ String value = tags.get(tag.getName());
+ if (value != null) {
+ pgnTags.put(tag, value);
+ }
+ }
+ }
+
+ @Override
+ public String getTag(Tag tag) {
+ return pgnTags.get(tag);
+ }
+
+ @Override
+ public void addMoves(String moves) {
+ pgnMoves = moves;
+ }
+
+ @Override
+ public void addMoves(Move[] moves) {
+ this.moves = Arrays.asList(moves);
+ }
+
+ @Override
+ public void addMoves(List<Move> moves) {
+ this.moves = moves;
+ }
+
+ @Override
+ public String formatDate(Date date) {
+ return dateFormat.format(date);
+ }
+
+ @Override
+ public String formatTime(Date time) {
+ return timeFormat.format(time);
+ }
+
+ protected String formatMoves() {
+ if (pgnMoves == null) {
+ pgnMoves = movesFormat().format(moves);
+ } else {
+ pgnMoves = PGNUtils.stripCrlf(pgnMoves);
+ }
+ return wrapper.wrap(
+ pgnMoves.concat(
+ PGN_SPACE
+ ).concat(
+ result()
+ )
+ );
+ }
+
+ private String result() {
+ String result = pgnTags.get(Tag.RESULT);
+ return result != null ? result : Tag.RESULT.defaultValue();
+ }
+
+}