2 * Copyright (c) 2019-2020. 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.scanner.regex;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
29 * @author Dmitry Samoshin aka gotty
31 public class RegexMatcherResult implements RegexMatcher, RegexResult {
33 private Pattern pattern;
34 private Pattern startPattern, endPattern;
35 private final boolean isSingle, isFirst, isMap;
37 private boolean isMatch = false;
38 private boolean isBreak = false;
40 private int matchNumber = 0;
42 private List<String> resultList = new ArrayList<>();
43 private Map<String, String> resultMap = new HashMap<>();
45 public RegexMatcherResult(String match, boolean isFirst) {
46 this(match, isFirst, false);
49 public RegexMatcherResult(String match, boolean isFirst, boolean isMap) {
50 this.pattern = Pattern.compile(match);
52 this.isFirst = isFirst;
56 public RegexMatcherResult(String startMatch, String endMatch, boolean isFirst) {
57 this(startMatch, endMatch, isFirst, false);
60 public RegexMatcherResult(String startMatch, String endMatch, boolean isFirst, boolean isMap) {
61 this.startPattern = Pattern.compile(startMatch);
62 this.endPattern = Pattern.compile(endMatch);
63 this.isSingle = false;
64 this.isFirst = isFirst;
69 public void match(String input) {
72 matcher = pattern.matcher(input);
80 matcher = isMatch ? endPattern.matcher(input) : startPattern.matcher(input);
83 if (isMatch && isFirst) {
95 private void add(String input) {
98 Integer.toString(matchNumber++),
108 private void addMatch(Matcher matcher) {
122 public boolean isBreak() {
127 public RegexResult result() {
132 public boolean isEmpty() {
133 return isMap ? resultMap.isEmpty() : resultList.isEmpty();
137 public List<String> resultList() {
142 public Map<String, String> resultMap() {