Class Expression

  • All Implemented Interfaces:
    java.io.Serializable

    public class Expression
    extends java.lang.Object
    implements java.io.Serializable
    This class represents a valid mathematical expression.
    See Also:
    ExpressionBuilder.build(), Serialized Form
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean containsVariable​(java.lang.String name)
      Tells if a variable exists in the expression
      Expression copy()
      Creates a new expression that is a copy of the existing one.
      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.
      java.util.concurrent.Future<java.lang.Double> evaluateAsync​(java.util.concurrent.ExecutorService executor)
      Simple wrapper for ExecutorService.submit(java.util.concurrent.Callable).

      Expressions are NOT thread safe (and most likely will never be).
      java.util.Set<java.lang.String> getVariableNames()
      Retrieves a Set containing all the variable names
      Expression setVariable​(java.lang.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.
      Expression setVariables​(java.util.Map<java.lang.String,java.lang.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.
      java.lang.String toString()  
      java.lang.String toTokenString()
      Retrieves the internal representation of the expression.
      This method is mostly useless for most users.
      ValidationResult validate()
      Alias for Expression#validate(true)
      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).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Method Detail

      • 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​(java.lang.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:
        java.lang.IllegalArgumentException - if the variable name is a function name or if the variable doesn't exist at build time.
        See Also:
        ExpressionBuilder.build(), containsVariable(java.lang.String), getVariableNames()
      • getVariableNames

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

        public boolean containsVariable​(java.lang.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
      • evaluateAsync

        public java.util.concurrent.Future<java.lang.Double> evaluateAsync​(java.util.concurrent.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()
      • 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:
        java.lang.IllegalArgumentException - if the expression isn't valid
        See Also:
        validate()
      • toString

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

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