diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:40:33 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-04-10 16:40:33 -0400 |
| commit | 889fac2bdf993dc86f64a8893c0260fdcf848acb (patch) | |
| tree | 99ed08552efa86fdc5fdf4ddb8720d10e599fafe /BJC-Utils2/src/main/java/bjc/utils/parserutils | |
| parent | 1656b02144446aeedebb3d1179e07ed99c01861c (diff) | |
Cleanup
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils')
22 files changed, 553 insertions, 559 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java index a91bf2a..888ea7a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java @@ -1,8 +1,9 @@ package bjc.utils.parserutils; -import java.util.regex.Pattern; +import static bjc.utils.PropertyDB.applyFormat; +import static bjc.utils.PropertyDB.getRegex; -import static bjc.utils.PropertyDB.*; +import java.util.regex.Pattern; /* * Checks if a string would pass Double.parseDouble. diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java index bb6ebac..aa366cf 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/IPrecedent.java @@ -15,7 +15,7 @@ public interface IPrecedent { * The precedence of the object to handle * @return A new object with set precedence */ - public static IPrecedent newSimplePrecedent(int precedence) { + public static IPrecedent newSimplePrecedent(final int precedence) { return () -> precedence; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ParserException.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ParserException.java index 2a41009..ae33aba 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ParserException.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ParserException.java @@ -2,27 +2,35 @@ package bjc.utils.parserutils; /** * General superclass for exceptions thrown during parsing. - * + * * @author EVE * */ public class ParserException extends Exception { /** + * + */ + private static final long serialVersionUID = 631298568113373233L; + + /** * Create a new exception with the provided message. - * - * @param msg The message for the exception. + * + * @param msg + * The message for the exception. */ - public ParserException(String msg) { + public ParserException(final String msg) { super(msg); } - + /** * Create a new exception with the provided message and cause. - * - * @param msg The message for the exception. - * @param cause The cause of the exception. + * + * @param msg + * The message for the exception. + * @param cause + * The cause of the exception. */ - public ParserException(String msg, Exception cause) { + public ParserException(final String msg, final Exception cause) { super(msg, cause); } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java index b30a69c..44744f5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -1,16 +1,16 @@ package bjc.utils.parserutils; +import java.util.Deque; +import java.util.LinkedList; +import java.util.function.Consumer; +import java.util.function.Function; + import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IList; import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.StringUtils; -import java.util.Deque; -import java.util.LinkedList; -import java.util.function.Consumer; -import java.util.function.Function; - /** * Utility to run the shunting yard algorithm on a bunch of tokens. * @@ -47,7 +47,7 @@ public class ShuntingYard<TokenType> { private final int precedence; - private Operator(int prec) { + private Operator(final int prec) { precedence = prec; } @@ -58,40 +58,40 @@ public class ShuntingYard<TokenType> { } private final class TokenShunter implements Consumer<String> { - private IList<TokenType> output; - private Deque<String> stack; - private Function<String, TokenType> transformer; + private final IList<TokenType> output; + private final Deque<String> stack; + private final Function<String, TokenType> transformer; - public TokenShunter(IList<TokenType> outpt, Deque<String> stack, - Function<String, TokenType> transformer) { + public TokenShunter(final IList<TokenType> outpt, final Deque<String> stack, + final Function<String, TokenType> transformer) { this.output = outpt; this.stack = stack; this.transformer = transformer; } @Override - public void accept(String token) { + public void accept(final String token) { // Handle operators - if(operators.containsKey(token)) { + if (operators.containsKey(token)) { // Pop operators while there isn't a higher // precedence one - while(!stack.isEmpty() && isHigherPrec(token, stack.peek())) { + while (!stack.isEmpty() && isHigherPrec(token, stack.peek())) { output.add(transformer.apply(stack.pop())); } // Put this operator onto the stack stack.push(token); - } else if(StringUtils.containsOnly(token, "\\(")) { + } else if (StringUtils.containsOnly(token, "\\(")) { // Handle groups of parenthesis for multiple // nesting levels stack.push(token); - } else if(StringUtils.containsOnly(token, "\\)")) { + } else if (StringUtils.containsOnly(token, "\\)")) { // Handle groups of parenthesis for multiple // nesting levels - String swappedToken = token.replace(')', '('); + final String swappedToken = token.replace(')', '('); // Remove tokens up to a matching parenthesis - while(!stack.peek().equals(swappedToken)) { + while (!stack.peek().equals(swappedToken)) { output.add(transformer.apply(stack.pop())); } @@ -116,11 +116,11 @@ public class ShuntingYard<TokenType> { * Whether or not basic math operators should be * provided. */ - public ShuntingYard(boolean configureBasics) { + public ShuntingYard(final boolean configureBasics) { operators = new FunctionalMap<>(); // Add basic operators if we're configured to do so - if(configureBasics) { + if (configureBasics) { operators.put("+", Operator.ADD); operators.put("-", Operator.SUBTRACT); operators.put("*", Operator.MULTIPLY); @@ -133,15 +133,15 @@ public class ShuntingYard<TokenType> { * * @param operator * The token representing the operator. - * + * * @param precedence * The precedence of the operator to add. */ - public void addOp(String operator, int precedence) { + public void addOp(final String operator, final int precedence) { /* * Create the precedence marker */ - IPrecedent prec = IPrecedent.newSimplePrecedent(precedence); + final IPrecedent prec = IPrecedent.newSimplePrecedent(precedence); this.addOp(operator, prec); } @@ -151,17 +151,17 @@ public class ShuntingYard<TokenType> { * * @param operator * The token representing the operator. - * + * * @param precedence * The precedence of the operator. */ - public void addOp(String operator, IPrecedent precedence) { + public void addOp(final String operator, final IPrecedent precedence) { /* * Complain about trying to add an incorrect operator */ - if(operator == null) + if (operator == null) throw new NullPointerException("Operator must not be null"); - else if(precedence == null) throw new NullPointerException("Precedence must not be null"); + else if (precedence == null) throw new NullPointerException("Precedence must not be null"); /* * Add the operator to the ones we handle @@ -169,16 +169,16 @@ public class ShuntingYard<TokenType> { operators.put(operator, precedence); } - private boolean isHigherPrec(String left, String right) { + private boolean isHigherPrec(final String left, final String right) { // Check if the right operator exists - boolean exists = operators.containsKey(right); + final boolean exists = operators.containsKey(right); // If it doesn't, the left is higher precedence. - if(!exists) return false; + if (!exists) return false; // Get the precedence of operators - int rightPrecedence = operators.get(right).getPrecedence(); - int leftPrecedence = operators.get(left).getPrecedence(); + final int rightPrecedence = operators.get(right).getPrecedence(); + final int leftPrecedence = operators.get(left).getPrecedence(); // Evaluate what we were asked return rightPrecedence >= leftPrecedence; @@ -189,23 +189,23 @@ public class ShuntingYard<TokenType> { * * @param input * The string to transform. - * + * * @param transformer * The function to use to transform strings to tokens. - * + * * @return A list of tokens in postfix notation. */ - public IList<TokenType> postfix(IList<String> input, Function<String, TokenType> transformer) { + public IList<TokenType> postfix(final IList<String> input, final Function<String, TokenType> transformer) { // Check our input - if(input == null) + if (input == null) throw new NullPointerException("Input must not be null"); - else if(transformer == null) throw new NullPointerException("Transformer must not be null"); + else if (transformer == null) throw new NullPointerException("Transformer must not be null"); // Here's what we're handing back - IList<TokenType> output = new FunctionalList<>(); + final IList<TokenType> output = new FunctionalList<>(); // The stack to put operators on - Deque<String> stack = new LinkedList<>(); + final Deque<String> stack = new LinkedList<>(); // Shunt the tokens input.forEach(new TokenShunter(output, stack, transformer)); @@ -225,9 +225,9 @@ public class ShuntingYard<TokenType> { * The token representing the operator. If null, remove * all operators. */ - public void removeOp(String operator) { + public void removeOp(final String operator) { // Check if we want to remove all operators - if(operator == null) { + if (operator == null) { operators = new FunctionalMap<>(); } else { operators.remove(operator); diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java index c441dff..89dc35f 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenTransformer.java @@ -1,5 +1,11 @@ package bjc.utils.parserutils; +import java.util.Deque; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; + import bjc.utils.data.IHolder; import bjc.utils.data.ITree; import bjc.utils.data.Pair; @@ -7,23 +13,17 @@ import bjc.utils.data.Tree; import bjc.utils.parserutils.TreeConstructor.ConstructorState; import bjc.utils.parserutils.TreeConstructor.QueueFlattener; -import java.util.Deque; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.UnaryOperator; - final class TokenTransformer<TokenType> implements Consumer<TokenType> { // Handle operators private final class OperatorHandler implements UnaryOperator<ConstructorState<TokenType>> { - private TokenType element; + private final TokenType element; - public OperatorHandler(TokenType element) { + public OperatorHandler(final TokenType element) { this.element = element; } @Override - public ConstructorState<TokenType> apply(ConstructorState<TokenType> pair) { + public ConstructorState<TokenType> apply(final ConstructorState<TokenType> pair) { // Replace the current AST with the result of handling // an operator return new ConstructorState<>(pair.bindLeft(queuedASTs -> { @@ -31,18 +31,18 @@ final class TokenTransformer<TokenType> implements Consumer<TokenType> { })); } - private ConstructorState<TokenType> handleOperator(Deque<ITree<TokenType>> queuedASTs) { + private ConstructorState<TokenType> handleOperator(final Deque<ITree<TokenType>> queuedASTs) { // The AST we're going to hand back ITree<TokenType> newAST; // Handle special operators - if(isSpecialOperator.test(element)) { + if (isSpecialOperator.test(element)) { newAST = handleSpecialOperator.apply(element).apply(queuedASTs); } else { // Error if we don't have enough for a binary // operator - if(queuedASTs.size() < 2) { - String msg = String.format( + if (queuedASTs.size() < 2) { + final String msg = String.format( "Attempted to parse binary operator without enough operands\n\tProblem operator is: %s\n\tPossible operand is: %s", element.toString(), queuedASTs.peek().toString()); @@ -50,8 +50,8 @@ final class TokenTransformer<TokenType> implements Consumer<TokenType> { } // Grab the two operands - ITree<TokenType> right = queuedASTs.pop(); - ITree<TokenType> left = queuedASTs.pop(); + final ITree<TokenType> right = queuedASTs.pop(); + final ITree<TokenType> left = queuedASTs.pop(); // Create a new AST newAST = new Tree<>(element, left, right); @@ -65,17 +65,17 @@ final class TokenTransformer<TokenType> implements Consumer<TokenType> { } } - private IHolder<ConstructorState<TokenType>> initialState; + private final IHolder<ConstructorState<TokenType>> initialState; - private Predicate<TokenType> operatorPredicate; + private final Predicate<TokenType> operatorPredicate; - private Predicate<TokenType> isSpecialOperator; - private Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator; + private final Predicate<TokenType> isSpecialOperator; + private final Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator; // Create a new transformer - public TokenTransformer(IHolder<ConstructorState<TokenType>> initialState, - Predicate<TokenType> operatorPredicate, Predicate<TokenType> isSpecialOperator, - Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator) { + public TokenTransformer(final IHolder<ConstructorState<TokenType>> initialState, + final Predicate<TokenType> operatorPredicate, final Predicate<TokenType> isSpecialOperator, + final Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator) { this.initialState = initialState; this.operatorPredicate = operatorPredicate; this.isSpecialOperator = isSpecialOperator; @@ -83,12 +83,12 @@ final class TokenTransformer<TokenType> implements Consumer<TokenType> { } @Override - public void accept(TokenType element) { + public void accept(final TokenType element) { // Handle operators - if(operatorPredicate.test(element)) { + if (operatorPredicate.test(element)) { initialState.transform(new OperatorHandler(element)); } else { - ITree<TokenType> newAST = new Tree<>(element); + final ITree<TokenType> newAST = new Tree<>(element); // Insert the new tree into the AST initialState.transform(pair -> { diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java index 39e0c4e..5de0586 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TokenUtils.java @@ -1,17 +1,17 @@ package bjc.utils.parserutils; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; -import bjc.utils.parserutils.splitterv2.TokenSplitter; +import static bjc.utils.PropertyDB.applyFormat; +import static bjc.utils.PropertyDB.getCompiledRegex; +import static bjc.utils.PropertyDB.getRegex; import java.util.LinkedList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import static bjc.utils.PropertyDB.getRegex; -import static bjc.utils.PropertyDB.getCompiledRegex; -import static bjc.utils.PropertyDB.applyFormat; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; +import bjc.utils.parserutils.splitterv2.TokenSplitter; /** * Utilities useful for operating on PL tokens. @@ -27,7 +27,7 @@ public class TokenUtils { */ public static class StringTokenSplitter implements TokenSplitter { @Override - public IList<String> split(String input) { + public IList<String> split(final String input) { return new FunctionalList<>(TokenUtils.removeDQuotedStrings(input)); } } @@ -67,34 +67,33 @@ public class TokenUtils { * @return An list containing alternating bits of the string and the * embedded double-quoted strings that separated them. */ - public static List<String> removeDQuotedStrings(String inp) { - if(inp == null) { - throw new NullPointerException("inp must not be null"); - } + public static List<String> removeDQuotedStrings(final String inp) { + if (inp == null) throw new NullPointerException("inp must not be null"); /* * What we need for piece-by-piece string building */ StringBuffer work = new StringBuffer(); - List<String> res = new LinkedList<>(); + final List<String> res = new LinkedList<>(); /* * Matcher for proper strings and single quotes. */ - Matcher mt = doubleQuotePatt.matcher(inp); - Matcher corr = quotePatt.matcher(inp); + final Matcher mt = doubleQuotePatt.matcher(inp); + final Matcher corr = quotePatt.matcher(inp); - if(corr.find() && !corr.find()) { + if (corr.find() && !corr.find()) { /* * There's a unmatched opening quote with no strings. */ - String msg = String.format("Unclosed string literal '%s'. Opening quote was at position %d", - inp, inp.indexOf("\"")); + final String msg = String.format( + "Unclosed string literal '%s'. Opening quote was at position %d", inp, + inp.indexOf("\"")); throw new IllegalArgumentException(msg); } - while(mt.find()) { + while (mt.find()) { /* * Remove the string until the quoted string. */ @@ -117,15 +116,16 @@ public class TokenUtils { * Grab the remainder of the string. */ mt.appendTail(work); - String tail = work.toString(); + final String tail = work.toString(); - if(tail.contains("\"")) { + if (tail.contains("\"")) { /* * There's a unmatched opening quote with at least one * string. */ - String msg = String.format("Unclosed string literal '%s'. Opening quote was at position %d", - inp, inp.lastIndexOf("\"")); + final String msg = String.format( + "Unclosed string literal '%s'. Opening quote was at position %d", inp, + inp.lastIndexOf("\"")); throw new IllegalArgumentException(msg); } @@ -133,7 +133,7 @@ public class TokenUtils { /* * Only add an empty tail if the string was empty. */ - if(!tail.equals("") || res.isEmpty()) { + if (!tail.equals("") || res.isEmpty()) { res.add(tail); } @@ -149,28 +149,26 @@ public class TokenUtils { * @return The string with escape sequences replaced by their equivalent * characters. */ - public static String descapeString(String inp) { - if(inp == null) { - throw new NullPointerException("inp must not be null"); - } + public static String descapeString(final String inp) { + if (inp == null) throw new NullPointerException("inp must not be null"); - StringBuffer work = new StringBuffer(); + final StringBuffer work = new StringBuffer(); - Matcher possibleEscapeFinder = possibleEscapePatt.matcher(inp); - Matcher escapeFinder = escapePatt.matcher(inp); + final Matcher possibleEscapeFinder = possibleEscapePatt.matcher(inp); + final Matcher escapeFinder = escapePatt.matcher(inp); - while(possibleEscapeFinder.find()) { - if(!escapeFinder.find()) { - String msg = String.format("Illegal escape sequence '%s' at position %d", + while (possibleEscapeFinder.find()) { + if (!escapeFinder.find()) { + final String msg = String.format("Illegal escape sequence '%s' at position %d", possibleEscapeFinder.group(), possibleEscapeFinder.start()); throw new IllegalArgumentException(msg); } - String escapeSeq = escapeFinder.group(); + final String escapeSeq = escapeFinder.group(); String escapeRep = ""; - switch(escapeSeq) { + switch (escapeSeq) { case "\\b": escapeRep = "\b"; break; @@ -200,7 +198,7 @@ public class TokenUtils { escapeRep = "\\"; break; default: - if(escapeSeq.startsWith("u")) { + if (escapeSeq.startsWith("u")) { escapeRep = handleUnicodeEscape(escapeSeq.substring(1)); } else { escapeRep = handleOctalEscape(escapeSeq); @@ -215,15 +213,15 @@ public class TokenUtils { return work.toString(); } - private static String handleUnicodeEscape(String seq) { + private static String handleUnicodeEscape(final String seq) { try { - int codepoint = Integer.parseInt(seq, 16); + final int codepoint = Integer.parseInt(seq, 16); return new String(Character.toChars(codepoint)); - } catch(IllegalArgumentException iaex) { - String msg = String.format("'%s' is not a valid Unicode escape sequence'", seq); + } catch (final IllegalArgumentException iaex) { + final String msg = String.format("'%s' is not a valid Unicode escape sequence'", seq); - IllegalArgumentException reiaex = new IllegalArgumentException(msg); + final IllegalArgumentException reiaex = new IllegalArgumentException(msg); reiaex.initCause(iaex); @@ -231,21 +229,22 @@ public class TokenUtils { } } - private static String handleOctalEscape(String seq) { + private static String handleOctalEscape(final String seq) { try { - int codepoint = Integer.parseInt(seq, 8); + final int codepoint = Integer.parseInt(seq, 8); - if(codepoint > 255) { - String msg = String.format("'%d' is outside the range of octal escapes', codepoint"); + if (codepoint > 255) { + final String msg = String + .format("'%d' is outside the range of octal escapes', codepoint"); throw new IllegalArgumentException(msg); } return new String(Character.toChars(codepoint)); - } catch(IllegalArgumentException iaex) { - String msg = String.format("'%s' is not a valid octal escape sequence'", seq); + } catch (final IllegalArgumentException iaex) { + final String msg = String.format("'%s' is not a valid octal escape sequence'", seq); - IllegalArgumentException reiaex = new IllegalArgumentException(msg); + final IllegalArgumentException reiaex = new IllegalArgumentException(msg); reiaex.initCause(iaex); @@ -256,27 +255,27 @@ public class TokenUtils { /** * Check if a given string would be successfully converted to a double * by {@link Double#parseDouble(String)}. - * + * * @param inp * The string to check. * @return Whether the string is a valid double or not. */ - public static boolean isDouble(String inp) { + public static boolean isDouble(final String inp) { return DoubleMatcher.doubleLiteral.matcher(inp).matches(); } /** * Check if a given string would be successfully converted to a integer * by {@link Integer#parseInt(String)}. - * + * * NOTE: This only checks syntax. Using values out of the range of * integers will still cause errors. - * + * * @param inp * The input to check. * @return Whether the string is a valid double or not. */ - public static boolean isInt(String inp) { + public static boolean isInt(final String inp) { return intLitPattern.matcher(inp).matches(); } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java index bd0ab97..d7ed5b0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -1,5 +1,10 @@ package bjc.utils.parserutils; +import java.util.Deque; +import java.util.LinkedList; +import java.util.function.Function; +import java.util.function.Predicate; + import bjc.utils.data.IHolder; import bjc.utils.data.IPair; import bjc.utils.data.ITree; @@ -7,11 +12,6 @@ import bjc.utils.data.Identity; import bjc.utils.data.Pair; import bjc.utils.funcdata.IList; -import java.util.Deque; -import java.util.LinkedList; -import java.util.function.Function; -import java.util.function.Predicate; - /** * Creates a parse tree from a postfix expression * @@ -21,7 +21,7 @@ import java.util.function.Predicate; public class TreeConstructor { /** * Alias interface for special operator types. - * + * * @param <TokenType> * The token type of the tree. */ @@ -33,11 +33,11 @@ public class TreeConstructor { * Alias for constructor state. */ static final class ConstructorState<TokenType> extends Pair<Deque<ITree<TokenType>>, ITree<TokenType>> { - public ConstructorState(Deque<ITree<TokenType>> left, ITree<TokenType> right) { + public ConstructorState(final Deque<ITree<TokenType>> left, final ITree<TokenType> right) { super(left, right); } - public ConstructorState(IPair<Deque<ITree<TokenType>>, ITree<TokenType>> par) { + public ConstructorState(final IPair<Deque<ITree<TokenType>>, ITree<TokenType>> par) { super(par.getLeft(), par.getRight()); } } @@ -56,8 +56,8 @@ public class TreeConstructor { * operator * @return A AST from the expression */ - public static <TokenType> ITree<TokenType> constructTree(IList<TokenType> tokens, - Predicate<TokenType> isOperator) { + public static <TokenType> ITree<TokenType> constructTree(final IList<TokenType> tokens, + final Predicate<TokenType> isOperator) { // Construct a tree with no special operators return constructTree(tokens, isOperator, op -> false, null); } @@ -70,37 +70,37 @@ public class TreeConstructor { * * @param <TokenType> * The elements of the parse tree. - * + * * @param tokens * The list of tokens to build a tree from. - * + * * @param isOperator * The predicate to use to determine if something is a * operator. - * + * * @param isSpecialOperator * The predicate to use to determine if an operator needs * special handling. - * + * * @param handleSpecialOperator * The function to use to handle special case operators. - * + * * @return A AST from the expression * */ - public static <TokenType> ITree<TokenType> constructTree(IList<TokenType> tokens, - Predicate<TokenType> isOperator, Predicate<TokenType> isSpecialOperator, - Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator) { + public static <TokenType> ITree<TokenType> constructTree(final IList<TokenType> tokens, + final Predicate<TokenType> isOperator, final Predicate<TokenType> isSpecialOperator, + final Function<TokenType, QueueFlattener<TokenType>> handleSpecialOperator) { // Make sure our parameters are valid - if(tokens == null) + if (tokens == null) throw new NullPointerException("Tokens must not be null"); - else if(isOperator == null) + else if (isOperator == null) throw new NullPointerException("Operator predicate must not be null"); - else if(isSpecialOperator == null) + else if (isSpecialOperator == null) throw new NullPointerException("Special operator determiner must not be null"); // Here is the state for the tree construction - IHolder<ConstructorState<TokenType>> initialState = new Identity<>( + final IHolder<ConstructorState<TokenType>> initialState = new Identity<>( new ConstructorState<>(new LinkedList<>(), null)); // Transform each of the tokens diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java index 3aba434..071afb4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterException.java @@ -5,12 +5,17 @@ package bjc.utils.parserutils.delims; */ public class DelimiterException extends RuntimeException { /** + * + */ + private static final long serialVersionUID = 2079514406049040888L; + + /** * Create a new generic delimiter exception. - * + * * @param res * The reason for this exception. */ - public DelimiterException(String res) { + public DelimiterException(final String res) { super(res); } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java index db6ae8c..85d4038 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/DelimiterGroup.java @@ -1,12 +1,5 @@ package bjc.utils.parserutils.delims; -import bjc.utils.data.IPair; -import bjc.utils.data.ITree; -import bjc.utils.data.Pair; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - import java.util.Arrays; import java.util.Deque; import java.util.HashMap; @@ -18,9 +11,16 @@ import java.util.Set; import java.util.function.BiPredicate; import java.util.function.Function; +import bjc.utils.data.IPair; +import bjc.utils.data.ITree; +import bjc.utils.data.Pair; +import bjc.utils.data.Tree; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + /** * Represents a possible delimiter group to match. - * + * * @author EVE * * @param <T> @@ -29,29 +29,29 @@ import java.util.function.Function; public class DelimiterGroup<T> { /** * Represents an instance of a delimiter group. - * + * * @author EVE * */ public class OpenGroup { - private Deque<ITree<T>> contents; + private final Deque<ITree<T>> contents; private IList<ITree<T>> currentGroup; - private T opener; + private final T opener; - private T[] params; + private final T[] params; /** * Create a new instance of a delimiter group. - * + * * @param open * The item that opened this group. - * + * * @param parms * Any parameters from the opener. */ - public OpenGroup(T open, T[] parms) { + public OpenGroup(final T open, final T[] parms) { opener = open; params = parms; @@ -62,36 +62,36 @@ public class DelimiterGroup<T> { /** * Add an item to this group instance. - * + * * @param itm * The item to add to this group instance. */ - public void addItem(ITree<T> itm) { + public void addItem(final ITree<T> itm) { currentGroup.add(itm); } /** * Mark a subgroup. - * + * * @param marker * The item that indicated this subgroup. - * + * * @param chars * The characteristics for building the tree. */ - public void markSubgroup(T marker, SequenceCharacteristics<T> chars) { - ITree<T> subgroupContents = new Tree<>(chars.contents); - for(ITree<T> itm : currentGroup) { + public void markSubgroup(final T marker, final SequenceCharacteristics<T> chars) { + final ITree<T> subgroupContents = new Tree<>(chars.contents); + for (final ITree<T> itm : currentGroup) { subgroupContents.addChild(itm); } - while(!contents.isEmpty()) { - ITree<T> possibleSubordinate = contents.peek(); + while (!contents.isEmpty()) { + final ITree<T> possibleSubordinate = contents.peek(); - if(possibleSubordinate.getHead().equals(chars.subgroup)) { - T otherMarker = possibleSubordinate.getChild(1).getHead(); + if (possibleSubordinate.getHead().equals(chars.subgroup)) { + final T otherMarker = possibleSubordinate.getChild(1).getHead(); - if(subgroups.get(marker) > subgroups.get(otherMarker)) { + if (subgroups.get(marker) > subgroups.get(otherMarker)) { subgroupContents.prependChild(contents.pop()); } else { break; @@ -101,7 +101,7 @@ public class DelimiterGroup<T> { } } - Tree<T> subgroup = new Tree<>(chars.subgroup, subgroupContents, new Tree<>(marker)); + final Tree<T> subgroup = new Tree<>(chars.subgroup, subgroupContents, new Tree<>(marker)); //System.out.println("\tTRACE: generated subgroup\n" + subgroup + "\n\n"); contents.push(subgroup); @@ -111,26 +111,26 @@ public class DelimiterGroup<T> { /** * Convert this group into a tree. - * + * * @param closer * The item that closed this group. - * + * * @param chars * The characteristics for building the tree. - * + * * @return This group as a tree. */ - public ITree<T> toTree(T closer, SequenceCharacteristics<T> chars) { - if(impliedSubgroups.containsKey(closer)) { + public ITree<T> toTree(final T closer, final SequenceCharacteristics<T> chars) { + if (impliedSubgroups.containsKey(closer)) { markSubgroup(impliedSubgroups.get(closer), chars); } - ITree<T> res = new Tree<>(chars.contents); + final ITree<T> res = new Tree<>(chars.contents); - if(contents.isEmpty()) { + if (contents.isEmpty()) { currentGroup.forEach(res::addChild); } else { - while(!contents.isEmpty()) { + while (!contents.isEmpty()) { res.prependChild(contents.poll()); } @@ -142,7 +142,7 @@ public class DelimiterGroup<T> { @Override public String toString() { - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); builder.append("OpenGroup [contents="); builder.append(contents); @@ -157,34 +157,30 @@ public class DelimiterGroup<T> { /** * Check if a group is excluded at the top level of this group. - * + * * @param groupName * The group to check. - * + * * @return Whether or not the provided group is excluded. */ - public boolean excludes(T groupName) { + public boolean excludes(final T groupName) { return topLevelExclusions.contains(groupName); } /** * Check if the provided delimiter would close this group. - * + * * @param del * The string to check as a closing delimiter. - * + * * @return Whether or not the provided delimiter closes this * group. */ - public boolean isClosing(T del) { - if(closingDelimiters.contains(del)) { - return true; - } + public boolean isClosing(final T del) { + if (closingDelimiters.contains(del)) return true; - for(BiPredicate<T, T[]> pred : predClosers) { - if(pred.test(del, params)) { - return true; - } + for (final BiPredicate<T, T[]> pred : predClosers) { + if (pred.test(del, params)) return true; } return closingDelimiters.contains(del); @@ -192,7 +188,7 @@ public class DelimiterGroup<T> { /** * Get the name of the group this is an instance of. - * + * * @return The name of the group this is an instance of. */ public T getName() { @@ -201,7 +197,7 @@ public class DelimiterGroup<T> { /** * Get the groups that aren't allowed at all in this group. - * + * * @return The groups that aren't allowed at all in this group. */ public Set<T> getNestingExclusions() { @@ -211,7 +207,7 @@ public class DelimiterGroup<T> { /** * Get the groups that are allowed to open anywhere inside this * group. - * + * * @return The groups allowed to open anywhere inside this * group. */ @@ -221,36 +217,32 @@ public class DelimiterGroup<T> { /** * Checks if a given token marks a subgroup. - * + * * @param tok * The token to check. - * + * * @return Whether or not the token marks a subgroup. */ - public boolean marksSubgroup(T tok) { + public boolean marksSubgroup(final T tok) { return subgroups.containsKey(tok); } /** * Checks if a given token opens a group. - * + * * @param marker * The token to check. - * + * * @return The name of the group T opens, or null if it doesn't * open one. */ - public IPair<T, T[]> doesOpen(T marker) { - if(openDelimiters.containsKey(marker)) { - return new Pair<>(openDelimiters.get(marker), null); - } + public IPair<T, T[]> doesOpen(final T marker) { + if (openDelimiters.containsKey(marker)) return new Pair<>(openDelimiters.get(marker), null); - for(Function<T, IPair<T, T[]>> pred : predOpeners) { - IPair<T, T[]> par = pred.apply(marker); + for (final Function<T, IPair<T, T[]>> pred : predOpeners) { + final IPair<T, T[]> par = pred.apply(marker); - if(par.getLeft() != null) { - return par; - } + if (par.getLeft() != null) return par; } return new Pair<>(null, null); @@ -258,7 +250,7 @@ public class DelimiterGroup<T> { /** * Check if this group starts a new nesting scope. - * + * * @return Whether this group starts a new nesting scope. */ public boolean isForgetful() { @@ -274,48 +266,48 @@ public class DelimiterGroup<T> { /* * The delimiters that open groups at the top level of this group. */ - private Map<T, T> openDelimiters; + private final Map<T, T> openDelimiters; /* * The delimiters that open groups inside of this group. */ - private Map<T, T> nestedOpenDelimiters; + private final Map<T, T> nestedOpenDelimiters; /* * The delimiters that close this group. */ - private Set<T> closingDelimiters; + private final Set<T> closingDelimiters; /* * The groups that can't occur in the top level of this group. */ - private Set<T> topLevelExclusions; + private final Set<T> topLevelExclusions; /* * The groups that can't occur anywhere inside this group. */ - private Set<T> groupExclusions; + private final Set<T> groupExclusions; /* * Mapping from sub-group delimiters, to any sub-groups enclosed in * them. */ - private Map<T, Integer> subgroups; + private final Map<T, Integer> subgroups; /* * Subgroups implied by a particular closing delimiter */ - private Map<T, T> impliedSubgroups; + private final Map<T, T> impliedSubgroups; /* * Allows more complex openings */ - private List<Function<T, IPair<T, T[]>>> predOpeners; + private final List<Function<T, IPair<T, T[]>>> predOpeners; /* * Allow more complex closings */ - private List<BiPredicate<T, T[]>> predClosers; + private final List<BiPredicate<T, T[]>> predClosers; /* * Whether or not this group starts a new nesting set. @@ -324,12 +316,12 @@ public class DelimiterGroup<T> { /** * Create a new empty delimiter group. - * + * * @param name * The name of the delimiter group */ - public DelimiterGroup(T name) { - if(name == null) throw new NullPointerException("Group name must not be null"); + public DelimiterGroup(final T name) { + if (name == null) throw new NullPointerException("Group name must not be null"); groupName = name; @@ -350,25 +342,25 @@ public class DelimiterGroup<T> { /** * Adds one or more delimiters that close this group. - * + * * @param closers * Delimiters that close this group. */ @SafeVarargs - public final void addClosing(T... closers) { - List<T> closerList = Arrays.asList(closers); + public final void addClosing(final T... closers) { + final List<T> closerList = Arrays.asList(closers); - for(T closer : closerList) { - if(closer == null) { + for (final T closer : closerList) { + if (closer == null) throw new NullPointerException("Closing delimiter must not be null"); - } else if(closer.equals("")) { + else if (closer.equals("")) /* * We can do this because equals works on * arbitrary objects, not just those of the same * type. */ throw new IllegalArgumentException("Empty string is not a valid exclusion"); - } else { + else { closingDelimiters.add(closer); } } @@ -377,23 +369,23 @@ public class DelimiterGroup<T> { /** * Adds one or more groups that cannot occur in the top level of this * group. - * + * * @param exclusions * The groups forbidden in the top level of this group. */ @SafeVarargs - public final void addTopLevelForbid(T... exclusions) { - for(T exclusion : exclusions) { - if(exclusion == null) { + public final void addTopLevelForbid(final T... exclusions) { + for (final T exclusion : exclusions) { + if (exclusion == null) throw new NullPointerException("Exclusion must not be null"); - } else if(exclusion.equals("")) { + else if (exclusion.equals("")) /* * We can do this because equals works on * arbitrary objects, not just those of the same * type. */ throw new IllegalArgumentException("Empty string is not a valid exclusion"); - } else { + else { topLevelExclusions.add(exclusion); } } @@ -401,23 +393,23 @@ public class DelimiterGroup<T> { /** * Adds one or more groups that cannot occur at all in this group. - * + * * @param exclusions * The groups forbidden inside this group. */ @SafeVarargs - public final void addGroupForbid(T... exclusions) { - for(T exclusion : exclusions) { - if(exclusion == null) { + public final void addGroupForbid(final T... exclusions) { + for (final T exclusion : exclusions) { + if (exclusion == null) throw new NullPointerException("Exclusion must not be null"); - } else if(exclusion.equals("")) { + else if (exclusion.equals("")) /* * We can do this because equals works on * arbitrary objects, not just those of the same * type. */ throw new IllegalArgumentException("Empty string is not a valid exclusion"); - } else { + else { groupExclusions.add(exclusion); } } @@ -425,85 +417,78 @@ public class DelimiterGroup<T> { /** * Adds sub-group markers to this group. - * + * * @param subgroup * The token to mark a sub-group. - * + * * @param priority * The priority of this sub-group. */ - public void addSubgroup(T subgroup, int priority) { - if(subgroup == null) { - throw new NullPointerException("Subgroup marker must not be null"); - } + public void addSubgroup(final T subgroup, final int priority) { + if (subgroup == null) throw new NullPointerException("Subgroup marker must not be null"); subgroups.put(subgroup, priority); } /** * Adds a marker that opens a group at the top level of this group. - * + * * @param opener * The marker that opens the group. - * + * * @param group * The group opened by the marker. */ - public void addOpener(T opener, T group) { - if(opener == null) { + public void addOpener(final T opener, final T group) { + if (opener == null) throw new NullPointerException("Opener must not be null"); - } else if(group == null) { - throw new NullPointerException("Group to open must not be null"); - } + else if (group == null) throw new NullPointerException("Group to open must not be null"); openDelimiters.put(opener, group); } /** * Adds a marker that opens a group inside of this group. - * + * * @param opener * The marker that opens the group. - * + * * @param group * The group opened by the marker. */ - public void addNestedOpener(T opener, T group) { - if(opener == null) { + public void addNestedOpener(final T opener, final T group) { + if (opener == null) throw new NullPointerException("Opener must not be null"); - } else if(group == null) { - throw new NullPointerException("Group to open must not be null"); - } + else if (group == null) throw new NullPointerException("Group to open must not be null"); nestedOpenDelimiters.put(opener, group); } /** * Mark a closing delimiter as implying a subgroup. - * + * * @param closer * The closing delimiter. - * + * * @param subgroup * The subgroup to imply. */ - public void implySubgroup(T closer, T subgroup) { - if(closer == null) { + public void implySubgroup(final T closer, final T subgroup) { + if (closer == null) throw new NullPointerException("Closer must not be null"); - } else if(subgroup == null) { + else if (subgroup == null) throw new NullPointerException("Subgroup must not be null"); - } else if(!closingDelimiters.contains(closer)) { + else if (!closingDelimiters.contains(closer)) throw new IllegalArgumentException(String.format("No closing delimiter '%s' defined", closer)); - } else if(!subgroups.containsKey(subgroup)) { + else if (!subgroups.containsKey(subgroup)) throw new IllegalArgumentException(String.format("No subgroup '%s' defined", subgroup)); - } impliedSubgroups.put(closer, subgroup); } @Override public String toString() { - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); builder.append("("); @@ -512,26 +497,26 @@ public class DelimiterGroup<T> { builder.append("], "); builder.append("closingDelimiters=["); - for(T closer : closingDelimiters) { + for (final T closer : closingDelimiters) { builder.append(closer + ","); } builder.deleteCharAt(builder.length() - 1); builder.append("]"); - if(topLevelExclusions != null && !topLevelExclusions.isEmpty()) { + if (topLevelExclusions != null && !topLevelExclusions.isEmpty()) { builder.append(", "); builder.append("topLevelExclusions=["); - for(T exclusion : topLevelExclusions) { + for (final T exclusion : topLevelExclusions) { builder.append(exclusion + ","); } builder.deleteCharAt(builder.length() - 1); builder.append("]"); } - if(groupExclusions != null && !groupExclusions.isEmpty()) { + if (groupExclusions != null && !groupExclusions.isEmpty()) { builder.append(", "); builder.append("groupExclusions=["); - for(T exclusion : groupExclusions) { + for (final T exclusion : groupExclusions) { builder.append(exclusion + ","); } builder.deleteCharAt(builder.length() - 1); @@ -545,47 +530,47 @@ public class DelimiterGroup<T> { /** * Open an instance of this group. - * + * * @param opener * The item that opened this group. - * + * * @param parms * The parameters that opened this group - * + * * @return An opened instance of this group. */ - public OpenGroup open(T opener, T[] parms) { + public OpenGroup open(final T opener, final T[] parms) { return new OpenGroup(opener, parms); } /** * Adds a predicated opener to the top level of this group. - * + * * @param pred * The predicate that defines the opener and its * parameters. */ - public void addPredOpener(Function<T, IPair<T, T[]>> pred) { + public void addPredOpener(final Function<T, IPair<T, T[]>> pred) { predOpeners.add(pred); } /** * Adds a predicated closer to the top level of this group. - * + * * @param pred * The predicate that defines the closer. */ - public void addPredCloser(BiPredicate<T, T[]> pred) { + public void addPredCloser(final BiPredicate<T, T[]> pred) { predClosers.add(pred); } /** * Set whether or not this group starts a new nesting set. - * + * * @param forgetful * Whether this group starts a new nesting set. */ - public void setForgetful(boolean forgetful) { + public void setForgetful(final boolean forgetful) { this.forgetful = forgetful; } }
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java index dc94686..4b29949 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/RegexCloser.java @@ -4,29 +4,29 @@ import java.util.function.BiPredicate; /** * A predicated closer for use with {@link RegexOpener}. - * + * * @author bjculkin * */ public class RegexCloser implements BiPredicate<String, String[]> { - private String rep; + private final String rep; /** * Create a new regex closer. - * + * * @param closer * The format string to use for closing. */ - public RegexCloser(String closer) { + public RegexCloser(final String closer) { rep = closer; } @Override - public boolean test(String closer, String[] params) { + public boolean test(final String closer, final String[] params) { /* * Confirm passing an array instead of a single var-arg. */ - String work = String.format(rep, (Object[]) params); + final String work = String.format(rep, (Object[]) params); return work.equals(closer); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java index 7b4aac0..98c1dc1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/RegexOpener.java @@ -1,46 +1,46 @@ package bjc.utils.parserutils.delims; -import bjc.utils.data.IPair; -import bjc.utils.data.Pair; - import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; +import bjc.utils.data.IPair; +import bjc.utils.data.Pair; + /** * A predicated opener for use with {@link RegexOpener} - * + * * @author bjculkin * */ public class RegexOpener implements Function<String, IPair<String, String[]>> { - private String name; + private final String name; - private Pattern patt; + private final Pattern patt; /** * Create a new regex opener. - * + * * @param groupName * The name of the opened group. - * + * * @param groupRegex * The regex that matches the opener. */ - public RegexOpener(String groupName, String groupRegex) { + public RegexOpener(final String groupName, final String groupRegex) { name = groupName; patt = Pattern.compile(groupRegex); } @Override - public IPair<String, String[]> apply(String str) { - Matcher m = patt.matcher(str); + public IPair<String, String[]> apply(final String str) { + final Matcher m = patt.matcher(str); if (m.matches()) { - int numGroups = m.groupCount(); + final int numGroups = m.groupCount(); - String[] parms = new String[numGroups + 1]; + final String[] parms = new String[numGroups + 1]; for (int i = 0; i <= numGroups; i++) { parms[i] = m.group(i); diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java index 5dcda2d..882b4c5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceCharacteristics.java @@ -2,7 +2,7 @@ package bjc.utils.parserutils.delims; /** * Marks the parameters for building a sequence tree. - * + * * @author EVE * * @param <T> @@ -27,7 +27,7 @@ public class SequenceCharacteristics<T> { /** * Create a new set of parameters for building a tree. - * + * * @param root * The root marker. * @param contents @@ -35,7 +35,7 @@ public class SequenceCharacteristics<T> { * @param subgroup * The subgroup marker. */ - public SequenceCharacteristics(T root, T contents, T subgroup) { + public SequenceCharacteristics(final T root, final T contents, final T subgroup) { this.root = root; this.contents = contents; this.subgroup = subgroup; @@ -46,39 +46,39 @@ public class SequenceCharacteristics<T> { final int prime = 31; int result = 1; - result = prime * result + ((contents == null) ? 0 : contents.hashCode()); - result = prime * result + ((root == null) ? 0 : root.hashCode()); - result = prime * result + ((subgroup == null) ? 0 : subgroup.hashCode()); + result = prime * result + (contents == null ? 0 : contents.hashCode()); + result = prime * result + (root == null ? 0 : root.hashCode()); + result = prime * result + (subgroup == null ? 0 : subgroup.hashCode()); return result; } @Override - public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(!(obj instanceof SequenceCharacteristics)) return false; + public boolean equals(final Object obj) { + if (this == obj) return true; + if (obj == null) return false; + if (!(obj instanceof SequenceCharacteristics)) return false; - SequenceCharacteristics<?> other = (SequenceCharacteristics<?>) obj; + final SequenceCharacteristics<?> other = (SequenceCharacteristics<?>) obj; - if(contents == null) { - if(other.contents != null) return false; - } else if(!contents.equals(other.contents)) return false; + if (contents == null) { + if (other.contents != null) return false; + } else if (!contents.equals(other.contents)) return false; - if(root == null) { - if(other.root != null) return false; - } else if(!root.equals(other.root)) return false; + if (root == null) { + if (other.root != null) return false; + } else if (!root.equals(other.root)) return false; - if(subgroup == null) { - if(other.subgroup != null) return false; - } else if(!subgroup.equals(other.subgroup)) return false; + if (subgroup == null) { + if (other.subgroup != null) return false; + } else if (!subgroup.equals(other.subgroup)) return false; return true; } @Override public String toString() { - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); builder.append("SequenceCharacteristics [root="); builder.append(root == null ? "(null)" : root); diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java index e723123..48d85c1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/SequenceDelimiter.java @@ -1,5 +1,14 @@ package bjc.utils.parserutils.delims; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; + +import com.google.common.collect.HashMultimap; +import com.google.common.collect.HashMultiset; +import com.google.common.collect.Multimap; +import com.google.common.collect.Multiset; + import bjc.utils.data.IPair; import bjc.utils.data.ITree; import bjc.utils.data.Tree; @@ -9,18 +18,9 @@ import bjc.utils.esodata.Stack; import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.StringUtils; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.HashMultiset; -import com.google.common.collect.Multimap; -import com.google.common.collect.Multiset; - -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - /** * Convert linear sequences into trees that represent group structure. - * + * * @author EVE * * @param <T> @@ -30,7 +30,7 @@ public class SequenceDelimiter<T> { /* * Mapping from group names to actual groups. */ - private Map<T, DelimiterGroup<T>> groups; + private final Map<T, DelimiterGroup<T>> groups; private DelimiterGroup<T> initialGroup; @@ -44,64 +44,62 @@ public class SequenceDelimiter<T> { /** * Convert a linear sequence into a tree that matches the delimiter * structure. - * + * * Essentially, creates a parse tree of the expression against the * following grammar while obeying the defined grouping rules. - * + * * <pre> * <tree> -> (<data> | <subgroup> | <group>)* * <subgroup> -> <tree> <marker> * <group> -> <open> <tree> <close> - * + * * <data> -> STRING * <open> -> STRING * <close> -> STRING * <marker> -> STRING * </pre> - * + * * @param chars * The parameters on how to mark certain portions of the * tree. * @param seq * The sequence to delimit. - * + * * @return The sequence as a tree that matches its group structure. Each * node in the tree is either a data node, a subgroup node, or a * group node. - * + * * A data node is a leaf node whose data is the string it * represents. - * + * * A subgroup node is a node with two children, and the name of * the sub-group as its label. The first child is the contents * of the sub-group, and the second is the marker that started * the subgroup. The marker is a leaf node labeled with its * contents, and the contents contains a recursive tree. - * + * * A group node is a node with three children, and the name of * the group as its label. The first child is the opening * delimiter, the second is the group contents, and the third is * the closing delimiter. The delimiters are leaf nodes labeled * with their contents, while the group node contains a * recursive tree. - * + * * @throws DelimiterException * Thrown if something went wrong during sequence * delimitation. - * + * */ - public ITree<T> delimitSequence(SequenceCharacteristics<T> chars, @SuppressWarnings("unchecked") T... seq) - throws DelimiterException { - if(initialGroup == null) { + public ITree<T> delimitSequence(final SequenceCharacteristics<T> chars, + @SuppressWarnings("unchecked") final T... seq) throws DelimiterException { + if (initialGroup == null) throw new NullPointerException("Initial group must be specified."); - } else if(chars == null) { - throw new NullPointerException("Sequence characteristics must not be null"); - } + else if (chars == null) throw new NullPointerException("Sequence characteristics must not be null"); /* * The stack of opened and not yet closed groups. */ - Stack<DelimiterGroup<T>.OpenGroup> groupStack = new SimpleStack<>(); + final Stack<DelimiterGroup<T>.OpenGroup> groupStack = new SimpleStack<>(); /* * Open initial group. @@ -111,34 +109,34 @@ public class SequenceDelimiter<T> { /* * Groups that aren't allowed to be opened at the moment. */ - Stack<Multiset<T>> forbiddenDelimiters = new SimpleStack<>(); + final Stack<Multiset<T>> forbiddenDelimiters = new SimpleStack<>(); forbiddenDelimiters.push(HashMultiset.create()); /* * Groups that are allowed to be opened at the moment. */ - Stack<Multimap<T, T>> allowedDelimiters = new SimpleStack<>(); + final Stack<Multimap<T, T>> allowedDelimiters = new SimpleStack<>(); allowedDelimiters.push(HashMultimap.create()); /* * Map of who forbid what for debugging purposes. */ - IMap<T, T> whoForbid = new PushdownMap<>(); + final IMap<T, T> whoForbid = new PushdownMap<>(); - for(int i = 0; i < seq.length; i++) { - T tok = seq[i]; + for (int i = 0; i < seq.length; i++) { + final T tok = seq[i]; - IPair<T, T[]> possibleOpenPar = groupStack.top().doesOpen(tok); + final IPair<T, T[]> possibleOpenPar = groupStack.top().doesOpen(tok); T possibleOpen = possibleOpenPar.getLeft(); - if(possibleOpen == null) { + if (possibleOpen == null) { /* * Handle nested openers. - * + * * Local openers take priority over nested ones * if they overlap. */ - if(allowedDelimiters.top().containsKey(tok)) { + if (allowedDelimiters.top().containsKey(tok)) { possibleOpen = allowedDelimiters.top().get(tok).iterator().next(); } } @@ -146,29 +144,29 @@ public class SequenceDelimiter<T> { /* * If we have an opening delimiter, handle it. */ - if(possibleOpen != null) { - DelimiterGroup<T> group = groups.get(possibleOpen); + if (possibleOpen != null) { + final DelimiterGroup<T> group = groups.get(possibleOpen); /* * Error on groups that can't open in this * context. - * + * * This means groups that can't occur at the * top-level of this group, as well as nested * exclusions from all enclosing groups. */ - if(isForbidden(groupStack, forbiddenDelimiters, possibleOpen)) { - StringBuilder msgBuilder = new StringBuilder(); + if (isForbidden(groupStack, forbiddenDelimiters, possibleOpen)) { + final StringBuilder msgBuilder = new StringBuilder(); T forbiddenBy; - if(whoForbid.containsKey(tok)) { + if (whoForbid.containsKey(tok)) { forbiddenBy = whoForbid.get(tok); } else { forbiddenBy = groupStack.top().getName(); } - String ctxList = StringUtils.toEnglishList(groupStack.toArray(), "then"); + final String ctxList = StringUtils.toEnglishList(groupStack.toArray(), "then"); msgBuilder.append("Group '"); msgBuilder.append(group); @@ -184,13 +182,13 @@ public class SequenceDelimiter<T> { /* * Add an open group. */ - DelimiterGroup<T>.OpenGroup open = group.open(tok, possibleOpenPar.getRight()); + final DelimiterGroup<T>.OpenGroup open = group.open(tok, possibleOpenPar.getRight()); groupStack.push(open); /* * Handle 'forgetful' groups that reset nesting */ - if(open.isForgetful()) { + if (open.isForgetful()) { allowedDelimiters.push(HashMultimap.create()); forbiddenDelimiters.push(HashMultiset.create()); } @@ -198,33 +196,33 @@ public class SequenceDelimiter<T> { /* * Add the nested opens from this group. */ - Multimap<T, T> currentAllowed = allowedDelimiters.top(); - for(Entry<T, T> opener : open.getNestingOpeners().entrySet()) { + final Multimap<T, T> currentAllowed = allowedDelimiters.top(); + for (final Entry<T, T> opener : open.getNestingOpeners().entrySet()) { currentAllowed.put(opener.getKey(), opener.getValue()); } /* * Add the nested exclusions from this group */ - Multiset<T> currentForbidden = forbiddenDelimiters.top(); - for(T exclusion : open.getNestingExclusions()) { + final Multiset<T> currentForbidden = forbiddenDelimiters.top(); + for (final T exclusion : open.getNestingExclusions()) { currentForbidden.add(exclusion); whoForbid.put(exclusion, possibleOpen); } - } else if(!groupStack.empty() && groupStack.top().isClosing(tok)) { + } else if (!groupStack.empty() && groupStack.top().isClosing(tok)) { /* * Close the group. */ - DelimiterGroup<T>.OpenGroup closed = groupStack.pop(); + final DelimiterGroup<T>.OpenGroup closed = groupStack.pop(); groupStack.top().addItem(closed.toTree(tok, chars)); /* * Remove nested exclusions from this group. */ - Multiset<T> currentForbidden = forbiddenDelimiters.top(); - for(T excludedGroup : closed.getNestingExclusions()) { + final Multiset<T> currentForbidden = forbiddenDelimiters.top(); + for (final T excludedGroup : closed.getNestingExclusions()) { currentForbidden.remove(excludedGroup); whoForbid.remove(excludedGroup); @@ -233,19 +231,19 @@ public class SequenceDelimiter<T> { /* * Remove the nested opens from this group. */ - Multimap<T, T> currentAllowed = allowedDelimiters.top(); - for(Entry<T, T> closer : closed.getNestingOpeners().entrySet()) { + final Multimap<T, T> currentAllowed = allowedDelimiters.top(); + for (final Entry<T, T> closer : closed.getNestingOpeners().entrySet()) { currentAllowed.remove(closer.getKey(), closer.getValue()); } /* * Handle 'forgetful' groups that reset nesting. */ - if(closed.isForgetful()) { + if (closed.isForgetful()) { allowedDelimiters.drop(); forbiddenDelimiters.drop(); } - } else if(!groupStack.empty() && groupStack.top().marksSubgroup(tok)) { + } else if (!groupStack.empty() && groupStack.top().marksSubgroup(tok)) { groupStack.top().markSubgroup(tok, chars); } else { groupStack.top().addItem(new Tree<>(tok)); @@ -255,14 +253,15 @@ public class SequenceDelimiter<T> { /* * Error if not all groups were closed. */ - if(groupStack.size() > 1) { - DelimiterGroup<T>.OpenGroup group = groupStack.top(); + if (groupStack.size() > 1) { + final DelimiterGroup<T>.OpenGroup group = groupStack.top(); - StringBuilder msgBuilder = new StringBuilder(); + final StringBuilder msgBuilder = new StringBuilder(); - String closingDelims = StringUtils.toEnglishList(group.getNestingExclusions().toArray(), false); + final String closingDelims = StringUtils.toEnglishList(group.getNestingExclusions().toArray(), + false); - String ctxList = StringUtils.toEnglishList(groupStack.toArray(), "then"); + final String ctxList = StringUtils.toEnglishList(groupStack.toArray(), "then"); msgBuilder.append("Unclosed group '"); msgBuilder.append(group.getName()); @@ -277,35 +276,34 @@ public class SequenceDelimiter<T> { return groupStack.pop().toTree(chars.root, chars); } - private boolean isForbidden(Stack<DelimiterGroup<T>.OpenGroup> groupStack, - Stack<Multiset<T>> forbiddenDelimiters, T groupName) { + private boolean isForbidden(final Stack<DelimiterGroup<T>.OpenGroup> groupStack, + final Stack<Multiset<T>> forbiddenDelimiters, final T groupName) { boolean localForbid; - if(groupStack.empty()) + if (groupStack.empty()) { localForbid = false; - else + } else { localForbid = groupStack.top().excludes(groupName); + } return localForbid || forbiddenDelimiters.top().contains(groupName); } /** * Add a delimiter group. - * + * * @param group * The delimiter group. */ - public void addGroup(DelimiterGroup<T> group) { - if(group == null) { - throw new NullPointerException("Group must not be null"); - } + public void addGroup(final DelimiterGroup<T> group) { + if (group == null) throw new NullPointerException("Group must not be null"); groups.put(group.groupName, group); } /** * Creates and adds a delimiter group using the provided settings. - * + * * @param openers * The tokens that open this group * @param groupName @@ -313,31 +311,31 @@ public class SequenceDelimiter<T> { * @param closers * The tokens that close this group */ - public void addGroup(T[] openers, T groupName, @SuppressWarnings("unchecked") T... closers) { - DelimiterGroup<T> group = new DelimiterGroup<>(groupName); + public void addGroup(final T[] openers, final T groupName, @SuppressWarnings("unchecked") final T... closers) { + final DelimiterGroup<T> group = new DelimiterGroup<>(groupName); group.addClosing(closers); addGroup(group); - for(T open : openers) { + for (final T open : openers) { group.addOpener(open, groupName); } } @Override public String toString() { - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); builder.append("SequenceDelimiter ["); - if(groups != null) { + if (groups != null) { builder.append("groups="); builder.append(groups); builder.append(","); } - if(initialGroup != null) { + if (initialGroup != null) { builder.append("initialGroup="); builder.append(initialGroup); } @@ -349,11 +347,11 @@ public class SequenceDelimiter<T> { /** * Set the initial group of this delimiter. - * + * * @param initialGroup * The initial group of this delimiter. */ - public void setInitialGroup(DelimiterGroup<T> initialGroup) { + public void setInitialGroup(final DelimiterGroup<T> initialGroup) { this.initialGroup = initialGroup; } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java index 6db1c98..e3eeea5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/delims/StringDelimiter.java @@ -4,7 +4,7 @@ import bjc.utils.data.ITree; /** * A sequence delimiter specialized for strings. - * + * * @author EVE * */ @@ -14,18 +14,18 @@ public class StringDelimiter extends SequenceDelimiter<String> { * Override of * {@link SequenceDelimiter#delimitSequence(SequenceCharacteristics, Object...)} * for ease of use for strings. - * + * * @param seq * The sequence to delimit. - * + * * @return The sequence as a tree. - * + * * @throws DelimiterException * if something went wrong with delimiting the sequence. - * + * * @see SequenceDelimiter */ - public ITree<String> delimitSequence(String... seq) throws DelimiterException { + public ITree<String> delimitSequence(final String... seq) throws DelimiterException { return super.delimitSequence(new SequenceCharacteristics<>("root", "contents", "subgroup"), seq); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/SimpleTokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/SimpleTokenSplitter.java index ccc823d..b30cec1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/SimpleTokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/SimpleTokenSplitter.java @@ -6,7 +6,7 @@ import java.util.regex.Pattern; /** * Simple implementation of {@link TokenSplitter} - * + * * @author EVE */ @Deprecated @@ -15,7 +15,7 @@ public class SimpleTokenSplitter implements 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> @@ -52,9 +52,9 @@ public class SimpleTokenSplitter implements TokenSplitter { /* * These represent info for debugging. */ - private Set<String> delimSet; - private Set<String> multidelimSet; - private Set<String> exclusionSet; + private final Set<String> delimSet; + private final Set<String> multidelimSet; + private final Set<String> exclusionSet; /** * Create a new token splitter. @@ -66,14 +66,14 @@ public class SimpleTokenSplitter implements TokenSplitter { } @Override - public String[] split(String inp) { - if(compPatt == null) throw new IllegalStateException("Token splitter has not been compiled yet"); + public String[] split(final 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 (exclusionPatt.matcher(inp).matches()) return new String[] { inp }; return compPatt.split(inp); } @@ -88,14 +88,14 @@ public class SimpleTokenSplitter implements TokenSplitter { * @param delims * The delimiters to match on. */ - public void addDelimiter(String... delims) { - for(String delim : delims) { - if(delim == null) throw new NullPointerException("Delim must not be null"); + public void addDelimiter(final String... delims) { + for (final String delim : delims) { + if (delim == null) throw new NullPointerException("Delim must not be null"); - String quoteDelim = Pattern.quote(delim); - String delimPat = String.format(WITH_DELIM, quoteDelim); + final String quoteDelim = Pattern.quote(delim); + final String delimPat = String.format(WITH_DELIM, quoteDelim); - if(currPatt == null) { + if (currPatt == null) { currPatt = new StringBuilder(); currExclusionPatt = new StringBuilder(); @@ -119,13 +119,13 @@ public class SimpleTokenSplitter implements TokenSplitter { * @param delims * The delimiter to split on. */ - public void addMultiDelimiter(String... delims) { - for(String delim : delims) { - if(delim == null) throw new NullPointerException("Delim must not be null"); + public void addMultiDelimiter(final String... delims) { + for (final String delim : delims) { + if (delim == null) throw new NullPointerException("Delim must not be null"); - String delimPat = String.format(WITH_MULTI_DELIM, "(?:" + delim + ")"); + final String delimPat = String.format(WITH_MULTI_DELIM, "(?:" + delim + ")"); - if(currPatt == null) { + if (currPatt == null) { currPatt = new StringBuilder(); currExclusionPatt = new StringBuilder(); @@ -147,11 +147,11 @@ public class SimpleTokenSplitter implements TokenSplitter { * @param delims * The regex to not splitting matching strings. */ - public void addNonMatcher(String... delims) { - for(String delim : delims) { - if(delim == null) throw new NullPointerException("Delim must not be null"); + public void addNonMatcher(final String... delims) { + for (final String delim : delims) { + if (delim == null) throw new NullPointerException("Delim must not be null"); - if(currPatt == null) { + if (currPatt == null) { currPatt = new StringBuilder(); currExclusionPatt = new StringBuilder(); @@ -170,8 +170,12 @@ public class SimpleTokenSplitter implements TokenSplitter { * Makes this splitter ready to use. */ public void compile() { - if(currPatt == null) currPatt = new StringBuilder(); - if(currExclusionPatt == null) currExclusionPatt = new StringBuilder(); + if (currPatt == null) { + currPatt = new StringBuilder(); + } + if (currExclusionPatt == null) { + currExclusionPatt = new StringBuilder(); + } compPatt = Pattern.compile(currPatt.toString()); exclusionPatt = Pattern.compile(currExclusionPatt.toString()); @@ -179,52 +183,52 @@ public class SimpleTokenSplitter implements TokenSplitter { /* * (non-Javadoc) - * + * * @see java.lang.Object#toString() */ @Override public String toString() { - StringBuilder builder = new StringBuilder(); + final StringBuilder builder = new StringBuilder(); builder.append("SimpleTokenSplitter ["); - if(currPatt != null) { + if (currPatt != null) { builder.append("currPatt="); builder.append(currPatt); builder.append("\n\t, "); } - if(currExclusionPatt != null) { + if (currExclusionPatt != null) { builder.append("currExclusionPatt="); builder.append(currExclusionPatt); builder.append("\n\t, "); } - if(compPatt != null) { + if (compPatt != null) { builder.append("compPatt="); builder.append(compPatt); builder.append("\n\t, "); } - if(exclusionPatt != null) { + if (exclusionPatt != null) { builder.append("exclusionPatt="); builder.append(exclusionPatt); builder.append("\n\t, "); } - if(delimSet != null) { + if (delimSet != null) { builder.append("delimSet="); builder.append(delimSet); builder.append("\n\t, "); } - if(multidelimSet != null) { + if (multidelimSet != null) { builder.append("multidelimSet="); builder.append(multidelimSet); builder.append("\n\t, "); } - if(exclusionSet != null) { + if (exclusionSet != null) { builder.append("exclusionSet="); builder.append(exclusionSet); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TokenSplitter.java index 04551a7..6fd9f7b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TokenSplitter.java @@ -14,7 +14,7 @@ public interface TokenSplitter { * <p> * The splitter must be compiled first. * </p> - * + * * @param inp * The string to split. * diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TwoLevelSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TwoLevelSplitter.java index 1d6d0a2..92b9de0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TwoLevelSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitter/TwoLevelSplitter.java @@ -6,22 +6,22 @@ import java.util.regex.Pattern; /** * Implementation of a splitter that runs in two passes. - * + * * This is useful because {@link SimpleTokenSplitter} doesn't like handling both * <= and = without mangling them. - * + * * The first pass splits on compound operators, which are built up from simple * operators. - * + * * The second pass removes simple operators. - * + * * @author EVE * */ @Deprecated public class TwoLevelSplitter implements TokenSplitter { - private SimpleTokenSplitter high; - private SimpleTokenSplitter low; + private final SimpleTokenSplitter high; + private final SimpleTokenSplitter low; /** * Create a new two level splitter. @@ -32,15 +32,15 @@ public class TwoLevelSplitter implements TokenSplitter { } @Override - public String[] split(String inp) { - List<String> ret = new ArrayList<>(); + public String[] split(final String inp) { + final List<String> ret = new ArrayList<>(); - String[] partials = high.split(inp); + final String[] partials = high.split(inp); - for(String partial : partials) { - String[] finals = low.split(partial); + for (final String partial : partials) { + final String[] finals = low.split(partial); - for(String fin : finals) { + for (final String fin : finals) { ret.add(fin); } } @@ -50,12 +50,12 @@ public class TwoLevelSplitter implements TokenSplitter { /** * Adds compound operators to split on. - * + * * @param delims * The compound operators to split on. */ - public void addCompoundDelim(String... delims) { - for(String delim : delims) { + public void addCompoundDelim(final String... delims) { + for (final String delim : delims) { high.addDelimiter(delim); low.addNonMatcher(Pattern.quote(delim)); @@ -64,24 +64,24 @@ public class TwoLevelSplitter implements TokenSplitter { /** * Adds simple operators to split on. - * + * * @param delims * The simple operators to split on. */ - public void addSimpleDelim(String... delims) { - for(String delim : delims) { + public void addSimpleDelim(final String... delims) { + for (final String delim : delims) { low.addDelimiter(delim); } } /** * Adds repeated compound operators to split on. - * + * * @param delims * The repeated compound operators to split on. */ - public void addCompoundMulti(String... delims) { - for(String delim : delims) { + public void addCompoundMulti(final String... delims) { + for (final String delim : delims) { high.addMultiDelimiter(delim); low.addNonMatcher("(?:" + delim + ")+"); @@ -90,12 +90,12 @@ public class TwoLevelSplitter implements TokenSplitter { /** * Adds simple compound operators to split on. - * + * * @param delims * The repeated simple operators to split on. */ - public void addSimpleMulti(String... delims) { - for(String delim : delims) { + public void addSimpleMulti(final String... delims) { + for (final String delim : delims) { low.addMultiDelimiter(delim); } } @@ -106,8 +106,8 @@ public class TwoLevelSplitter implements TokenSplitter { * @param exclusions * The regexes to exclude matches for. */ - public void exclude(String... exclusions) { - for(String exclusion : exclusions) { + public void exclude(final String... exclusions) { + for (final String exclusion : exclusions) { high.addNonMatcher(exclusion); low.addNonMatcher(exclusion); diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ChainTokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ChainTokenSplitter.java index 2ecadaf..75afd34 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ChainTokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ChainTokenSplitter.java @@ -6,12 +6,12 @@ import bjc.utils.functypes.ID; /** * A token splitter that chains several other splitters together. - * + * * @author EVE * */ public class ChainTokenSplitter implements TokenSplitter { - private IList<TokenSplitter> spliters; + private final IList<TokenSplitter> spliters; /** * Create a new chain token splitter. @@ -22,27 +22,27 @@ public class ChainTokenSplitter implements TokenSplitter { /** * Append a series of splitters to the chain. - * + * * @param splitters * The splitters to append to the chain. */ - public void appendSplitters(TokenSplitter... splitters) { + public void appendSplitters(final TokenSplitter... splitters) { spliters.addAll(splitters); } /** * Prepend a series of splitters to the chain. - * + * * @param splitters * The splitters to append to the chain. */ - public void prependSplitters(TokenSplitter... splitters) { + public void prependSplitters(final TokenSplitter... splitters) { spliters.prependAll(splitters); } @Override - public IList<String> split(String input) { - IList<String> initList = new FunctionalList<>(input); + public IList<String> split(final String input) { + final IList<String> initList = new FunctionalList<>(input); return spliters.reduceAux(initList, (splitter, strangs) -> { return strangs.flatMap(splitter::split); diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ConfigurableTokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ConfigurableTokenSplitter.java index 021821a..b9503bc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ConfigurableTokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ConfigurableTokenSplitter.java @@ -1,32 +1,32 @@ package bjc.utils.parserutils.splitterv2; -import bjc.utils.funcdata.IList; +import static bjc.utils.PropertyDB.applyFormat; import java.util.LinkedHashSet; import java.util.Set; import java.util.regex.Pattern; -import static bjc.utils.PropertyDB.applyFormat; +import bjc.utils.funcdata.IList; /** * Split a string into pieces around a regular expression, and offer an easy way * to configure the regular expression. - * + * * @author EVE * */ public class ConfigurableTokenSplitter extends SimpleTokenSplitter { - private Set<String> simpleDelimiters; - private Set<String> multipleDelimiters; - private Set<String> rRawDelimiters; + private final Set<String> simpleDelimiters; + private final Set<String> multipleDelimiters; + private final Set<String> rRawDelimiters; /** * Create a new token splitter with blank configuration. - * + * * @param keepDelims * Whether or not to keep delimiters. */ - public ConfigurableTokenSplitter(boolean keepDelims) { + public ConfigurableTokenSplitter(final boolean keepDelims) { super(null, keepDelims); /* @@ -39,44 +39,44 @@ public class ConfigurableTokenSplitter extends SimpleTokenSplitter { /** * Add a set of simple delimiters to this splitter. - * + * * Simple delimiters match one occurrence of themselves as literals. - * + * * @param simpleDelims * The simple delimiters to add. */ - public void addSimpleDelimiters(String... simpleDelims) { - for(String simpleDelim : simpleDelims) { + public void addSimpleDelimiters(final String... simpleDelims) { + for (final String simpleDelim : simpleDelims) { simpleDelimiters.add(simpleDelim); } } /** * Add a set of multiple delimiters to this splitter. - * + * * Multiple delimiters match one or more occurrences of themselves as * literals. - * + * * @param multiDelims * The multiple delimiters to add. */ - public void addMultiDelimiters(String... multiDelims) { - for(String multiDelim : multiDelims) { + public void addMultiDelimiters(final String... multiDelims) { + for (final String multiDelim : multiDelims) { multipleDelimiters.add(multiDelim); } } /** * Add a set of raw delimiters to this splitter. - * + * * Raw delimiters match one occurrence of themselves as regular * expressions. - * + * * @param rRawDelims * The raw delimiters to add. */ - public void addRawDelimiters(String... rRawDelims) { - for(String rRawDelim : rRawDelims) { + public void addRawDelimiters(final String... rRawDelims) { + for (final String rRawDelim : rRawDelims) { rRawDelimiters.add(rRawDelim); } } @@ -86,17 +86,17 @@ public class ConfigurableTokenSplitter extends SimpleTokenSplitter { * use when splitting. */ public void compile() { - StringBuilder rPattern = new StringBuilder(); + final StringBuilder rPattern = new StringBuilder(); - for(String rRawDelimiter : rRawDelimiters) { + for (final String rRawDelimiter : rRawDelimiters) { rPattern.append(applyFormat("rawDelim", rRawDelimiter)); } - for(String multipleDelimiter : multipleDelimiters) { + for (final String multipleDelimiter : multipleDelimiters) { rPattern.append(applyFormat("multipleDelim", multipleDelimiter)); } - for(String simpleDelimiter : simpleDelimiters) { + for (final String simpleDelimiter : simpleDelimiters) { rPattern.append(applyFormat("simpleDelim", simpleDelimiter)); } @@ -106,17 +106,15 @@ public class ConfigurableTokenSplitter extends SimpleTokenSplitter { } @Override - public IList<String> split(String input) { - if(spliter == null) { - throw new IllegalStateException("Must compile splitter before use"); - } + public IList<String> split(final String input) { + if (spliter == null) throw new IllegalStateException("Must compile splitter before use"); return super.split(input); } @Override public String toString() { - String fmt = "ConfigurableTokenSplitter [simpleDelimiters=%s, multipleDelimiters=%s," + final String fmt = "ConfigurableTokenSplitter [simpleDelimiters=%s, multipleDelimiters=%s," + " rRawDelimiters=%s, spliter=%s]"; return String.format(fmt, simpleDelimiters, multipleDelimiters, rRawDelimiters, spliter); diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ExcludingTokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ExcludingTokenSplitter.java index 25bddf5..c5f6f03 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ExcludingTokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/ExcludingTokenSplitter.java @@ -1,32 +1,32 @@ package bjc.utils.parserutils.splitterv2; -import bjc.utils.funcdata.FunctionalList; -import bjc.utils.funcdata.IList; - import java.util.HashSet; import java.util.Set; import java.util.function.Predicate; +import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IList; + /** * A token splitter that will not split certain tokens. - * + * * @author EVE * */ public class ExcludingTokenSplitter implements TokenSplitter { - private Set<String> literalExclusions; + private final Set<String> literalExclusions; - private IList<Predicate<String>> predExclusions; + private final IList<Predicate<String>> predExclusions; - private TokenSplitter spliter; + private final TokenSplitter spliter; /** * Create a new excluding token splitter. - * + * * @param splitter * The splitter to apply to non-excluded strings. */ - public ExcludingTokenSplitter(TokenSplitter splitter) { + public ExcludingTokenSplitter(final TokenSplitter splitter) { spliter = splitter; literalExclusions = new HashSet<>(); @@ -36,12 +36,12 @@ public class ExcludingTokenSplitter implements TokenSplitter { /** * Exclude literal strings from splitting. - * + * * @param exclusions * The strings to exclude from splitting. */ - public final void addLiteralExclusions(String... exclusions) { - for (String exclusion : exclusions) { + public final void addLiteralExclusions(final String... exclusions) { + for (final String exclusion : exclusions) { literalExclusions.add(exclusion); } } @@ -49,25 +49,23 @@ public class ExcludingTokenSplitter implements TokenSplitter { /** * Exclude all of the strings matching any of the predicates from * splitting. - * + * * @param exclusions * The predicates to use for exclusions. */ @SafeVarargs - public final void addPredicateExclusion(Predicate<String>... exclusions) { - for (Predicate<String> exclusion : exclusions) { + public final void addPredicateExclusion(final Predicate<String>... exclusions) { + for (final Predicate<String> exclusion : exclusions) { predExclusions.add(exclusion); } } @Override - public IList<String> split(String input) { - if (literalExclusions.contains(input)) { + public IList<String> split(final String input) { + if (literalExclusions.contains(input)) return new FunctionalList<>(input); - } else if (predExclusions.anyMatch(pred -> pred.test(input))) { + else if (predExclusions.anyMatch(pred -> pred.test(input))) return new FunctionalList<>(input); - } else { - return spliter.split(input); - } + else return spliter.split(input); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/SimpleTokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/SimpleTokenSplitter.java index b111ca3..ce1c336 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/SimpleTokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/SimpleTokenSplitter.java @@ -1,44 +1,42 @@ package bjc.utils.parserutils.splitterv2; +import java.util.regex.Pattern; + import bjc.utils.funcdata.IList; import bjc.utils.functypes.ID; import bjc.utils.ioutils.RegexStringEditor; -import java.util.regex.Pattern; - /** * Splits a string into pieces around a regular expression. - * + * * @author EVE * */ public class SimpleTokenSplitter implements TokenSplitter { protected Pattern spliter; - private boolean keepDelim; + private final boolean keepDelim; /** * Create a new simple token splitter. - * + * * @param splitter * The pattern to split around. - * + * * @param keepDelims * Whether or not delimiters should be kept. */ - public SimpleTokenSplitter(Pattern splitter, boolean keepDelims) { + public SimpleTokenSplitter(final Pattern splitter, final boolean keepDelims) { spliter = splitter; keepDelim = keepDelims; } @Override - public IList<String> split(String input) { - if(keepDelim) { + public IList<String> split(final String input) { + if (keepDelim) return RegexStringEditor.mapOccurances(input, spliter, ID.id(), ID.id()); - } else { - return RegexStringEditor.mapOccurances(input, spliter, ID.id(), strang -> ""); - } + else return RegexStringEditor.mapOccurances(input, spliter, ID.id(), strang -> ""); } @Override diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/TokenSplitter.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/TokenSplitter.java index 5d510c1..ad6865d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/TokenSplitter.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/splitterv2/TokenSplitter.java @@ -4,17 +4,17 @@ import bjc.utils.funcdata.IList; /** * Split a string into a list of pieces. - * + * * @author EVE * */ public interface TokenSplitter { /** * Split a string into a list of pieces. - * + * * @param input * The string to split. - * + * * @return The pieces of the string. */ public IList<String> split(String input); |
