[LIB-9] Add ability to archive qrcode content
[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     public ChessQRResult(ChessQRCodeMode mode, String contents) {
38         this.mode = mode;
39         this.contents = contents;
40     }
41
42 /*
43     ChessQRResult(Position position) {
44         this.position = position;
45     }
46
47     ChessQRResult(Game game) {
48         this.game = game;
49     }
50 */
51
52     public ChessQRCodeMode getMode() {
53         return mode;
54     }
55
56     public String getContents() {
57         return contents;
58     }
59
60     public Position getPosition() throws ChessQRCodeException {
61         if (position == null) {
62             if (mode != null && mode.isPosition()) {
63                 try {
64                     position = getPositionParser().parse(contents);
65                 } catch (ParseException e) {
66                     throw new ChessQRCodeException(
67                             ChessQRCodeException.Type.PARSE, null, e.getMessage()
68                     );
69                 }
70             }
71         }
72         return position;
73     }
74
75     public Game getGame() {
76         // todo
77         return game;
78     }
79
80     private Parser getPositionParser() throws ChessQRCodeException {
81         switch (mode) {
82             case FEN:
83                 return Parsers.FEN.parser();
84             case TCD:
85                 return Parsers.TCD.parser();
86             default:
87                 throw new ChessQRCodeException(
88                         ChessQRCodeException.Type.PARSE, "parse.unknown.position.format"
89                 );
90         }
91     }
92
93     public static ChessQRCodeMode getMode(String contents) {
94         return ChessQRCodeMode.byCode(
95                 contents.substring(0, ChessQRCodeMode.CODE_LENGTH)
96         );
97     }
98
99     public static String getContents(String contents) {
100         return contents.substring(ChessQRCodeMode.CODE_LENGTH);
101     }
102
103 }