[LIB-9] Add functional for build image chess diagrams
[chesshog.git] / src / main / java / org / hedgecode / chess / img / piece / PieceSet.java
diff --git a/src/main/java/org/hedgecode/chess/img/piece/PieceSet.java b/src/main/java/org/hedgecode/chess/img/piece/PieceSet.java
new file mode 100644 (file)
index 0000000..342ba1a
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2018-2019. 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.img.piece;
+
+import java.awt.image.BufferedImage;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.hedgecode.chess.position.Color;
+import org.hedgecode.chess.position.ColorPiece;
+import org.hedgecode.chess.position.Piece;
+
+/**
+ *
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class PieceSet {
+
+    private Map<ColorPiece, BufferedImage> pieceSetMap = new HashMap<>();
+
+    private static String[] allPieceNames;
+
+    PieceSet() {
+    }
+
+    public void add(String name, BufferedImage image) {
+        if (name != null && name.length() == 2) {
+            name = name.toLowerCase();
+            ColorPiece colorPiece = ColorPiece.getColorPiece(
+                    Color.byLetter(name.charAt(0)),
+                    Piece.byLetter(name.charAt(1))
+            );
+            if (colorPiece != null){
+                add(colorPiece, image);
+            }
+        }
+    }
+
+    public void add(ColorPiece colorPiece, BufferedImage image) {
+        if (pieceSetMap.containsKey(colorPiece)) {
+            pieceSetMap.replace(colorPiece, image);
+        } else {
+            pieceSetMap.put(colorPiece, image);
+        }
+    }
+
+    public BufferedImage get(ColorPiece colorPiece) {
+        return pieceSetMap.get(colorPiece);
+    }
+
+    public boolean isFull() {
+        for (ColorPiece colorPiece : ColorPiece.values()) {
+            if (!pieceSetMap.containsKey(colorPiece)
+                    || (pieceSetMap.get(colorPiece) == null)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public static String[] getAllPieceNames() {
+        if (allPieceNames == null) {
+            List<String> listNames = new ArrayList<>();
+            for (Color color : Color.values()) {
+                for (Piece piece : Piece.values()) {
+                    listNames.add(
+                            String.valueOf(
+                                    new char[] {color.letter(), piece.letter()}
+                            )
+                    );
+                }
+            }
+            allPieceNames = listNames.toArray(new String[listNames.size()]);
+        }
+        return allPieceNames;
+    }
+
+    public static PieceSet create() {
+        return new PieceSet();
+    }
+
+}