[LIB-9] Separate chesshog-hedgefish module
[chesshog.git] / src / main / java / org / hedgecode / chess / wiki / Wiki.java
1 /*
2  * Copyright (c) 2018. 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.wiki;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.hedgecode.chess.position.Color;
23 import org.hedgecode.chess.position.ColorPiece;
24 import org.hedgecode.chess.position.Piece;
25
26 /**
27  * Wikipedia chess diagram constants.
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public final class Wiki {
32
33     public static final String CRLF = System.lineSeparator();
34
35     public static final String EMPTY = "  ";
36     public static final String SEPARATOR = "|";
37
38     public static final String TEMPLATE_BEGIN = "{{";
39     public static final String TEMPLATE_END = "}}";
40
41     public static final String WHITE_PAWN   = "pl";
42     public static final String WHITE_KNIGHT = "nl";
43     public static final String WHITE_BISHOP = "bl";
44     public static final String WHITE_ROOK   = "rl";
45     public static final String WHITE_QUEEN  = "ql";
46     public static final String WHITE_KING   = "kl";
47
48     public static final String BLACK_PAWN   = "pd";
49     public static final String BLACK_KNIGHT = "nd";
50     public static final String BLACK_BISHOP = "bd";
51     public static final String BLACK_ROOK   = "rd";
52     public static final String BLACK_QUEEN  = "qd";
53     public static final String BLACK_KING   = "kd";
54
55     private static final Map<ColorPiece, String> PIECES = new HashMap<ColorPiece, String>() {
56         {
57             put(ColorPiece.WHITE_PAWN, WHITE_PAWN);     put(ColorPiece.BLACK_PAWN, BLACK_PAWN);
58             put(ColorPiece.WHITE_KNIGHT, WHITE_KNIGHT); put(ColorPiece.BLACK_KNIGHT, BLACK_KNIGHT);
59             put(ColorPiece.WHITE_BISHOP, WHITE_BISHOP); put(ColorPiece.BLACK_BISHOP, BLACK_BISHOP);
60             put(ColorPiece.WHITE_ROOK, WHITE_ROOK);     put(ColorPiece.BLACK_ROOK, BLACK_ROOK);
61             put(ColorPiece.WHITE_QUEEN, WHITE_QUEEN);   put(ColorPiece.BLACK_QUEEN, BLACK_QUEEN);
62             put(ColorPiece.WHITE_KING, WHITE_KING);     put(ColorPiece.BLACK_KING, BLACK_KING);
63         }
64     };
65
66     private static boolean withTemplate = false;
67
68     private Wiki() {
69     }
70
71     public static boolean withTemplate() {
72         return withTemplate;
73     }
74
75     public static void setTemplate(boolean isTemplate) {
76         withTemplate = isTemplate;
77     }
78
79     public static ColorPiece getColorPiece(String piece) {
80         if (piece != null && !EMPTY.equals(piece)) {
81             for (Map.Entry<ColorPiece, String> entry : PIECES.entrySet()) {
82                 if (entry.getValue().equals(piece))
83                     return entry.getKey();
84             }
85         }
86         return null;
87     }
88
89     public static String getWikiPiece(Color color, Piece piece) {
90         return getWikiPiece(
91                 ColorPiece.getColorPiece(color, piece)
92         );
93     }
94
95     public static String getWikiPiece(ColorPiece colorPiece) {
96         return (colorPiece != null)
97                 ? PIECES.get(colorPiece)
98                 : EMPTY;
99     }
100
101 }