+++ /dev/null
-/*
- * Copyright (c) 2019-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.scanner.regex;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-/**
- * RegexMatcherResult
- *
- * @author Dmitry Samoshin aka gotty
- */
-public class RegexMatcherResult implements RegexMatcher, RegexResult {
-
- private Pattern pattern;
- private Pattern startPattern, endPattern;
- private final boolean isSingle, isFirst, isMap;
-
- private boolean isMatch = false;
- private boolean isBreak = false;
-
- private int matchNumber = 0;
-
- private List<String> resultList = new ArrayList<>();
- private Map<String, String> resultMap = new HashMap<>();
-
- public RegexMatcherResult(String match, boolean isFirst) {
- this(match, isFirst, false);
- }
-
- public RegexMatcherResult(String match, boolean isFirst, boolean isMap) {
- this.pattern = Pattern.compile(match);
- this.isSingle = true;
- this.isFirst = isFirst;
- this.isMap = isMap;
- }
-
- public RegexMatcherResult(String startMatch, String endMatch, boolean isFirst) {
- this(startMatch, endMatch, isFirst, false);
- }
-
- public RegexMatcherResult(String startMatch, String endMatch, boolean isFirst, boolean isMap) {
- this.startPattern = Pattern.compile(startMatch);
- this.endPattern = Pattern.compile(endMatch);
- this.isSingle = false;
- this.isFirst = isFirst;
- this.isMap = isMap;
- }
-
- @Override
- public void match(String input) {
- Matcher matcher;
- if (isSingle) {
- matcher = pattern.matcher(input);
- if (matcher.find()) {
- addMatch(matcher);
- if (isFirst) {
- isBreak = true;
- }
- }
- } else {
- matcher = isMatch ? endPattern.matcher(input) : startPattern.matcher(input);
- if (matcher.find()) {
- add(input);
- if (isMatch && isFirst) {
- isBreak = true;
- }
- isMatch = !isMatch;
- } else {
- if (isMatch) {
- add(input);
- }
- }
- }
- }
-
- private void add(String input) {
- if (isMap) {
- resultMap.put(
- Integer.toString(matchNumber++),
- input
- );
- } else {
- resultList.add(
- input
- );
- }
- }
-
- private void addMatch(Matcher matcher) {
- if (isMap) {
- resultMap.put(
- matcher.group(1),
- matcher.group(2)
- );
- } else {
- resultList.add(
- matcher.group(1)
- );
- }
- }
-
- @Override
- public boolean isBreak() {
- return isBreak;
- }
-
- @Override
- public RegexResult result() {
- return this;
- }
-
- @Override
- public boolean isEmpty() {
- return isMap ? resultMap.isEmpty() : resultList.isEmpty();
- }
-
- @Override
- public List<String> resultList() {
- return resultList;
- }
-
- @Override
- public Map<String, String> resultMap() {
- return resultMap;
- }
-
-}