diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc')
4 files changed, 141 insertions, 235 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index c231767..f55eb36 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -80,7 +80,7 @@ public class ListUtils { * The affixes to remove * @return The tokens that have been deaffixed * - * @deprecated Replaced by NeoTokenSplitter. + * @deprecated Replaced by TokenSplitter. */ @Deprecated public static IList<String> deAffixTokens(IList<String> input, Deque<IPair<String, String>> operators) { @@ -301,7 +301,7 @@ public class ListUtils { * those operators * @return A list of tokens split on all the operators * - * @deprecated Use NeoTokenSplitter now + * @deprecated Use TokenSplitter now */ @Deprecated public static IList<String> splitTokens(IList<String> input, Deque<IPair<String, String>> operators) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/NeoTokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/NeoTokenSplitter.java deleted file mode 100644 index 9da457e..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/NeoTokenSplitter.java +++ /dev/null @@ -1,157 +0,0 @@ -package bjc.utils.funcutils; - -import java.util.regex.Pattern; - -/** - * Split a string and keep given delimiters. - * - * @author Ben Culkin - */ -public class NeoTokenSplitter { - /* - * This string is a format template for the delimiter matching regex - * - * It does two things: - * - * <ol> - * <li> Match to the left of the provided delimiter by positive lookahead </li> - * <li> Match to the right of the provided delimiter by positive lookbehind </li> - * </ol> - * - * Thus, it will only match in places where the delimiter is, but won't - * actually match the delimiter, leaving split to put it into the stream - */ - private static String WITH_DELIM = "((?<=%1$s)|(?=%1$s))"; - - /* - * This string is a format template for the multi-delimiter matching - * regex. - * - * It does the same thing as the single delimiter regex, but has to have - * some negative lookahead/lookbehind assertions to avoid splitting a - * delimiter into pieces. - */ - private static String WITH_MULTI_DELIM = "((?<=%1$s+)(?!%1$s)|(?<!%1$s)(?=%1$s+))"; - - /* - * These represent the internal state of the splitter. - */ - private StringBuilder currPatt; - private StringBuilder currExclusionPatt; - - /* - * These represent the external state of the splitter. - * - * Compilation causes internal to become external. - */ - private Pattern compPatt; - private Pattern exclusionPatt; - - /** - * Create a new token splitter. - */ - public NeoTokenSplitter() { - } - - /** - * Split a provided string using configured delimiters, and keeping the - * delimiters. - * - * The splitter must be compiled first. - * - * @param inp - * The string to split. - * - * @return The split string, including delimiters. - * - * @throws IllegalStateException - * If the splitter isn't compiled. - */ - public String[] split(String inp) { - if(compPatt == null) throw new IllegalStateException("Token splitter has not been compiled yet"); - - /* - * Don't split something that we should exclude from being split. - */ - if(exclusionPatt.matcher(inp).matches()) return new String[] { inp }; - - return compPatt.split(inp); - } - - /** - * Adds a string as a matched delimiter to split on. - * - * Only works for fixed length delimiters. - * - * The provided string is regex-escaped before being used. - * - * @param delim - * The delimiter to match on. - */ - public void addDelimiter(String delim) { - String quoteDelim = Pattern.quote(delim); - String delimPat = String.format(WITH_DELIM, quoteDelim); - - if(currPatt == null) { - currPatt = new StringBuilder(); - currExclusionPatt = new StringBuilder(); - - currPatt.append("(?:" + delimPat + ")"); - currExclusionPatt.append("(?:" + quoteDelim + ")"); - } else { - currPatt.append("|(?:" + delimPat + ")"); - currExclusionPatt.append("|(?:" + quoteDelim + ")"); - } - } - - /** - * Adds a character class as a matched delimiter to split on. - * - * The provided string should be a pattern to match one or more - * occurances of. - * - * @param delim - * The delimiter to split on. - */ - public void addMultiDelimiter(String delim) { - String delimPat = String.format(WITH_MULTI_DELIM, "(?:" + delim + ")"); - - if(currPatt == null) { - currPatt = new StringBuilder(); - currExclusionPatt = new StringBuilder(); - - currPatt.append("(?:" + delimPat + ")"); - currExclusionPatt.append("(?:(?:" + delim + ")+)"); - - } else { - currPatt.append("|(?:" + delimPat + ")"); - currExclusionPatt.append("|(?:(?:" + delim + ")+)"); - } - } - - /** - * Marks strings matching the pattern delim as non-splittable. - * - * @param delim - * The regex to not splitting matching strings. - */ - public void addNonMatcher(String delim) { - if(currPatt == null) { - currPatt = new StringBuilder(); - currExclusionPatt = new StringBuilder(); - - currExclusionPatt.append("(?:" + delim + ")"); - } else { - currExclusionPatt.append("|(?:" + delim + ")"); - } - } - /** - * Compiles the current set of delimiters to a pattern. - * - * Makes this splitter ready to use. - */ - public void compile() { - compPatt = Pattern.compile(currPatt.toString()); - exclusionPatt = Pattern.compile(currExclusionPatt.toString()); - } -} diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java deleted file mode 100644 index f550b65..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenDeaffixer.java +++ /dev/null @@ -1,47 +0,0 @@ -package bjc.utils.funcutils; - -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - -import java.util.function.BiFunction; - -final class TokenDeaffixer implements BiFunction<String, String, IList<String>> { - private String token; - - public TokenDeaffixer(String tok) { - token = tok; - } - - @Override - public IList<String> apply(String operatorName, String operatorRegex) { - if(operatorName == null) - throw new NullPointerException("Operator name must not be null"); - else if(operatorRegex == null) throw new NullPointerException("Operator regex must not be null"); - - if(StringUtils.containsOnly(token, operatorRegex)) - return new FunctionalList<>(token); - else if(token.startsWith(operatorName)) { - if(token.endsWith(operatorName)) - return new FunctionalList<>(operatorName, token.split(operatorRegex)[1], operatorName); - - return new FunctionalList<>(operatorName, token.split(operatorRegex)[1]); - } else if(token.endsWith(operatorName)) - return new FunctionalList<>(token.split(operatorRegex)[0], operatorName); - else if(token.contains(operatorName)) { - String[] tokenParts = token.split(operatorRegex); - - IList<String> returned = new FunctionalList<>(); - - for(int i = 0; i < tokenParts.length; i++) { - returned.add(tokenParts[i]); - - if(i < tokenParts.length - 1) { - returned.add(operatorName); - } - } - - return returned; - } else - return new FunctionalList<>(token); - } -} diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java index 84f5270..084bdae 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/TokenSplitter.java @@ -1,47 +1,157 @@ package bjc.utils.funcutils; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; +import java.util.regex.Pattern; -import java.util.Iterator; -import java.util.function.BiFunction; +/** + * Split a string and keep given delimiters. + * + * @author Ben Culkin + */ +public class TokenSplitter { + /* + * This string is a format template for the delimiter matching regex + * + * It does two things: + * + * <ol> + * <li> Match to the left of the provided delimiter by positive lookahead </li> + * <li> Match to the right of the provided delimiter by positive lookbehind </li> + * </ol> + * + * Thus, it will only match in places where the delimiter is, but won't + * actually match the delimiter, leaving split to put it into the stream + */ + private static String WITH_DELIM = "((?<=%1$s)|(?=%1$s))"; -final class TokenSplitter implements BiFunction<String, String, IList<String>> { - private String tokenToSplit; + /* + * This string is a format template for the multi-delimiter matching + * regex. + * + * It does the same thing as the single delimiter regex, but has to have + * some negative lookahead/lookbehind assertions to avoid splitting a + * delimiter into pieces. + */ + private static String WITH_MULTI_DELIM = "((?<=%1$s+)(?!%1$s)|(?<!%1$s)(?=%1$s+))"; - public TokenSplitter(String tok) { - this.tokenToSplit = tok; + /* + * These represent the internal state of the splitter. + */ + private StringBuilder currPatt; + private StringBuilder currExclusionPatt; + + /* + * These represent the external state of the splitter. + * + * Compilation causes internal to become external. + */ + private Pattern compPatt; + private Pattern exclusionPatt; + + /** + * Create a new token splitter. + */ + public TokenSplitter() { } - @Override - public IList<String> apply(String operatorName, String operatorRegex) { - if(operatorName == null) - throw new NullPointerException("Operator name must not be null"); - else if(operatorRegex == null) throw new NullPointerException("Operator regex must not be null"); + /** + * Split a provided string using configured delimiters, and keeping the + * delimiters. + * + * The splitter must be compiled first. + * + * @param inp + * The string to split. + * + * @return The split string, including delimiters. + * + * @throws IllegalStateException + * If the splitter isn't compiled. + */ + public String[] split(String inp) { + if(compPatt == null) throw new IllegalStateException("Token splitter has not been compiled yet"); + + /* + * Don't split something that we should exclude from being split. + */ + if(exclusionPatt.matcher(inp).matches()) return new String[] { inp }; - if(tokenToSplit.contains(operatorName)) { - if(StringUtils.containsOnly(tokenToSplit, operatorRegex)) - return new FunctionalList<>(tokenToSplit); + return compPatt.split(inp); + } - IList<String> splitTokens = new FunctionalList<>(tokenToSplit.split(operatorRegex)); - IList<String> result = new FunctionalList<>(); + /** + * Adds a string as a matched delimiter to split on. + * + * Only works for fixed length delimiters. + * + * The provided string is regex-escaped before being used. + * + * @param delim + * The delimiter to match on. + */ + public void addDelimiter(String delim) { + String quoteDelim = Pattern.quote(delim); + String delimPat = String.format(WITH_DELIM, quoteDelim); - Iterator<String> itr = splitTokens.toIterable().iterator(); - int tokenExpansionSize = splitTokens.getSize(); + if(currPatt == null) { + currPatt = new StringBuilder(); + currExclusionPatt = new StringBuilder(); - String elm = itr.next(); + currPatt.append("(?:" + delimPat + ")"); + currExclusionPatt.append("(?:" + quoteDelim + ")"); + } else { + currPatt.append("|(?:" + delimPat + ")"); + currExclusionPatt.append("|(?:" + quoteDelim + ")"); + } + } - for(int i = 0; itr.hasNext(); elm = itr.next()) { - result.add(elm); + /** + * Adds a character class as a matched delimiter to split on. + * + * The provided string should be a pattern to match one or more + * occurances of. + * + * @param delim + * The delimiter to split on. + */ + public void addMultiDelimiter(String delim) { + String delimPat = String.format(WITH_MULTI_DELIM, "(?:" + delim + ")"); - if(i != tokenExpansionSize) { - result.add(operatorName); - } - } + if(currPatt == null) { + currPatt = new StringBuilder(); + currExclusionPatt = new StringBuilder(); - return result; + currPatt.append("(?:" + delimPat + ")"); + currExclusionPatt.append("(?:(?:" + delim + ")+)"); + + } else { + currPatt.append("|(?:" + delimPat + ")"); + currExclusionPatt.append("|(?:(?:" + delim + ")+)"); } + } + + /** + * Marks strings matching the pattern delim as non-splittable. + * + * @param delim + * The regex to not splitting matching strings. + */ + public void addNonMatcher(String delim) { + if(currPatt == null) { + currPatt = new StringBuilder(); + currExclusionPatt = new StringBuilder(); - return new FunctionalList<>(tokenToSplit); + currExclusionPatt.append("(?:" + delim + ")"); + } else { + currExclusionPatt.append("|(?:" + delim + ")"); + } + } + /** + * Compiles the current set of delimiters to a pattern. + * + * Makes this splitter ready to use. + */ + public void compile() { + compPatt = Pattern.compile(currPatt.toString()); + exclusionPatt = Pattern.compile(currExclusionPatt.toString()); } } |
