Class Expression

java.lang.Object
net.objecthunter.exp4j.Expression
All Implemented Interfaces:
Serializable

public final class Expression extends Object implements Serializable
This class represents a valid mathematical expression.
Author:
Frank Asseg
See Also:
  • Method Details

    • copy

      public Expression copy()
      Creates a new expression that is a copy of the existing one.
      Returns:
      copy of this Expression
    • setVariable

      public Expression setVariable(String name, double value)
      Sets the value of a variable, the variable to set must exist at build time and can't be the name of a function. All variables must be set before calling evaluate()
      Parameters:
      name - variable name as passed to ExpressionBuilder
      value - value of the variable
      Returns:
      this
      Throws:
      IllegalArgumentException - if the variable name is a function name or if the variable doesn't exist at build time.
      See Also:
    • setVariables

      public Expression setVariables(Map<String,Double> variables)
      Sets the value of a set of variables, the variables to set must exist at build time and can't be the name of a function. All variables must be set before calling evaluate()
      Parameters:
      variables - a Map<String,Double> containing all the (name, value) pairs.
      Returns:
      this
      Throws:
      IllegalArgumentException - if the variable name is a function name or if the variable doesn't exist at build time.
      See Also:
    • getVariableNames

      public Set<String> getVariableNames()
      Retrieves a Set containing all the variable names
      Returns:
      variable names
    • containsVariable

      public boolean containsVariable(String name)
      Tells if a variable exists in the expression
      Parameters:
      name - variable name
      Returns:
      true if the variable exists and false otherwise
    • validate

      public ValidationResult validate(boolean checkVariablesSet)
      Validates an expression.
      Building an expression is not the only metric of correctness, this method will generate a ValidationResult telling if a variables are set, if the number of operands is correct, and if all functions have the right number of parameters.

      Note: future version will most likely fail on build, and not at this stage (at least that's my plan).
      Parameters:
      checkVariablesSet - true to check if all variables are set and false otherwise
      Returns:
      ValidationResult
    • validate

      public ValidationResult validate()
      Alias for Expression#validate(true)
      Returns:
      ValidationResult
      See Also:
    • evaluateAsync

      public Future<Double> evaluateAsync(ExecutorService executor)
      Simple wrapper for ExecutorService.submit(java.util.concurrent.Callable).

      Expressions are NOT thread safe (and most likely will never be).
      Parameters:
      executor - ExecutorService to use
      Returns:
      Future task that will eventually have the result of evaluate()
      See Also:
    • evaluate

      public double evaluate()
      Evaluates the expression with the given values, this method will fail if validate() returns a ValidationResult different that ValidationResult.SUCCESS.

      Note: future version will most likely fail on build, and not at this stage, this method will only fail if variables aren't set.
      Returns:
      result of the evaluation
      Throws:
      IllegalArgumentException - if the expression isn't valid
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toTokenString

      public String toTokenString()
      Retrieves the internal representation of the expression.
      This method is mostly useless for most users.
      Returns:
      RPN of the expression