Class Expression


  • public class Expression
    extends java.lang.Object

    EvalEx - Java Expression Evaluator

    Introduction

    EvalEx is a handy expression evaluator for Java, that allows to evaluate simple mathematical and boolean expressions.
    For more information, see: EvalEx GitHub repository
    See Also:
    GitHub repository
    • Field Detail

      • OPERATOR_PRECEDENCE_UNARY

        public static final int OPERATOR_PRECEDENCE_UNARY
        Unary operators precedence: + and - as prefix
        See Also:
        Constant Field Values
      • OPERATOR_PRECEDENCE_EQUALITY

        public static final int OPERATOR_PRECEDENCE_EQUALITY
        Equality operators precedence: =, ==, !=. <>
        See Also:
        Constant Field Values
      • OPERATOR_PRECEDENCE_COMPARISON

        public static final int OPERATOR_PRECEDENCE_COMPARISON
        Comparative operators precedence: <,>,<=,>=
        See Also:
        Constant Field Values
      • OPERATOR_PRECEDENCE_OR

        public static final int OPERATOR_PRECEDENCE_OR
        Or operator precedence: ||
        See Also:
        Constant Field Values
      • OPERATOR_PRECEDENCE_AND

        public static final int OPERATOR_PRECEDENCE_AND
        And operator precedence: &&
        See Also:
        Constant Field Values
      • OPERATOR_PRECEDENCE_POWER

        public static final int OPERATOR_PRECEDENCE_POWER
        Power operator precedence: ^
        See Also:
        Constant Field Values
      • OPERATOR_PRECEDENCE_MULTIPLICATIVE

        public static final int OPERATOR_PRECEDENCE_MULTIPLICATIVE
        Multiplicative operators precedence: *,/,%
        See Also:
        Constant Field Values
      • OPERATOR_PRECEDENCE_ADDITIVE

        public static final int OPERATOR_PRECEDENCE_ADDITIVE
        Additive operators precedence: + and -
        See Also:
        Constant Field Values
      • PI

        public static final java.math.BigDecimal PI
        Definition of PI as a constant, can be used in expressions as variable.
      • e

        public static final java.math.BigDecimal e
        Definition of e: "Euler's number" as a constant, can be used in expressions as variable.
    • Constructor Detail

      • Expression

        public Expression​(java.lang.String expression)
        Creates a new expression instance from an expression string with a given default match context of MathContext.DECIMAL32.
        Parameters:
        expression - The expression. E.g. "2.4*sin(3)/(2-4)" or "sin(y)>0 & max(z, 3)>3"
      • Expression

        public Expression​(java.lang.String expression,
                          java.math.MathContext defaultMathContext)
        Creates a new expression instance from an expression string with a given default match context.
        Parameters:
        expression - The expression. E.g. "2.4*sin(3)/(2-4)" or "sin(y)>0 & max(z, 3)>3"
        defaultMathContext - The MathContext to use by default.
    • Method Detail

      • eval

        public java.lang.Number eval()
        Evaluates the expression.
        Returns:
        The result of the expression. Trailing zeros are stripped.
      • eval

        public java.lang.Number eval​(boolean stripTrailingZeros)
        Evaluates the expression.
        Parameters:
        stripTrailingZeros - If set to true trailing zeros in the result are stripped.
        Returns:
        The result of the expression.
      • setPrecision

        public Expression setPrecision​(int precision)
        Sets the precision for expression evaluation.
        Parameters:
        precision - The new precision.
        Returns:
        The expression, allows to chain methods.
      • setRoundingMode

        public Expression setRoundingMode​(java.math.RoundingMode roundingMode)
        Sets the rounding mode for expression evaluation.
        Parameters:
        roundingMode - The new rounding mode.
        Returns:
        The expression, allows to chain methods.
      • setFirstVariableCharacters

        public Expression setFirstVariableCharacters​(java.lang.String chars)
        Sets the characters other than letters and digits that are valid as the first character of a variable.
        Parameters:
        chars - The new set of variable characters.
        Returns:
        The expression, allows to chain methods.
      • setVariableCharacters

        public Expression setVariableCharacters​(java.lang.String chars)
        Sets the characters other than letters and digits that are valid as the second and subsequent characters of a variable.
        Parameters:
        chars - The new set of variable characters.
        Returns:
        The expression, allows to chain methods.
      • addOperator

        public <OPERATOR extends LazyOperator> OPERATOR addOperator​(OPERATOR operator)
        Adds an operator to the list of supported operators.
        Parameters:
        operator - The operator to add.
        Returns:
        The previous operator with that name, or null if there was none.
      • addFunction

        public Function addFunction​(Function function)
        Adds a function to the list of supported functions
        Parameters:
        function - The function to add.
        Returns:
        The previous operator with that name, or null if there was none.
      • addLazyFunction

        public LazyFunction addLazyFunction​(LazyFunction function)
        Adds a lazy function function to the list of supported functions
        Parameters:
        function - The function to add.
        Returns:
        The previous operator with that name, or null if there was none.
      • setVariable

        public Expression setVariable​(java.lang.String variable,
                                      java.math.BigDecimal value)
        Sets a variable value.
        Parameters:
        variable - The variable name.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • setVariable

        public Expression setVariable​(java.lang.String variable,
                                      Expression.LazyNumber value)
        Sets a variable value.
        Parameters:
        variable - The variable name.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • setVariable

        public Expression setVariable​(java.lang.String variable,
                                      java.lang.String value)
        Sets a variable value.
        Parameters:
        variable - The variable to set.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • with

        public Expression with​(java.lang.String variable,
                               java.math.BigDecimal value)
        Sets a variable value.
        Parameters:
        variable - The variable to set.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • with

        public Expression with​(java.lang.String variable,
                               Expression.LazyNumber value)
        Sets a variable value.
        Parameters:
        variable - The variable to set.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • and

        public Expression and​(java.lang.String variable,
                              java.lang.String value)
        Sets a variable value.
        Parameters:
        variable - The variable to set.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • and

        public Expression and​(java.lang.String variable,
                              java.math.BigDecimal value)
        Sets a variable value.
        Parameters:
        variable - The variable to set.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • and

        public Expression and​(java.lang.String variable,
                              Expression.LazyNumber value)
        Sets a variable value.
        Parameters:
        variable - The variable to set.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • with

        public Expression with​(java.lang.String variable,
                               java.lang.String value)
        Sets a variable value.
        Parameters:
        variable - The variable to set.
        value - The variable value.
        Returns:
        The expression, allows to chain methods.
      • getExpressionTokenizer

        public java.util.Iterator<net.sourceforge.plantuml.evalex.Expression.Token> getExpressionTokenizer()
        Get an iterator for this expression, allows iterating over an expression token by token.
        Returns:
        A new iterator instance for this expression.
      • toRPN

        public java.lang.String toRPN()
        Get a string representation of the RPN (Reverse Polish Notation) for this expression.
        Returns:
        A string with the RPN representation for this expression.
      • getDeclaredVariables

        public java.util.Set<java.lang.String> getDeclaredVariables()
        Exposing declared variables in the expression.
        Returns:
        All declared variables.
      • getDeclaredOperators

        public java.util.Set<java.lang.String> getDeclaredOperators()
        Exposing declared operators in the expression.
        Returns:
        All declared operators.
      • getDeclaredFunctions

        public java.util.Set<java.lang.String> getDeclaredFunctions()
        Exposing declared functions.
        Returns:
        All declared functions.
      • getExpression

        public java.lang.String getExpression()
        Returns:
        The original expression string
      • getUsedVariables

        public java.util.List<java.lang.String> getUsedVariables()
        Returns a list of the variables in the expression.
        Returns:
        A list of the variable names in this expression.
      • getOriginalExpression

        public java.lang.String getOriginalExpression()
        The original expression used to construct this expression, without variables substituted.
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

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

        public boolean isBoolean()
        Checks whether the expression is a boolean expression. An expression is considered a boolean expression, if the last operator or function is boolean. The IF function is handled special. If the third parameter is boolean, then the IF is also considered boolean, else non-boolean.
        Returns:
        true if the last operator/function was a boolean.