Class AbstractEvaluator<T>

java.lang.Object
studio.magemonkey.codex.util.eval.javaluator.AbstractEvaluator<T>
Type Parameters:
T - The type of values handled by the evaluator
All Implemented Interfaces:
EvaluationContext
Direct Known Subclasses:
DoubleEvaluator

public abstract class AbstractEvaluator<T> extends Object implements EvaluationContext
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.
This class is thread safe.
Author:
Jean-Marc Astesana
See Also:
  • Field Details

  • Constructor Details

    • AbstractEvaluator

      protected AbstractEvaluator(Parameters parameters)
      Constructor.
      Parameters:
      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.
  • Method Details

    • output

      protected void output(Deque<T> values, Token token, EvaluationContext evaluationContext)
    • evaluate

      protected T evaluate(Constant constant, EvaluationContext evaluationContext)
      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.
      Parameters:
      constant - The constant
      evaluationContext - The context of the evaluation
      Returns:
      The constant's value
    • evaluate

      protected T evaluate(Operator operator, Iterator<T> operands, EvaluationContext evaluationContext)
      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.
      Parameters:
      operator - The operator
      operands - The operands
      evaluationContext - The context of the evaluation
      Returns:
      The result of the operation
    • evaluate

      protected T evaluate(Function function, Iterator<T> arguments, EvaluationContext evaluationContext)
      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.
      Parameters:
      function - The function
      arguments - The function's arguments
      evaluationContext - The context of the evaluation
      Returns:
      The result of the function
    • doFunction

      protected void doFunction(Deque<T> values, Token functionTok, int argCount, EvaluationContext evaluationContext)
    • getArguments

      protected Iterator<T> getArguments(Deque<T> values, int nb)
    • toValue

      protected abstract T toValue(Token literal, EvaluationContext evaluationContext)
      Evaluates a literal (Converts it to a value).
      Parameters:
      literal - The literal to evaluate.
      evaluationContext - The context of the evaluation
      Returns:
      an instance of T.
      Throws:
      IllegalArgumentException - if the literal can't be converted to a value.
    • evaluate

      public T evaluate(String expression)
      Evaluates an expression.
      Parameters:
      expression - The expression to evaluate.
      Returns:
      the result of the evaluation.
      Throws:
      IllegalArgumentException - if the expression is not correct.
    • evaluate

      public T evaluate(String expression, EvaluationContext evaluationContext)
      Evaluates an expression that contains variables.
      Parameters:
      expression - The expression to evaluate.
      evaluationContext - The context of the evaluation.
      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.
      Returns:
      the result of the evaluation.
      Throws:
      IllegalArgumentException - if the expression is not correct.
      See Also:
    • getBracketPair

      protected BracketPair getBracketPair(String token)
    • getOperators

      public Collection<Operator> getOperators()
      Gets the operators supported by this evaluator.
      Returns:
      a collection of operators.
    • getFunctions

      public Collection<Function> getFunctions()
      Gets the functions supported by this evaluator.
      Returns:
      a collection of functions.
    • getConstants

      public Collection<Constant> getConstants()
      Gets the constants supported by this evaluator.
      Returns:
      a collection of constants.
    • tokenize

      protected Collection<Token> tokenize(String expression)
      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.
      Parameters:
      expression - The expression that is evaluated
      Returns:
      A string iterator.