Package net.sourceforge.plantuml.evalex
Class Expression
- java.lang.Object
-
- net.sourceforge.plantuml.evalex.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- The software is licensed under the MIT Open Source license (see LICENSE file).
- The *power of* operator (^) implementation was copied from Stack Overflow. Thanks to Gene Marin.
- The SQRT() function implementation was taken from the book The Java Programmers Guide To numerical Computing (Ronald Mak, 2002).
- See Also:
- GitHub repository
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Expression.ExpressionException
The expression evaluators exception class.class
Expression.Function
Abstract definition of a supported expression function.class
Expression.LazyFunction
static interface
Expression.LazyNumber
LazyNumber interface created for lazily evaluated functionsclass
Expression.Operator
Abstract definition of a supported operator.class
Expression.UnaryOperator
-
Field Summary
Fields Modifier and Type Field Description static java.math.BigDecimal
e
Definition of e: "Euler's number" as a constant, can be used in expressions as variable.static int
OPERATOR_PRECEDENCE_ADDITIVE
Additive operators precedence: + and -static int
OPERATOR_PRECEDENCE_AND
And operator precedence: &&static int
OPERATOR_PRECEDENCE_COMPARISON
Comparative operators precedence: <,>,<=,>=static int
OPERATOR_PRECEDENCE_EQUALITY
Equality operators precedence: =, ==, !=.static int
OPERATOR_PRECEDENCE_MULTIPLICATIVE
Multiplicative operators precedence: *,/,%static int
OPERATOR_PRECEDENCE_OR
Or operator precedence: ||static int
OPERATOR_PRECEDENCE_POWER
Power operator precedence: ^static int
OPERATOR_PRECEDENCE_UNARY
Unary operators precedence: + and - as prefixstatic java.math.BigDecimal
PI
Definition of PI as a constant, can be used in expressions as variable.
-
Constructor Summary
Constructors Constructor Description Expression(java.lang.String expression)
Creates a new expression instance from an expression string with a given default match context ofMathContext.DECIMAL32
.Expression(java.lang.String expression, java.math.MathContext defaultMathContext)
Creates a new expression instance from an expression string with a given default match context.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Function
addFunction(Function function)
Adds a function to the list of supported functionsLazyFunction
addLazyFunction(LazyFunction function)
Adds a lazy function function to the list of supported functions<OPERATOR extends LazyOperator>
OPERATORaddOperator(OPERATOR operator)
Adds an operator to the list of supported operators.Expression
and(java.lang.String variable, java.lang.String value)
Sets a variable value.Expression
and(java.lang.String variable, java.math.BigDecimal value)
Sets a variable value.Expression
and(java.lang.String variable, Expression.LazyNumber value)
Sets a variable value.boolean
equals(java.lang.Object o)
java.lang.Number
eval()
Evaluates the expression.java.lang.Number
eval(boolean stripTrailingZeros)
Evaluates the expression.java.util.Set<java.lang.String>
getDeclaredFunctions()
Exposing declared functions.java.util.Set<java.lang.String>
getDeclaredOperators()
Exposing declared operators in the expression.java.util.Set<java.lang.String>
getDeclaredVariables()
Exposing declared variables in the expression.java.lang.String
getExpression()
java.util.Iterator<net.sourceforge.plantuml.evalex.Expression.Token>
getExpressionTokenizer()
Get an iterator for this expression, allows iterating over an expression token by token.java.lang.String
getOriginalExpression()
The original expression used to construct this expression, without variables substituted.java.util.List<java.lang.String>
getUsedVariables()
Returns a list of the variables in the expression.int
hashCode()
boolean
isBoolean()
Checks whether the expression is a boolean expression.Expression
setFirstVariableCharacters(java.lang.String chars)
Sets the characters other than letters and digits that are valid as the first character of a variable.Expression
setPrecision(int precision)
Sets the precision for expression evaluation.Expression
setRoundingMode(java.math.RoundingMode roundingMode)
Sets the rounding mode for expression evaluation.Expression
setVariable(java.lang.String variable, java.lang.String value)
Sets a variable value.Expression
setVariable(java.lang.String variable, java.math.BigDecimal value)
Sets a variable value.Expression
setVariable(java.lang.String variable, Expression.LazyNumber value)
Sets a variable value.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.java.lang.String
toRPN()
Get a string representation of the RPN (Reverse Polish Notation) for this expression.java.lang.String
toString()
Expression
with(java.lang.String variable, java.lang.String value)
Sets a variable value.Expression
with(java.lang.String variable, java.math.BigDecimal value)
Sets a variable value.Expression
with(java.lang.String variable, Expression.LazyNumber value)
Sets a variable value.
-
-
-
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 ofMathContext.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
- TheMathContext
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 totrue
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 classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.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.
-
-