X-Git-Url: https://git.hedgecode.org/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2Fregex%2FRegexMatcherResult.java;fp=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fchess%2Fscanner%2Fregex%2FRegexMatcherResult.java;h=0000000000000000000000000000000000000000;hb=9cfd5f43a9d43b931ea51aaba514e25ee50eedce;hp=80bd66d4d81e863394f311eaa92ad288180f78e5;hpb=43e86c6691aa54c7c583902fbe72a0875e098a91;p=chesshog-scanner.git diff --git a/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcherResult.java b/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcherResult.java deleted file mode 100644 index 80bd66d..0000000 --- a/src/main/java/org/hedgecode/chess/scanner/regex/RegexMatcherResult.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * 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 resultList = new ArrayList<>(); - private Map 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 resultList() { - return resultList; - } - - @Override - public Map resultMap() { - return resultMap; - } - -}