X-Git-Url: https://git.hedgecode.org/?p=chesshog.git;a=blobdiff_plain;f=chesshog-format%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fpgn%2Fformat%2FAbstractPGNFormat.java;fp=chesshog-format%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fpgn%2Fformat%2FAbstractPGNFormat.java;h=b5b0e4743910ce4bfb01d9f52caaaddf555fd90d;hp=0000000000000000000000000000000000000000;hb=5d271f23371d6a435a0435f000370f4d9b8c621a;hpb=569af88b0115065e1a4551a20501bbd200fcba17 diff --git a/chesshog-format/src/main/java/org/hedgecode/chess/pgn/format/AbstractPGNFormat.java b/chesshog-format/src/main/java/org/hedgecode/chess/pgn/format/AbstractPGNFormat.java new file mode 100644 index 0000000..b5b0e47 --- /dev/null +++ b/chesshog-format/src/main/java/org/hedgecode/chess/pgn/format/AbstractPGNFormat.java @@ -0,0 +1,125 @@ +/* + * 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 implements PGNFormat { + + private final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd"); + private final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); + + private final Map pgnTags = new HashMap<>(); + + private String pgnMoves; + private List 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 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 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(); + } + +}