[LIB-9] Separate chesshog-hedgefish module
[chesshog.git] / 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.io.File;
20 import java.io.IOException;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import javax.imageio.ImageIO;
25
26 import org.hedgecode.chess.img.AbstractImageLoader;
27 import org.hedgecode.chess.img.ImageConstants;
28 import org.hedgecode.chess.img.ImageException;
29 import org.hedgecode.chess.img.ImageFilter;
30
31 /**
32  *
33  *
34  * @author Dmitry Samoshin aka gotty
35  */
36 public class PieceSetLoader extends AbstractImageLoader {
37
38     private static final File PIECES_PATH = new File(IMAGES_PATH, ImageConstants.RESOURCE_PIECES_DIR);
39
40     private static final String[] PIECES_NAMES = PieceSet.getAllPieceNames();
41
42     private Map<String, PieceSet> pieceSetMap = new HashMap<>();
43
44     @Override
45     public PieceSet load(String name) throws ImageException {
46         PieceSet pieceSet = null;
47         if (Type.STATELESS.equals(loadType()) || !pieceSetMap.containsKey(name)) {
48             File piecePath = new File(PIECES_PATH, name);
49             if (piecePath.exists() && piecePath.isDirectory()) {
50                 pieceSet = loadPieces(piecePath);
51             }
52             if (Type.STATEFUL.equals(loadType()) && pieceSet != null) {
53                 pieceSetMap.put(name, pieceSet);
54             }
55         } else {
56             pieceSet = pieceSetMap.get(name);
57         }
58         return pieceSet;
59     }
60
61     @Override
62     public void unload(String name) {
63         if (Type.STATEFUL.equals(loadType()) && pieceSetMap.containsKey(name)) {
64             pieceSetMap.remove(name);
65         }
66     }
67
68     @Override
69     protected void clear() {
70         pieceSetMap.clear();
71     }
72
73     private PieceSet loadPieces(File piecePath) throws ImageException {
74         PieceSet pieceSet = PieceSet.create();
75         File[] images = piecePath.listFiles(new ImageFilter(PIECES_NAMES));
76         if (images != null) {
77             for (File image : images) {
78                 if (image.isFile()) {
79                     String filename = image.getName();
80                     try {
81                         pieceSet.add(
82                                 filename.substring(0, filename.lastIndexOf('.')),
83                                 ImageIO.read(image)
84                         );
85                     } catch (IOException e) {
86                         throw new ImageException(e.getMessage()); // todo: locale
87                     }
88                 }
89             }
90         }
91         return pieceSet.isFull() ? pieceSet : null;
92     }
93
94 }