From a7acfb3ca856d1b9f1fd16d18b49c1a505a8dad3 Mon Sep 17 00:00:00 2001 From: mcmonkey4eva Date: Sat, 4 Oct 2014 20:28:04 -0700 Subject: [PATCH] Move more code to the core Yay for coreward work. --- .../denizen/scripts/ScriptEntry.java | 4 +- .../denizen/scripts/commands/Holdable.java | 11 - .../scripts/commands/core/RunCommand.java | 2 +- .../scripts/commands/core/SQLCommand.java | 2 +- .../scripts/commands/entity/PushCommand.java | 2 +- .../commands/entity/RotateCommand.java | 2 +- .../scripts/commands/entity/ShootCommand.java | 2 +- .../scripts/commands/npc/WalkCommand.java | 2 +- .../scripts/commands/world/BreakCommand.java | 2 +- .../scripts/containers/ScriptContainer.java | 2 +- .../denizen/scripts/queues/ScriptQueue.java | 2 +- .../denizen/tags/core/UtilTags.java | 2 +- .../utilities/debugging/Debuggable.java | 8 - .../denizen/utilities/debugging/dB.java | 1 + .../javaluator/AbstractEvaluator.java | 401 ------------------ .../javaluator/AbstractVariableSet.java | 16 - .../utilities/javaluator/BracketPair.java | 48 --- .../utilities/javaluator/Constant.java | 31 -- .../utilities/javaluator/DoubleEvaluator.java | 314 -------------- .../utilities/javaluator/Function.java | 58 --- .../utilities/javaluator/Operator.java | 104 ----- .../utilities/javaluator/Parameters.java | 185 -------- .../javaluator/StaticVariableSet.java | 33 -- .../denizen/utilities/javaluator/Token.java | 122 ------ .../utilities/javaluator/Tokenizer.java | 110 ----- .../utilities/javaluator/package-info.java | 3 - 26 files changed, 13 insertions(+), 1456 deletions(-) delete mode 100644 src/main/java/net/aufdemrand/denizen/scripts/commands/Holdable.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/debugging/Debuggable.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/AbstractEvaluator.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/AbstractVariableSet.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/BracketPair.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/Constant.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/DoubleEvaluator.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/Function.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/Operator.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/Parameters.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/StaticVariableSet.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/Token.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/Tokenizer.java delete mode 100644 src/main/java/net/aufdemrand/denizen/utilities/javaluator/package-info.java diff --git a/src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java b/src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java index ca1f7f720e..11c32aae4c 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java @@ -12,11 +12,11 @@ import net.aufdemrand.denizen.objects.dScript; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; import net.aufdemrand.denizen.scripts.commands.BracedCommand; -import net.aufdemrand.denizen.scripts.commands.Holdable; +import net.aufdemrand.denizencore.scripts.commands.Holdable; import net.aufdemrand.denizen.scripts.containers.ScriptContainer; import net.aufdemrand.denizen.scripts.queues.ScriptQueue; import net.aufdemrand.denizen.utilities.DenizenAPI; -import net.aufdemrand.denizen.utilities.debugging.Debuggable; +import net.aufdemrand.denizencore.utilities.debugging.Debuggable; import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizen.utilities.depends.Depends; import net.citizensnpcs.api.CitizensAPI; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/Holdable.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/Holdable.java deleted file mode 100644 index a554bc886a..0000000000 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/Holdable.java +++ /dev/null @@ -1,11 +0,0 @@ -package net.aufdemrand.denizen.scripts.commands; - -/** - * Simply used to indicate that a command can be 'held', so we don't wait for - * commands that will never mark themselves 'finished'. - * - */ -public interface Holdable { - - // Just so we know! We don't want to wait for commands that can't be 'held up'. -} diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/RunCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/RunCommand.java index bd66665355..1af55f3f45 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/RunCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/RunCommand.java @@ -13,7 +13,7 @@ import net.aufdemrand.denizen.objects.dScript; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.scripts.commands.Holdable; +import net.aufdemrand.denizencore.scripts.commands.Holdable; import net.aufdemrand.denizen.scripts.queues.ScriptQueue; import net.aufdemrand.denizen.scripts.queues.core.InstantQueue; import net.aufdemrand.denizen.scripts.queues.core.TimedQueue; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/SQLCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/SQLCommand.java index e3085278fb..230cf3f24e 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/SQLCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/SQLCommand.java @@ -6,7 +6,7 @@ import net.aufdemrand.denizen.objects.*; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.scripts.commands.Holdable; +import net.aufdemrand.denizencore.scripts.commands.Holdable; import net.aufdemrand.denizen.utilities.DenizenAPI; import net.aufdemrand.denizen.utilities.debugging.dB; import org.bukkit.Bukkit; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/PushCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/PushCommand.java index 0cb75edf60..0e10215839 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/PushCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/PushCommand.java @@ -13,7 +13,7 @@ import net.aufdemrand.denizen.objects.dScript; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.scripts.commands.Holdable; +import net.aufdemrand.denizencore.scripts.commands.Holdable; import net.aufdemrand.denizen.scripts.queues.ScriptQueue; import net.aufdemrand.denizen.scripts.queues.core.InstantQueue; import net.aufdemrand.denizen.utilities.Conversion; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/RotateCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/RotateCommand.java index ac6cd7da1c..e0d42a0485 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/RotateCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/RotateCommand.java @@ -17,7 +17,7 @@ import net.aufdemrand.denizen.objects.dList; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.scripts.commands.Holdable; +import net.aufdemrand.denizencore.scripts.commands.Holdable; import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizen.utilities.entity.Rotation; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/ShootCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/ShootCommand.java index a239421b23..4d6900e24a 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/ShootCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/ShootCommand.java @@ -15,7 +15,7 @@ import net.aufdemrand.denizen.objects.dScript; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.scripts.commands.Holdable; +import net.aufdemrand.denizencore.scripts.commands.Holdable; import net.aufdemrand.denizen.scripts.queues.ScriptQueue; import net.aufdemrand.denizen.scripts.queues.core.InstantQueue; import net.aufdemrand.denizen.utilities.DenizenAPI; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/npc/WalkCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/npc/WalkCommand.java index b0a66c6b73..60632cc32b 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/npc/WalkCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/npc/WalkCommand.java @@ -5,7 +5,7 @@ import net.aufdemrand.denizen.objects.*; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.scripts.commands.Holdable; +import net.aufdemrand.denizencore.scripts.commands.Holdable; import net.aufdemrand.denizen.utilities.DenizenAPI; import net.aufdemrand.denizen.utilities.debugging.dB; import net.citizensnpcs.api.ai.event.NavigationCancelEvent; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/world/BreakCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/BreakCommand.java index 5470f3af8f..7e6b5f01ab 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/world/BreakCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/BreakCommand.java @@ -5,7 +5,7 @@ import net.aufdemrand.denizen.objects.*; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.scripts.commands.Holdable; +import net.aufdemrand.denizencore.scripts.commands.Holdable; import net.aufdemrand.denizen.utilities.DenizenAPI; import net.aufdemrand.denizen.utilities.debugging.dB; import net.citizensnpcs.api.ai.tree.BehaviorStatus; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/containers/ScriptContainer.java b/src/main/java/net/aufdemrand/denizen/scripts/containers/ScriptContainer.java index 648e670c62..20de91ad29 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/containers/ScriptContainer.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/containers/ScriptContainer.java @@ -11,7 +11,7 @@ import net.aufdemrand.denizen.scripts.requirements.RequirementsContext; import net.aufdemrand.denizen.scripts.requirements.RequirementsMode; import net.aufdemrand.denizen.utilities.DenizenAPI; -import net.aufdemrand.denizen.utilities.debugging.Debuggable; +import net.aufdemrand.denizencore.utilities.debugging.Debuggable; import org.bukkit.configuration.ConfigurationSection; import java.util.ArrayList; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/queues/ScriptQueue.java b/src/main/java/net/aufdemrand/denizen/scripts/queues/ScriptQueue.java index 2c6f67571c..0650e0757c 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/queues/ScriptQueue.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/queues/ScriptQueue.java @@ -13,7 +13,7 @@ import net.aufdemrand.denizen.scripts.queues.core.TimedQueue; import net.aufdemrand.denizen.tags.Attribute; import net.aufdemrand.denizen.utilities.DenizenAPI; -import net.aufdemrand.denizen.utilities.debugging.Debuggable; +import net.aufdemrand.denizencore.utilities.debugging.Debuggable; import net.aufdemrand.denizen.utilities.debugging.dB; import org.apache.commons.lang.RandomStringUtils; diff --git a/src/main/java/net/aufdemrand/denizen/tags/core/UtilTags.java b/src/main/java/net/aufdemrand/denizen/tags/core/UtilTags.java index 25e314549d..1b499211db 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/core/UtilTags.java +++ b/src/main/java/net/aufdemrand/denizen/tags/core/UtilTags.java @@ -24,7 +24,7 @@ import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizen.utilities.depends.Depends; -import net.aufdemrand.denizen.utilities.javaluator.DoubleEvaluator; +import net.aufdemrand.denizencore.utilities.javaluator.DoubleEvaluator; import net.citizensnpcs.Citizens; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; diff --git a/src/main/java/net/aufdemrand/denizen/utilities/debugging/Debuggable.java b/src/main/java/net/aufdemrand/denizen/utilities/debugging/Debuggable.java deleted file mode 100644 index 55ca4708bb..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/debugging/Debuggable.java +++ /dev/null @@ -1,8 +0,0 @@ -package net.aufdemrand.denizen.utilities.debugging; - -public interface Debuggable { - - public boolean shouldDebug() throws Exception; - - public boolean shouldFilter(String criteria) throws Exception; -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/debugging/dB.java b/src/main/java/net/aufdemrand/denizen/utilities/debugging/dB.java index e2866137ee..c26cb12273 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/debugging/dB.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/debugging/dB.java @@ -17,6 +17,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import net.aufdemrand.denizencore.utilities.debugging.Debuggable; /** * Preferred method of outputting debugger information with Denizen and diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/AbstractEvaluator.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/AbstractEvaluator.java deleted file mode 100644 index 237fe09057..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/AbstractEvaluator.java +++ /dev/null @@ -1,401 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Stack; - -/** An abstract evaluator, able to evaluate infix expressions. - *
Some standard evaluators are included in the library, you can define your own by subclassing this class. - * @param The type of values handled by the evaluator - * @author Jean-Marc Astesana - * @see License information - */ -public abstract class AbstractEvaluator { - private final Tokenizer tokenizer; - private final Map functions; - private final Map> operators; - private final Map constants; - private final String functionArgumentSeparator; - private final Map functionBrackets; - private final Map expressionBrackets; - - /** Constructor. - * @param parameters The evaluator parameters. - *
Please note that there's no side effect between the evaluator and the parameters. - * So, changes made to the parameters after the call to this constructor are ignored by the instance. - */ - protected AbstractEvaluator(Parameters parameters) { - //TODO if constants, operators, functions are duplicated => error - final ArrayList tokenDelimitersBuilder = new ArrayList(); - this.functions = new HashMap(); - this.operators = new HashMap>(); - this.constants = new HashMap(); - this.functionBrackets = new HashMap(); - for (final BracketPair pair : parameters.getFunctionBrackets()) { - functionBrackets.put(pair.getOpen(), pair); - functionBrackets.put(pair.getClose(), pair); - tokenDelimitersBuilder.add(pair.getOpen()); - tokenDelimitersBuilder.add(pair.getClose()); - } - this.expressionBrackets = new HashMap(); - for (final BracketPair pair : parameters.getExpressionBrackets()) { - expressionBrackets.put(pair.getOpen(), pair); - expressionBrackets.put(pair.getClose(), pair); - tokenDelimitersBuilder.add(pair.getOpen()); - tokenDelimitersBuilder.add(pair.getClose()); - } - if (operators!=null) { - for (Operator ope : parameters.getOperators()) { - tokenDelimitersBuilder.add(ope.getSymbol()); - List known = this.operators.get(ope.getSymbol()); - if (known==null) { - known = new ArrayList(); - this.operators.put(ope.getSymbol(), known); - } - known.add(ope); - if (known.size()>1) validateHomonyms(known); - } - } - boolean needFunctionSeparator = false; - if (parameters.getFunctions()!=null) { - for (Function function : parameters.getFunctions()) { - //TODO if function name contains operators or reserved chars => error - this.functions.put(parameters.getTranslation(function.getName()), function); - if (function.getMaximumArgumentCount()>1) needFunctionSeparator = true; - } - } - if (parameters.getConstants()!=null) { - for (Constant constant : parameters.getConstants()) { - this.constants.put(parameters.getTranslation(constant.getName()), constant); - } - } - functionArgumentSeparator = parameters.getFunctionArgumentSeparator(); - if (needFunctionSeparator) { - tokenDelimitersBuilder.add(functionArgumentSeparator); - } - tokenizer = new Tokenizer(tokenDelimitersBuilder); - } - - /** Validates that homonym operators are valid. - *
Homonym operators are operators with the same name (like the unary - and the binary - operators) - *
This method is called when homonyms are passed to the constructor. - *
This default implementation only allows the case where there's two operators, one binary and one unary. - * Subclasses can override this method in order to accept others configurations. - * @param operators The operators to validate. - * @throws IllegalArgumentException if the homonyms are not compatibles. - * @see #guessOperator(Token, List) - */ - protected void validateHomonyms(List operators) { - if (operators.size()>2) throw new IllegalArgumentException(); - } - - /** When a token can be more than one operator (homonym operators), this method guesses the right operator. - *
A very common case is the - sign in arithmetic computation which can be an unary or a binary operator, depending - * on what was the previous token. - *
Warning: maybe the arguments of this function are not enough to deal with all the cases. - * So, this part of the evaluation is in alpha state (method may change in the future). - * @param previous The last parsed tokens (the previous token in the infix expression we are evaluating). - * @param candidates The candidate tokens. - * @return A token - * @see #validateHomonyms(List) - */ - protected Operator guessOperator(Token previous, List candidates) { - final int argCount = ((previous!=null) && (previous.isCloseBracket() || previous.isLiteral())) ? 2 : 1; - for (Operator operator : candidates) { - if (operator.getOperandCount()==argCount) return operator; - } - return null; - } - - @SuppressWarnings("unchecked") - private void output(Stack values, Token token, Object evaluationContext) { - if (token.isLiteral()) { // If the token is a literal, a constant, or a variable name - String literal = token.getLiteral(); - Constant ct = this.constants.get(literal); - T value = ct==null?null:evaluate(ct, evaluationContext); - if (value==null && evaluationContext!=null && (evaluationContext instanceof AbstractVariableSet)) value = ((AbstractVariableSet)evaluationContext).get(literal); - values.push(value!=null ? value : toValue(literal, evaluationContext)); - } else if (token.isOperator()) { - Operator operator = token.getOperator(); - values.push(evaluate(operator, getArguments(values, operator.getOperandCount()), evaluationContext)); - } else { - throw new IllegalArgumentException(); - } - } - - /** Evaluates a constant. - *
Subclasses that support constants must override this method. - * The default implementation throws a RuntimeException meaning that implementor forget to implement this method - * while creating a subclass that accepts constants. - * @param constant The constant - * @param evaluationContext The context of the evaluation - * @return The constant's value - */ - protected T evaluate(Constant constant, Object evaluationContext) { - throw new RuntimeException("evaluate(Constant) is not implemented for "+constant.getName()); - } - - /** Evaluates an operation. - *
Subclasses that support operators must override this method. - * The default implementation throws a RuntimeException meaning that implementor forget to implement this method - * while creating a subclass that accepts operators. - * @param operator The operator - * @param operands The operands - * @param evaluationContext The context of the evaluation - * @return The result of the operation - */ - protected T evaluate(Operator operator, Iterator operands, Object evaluationContext) { - throw new RuntimeException("evaluate(Operator, Iterator) is not implemented for "+operator.getSymbol()); - } - - /** Evaluates a function. - *
Subclasses that support functions must override this method. - * The default implementation throws a RuntimeException meaning that implementor forget to implement this method - * while creating a subclass that accepts functions. - * @param function The function - * @param arguments The function's arguments - * @param evaluationContext The context of the evaluation - * @return The result of the function - */ - protected T evaluate(Function function, Iterator arguments, Object evaluationContext) { - throw new RuntimeException("evaluate(Function, Iterator) is not implemented for "+function.getName()); - } - - private void doFunction(Stack values, Function function, int argCount, Object evaluationContext) { - if (function.getMinimumArgumentCount()>argCount || function.getMaximumArgumentCount() getArguments(Stack values, int nb) { - // Be aware that arguments are in reverse order on the values stack. - // Don't forget to reorder them in the original order (the one they appear in the evaluated formula) - if (values.size() result = new LinkedList(); - for (int i = 0; i This context is an object that can contain useful dynamic data, for example the values of the variables - * used in the expression (Use an AbstractVariableSet to do that).
The context is not limited to variable values but - * can be used for any dynamic information. A good example is the BooleanSetEvaluator one. - * @return the result of the evaluation. - * @throws IllegalArgumentException if the expression is not correct. - * @see AbstractVariableSet - */ - public T evaluate(String expression, Object evaluationContext) { - final Stack values = new Stack(); // values stack - final Stack stack = new Stack(); // operator stack - final Stack previousValuesSize = functions.size()==0?null:new Stack(); - final Iterator tokens = tokenize(expression); - Token previous = null; - while (tokens.hasNext()) { - // read one token from the input stream - String trimmed = tokens.next().trim(); - if (trimmed.length()==0) continue; // Ignored blank tokens - final Token token = toToken(previous, trimmed); - if (token.isOpenBracket()) { - // If the token is a left parenthesis, then push it onto the stack. - stack.push(token); - if (previous!=null && previous.isFunction()) { - if (!functionBrackets.containsKey(token.getBrackets().getOpen())) throw new IllegalArgumentException("Invalid bracket after function: "+trimmed); - } else { - if (!expressionBrackets.containsKey(token.getBrackets().getOpen())) throw new IllegalArgumentException("Invalid bracket in expression: "+trimmed); - } - } else if (token.isCloseBracket()) { - if (previous==null) throw new IllegalArgumentException("expression can't start with a close bracket"); - if (previous.isFunctionArgumentSeparator()) throw new IllegalArgumentException("argument is missing"); - BracketPair brackets = token.getBrackets(); - // If the token is a right parenthesis: - boolean openBracketFound = false; - // Until the token at the top of the stack is a left parenthesis, - // pop operators off the stack onto the output queue - while (!stack.isEmpty()) { - Token sc = stack.pop(); - if (sc.isOpenBracket()) { - if (sc.getBrackets().equals(brackets)) { - openBracketFound = true; - break; - } else { - throw new IllegalArgumentException("Invalid parenthesis match "+sc.getBrackets().getOpen()+brackets.getClose()); - } - } else { - output(values, sc, evaluationContext); - } - } - if (!openBracketFound) { - // If the stack runs out without finding a left parenthesis, then - // there are mismatched parentheses. - throw new IllegalArgumentException("Parentheses mismatched"); - } - if (!stack.isEmpty()) { - if (stack.peek().isFunction()) { - // If the token at the top of the stack is a function token, pop it - // onto the output queue. - int argCount = values.size()-previousValuesSize.pop(); - doFunction(values, stack.pop().getFunction(), argCount, evaluationContext); - } - } - } else if (token.isFunctionArgumentSeparator()) { - if (previous==null) throw new IllegalArgumentException("expression can't start with a function argument separator"); - // Verify that there was an argument before this separator - if (previous.isOpenBracket() || previous.isFunctionArgumentSeparator()) { - // The cases were operator miss an operand are detected elsewhere. - throw new IllegalArgumentException("argument is missing"); - } - // If the token is a function argument separator - boolean pe = false; - while (!stack.isEmpty()) { - if (stack.peek().isOpenBracket()) { - pe = true; - break; - } else { - // Until the token at the top of the stack is a left parenthesis, - // pop operators off the stack onto the output queue. - output(values, stack.pop(), evaluationContext); - } - } - if (!pe) { - // If no left parentheses are encountered, either the separator was misplaced - // or parentheses were mismatched. - throw new IllegalArgumentException("Separator or parentheses mismatched"); - } - } else if (token.isFunction()) { - // If the token is a function token, then push it onto the stack. - stack.push(token); - previousValuesSize.push(values.size()); - } else if (token.isOperator()) { - // If the token is an operator, op1, then: - while (!stack.isEmpty()) { - Token sc = stack.peek(); - // While there is an operator token, o2, at the top of the stack - // op1 is left-associative and its precedence is less than or equal - // to that of op2, - // or op1 has precedence less than that of op2, - // Let + and ^ be right associative. - // Correct transformation from 1^2+3 is 12^3+ - // The differing operator priority decides pop / push - // If 2 operators have equal priority then associativity decides. - if (sc.isOperator() - && ((token.getAssociativity().equals(Operator.Associativity.LEFT) && (token.getPrecedence() <= sc.getPrecedence())) || - (token.getPrecedence() < sc.getPrecedence()))) { - // Pop o2 off the stack, onto the output queue; - output(values, stack.pop(), evaluationContext); - } else { - break; - } - } - // push op1 onto the stack. - stack.push(token); - } else { - // If the token is a number (identifier), a constant or a variable, then add its value to the output queue. - if ((previous!=null) && previous.isLiteral()) throw new IllegalArgumentException("A literal can't follow another literal"); - output(values, token, evaluationContext); - } - previous = token; - } - // When there are no more tokens to read: - // While there are still operator tokens in the stack: - while (!stack.isEmpty()) { - Token sc = stack.pop(); - if (sc.isOpenBracket() || sc.isCloseBracket()) { - throw new IllegalArgumentException("Parentheses mismatched"); - } - output(values, sc, evaluationContext); - } - if (values.size()!=1) throw new IllegalArgumentException(); - return values.pop(); - } - - private Token toToken(Token previous, String token) { - if (token.equals(functionArgumentSeparator)) { - return Token.FUNCTION_ARG_SEPARATOR; - } else if (functions.containsKey(token)) { - return Token.buildFunction(functions.get(token)); - } else if (operators.containsKey(token)) { - List list = operators.get(token); - if (list.size()==1) return Token.buildOperator(list.get(0)); - return Token.buildOperator(guessOperator(previous, list)); - } else { - final BracketPair brackets = getBracketPair(token); - if (brackets!=null) { - if (brackets.getOpen().equals(token)) { - return Token.buildOpenToken(brackets); - } else { - return Token.buildCloseToken(brackets); - } - } else { - return Token.buildLiteral(token); - } - } - } - - private BracketPair getBracketPair(String token) { - BracketPair result = expressionBrackets.get(token); - return result==null ? functionBrackets.get(token) : result; - } - - /** Gets the operators supported by this evaluator. - * @return a collection of operators. - */ - public Collection getOperators() { - ArrayList result = new ArrayList(); - Collection> values = this.operators.values(); - for (List list : values) { - result.addAll(list); - } - return result; - } - - /** Gets the functions supported by this evaluator. - * @return a collection of functions. - */ - public Collection getFunctions() { - return this.functions.values(); - } - - /** Gets the constants supported by this evaluator. - * @return a collection of constants. - */ - public Collection getConstants() { - return this.constants.values(); - } - - /** Converts the evaluated expression into tokens. - *
Example: The result for the expression "-1+min(10,3)" is an iterator on "-", "1", "+", "min", "(", "10", ",", "3", ")". - *
By default, the operators symbols, the brackets and the function argument separator are used as delimiter in the string. - * @param expression The expression that is evaluated - * @return A string iterator. - */ - protected Iterator tokenize(String expression) { - return tokenizer.tokenize(expression); - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/AbstractVariableSet.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/AbstractVariableSet.java deleted file mode 100644 index a767f3f183..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/AbstractVariableSet.java +++ /dev/null @@ -1,16 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -/** An abstract variable set. - *
Javaluator supports expression that contains variables (for example sin(x)). - *
An AbstractVariableSet converts, during the expression evaluation, each variable to its value. - * @param The type of the values of the variable (the one handled by the evaluator). - * @author Jean-Marc Astesana - * @see License information - */ -public interface AbstractVariableSet { - /** Gets the value of a variable. - * @param variableName The name of a variable - * @return the variable's value or null if the variable is unknown - */ - public T get(String variableName); -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/BracketPair.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/BracketPair.java deleted file mode 100644 index c9bdcb5eff..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/BracketPair.java +++ /dev/null @@ -1,48 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -/** A bracket pair. - * @author Jean-Marc Astesana - * @see License information - */ -public class BracketPair { - /** The parentheses pair: ().*/ - public static final BracketPair PARENTHESES = new BracketPair('(', ')'); - /** The square brackets pair: [].*/ - public static final BracketPair BRACKETS = new BracketPair('[', ']'); - /** The braces pair: {}.*/ - public static final BracketPair BRACES = new BracketPair('{', '}'); - /** The angle brackets pair: <>.*/ - public static final BracketPair ANGLES = new BracketPair('<', '>'); - - private String open; - private String close; - - /** Constructor. - * @param open The character used to open the brackets. - * @param close The character used to close the brackets. - */ - public BracketPair(char open, char close) { - super(); - this.open = new String(new char[]{open}); - this.close = new String(new char[]{close}); - } - - /** Gets the open bracket character. - * @return a char - */ - public String getOpen() { - return open; - } - - /** Gets the close bracket character. - * @return a char - */ - public String getClose() { - return close; - } - - @Override - public String toString() { - return open + close; - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Constant.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Constant.java deleted file mode 100644 index 642afdfada..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Constant.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -/** - * A constant in an expression. - *
Some expressions needs constants. For instance it is impossible to perform trigonometric calculus without using pi. - * A constant allows you to use mnemonic in your expressions instead of the raw value of the constant. - *
A constant for pi would be defined by :
- * Constant pi = new Constant("pi"); - *
With such a constant, you will be able to evaluate the expression "sin(pi/4)" - * @author Jean-Marc Astesana - * @see License information - * @see AbstractEvaluator#evaluate(Constant, Object) - */ -public class Constant { - private String name; - - /** Constructor - * @param name The mnemonic of the constant. - *
The name is used in expressions to identified the constants. - */ - public Constant(String name) { - this.name = name; - } - - /** Gets the mnemonic of the constant. - * @return the id - */ - public String getName() { - return name; - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/DoubleEvaluator.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/DoubleEvaluator.java deleted file mode 100644 index c33da4fa09..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/DoubleEvaluator.java +++ /dev/null @@ -1,314 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -import java.text.NumberFormat; -import java.text.ParsePosition; -import java.util.Arrays; -import java.util.Iterator; -import java.util.Locale; - -/** An evaluator that is able to evaluate arithmetic expressions on real numbers. - *
Built-in operators:
    - *
  • +: Addition
  • - *
  • -: Subtraction
  • - *
  • -: Unary minus
  • - *
  • *: Multiplication
  • - *
  • /: Division
  • - *
  • ^: Exponentiation
  • - *
  • %: Modulo
  • - *
- * Built-in functions:
    - *
  • abs: absolute value
  • - *
  • acos: arc cosine
  • - *
  • asin: arc sine
  • - *
  • atan: arc tangent
  • - *
  • average: average of arguments
  • - *
  • ceil: nearest upper integer
  • - *
  • cos: cosine
  • - *
  • cosh: hyperbolic cosine
  • - *
  • floor: nearest lower integer
  • - *
  • ln: natural logarithm (base e)
  • - *
  • log: base 10 logarithm
  • - *
  • max: maximum of arguments
  • - *
  • min: minimum of arguments
  • - *
  • round: nearest integer
  • - *
  • sin: sine
  • - *
  • sinh: hyperbolic sine
  • - *
  • sum: sum of arguments
  • - *
  • tan: tangent
  • - *
  • tanh: hyperbolic tangent
  • - *
  • random: pseudo-random number (between 0 and 1)
  • - *
- * Built-in constants:
    - *
  • e: Base of natural algorithms
  • - *
  • pi: Ratio of the circumference of a circle to its diameter
  • - *
- * @author Jean-Marc Astesana - * @see License information - */ -public class DoubleEvaluator extends AbstractEvaluator { - /** The order or operations (operator precedence) is not clearly defined, especially between the unary minus operator and exponentiation - * operator (see http://en.wikipedia.org/wiki/Order_of_operations). - * These constants define the operator precedence styles. - */ - public static enum Style { - /** The most commonly operator precedence, where the unary minus as a lower precedence than the exponentiation. - *
With this style, used by Google, Wolfram alpha, and many others, -2^2=-4. - */ - STANDARD, - /** The operator precedence used by Excel, or bash shell script language, where the unary minus as a higher precedence than the exponentiation. - *
With this style, -2^2=4. - */ - EXCEL - } - - /** A constant that represents pi (3.14159...) */ - public final static Constant PI = new Constant("pi"); - /** A constant that represents e (2.718281...) */ - public final static Constant E = new Constant("e"); - - /** Returns the smallest integer >= argument */ - public final static Function CEIL = new Function("ceil", 1); - /** Returns the largest integer <= argument */ - public final static Function FLOOR = new Function("floor", 1); - /** Returns the closest integer of a number */ - public final static Function ROUND = new Function("round", 1); - /** Returns the absolute value of a number */ - public final static Function ABS = new Function("abs", 1); - - /** Returns the trigonometric sine of an angle. The angle is expressed in radian.*/ - public final static Function SINE = new Function("sin", 1); - /** Returns the trigonometric cosine of an angle. The angle is expressed in radian.*/ - public final static Function COSINE = new Function("cos", 1); - /** Returns the trigonometric tangent of an angle. The angle is expressed in radian.*/ - public final static Function TANGENT = new Function("tan", 1); - /** Returns the trigonometric arc-cosine of an angle. The angle is expressed in radian.*/ - public final static Function ACOSINE = new Function("acos", 1); - /** Returns the trigonometric arc-sine of an angle. The angle is expressed in radian.*/ - public final static Function ASINE = new Function("asin", 1); - /** Returns the trigonometric arc-tangent of an angle. The angle is expressed in radian.*/ - public final static Function ATAN = new Function("atan", 1); - - /** Returns the hyperbolic sine of a number.*/ - public final static Function SINEH = new Function("sinh", 1); - /** Returns the hyperbolic cosine of a number.*/ - public final static Function COSINEH = new Function("cosh", 1); - /** Returns the hyperbolic tangent of a number.*/ - public final static Function TANGENTH = new Function("tanh", 1); - - /** Returns the minimum of n numbers (n>=1) */ - public final static Function MIN = new Function("min", 1, Integer.MAX_VALUE); - /** Returns the maximum of n numbers (n>=1) */ - public final static Function MAX = new Function("max", 1, Integer.MAX_VALUE); - /** Returns the sum of n numbers (n>=1) */ - public final static Function SUM = new Function("sum", 1, Integer.MAX_VALUE); - /** Returns the average of n numbers (n>=1) */ - public final static Function AVERAGE = new Function("avg", 1, Integer.MAX_VALUE); - - /** Returns the natural logarithm of a number */ - public final static Function LN = new Function("ln", 1); - /** Returns the decimal logarithm of a number */ - public final static Function LOG = new Function("log", 1); - - /** Returns a pseudo random number */ - public final static Function RANDOM = new Function("random", 0); - - /** The negate unary operator in the standard operator precedence.*/ - public final static Operator NEGATE = new Operator("-", 1, Operator.Associativity.RIGHT, 3); - /** The negate unary operator in the Excel like operator precedence.*/ - public final static Operator NEGATE_HIGH = new Operator("-", 1, Operator.Associativity.RIGHT, 5); - /** The substraction operator.*/ - public final static Operator MINUS = new Operator("-", 2, Operator.Associativity.LEFT, 1); - /** The addition operator.*/ - public final static Operator PLUS = new Operator("+", 2, Operator.Associativity.LEFT, 1); - /** The multiplication operator.*/ - public final static Operator MULTIPLY = new Operator("*", 2, Operator.Associativity.LEFT, 2); - /** The division operator.*/ - public final static Operator DIVIDE = new Operator("/", 2, Operator.Associativity.LEFT, 2); - /** The exponentiation operator.*/ - public final static Operator EXPONENT = new Operator("^", 2, Operator.Associativity.LEFT, 4); - /** The modulo operator.*/ - public final static Operator MODULO = new Operator("%", 2, Operator.Associativity.LEFT, 2); - - /** The standard whole set of predefined operators */ - private static final Operator[] OPERATORS = new Operator[]{NEGATE, MINUS, PLUS, MULTIPLY, DIVIDE, EXPONENT, MODULO}; - /** The excel like whole set of predefined operators */ - private static final Operator[] OPERATORS_EXCEL = new Operator[]{NEGATE_HIGH, MINUS, PLUS, MULTIPLY, DIVIDE, EXPONENT, MODULO}; - /** The whole set of predefined functions */ - private static final Function[] FUNCTIONS = new Function[]{SINE, COSINE, TANGENT, ASINE, ACOSINE, ATAN, SINEH, COSINEH, TANGENTH, MIN, MAX, SUM, AVERAGE, LN, LOG, ROUND, CEIL, FLOOR, ABS, RANDOM}; - /** The whole set of predefined constants */ - private static final Constant[] CONSTANTS = new Constant[]{PI, E}; - - private static Parameters DEFAULT_PARAMETERS; - private static final NumberFormat FORMATTER = NumberFormat.getNumberInstance(Locale.US); - - /** Gets a copy of DoubleEvaluator standard default parameters. - *
The returned parameters contains all the predefined operators, functions and constants. - *
Each call to this method create a new instance of Parameters. - * @return a Paramaters instance - * @see Style - */ - public static Parameters getDefaultParameters() { - return getDefaultParameters(Style.STANDARD); - } - - /** Gets a copy of DoubleEvaluator default parameters. - *
The returned parameters contains all the predefined operators, functions and constants. - *
Each call to this method create a new instance of Parameters. - * @return a Paramaters instance - */ - public static Parameters getDefaultParameters(Style style) { - Parameters result = new Parameters(); - result.addOperators(style==Style.STANDARD?Arrays.asList(OPERATORS):Arrays.asList(OPERATORS_EXCEL)); - result.addFunctions(Arrays.asList(FUNCTIONS)); - result.addConstants(Arrays.asList(CONSTANTS)); - result.addFunctionBracket(BracketPair.PARENTHESES); - result.addExpressionBracket(BracketPair.PARENTHESES); - return result; - } - - private static Parameters getParameters() { - if (DEFAULT_PARAMETERS == null) { - DEFAULT_PARAMETERS = getDefaultParameters(); - } - return DEFAULT_PARAMETERS; - } - - /** Constructor. - *
This default constructor builds an instance with all predefined operators, functions and constants. - */ - public DoubleEvaluator() { - this(getParameters()); - } - - /** Constructor. - *
This constructor can be used to reduce the set of supported operators, functions or constants, - * or to localize some function or constant's names. - * @param parameters The parameters of the evaluator. - */ - public DoubleEvaluator(Parameters parameters) { - super(parameters); - } - - @Override - protected Double toValue(String literal, Object evaluationContext) { - ParsePosition p = new ParsePosition(0); - Number result = FORMATTER.parse(literal, p); - if (p.getIndex()==0 || p.getIndex()!=literal.length()) throw new IllegalArgumentException(literal+" is not a number"); - return result.doubleValue(); - } - - /* (non-Javadoc) - * @see net.astesana.javaluator.AbstractEvaluator#evaluate(net.astesana.javaluator.Constant) - */ - @Override - protected Double evaluate(Constant constant, Object evaluationContext) { - if (constant==PI) { - return Math.PI; - } else if (constant==E) { - return Math.E; - } else { - return super.evaluate(constant, evaluationContext); - } - } - - /* (non-Javadoc) - * @see net.astesana.javaluator.AbstractEvaluator#evaluate(net.astesana.javaluator.Operator, java.util.Iterator) - */ - @Override - protected Double evaluate(Operator operator, Iterator operands, Object evaluationContext) { - if (operator==NEGATE || operator==NEGATE_HIGH) { - return -operands.next(); - } else if (operator==MINUS) { - return operands.next() - operands.next(); - } else if (operator==PLUS) { - return operands.next() + operands.next(); - } else if (operator==MULTIPLY) { - return operands.next() * operands.next(); - } else if (operator==DIVIDE) { - return operands.next() / operands.next(); - } else if (operator==EXPONENT) { - return Math.pow(operands.next(),operands.next()); - } else if (operator==MODULO) { - return operands.next() % operands.next(); - } else { - return super.evaluate(operator, operands, evaluationContext); - } - } - - /* (non-Javadoc) - * @see net.astesana.javaluator.AbstractEvaluator#evaluate(net.astesana.javaluator.Function, java.util.Iterator) - */ - @Override - protected Double evaluate(Function function, Iterator arguments, Object evaluationContext) { - Double result; - if (function==ABS) { - result = Math.abs(arguments.next()); - } else if (function==CEIL) { - result = Math.ceil(arguments.next()); - } else if (function==FLOOR) { - result = Math.floor(arguments.next()); - } else if (function==ROUND) { - Double arg = arguments.next(); - if (arg==Double.NEGATIVE_INFINITY || arg==Double.POSITIVE_INFINITY) { - result = arg; - } else { - result = (double) Math.round(arg); - } - } else if (function==SINEH) { - result = Math.sinh(arguments.next()); - } else if (function==COSINEH) { - result = Math.cosh(arguments.next()); - } else if (function==TANGENTH) { - result = Math.tanh(arguments.next()); - } else if (function==SINE) { - result = Math.sin(arguments.next()); - } else if (function==COSINE) { - result = Math.cos(arguments.next()); - } else if (function==TANGENT) { - result = Math.tan(arguments.next()); - } else if (function==ACOSINE) { - result = Math.acos(arguments.next()); - } else if (function==ASINE) { - result = Math.asin(arguments.next()); - } else if (function==ATAN) { - result = Math.atan(arguments.next()); - } else if (function==MIN) { - result = arguments.next(); - while (arguments.hasNext()) { - result = Math.min(result, arguments.next()); - } - } else if (function==MAX) { - result = arguments.next(); - while (arguments.hasNext()) { - result = Math.max(result, arguments.next()); - } - } else if (function==SUM) { - result = 0.; - while (arguments.hasNext()) { - result = result + arguments.next(); - } - } else if (function==AVERAGE) { - result = 0.; - int nb = 0; - while (arguments.hasNext()) { - result = result + arguments.next(); - nb++; - } - result = result/nb; - } else if (function==LN) { - result = Math.log(arguments.next()); - } else if (function==LOG) { - result = Math.log10(arguments.next()); - } else if (function==RANDOM) { - result = Math.random(); - } else { - result = super.evaluate(function, arguments, evaluationContext); - } - errIfNaN(result, function); - return result; - } - - private void errIfNaN(Double result, Function function) { - if (result.equals(Double.NaN)) throw new IllegalArgumentException("Invalid argument passed to "+function.getName()); - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Function.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Function.java deleted file mode 100644 index a3935bed45..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Function.java +++ /dev/null @@ -1,58 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -/** A function. - * @author Jean-Marc Astesana - * @see License information - */ -public class Function { - private String name; - private int minArgumentCount; - private int maxArgumentCount; - - /** Constructor. - *
This constructor builds a function with a fixed arguments count. - * Throws IllegalArgumentException if argumentCount is lower than 0 or if the function name is null or empty. - * @param name The function's name - * @param argumentCount The function's argument count. - */ - public Function(String name, int argumentCount) { - this(name, argumentCount, argumentCount); - } - - /** Constructor. - *
This constructor builds a function with a variable arguments count. - *
For instance, a minimum function may have at least one argument. - * Throws IllegalArgumentException if minArgumentCount is less than 0 or greater than maxArgumentCount or if the function name is null or empty. - * @param name The function's name - * @param minArgumentCount The function's minimum argument count. - * @param maxArgumentCount The function's maximum argument count (Integer.MAX_VALUE to specify no upper limit). - */ - public Function(String name, int minArgumentCount, int maxArgumentCount) { - if ((minArgumentCount<0) || (minArgumentCount>maxArgumentCount)) throw new IllegalArgumentException("Invalid argument count"); - if (name==null || name.length()==0) throw new IllegalArgumentException("Invalid function name"); - this.name = name; - this.minArgumentCount = minArgumentCount; - this.maxArgumentCount = maxArgumentCount; - } - - /** Gets the function's name. - * @return the name of the function - */ - public String getName() { - return this.name; - } - - /** Gets the function's minimum argument count. - * @return an integer - */ - public int getMinimumArgumentCount() { - return this.minArgumentCount; - } - - /** Gets the function's maximum argument count. - * @return an integer - */ - public int getMaximumArgumentCount() { - return this.maxArgumentCount; - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Operator.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Operator.java deleted file mode 100644 index af2a4b5bd4..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Operator.java +++ /dev/null @@ -1,104 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -/** An operator. - * @author Jean-Marc Astesana - * @see License information - */ -public class Operator { - /** An Operator's associativity. - */ - public enum Associativity { - /** Left associativity.*/ - LEFT, - /** Right associativity. */ - RIGHT, - /** No associativity.*/ - NONE - } - private String symbol; - private int precedence; - private int operandCount; - private Associativity associativity; - - /** Constructor. - * Throws IllegalArgumentException if operandCount if not 1 or 2 or if associativity is none - * Throws NullPointerException if symbol or associativity are null - * @param symbol The operator name (Currently, the name's length must be one character). - * @param operandCount The number of operands of the operator (must be 1 or 2). - * @param associativity true if operator is left associative - * @param precedence The precedence of the operator. - *
The precedence is the priority of the operator. An operator with an higher precedence will be executed before an operator with a lower precedence. - * Example : In "1+3*4" * has a higher precedence than +, so the expression is interpreted as 1+(3*4). - */ - public Operator(String symbol, int operandCount, Associativity associativity, int precedence) { - if (symbol==null || associativity==null) throw new NullPointerException(); - if (symbol.length()==0) throw new IllegalArgumentException("Operator symbol can't be null"); - if ((operandCount<1) || (operandCount>2)) throw new IllegalArgumentException("Only unary and binary operators are supported"); - if (Associativity.NONE.equals(associativity)) throw new IllegalArgumentException("None associativity operators are not supported"); - this.symbol = symbol; - this.operandCount = operandCount; - this.associativity = associativity; - this.precedence = precedence; - } - - /** Gets the operator's symbol. - * @return a String - */ - public String getSymbol() { - return this.symbol; - } - - /** Gets the operator's operand count. - * @return an integer - */ - public int getOperandCount() { - return this.operandCount; - } - - /** Gets this operator's associativity. - * @return true if the operator is left associative. - * @see Operator's associativity in Wikipedia - */ - public Associativity getAssociativity() { - return this.associativity; - } - - /** Gets the operator's precedence. - * @return an integer - * @see Operator's associativity in Wikipedia - */ - public int getPrecedence() { - return this.precedence; - } - - /* (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + operandCount; - result = prime * result + ((associativity == null) ? 0 : associativity.hashCode()); - result = prime * result + ((symbol == null) ? 0 : symbol.hashCode()); - result = prime * result + precedence; - return result; - } - - /* (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; - Operator other = (Operator) obj; - if (operandCount != other.operandCount) return false; - if (associativity != other.associativity) return false; - if (symbol == null) { - if (other.symbol != null) return false; - } else if (!symbol.equals(other.symbol)) return false; - return precedence == other.precedence; - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Parameters.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Parameters.java deleted file mode 100644 index d8b642ffaf..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Parameters.java +++ /dev/null @@ -1,185 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -/** The parameters of an evaluator. - *
An evaluator may have different parameters as the supported operators, the supported functions, etc ... - * @author Jean-Marc Astesana - * @see License information - */ -public class Parameters { - private String functionSeparator; - private final ArrayList operators; - private final ArrayList functions; - private final ArrayList constants; - private final Map translations; - private final ArrayList expressionBrackets; - private final ArrayList functionBrackets; - - /** Constructor. - *
This method builds an instance with no operator, no function, no constant, no translation and no bracket - *
Function argument separator is set to ','. - */ - public Parameters() { - this.operators = new ArrayList(); - this.functions = new ArrayList(); - this.constants = new ArrayList(); - this.translations = new HashMap(); - this.expressionBrackets = new ArrayList(); - this.functionBrackets = new ArrayList(); - setFunctionArgumentSeparator(','); - } - - /** Gets the supported operators. - * @return a Collection of operators. - */ - public Collection getOperators() { - return this.operators; - } - - /** Gets the supported functions. - * @return a Collection of functions. - */ - public Collection getFunctions() { - return this.functions; - } - - /** Gets the supported constants. - * @return a Collection of constants. - */ - public Collection getConstants() { - return this.constants; - } - - /** Gets the supported bracket pairs for expressions. - * @return a Collection of bracket pairs. - */ - public Collection getExpressionBrackets() { - return this.expressionBrackets; - } - - /** Gets the supported bracket pairs for functions. - * @return a Collection of bracket pairs. - */ - public Collection getFunctionBrackets() { - return this.functionBrackets; - } - - /** Adds operators to the supported ones. - * @param operators The operators to be added. - */ - public void addOperators(Collection operators) { - this.operators.addAll(operators); - } - - /** Adds an operator to the supported ones. - * @param operator The added operator - */ - public void add(Operator operator) { - this.operators.add(operator); - } - - /** Adds functions to the supported ones. - * @param functions The functions to be added. - */ - public void addFunctions(Collection functions) { - this.functions.addAll(functions); - } - - /** Adds a function to the supported ones. - * @param function The added function - */ - public void add(Function function) { - this.functions.add(function); - } - - /** Adds constants to the supported ones. - * @param constants The constants to be added. - */ - public void addConstants(Collection constants) { - this.constants.addAll(constants); - } - - /** Adds a constant to the supported ones. - * @param constant The added constant - */ - public void add(Constant constant) { - this.constants.add(constant); - } - - /** Adds a new bracket pair to the expression bracket list. - * @param pair A bracket pair - */ - public void addExpressionBracket(BracketPair pair) { - this.expressionBrackets.add(pair); - } - - /** Adds bracket pairs to the expression bracket list. - * @param brackets The brackets to be added. - */ - public void addExpressionBrackets(Collection brackets) { - this.expressionBrackets.addAll(brackets); - } - - /** Adds a new bracket pair to the function bracket list. - * @param pair A bracket pair - */ - public void addFunctionBracket(BracketPair pair) { - this.functionBrackets.add(pair); - } - - /** Adds bracket pairs to the function bracket list. - * @param brackets The brackets to be added. - */ - public void addFunctionBrackets(Collection brackets) { - this.functionBrackets.addAll(brackets); - } - - /** Sets the translated term for a function. - *
Using this method, you can localize the names of some built-in functions. For instance, - * for french people,you can use this method to use "somme" instead of "sum" with the SUM built-in - * function of DoubleEvaluator. - * @param function The function you want to translate the name - * @param translatedName The translated name - * @see DoubleEvaluator#SUM - */ - public void setTranslation(Function function, String translatedName) { - setTranslation(function.getName(), translatedName); - } - - /** Sets the translated term for a constant. - * @param constant The constant you want to translate the name - * @param translatedName The translated name - * @see #setTranslation(Function, String) - */ - public void setTranslation(Constant constant, String translatedName) { - setTranslation(constant.getName(), translatedName); - } - - private void setTranslation(String name, String translatedName) { - this.translations.put(name, translatedName); - } - - String getTranslation(String originalName) { - String translation = this.translations.get(originalName); - return translation==null?originalName:translation; - } - - /** Sets the function argument separator. - *
Its default value is ','. - * @param separator The new separator - */ - public void setFunctionArgumentSeparator(char separator) { - this.functionSeparator = new String(new char[]{separator}); - } - - /** Gets the function argument separator. - * @return a string - */ - public String getFunctionArgumentSeparator() { - return this.functionSeparator; - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/StaticVariableSet.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/StaticVariableSet.java deleted file mode 100644 index 7956da00b2..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/StaticVariableSet.java +++ /dev/null @@ -1,33 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -import java.util.HashMap; -import java.util.Map; - -/** A static variable set. - *
Here, static means that the values of variables are set before starting to evaluate the expressions. - * @param The type of the values of the variable (the one handled by the evaluator). - * @author Jean-Marc Astesana - * @see License information - */ -public class StaticVariableSet implements AbstractVariableSet { - private final Map varToValue; - - /** Constructor. - *
Builds a new empty variable set. - */ - public StaticVariableSet() { - this.varToValue = new HashMap(); - } - - public T get(String variableName) { - return this.varToValue.get(variableName); - } - - /** Sets a variable value. - * @param variableName The variable name - * @param value The variable value (null to remove a variable from the set). - */ - public void set(String variableName, T value) { - this.varToValue.put(variableName, value); - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Token.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Token.java deleted file mode 100644 index 86e3c79629..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Token.java +++ /dev/null @@ -1,122 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -/** A token. - *
When evaluating an expression, it is first split into tokens. - * These tokens can be operators, constants, etc ... - * @author Jean-Marc Astesana - * @see License information - */ -public class Token { - private enum Kind { - OPEN_BRACKET, - CLOSE_BRACKET, - FUNCTION_SEPARATOR, - FUNCTION, - OPERATOR, - LITERAL - } - static final Token FUNCTION_ARG_SEPARATOR = new Token(Kind.FUNCTION_SEPARATOR, null); - - private Kind kind; - private Object content; - - static Token buildLiteral(String literal) { - return new Token(Kind.LITERAL, literal); - } - - static Token buildOperator(Operator ope) { - return new Token(Kind.OPERATOR, ope); - } - - static Token buildFunction(Function function) { - return new Token(Kind.FUNCTION, function); - } - - static Token buildOpenToken(BracketPair pair) { - return new Token(Kind.OPEN_BRACKET, pair); - } - - static Token buildCloseToken(BracketPair pair) { - return new Token(Kind.CLOSE_BRACKET, pair); - } - - private Token(Kind kind, Object content) { - super(); - if ((kind.equals(Kind.OPERATOR) && !(content instanceof Operator)) || - (kind.equals(Kind.FUNCTION) && !(content instanceof Function)) || - (kind.equals(Kind.LITERAL) && !(content instanceof String))) throw new IllegalArgumentException(); - this.kind = kind; - this.content = content; - } - - BracketPair getBrackets() { - return (BracketPair) this.content; - } - - Operator getOperator() { - return (Operator) this.content; - } - - Function getFunction() { - return (Function) this.content; - } - - Kind getKind() { - return kind; - } - - /** Tests whether the token is an operator. - * @return true if the token is an operator - */ - public boolean isOperator() { - return kind.equals(Kind.OPERATOR); - } - - /** Tests whether the token is a function. - * @return true if the token is a function - */ - public boolean isFunction() { - return kind.equals(Kind.FUNCTION); - } - - /** Tests whether the token is an open bracket. - * @return true if the token is an open bracket - */ - public boolean isOpenBracket() { - return kind.equals(Kind.OPEN_BRACKET); - } - - /** Tests whether the token is a close bracket. - * @return true if the token is a close bracket - */ - public boolean isCloseBracket() { - return kind.equals(Kind.CLOSE_BRACKET); - } - - /** Tests whether the token is a function argument separator. - * @return true if the token is a function argument separator - */ - public boolean isFunctionArgumentSeparator() { - return kind.equals(Kind.FUNCTION_SEPARATOR); - } - - /** Tests whether the token is a literal or a constant or a variable name. - * @return true if the token is a literal, a constant or a variable name - */ - public boolean isLiteral() { - return kind.equals(Kind.LITERAL); - } - - Operator.Associativity getAssociativity() { - return getOperator().getAssociativity(); - } - - int getPrecedence() { - return getOperator().getPrecedence(); - } - - String getLiteral() { - if (!this.kind.equals(Kind.LITERAL)) throw new IllegalArgumentException(); - return (String)this.content; - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Tokenizer.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Tokenizer.java deleted file mode 100644 index 424bcea1dd..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/Tokenizer.java +++ /dev/null @@ -1,110 +0,0 @@ -package net.aufdemrand.denizen.utilities.javaluator; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.Iterator; -import java.util.List; -import java.util.StringTokenizer; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** A String tokenizer that accepts delimiters that are greater than one character. - * @author Jean-Marc Astesana - * @see License information - */ -public class Tokenizer { - private Pattern pattern; - private String tokenDelimiters; - - /** Constructor. - * @param delimiters the delimiters - */ - Tokenizer(List delimiters) { - if (onlyOneChar(delimiters)) { - StringBuilder builder = new StringBuilder(); - for (String delimiter : delimiters) { - builder.append(delimiter); - } - tokenDelimiters = builder.toString(); - } else { - this.pattern = delimitersToRegexp(delimiters); - } - } - - /** Tests whether a String list contains only 1 character length elements. - * @param delimiters The list to test - * @return true if it contains only one char length elements (or no elements) - */ - private boolean onlyOneChar(List delimiters) { - for (String delimiter : delimiters) { - if (delimiter.length()!=1) return false; - } - return true; - } - - private static Pattern delimitersToRegexp(List delimiters) { - // First, create a regular expression that match the union of the delimiters - // Be aware that, in case of delimiters containing others (example && and &), - // the longer may be before the shorter (&& should be before &) or the regexpr - // parser will recognize && as two &. - Collections.sort(delimiters, new Comparator() { - public int compare(String o1, String o2) { - return -o1.compareTo(o2); - } - }); - // Build a string that will contain the regular expression - StringBuilder result = new StringBuilder(); - result.append('('); - for (String delim : delimiters) { // For each delimiter - if (result.length()!=1) result.append('|'); // Add it to the union - for (int i=0;i tokenize(String string) { - if (pattern!=null) { - List res = new ArrayList(); - Matcher m = pattern.matcher(string); - int pos = 0; - while (m.find()) { // While there's a delimiter in the string - if (pos != m.start()) { - // If there's something between the current and the previous delimiter - // Add to the tokens list - res.add(string.substring(pos, m.start())); - } - res.add(m.group()); // add the delimiter - pos = m.end(); // Remember end of delimiter - } - if (pos != string.length()) { - // If it remains some characters in the string after last delimiter - res.add(string.substring(pos)); - } - // Return the result - return res.iterator(); - } else { - final StringTokenizer tokens = new StringTokenizer(string, tokenDelimiters, true); - return new Iterator() { - public boolean hasNext() { - return tokens.hasMoreTokens(); - } - public String next() { - return tokens.nextToken(); - } - public void remove() { - throw new UnsupportedOperationException(); - } - }; - } - } -} diff --git a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/package-info.java b/src/main/java/net/aufdemrand/denizen/utilities/javaluator/package-info.java deleted file mode 100644 index aa688712cf..0000000000 --- a/src/main/java/net/aufdemrand/denizen/utilities/javaluator/package-info.java +++ /dev/null @@ -1,3 +0,0 @@ -/** The Javaluator package */ -package net.aufdemrand.denizen.utilities.javaluator; -