[LIB-9] Add chesshog-qrcode module
[chesshog.git] / chesshog-qrcode / src / main / java / org / hedgecode / chess / qrcode / ChessQRResult.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.qrcode;
18
19 import org.hedgecode.chess.Parsers;
20 import org.hedgecode.chess.game.Game;
21 import org.hedgecode.chess.position.ParseException;
22 import org.hedgecode.chess.position.Parser;
23 import org.hedgecode.chess.position.Position;
24
25 /**
26  * Result storage for Chess-specific QR Codes data.
27  *
28  * @author Dmitry Samoshin aka gotty
29  */
30 public class ChessQRResult {
31
32     private ChessQRCodeMode mode;
33     private String contents;
34     private Position position;
35     private Game game;
36
37     ChessQRResult(ChessQRCodeMode mode, String contents) {
38         this.mode = mode;
39         this.contents = contents;
40     }
41
42     ChessQRResult(Position position) {
43         this.position = position;
44     }
45
46     ChessQRResult(Game game) {
47         this.game = game;
48     }
49
50     public ChessQRCodeMode getMode() {
51         return mode;
52     }
53
54     public String getContents() {
55         return contents;
56     }
57
58     public Position getPosition() throws ChessQRCodeException {
59         if (position == null) {
60             if (mode != null && mode.isPosition()) {
61                 try {
62                     position = getPositionParser().parse(contents);
63                 } catch (ParseException e) {
64                     throw new ChessQRCodeException(
65                             ChessQRCodeException.Type.PARSE, null, e.getMessage()
66                     );
67                 }
68             }
69         }
70         return position;
71     }
72
73     public Game getGame() {
74         // todo
75         return game;
76     }
77
78     private Parser getPositionParser() throws ChessQRCodeException {
79         switch (mode) {
80             case FEN:
81                 return Parsers.FEN.parser();
82             case TCD:
83                 return Parsers.TCD.parser();
84             default:
85                 throw new ChessQRCodeException(
86                         ChessQRCodeException.Type.PARSE, "parse.unknown.position.format"
87                 );
88         }
89     }
90
91 }