2 * Copyright (c) 2018-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.img.piece;
19 import java.awt.image.BufferedImage;
20 import java.util.HashMap;
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;
32 * @author Dmitry Samoshin aka gotty
34 public class PieceSetLoader extends AbstractImageLoader {
36 private static final String PIECES_DIR = ImageConstants.RESOURCE_PIECES_DIR;
38 private static final String[] PIECES_NAMES = PieceSet.getAllPieceNames();
40 private Map<String, PieceSet> pieceSetMap = new HashMap<>();
43 public PieceSet load(String name) throws ImageException {
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);
52 pieceSet = pieceSetMap.get(name);
58 public void unload(String name) {
59 if (Type.STATEFUL.equals(loadType()) && pieceSetMap.containsKey(name)) {
60 pieceSetMap.remove(name);
65 protected void clear() {
69 private PieceSet loadPieces(String piecePath) throws ImageException {
70 PieceSet pieceSet = PieceSet.create();
71 Map<String, BufferedImage> images =
74 new ImageFilter(PIECES_NAMES)
76 for (String name : PIECES_NAMES) {
82 return pieceSet.isFull()
84 : null; // todo: ImageException