[LIB-9] Set of entities for PGN parsing and formatting
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / pgn / format / AbstractPGNFormat.java
1 /*
2  * Copyright (c) 2018-2020. Developed by Hedgecode.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.hedgecode.chess.pgn.format;
18
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;
25 import java.util.Map;
26
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;
32
33 import static org.hedgecode.chess.pgn.PGNConstants.*;
34
35 /**
36  * AbstractPGNFormat
37  *
38  * @author Dmitry Samoshin aka gotty
39  */
40 public abstract class AbstractPGNFormat<Format extends MovesFormat> implements PGNFormat<Format> {
41
42     private final DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
43     private final DateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
44
45     private final Map<Tag, String> pgnTags = new HashMap<>();
46
47     private String pgnMoves;
48     private List<Move> moves;
49
50     private MovesWrapper wrapper;
51
52     public AbstractPGNFormat() {
53         this.wrapper = new ExportWrapper();
54     }
55
56     public AbstractPGNFormat(MovesWrapper wrapper) {
57         this.wrapper = wrapper;
58     }
59
60     @Override
61     public void addTag(Tag tag, String value) {
62         pgnTags.put(tag, value);
63     }
64
65     @Override
66     public void addTags(Map<String, String> tags) {
67         for (Tag tag : Tag.tags()) {
68             String value = tags.get(tag.getName());
69             if (value != null) {
70                 pgnTags.put(tag, value);
71             }
72         }
73     }
74
75     @Override
76     public String getTag(Tag tag) {
77         return pgnTags.get(tag);
78     }
79
80     @Override
81     public void addMoves(String moves) {
82         pgnMoves = moves;
83     }
84
85     @Override
86     public void addMoves(Move[] moves) {
87         this.moves = Arrays.asList(moves);
88     }
89
90     @Override
91     public void addMoves(List<Move> moves) {
92         this.moves = moves;
93     }
94
95     @Override
96     public String formatDate(Date date) {
97         return dateFormat.format(date);
98     }
99
100     @Override
101     public String formatTime(Date time) {
102         return timeFormat.format(time);
103     }
104
105     protected String formatMoves() {
106         if (pgnMoves == null) {
107             pgnMoves = movesFormat().format(moves);
108         } else {
109             pgnMoves = PGNUtils.stripCrlf(pgnMoves);
110         }
111         return wrapper.wrap(
112                 pgnMoves.concat(
113                         PGN_SPACE
114                 ).concat(
115                         result()
116                 )
117         );
118     }
119
120     private String result() {
121         String result = pgnTags.get(Tag.RESULT);
122         return result != null ? result : Tag.RESULT.defaultValue();
123     }
124
125 }