Class AbstractEvaluator<T>

java.lang.Object
com.fathzer.soft.javaluator.AbstractEvaluator<T>
Type Parameters:
T - The type of values handled by the evaluator
Direct Known Subclasses:
DoubleEvaluator

public abstract class AbstractEvaluator<T> extends Object
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:
  • 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

    • validateHomonyms

      protected void validateHomonyms(List<Operator> operators)
      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.
      Parameters:
      operators - The operators to validate.
      Throws:
      IllegalArgumentException - if the homonyms are not compatibles.
      See Also:
    • guessOperator

      protected Operator guessOperator(Token previous, List<Operator> candidates)
      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).
      Parameters:
      previous - The last parsed tokens (the previous token in the infix expression we are evaluating).
      candidates - The candidate tokens.
      Returns:
      A token
      See Also:
    • evaluate

      protected T evaluate(Constant constant, Object evaluationContext)
      Evaluates a constant.
      Subclasses that support constants must override this method (The default implementation throws a UnsupportedOperationException).
      Parameters:
      constant - The constant
      evaluationContext - The context of the evaluation
      Returns:
      The constant's value
      Throws:
      UnsupportedOperationException - if implementor of a subclass defined constants but forgot to override this method
    • evaluate

      protected T evaluate(Operator operator, Iterator<T> operands, Object evaluationContext)
      Evaluates an operation.
      Subclasses that support operators must override this method (The default implementation throws a UnsupportedOperationException).
      Parameters:
      operator - The operator
      operands - The operands
      evaluationContext - The context of the evaluation
      Returns:
      The result of the operation
      Throws:
      UnsupportedOperationException - if implementor of a subclass defined operators but forgot to override this method
    • evaluate

      protected T evaluate(Function function, Iterator<T> arguments, Object evaluationContext)
      Evaluates a function.
      Subclasses that support functions must override this method (The default implementation throws a UnsupportedOperationException).
      Parameters:
      function - The function
      arguments - The function's arguments
      evaluationContext - The context of the evaluation
      Returns:
      The result of the function
      Throws:
      UnsupportedOperationException - if implementor of a subclass defined functions but forgot to override this method
    • toValue

      protected abstract T toValue(String literal, Object 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, Object 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:
    • 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 Iterator<String> 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.