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.format;
19 import java.util.Arrays;
21 import static org.hedgecode.chess.scanner.format.PGNConstants.*;
26 * @author Dmitry Samoshin aka gotty
28 public enum TypeMovesFormat {
30 LINE ( new LineMovesFormat() ),
31 WRAP ( new WrapMovesFormat() );
33 private MovesFormat movesFormat;
35 TypeMovesFormat(MovesFormat movesFormat) {
36 this.movesFormat = movesFormat;
39 public String format(Move[] moves) {
40 return movesFormat.format(moves);
43 static class LineMovesFormat extends WrapMovesFormat {
46 super(PGN_MAX_LINE_LENGTH);
51 static class WrapMovesFormat implements MovesFormat {
53 private int lineLength;
56 this(PGN_DEF_LINE_LENGTH);
59 WrapMovesFormat(int length) {
64 public String format(Move[] moves) {
66 int maxLength = lineLength - PGN_CRLF.length();
68 StringBuilder sb = new StringBuilder();
69 for (Move move : moves) {
70 String nextMove = move.ply() % 2 != 0
71 ? String.format(WHITE_MOVE_FORMAT, move.ply() / 2 + 1, move.move())
72 : String.format(BLACK_MOVE_FORMAT, move.move());
73 if (length + nextMove.length() > maxLength) {
78 length += nextMove.length();
80 return sb.toString().trim();