[LIB-9] Fix some pom.xml dependencies
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / piece / PieceSetLoader.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.HashMap;
21 import java.util.Map;
22
23 import org.hedgecode.chess.img.AbstractImageLoader;
24 import org.hedgecode.chess.img.FilenameUtils;
25 import org.hedgecode.chess.img.ImageConstants;
26 import org.hedgecode.chess.img.ImageException;
27 import org.hedgecode.chess.img.ImageFilter;
28
29 /**
30  *
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public class PieceSetLoader extends AbstractImageLoader {
35
36     private static final String PIECES_DIR = ImageConstants.RESOURCE_PIECES_DIR;
37
38     private static final String[] PIECES_NAMES = PieceSet.getAllPieceNames();
39
40     private Map<String, PieceSet> pieceSetMap = new HashMap<>();
41
42     @Override
43     public PieceSet load(String name) throws ImageException {
44         PieceSet pieceSet;
45         if (Type.STATELESS.equals(loadType()) || !pieceSetMap.containsKey(name)) {
46             String piecePath = FilenameUtils.getFullPath(PIECES_DIR, name);
47             pieceSet = loadPieces(piecePath);
48             if (Type.STATEFUL.equals(loadType()) && pieceSet != null) {
49                 pieceSetMap.put(name, pieceSet);
50             }
51         } else {
52             pieceSet = pieceSetMap.get(name);
53         }
54         return pieceSet;
55     }
56
57     @Override
58     public void unload(String name) {
59         if (Type.STATEFUL.equals(loadType()) && pieceSetMap.containsKey(name)) {
60             pieceSetMap.remove(name);
61         }
62     }
63
64     @Override
65     protected void clear() {
66         pieceSetMap.clear();
67     }
68
69     private PieceSet loadPieces(String piecePath) throws ImageException {
70         PieceSet pieceSet = PieceSet.create();
71         Map<String, BufferedImage> images =
72                 loadImages(
73                         piecePath,
74                         new ImageFilter(PIECES_NAMES)
75                 );
76         for (String name : PIECES_NAMES) {
77             pieceSet.add(
78                     name,
79                     images.get(name)
80             );
81         }
82         return pieceSet.isFull()
83                 ? pieceSet
84                 : null; // todo: ImageException
85     }
86
87 }