2 * Copyright (c) 2018-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.qrcode;
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;
26 * Result storage for chess-specific QR Codes data.
28 * @author Dmitry Samoshin aka gotty
30 public class ChessQRResult {
32 private ChessQRCodeMode mode;
33 private String contents;
34 private Position position;
37 public ChessQRResult(ChessQRCodeMode mode, String contents) {
39 this.contents = contents;
43 ChessQRResult(Position position) {
44 this.position = position;
47 ChessQRResult(Game game) {
52 public ChessQRCodeMode getMode() {
56 public String getContents() {
60 public Position getPosition() throws ChessQRCodeException {
61 if (position == null) {
62 if (mode != null && mode.isPosition()) {
64 position = getPositionParser().parse(contents);
65 } catch (ParseException e) {
66 throw new ChessQRCodeException(
67 ChessQRCodeException.Type.PARSE, null, e.getMessage()
75 public Game getGame() {
80 private Parser getPositionParser() throws ChessQRCodeException {
83 return Parsers.FEN.parser();
85 return Parsers.TCD.parser();
87 throw new ChessQRCodeException(
88 ChessQRCodeException.Type.PARSE, "parse.unknown.position.format"
93 public static ChessQRCodeMode getMode(String contents) {
94 return ChessQRCodeMode.byCode(
95 contents.substring(0, ChessQRCodeMode.CODE_LENGTH)
99 public static String getContents(String contents) {
100 return contents.substring(ChessQRCodeMode.CODE_LENGTH);