--- /dev/null
+/*
+ * Copyright (c) 2018-2020. Developed by Hedgecode.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.hedgecode.chess.pgn.token;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.hedgecode.chess.ParseException;
+import org.hedgecode.chess.pgn.PGNUtils;
+import org.hedgecode.chess.pgn.entity.Moves;
+
+/**
+ * ResultToken
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public class ResultToken implements Token<Moves> {
+
+ private static final String WHITE_WIN = "1-0";
+ private static final String BLACK_WIN = "0-1";
+ private static final String DRAW_GAME = "1/2-1/2";
+ private static final String PROGRESS_GAME = "*";
+
+ private static final String RESULT_REGEX = String.format(
+ "^\\s*(%s|%s|%s|%s)",
+ WHITE_WIN, BLACK_WIN, DRAW_GAME,
+ PGNUtils.shield(
+ PROGRESS_GAME, PROGRESS_GAME.toCharArray()
+ )
+ );
+ private static final Pattern RESULT_PATTERN = Pattern.compile(RESULT_REGEX);
+ private static final int RESULT_GROUP = 1;
+
+ @Override
+ public int token(Moves moves, String pgn) throws ParseException {
+ Matcher matcher = RESULT_PATTERN.matcher(pgn);
+ if (!matcher.find()) {
+ throw new ParseException("parse.pgn.incorrect.result", pgn);
+ } else {
+ String result = matcher.group(RESULT_GROUP);
+ moves.setResult(result);
+ }
+ return matcher.group().length();
+ }
+
+}