[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / board / BoardLoader.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.board;
18
19 import java.awt.image.BufferedImage;
20 import java.util.HashMap;
21 import java.util.Map;
22
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;
28
29 /**
30  * Loader for chess board's set of images.
31  *
32  * @author Dmitry Samoshin aka gotty
33  */
34 public class BoardLoader extends AbstractImageLoader {
35
36     private static final String SQUARES_DIR = ImageConstants.RESOURCE_SQUARES_DIR;
37
38     private static final String SQUARE_DARK = ImageConstants.DARK_SQUARE_FILENAME;
39     private static final String SQUARE_LIGHT = ImageConstants.LIGHT_SQUARE_FILENAME;
40
41     private static final String BOARD = ImageConstants.BOARD_FILENAME;
42
43     private static final String[] BOARD_SQUARE_NAMES = { BOARD, SQUARE_DARK, SQUARE_LIGHT };
44
45     private Map<String, Board> squareSetMap = new HashMap<>();
46
47     @Override
48     public Board load(String name) throws ImageException {
49         Board board;
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);
55             }
56         } else {
57             board = squareSetMap.get(name);
58         }
59         return board;
60     }
61
62     @Override
63     public void unload(String name) {
64         if (Type.STATEFUL.equals(loadType()) && squareSetMap.containsKey(name)) {
65             squareSetMap.remove(name);
66         }
67     }
68
69     @Override
70     protected void clear() {
71         squareSetMap.clear();
72     }
73
74     private Board loadBoard(String boardPath) throws ImageException {
75         Map<String, BufferedImage> images =
76                 loadImages(
77                         boardPath,
78                         new ImageFilter(BOARD_SQUARE_NAMES)
79                 );
80         BufferedImage board = images.get(BOARD);
81         SquarePair<BufferedImage> squarePair = SquarePair.create(
82                 images.get(SQUARE_DARK), images.get(SQUARE_LIGHT)
83         );
84         if (board == null && !squarePair.isFilled()) {
85             throw new ImageException("image.incomplete.board.set", boardPath);
86         }
87         return board != null
88                 ? Board.create(board)
89                 : Board.create(squarePair);
90     }
91
92 }