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.board;
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;
30 * Loader for chess board's set of images.
32 * @author Dmitry Samoshin aka gotty
34 public class BoardLoader extends AbstractImageLoader {
36 private static final String SQUARES_DIR = ImageConstants.RESOURCE_SQUARES_DIR;
38 private static final String SQUARE_DARK = ImageConstants.DARK_SQUARE_FILENAME;
39 private static final String SQUARE_LIGHT = ImageConstants.LIGHT_SQUARE_FILENAME;
41 private static final String BOARD = ImageConstants.BOARD_FILENAME;
43 private static final String[] BOARD_SQUARE_NAMES = { BOARD, SQUARE_DARK, SQUARE_LIGHT };
45 private Map<String, Board> squareSetMap = new HashMap<>();
48 public Board load(String name) throws ImageException {
50 if (Type.STATELESS.equals(loadType()) || !squareSetMap.containsKey(name)) {
51 String boardPath = FilenameUtils.getFullPath(SQUARES_DIR, name);
52 board = loadBoard(boardPath);
53 if (Type.STATEFUL.equals(loadType())) {
54 squareSetMap.put(name, board);
57 board = squareSetMap.get(name);
63 public void unload(String name) {
64 if (Type.STATEFUL.equals(loadType()) && squareSetMap.containsKey(name)) {
65 squareSetMap.remove(name);
70 protected void clear() {
74 private Board loadBoard(String boardPath) throws ImageException {
75 Map<String, BufferedImage> images =
78 new ImageFilter(BOARD_SQUARE_NAMES)
80 BufferedImage board = images.get(BOARD);
81 SquarePair<BufferedImage> squarePair = SquarePair.create(
82 images.get(SQUARE_DARK), images.get(SQUARE_LIGHT)
84 if (board == null && !squarePair.isFilled()) {
85 throw new ImageException("image.incomplete.board.set", boardPath);
89 : Board.create(squarePair);