import org.hedgecode.chess.img.ImageFilter;
/**
- *
+ * Loader for chess board's set of images.
*
* @author Dmitry Samoshin aka gotty
*/
private static final String SQUARE_DARK = ImageConstants.DARK_SQUARE_FILENAME;
private static final String SQUARE_LIGHT = ImageConstants.LIGHT_SQUARE_FILENAME;
- private static final String[] SQUARE_NAMES = {SQUARE_DARK, SQUARE_LIGHT};
+ private static final String BOARD = ImageConstants.BOARD_FILENAME;
+
+ private static final String[] BOARD_SQUARE_NAMES = { BOARD, SQUARE_DARK, SQUARE_LIGHT };
private Map<String, Board> squareSetMap = new HashMap<>();
@Override
public Board load(String name) throws ImageException {
- Board board = null;
+ Board board;
if (Type.STATELESS.equals(loadType()) || !squareSetMap.containsKey(name)) {
String boardPath = FilenameUtils.getFullPath(SQUARES_DIR, name);
- SquarePair<BufferedImage> squarePair = loadSquares(boardPath);
- if (squarePair != null) {
- board = Board.create(squarePair);
- if (Type.STATEFUL.equals(loadType())) {
- squareSetMap.put(name, board);
- }
+ board = loadBoard(boardPath);
+ if (Type.STATEFUL.equals(loadType())) {
+ squareSetMap.put(name, board);
}
} else {
board = squareSetMap.get(name);
squareSetMap.clear();
}
- private SquarePair<BufferedImage> loadSquares(String boardPath) throws ImageException {
+ private Board loadBoard(String boardPath) throws ImageException {
Map<String, BufferedImage> images =
loadImages(
boardPath,
- new ImageFilter(SQUARE_NAMES)
+ new ImageFilter(BOARD_SQUARE_NAMES)
);
- BufferedImage dark = images.get(SQUARE_DARK);
- BufferedImage light = images.get(SQUARE_LIGHT);
- return (dark != null && light != null)
- ? SquarePair.create(dark, light)
- : null; // todo: ImageException
+ BufferedImage board = images.get(BOARD);
+ SquarePair<BufferedImage> squarePair = SquarePair.create(
+ images.get(SQUARE_DARK), images.get(SQUARE_LIGHT)
+ );
+ if (board == null && !squarePair.isFilled()) {
+ throw new ImageException("image.incomplete.board.set", boardPath);
+ }
+ return board != null
+ ? Board.create(board)
+ : Board.create(squarePair);
}
}