From b4f5f98c0aa7fc892e96771ff2df729e61c21f74 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 18 Apr 2016 19:56:32 -0400 Subject: Minor code changes --- .../java/bjc/utils/parserutils/ShuntingYard.java | 30 ++++++++-------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java') 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 9c7618b..0e34b97 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/ShuntingYard.java @@ -87,7 +87,9 @@ public class ShuntingYard { stack.push(token); } else if (StringUtils.containsOnly(token, "\\)")) { // Handle groups of parenthesis for multiple nesting levels - while (stack.peek().equals(token.replace(')', '('))) { + String swappedToken = token.replace(')', '('); + + while (!stack.peek().equals(swappedToken)) { output.add(transform.apply(stack.pop())); } @@ -98,21 +100,6 @@ public class ShuntingYard { } } - private static boolean shouldConfigureBasicOperators = true; - - /** - * Set whether the shunter should configure the four basic math - * operators - * - * @param configureBasicOperators - * Whether or not the four basic math operators should be - * configured - */ - public static void setBasicOperatorConfiguration( - boolean configureBasicOperators) { - shouldConfigureBasicOperators = configureBasicOperators; - } - /** * Holds all the shuntable operations */ @@ -120,11 +107,12 @@ public class ShuntingYard { /** * Create a new shunting yard with a default set of operators + * @param configureBasics Whether or not basic math operators should be provided */ - public ShuntingYard() { + public ShuntingYard(boolean configureBasics) { operators = new FunctionalMap<>(); - if (shouldConfigureBasicOperators) { + if (configureBasics) { operators.put("+", Operator.ADD); operators.put("-", Operator.SUBTRACT); operators.put("*", Operator.MULTIPLY); @@ -165,11 +153,15 @@ public class ShuntingYard { String rightOperator) { boolean operatorExists = operators.containsKey(rightOperator); + if (!operatorExists) { + return false; + } + boolean hasHigherPrecedence = operators.get(rightOperator).getPrecedence() >= operators .get(leftOperator).getPrecedence(); - return (operatorExists && hasHigherPrecedence); + return hasHigherPrecedence; } /** -- cgit v1.2.3