X-Git-Url: https://git.hedgecode.org/?p=chesshog.git;a=blobdiff_plain;f=chesshog-graphics%2Fsrc%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fimg%2Fboard%2FBoardLoader.java;h=e171418554a58d6d357ef9e22964fc4efcd734cd;hp=c61bd98bacab3f8985d5887822046e6d4c780db8;hb=0b24d5b71c19ba38060297084ad39685f76a8539;hpb=cc2bc357ce3c21a0b092af8b2ba3e174d10cac81 diff --git a/chesshog-graphics/src/main/java/org/hedgecode/chess/img/board/BoardLoader.java b/chesshog-graphics/src/main/java/org/hedgecode/chess/img/board/BoardLoader.java index c61bd98..e171418 100644 --- a/chesshog-graphics/src/main/java/org/hedgecode/chess/img/board/BoardLoader.java +++ b/chesshog-graphics/src/main/java/org/hedgecode/chess/img/board/BoardLoader.java @@ -27,7 +27,7 @@ import org.hedgecode.chess.img.ImageException; import org.hedgecode.chess.img.ImageFilter; /** - * + * Loader for chess board's set of images. * * @author Dmitry Samoshin aka gotty */ @@ -38,21 +38,20 @@ public class BoardLoader extends AbstractImageLoader { 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 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 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); @@ -72,17 +71,22 @@ public class BoardLoader extends AbstractImageLoader { squareSetMap.clear(); } - private SquarePair loadSquares(String boardPath) throws ImageException { + private Board loadBoard(String boardPath) throws ImageException { Map 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 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); } }