[LIB-13] Add regular expressions classes
[chesshog-scanner.git] / src / main / java / org / hedgecode / chess / scanner / regex / RegexMatcherResult.java
1 /*
2  * Copyright (c) 2019-2020. 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.scanner.regex;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25
26 /**
27  * RegexMatcherResult
28  *
29  * @author Dmitry Samoshin aka gotty
30  */
31 public class RegexMatcherResult implements RegexMatcher, RegexResult {
32
33     private Pattern pattern;
34     private Pattern startPattern, endPattern;
35     private final boolean isSingle, isFirst, isMap;
36
37     private boolean isMatch = false;
38     private boolean isBreak = false;
39
40     private int matchNumber = 0;
41
42     private List<String> resultList = new ArrayList<>();
43     private Map<String, String> resultMap = new HashMap<>();
44
45     public RegexMatcherResult(String match, boolean isFirst) {
46         this(match, isFirst, false);
47     }
48
49     public RegexMatcherResult(String match, boolean isFirst, boolean isMap) {
50         this.pattern = Pattern.compile(match);
51         this.isSingle = true;
52         this.isFirst = isFirst;
53         this.isMap = isMap;
54     }
55
56     public RegexMatcherResult(String startMatch, String endMatch, boolean isFirst) {
57         this(startMatch, endMatch, isFirst, false);
58     }
59
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;
65         this.isMap = isMap;
66     }
67
68     @Override
69     public void match(String input) {
70         Matcher matcher;
71         if (isSingle) {
72             matcher = pattern.matcher(input);
73             if (matcher.find()) {
74                 addMatch(matcher);
75                 if (isFirst) {
76                     isBreak = true;
77                 }
78             }
79         } else {
80             matcher = isMatch ? endPattern.matcher(input) : startPattern.matcher(input);
81             if (matcher.find()) {
82                 add(input);
83                 if (isMatch && isFirst) {
84                     isBreak = true;
85                 }
86                 isMatch = !isMatch;
87             } else {
88                 if (isMatch) {
89                     add(input);
90                 }
91             }
92         }
93     }
94
95     private void add(String input) {
96         if (isMap) {
97             resultMap.put(
98                     Integer.toString(matchNumber++),
99                     input
100             );
101         } else {
102             resultList.add(
103                     input
104             );
105         }
106     }
107
108     private void addMatch(Matcher matcher) {
109         if (isMap) {
110             resultMap.put(
111                     matcher.group(1),
112                     matcher.group(2)
113             );
114         } else {
115             resultList.add(
116                     matcher.group(1)
117             );
118         }
119     }
120
121     @Override
122     public boolean isBreak() {
123         return isBreak;
124     }
125
126     @Override
127     public RegexResult result() {
128         return this;
129     }
130
131     @Override
132     public boolean isEmpty() {
133         return isMap ? resultMap.isEmpty() : resultList.isEmpty();
134     }
135
136     @Override
137     public List<String> resultList() {
138         return resultList;
139     }
140
141     @Override
142     public Map<String, String> resultMap() {
143         return resultMap;
144     }
145
146 }