[LIB-9] Separate chesshog-hedgefish module
[chesshog.git] / chesshog-format / src / main / java / org / hedgecode / chess / img / piece / PieceSet.java
1 /*
2  * Copyright (c) 2018-2019. 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.img.piece;
18
19 import java.awt.image.BufferedImage;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 import org.hedgecode.chess.position.Color;
26 import org.hedgecode.chess.position.ColorPiece;
27 import org.hedgecode.chess.position.Piece;
28
29 /**
30  *
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public class PieceSet {
35
36     private Map<ColorPiece, BufferedImage> pieceSetMap = new HashMap<>();
37
38     private static String[] allPieceNames;
39
40     PieceSet() {
41     }
42
43     public void add(String name, BufferedImage image) {
44         if (name != null && name.length() == 2) {
45             name = name.toLowerCase();
46             ColorPiece colorPiece = ColorPiece.getColorPiece(
47                     Color.byLetter(name.charAt(0)),
48                     Piece.byLetter(name.charAt(1))
49             );
50             if (colorPiece != null){
51                 add(colorPiece, image);
52             }
53         }
54     }
55
56     public void add(ColorPiece colorPiece, BufferedImage image) {
57         if (pieceSetMap.containsKey(colorPiece)) {
58             pieceSetMap.replace(colorPiece, image);
59         } else {
60             pieceSetMap.put(colorPiece, image);
61         }
62     }
63
64     public BufferedImage get(ColorPiece colorPiece) {
65         return pieceSetMap.get(colorPiece);
66     }
67
68     public boolean isFull() {
69         for (ColorPiece colorPiece : ColorPiece.values()) {
70             if (!pieceSetMap.containsKey(colorPiece)
71                     || (pieceSetMap.get(colorPiece) == null)) {
72                 return false;
73             }
74         }
75         return true;
76     }
77
78     public static String[] getAllPieceNames() {
79         if (allPieceNames == null) {
80             List<String> listNames = new ArrayList<>();
81             for (Color color : Color.values()) {
82                 for (Piece piece : Piece.values()) {
83                     listNames.add(
84                             String.valueOf(
85                                     new char[] {color.letter(), piece.letter()}
86                             )
87                     );
88                 }
89             }
90             allPieceNames = listNames.toArray(new String[listNames.size()]);
91         }
92         return allPieceNames;
93     }
94
95     public static PieceSet create() {
96         return new PieceSet();
97     }
98
99 }