Package com.udojava.evalex
Class Expression
- java.lang.Object
-
- com.udojava.evalex.Expression
-
public class Expression extends java.lang.ObjectEvalEx - 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).
Thanks to all who contributed to this project: Contributors
- See Also:
- GitHub repository
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classExpression.ExpressionExceptionThe expression evaluators exception class.classExpression.FunctionAbstract definition of a supported expression function.classExpression.LazyFunctionLazyFunction classstatic interfaceExpression.LazyNumberLazyNumber interface created for lazily evaluated functionsclassExpression.OperatorAbstract definition of a supported operator.classExpression.TokenToken classclassExpression.UnaryOperatorAbstract definition of a prefix unary operator with right-side operand.
-
Field Summary
Fields Modifier and Type Field Description static java.math.BigDecimaleDefinition of e: "Euler's number" as a constant, can be used in expressions as variable.protected java.util.Map<java.lang.String,LazyFunction>functionsAll defined functions with name and implementation.static java.lang.StringMISSING_PARAMETERS_FOR_OPERATORException message for missing operators.static intOPERATOR_PRECEDENCE_ADDITIVEAdditive operators precedence: + and -static intOPERATOR_PRECEDENCE_ANDAnd operator precedence: &&static intOPERATOR_PRECEDENCE_COMPARISONComparative operators precedence: <,>,<=,>=static intOPERATOR_PRECEDENCE_EQUALITYEquality operators precedence: =, ==, !=.static intOPERATOR_PRECEDENCE_MULTIPLICATIVEMultiplicative operators precedence: *,/,%static intOPERATOR_PRECEDENCE_OROr operator precedence: ||static intOPERATOR_PRECEDENCE_POWERPower operator precedence: ^static intOPERATOR_PRECEDENCE_POWER_HIGHERAn optional higher power operator precedence.static intOPERATOR_PRECEDENCE_UNARYUnary operators precedence: + and - as prefixprotected java.util.Map<java.lang.String,LazyOperator>operatorsAll defined operators with name and implementation.static java.math.BigDecimalPIDefinition of PI as a constant, can be used in expressions as variable.protected java.util.Map<java.lang.String,Expression.LazyNumber>variablesAll defined variables with name and value.
-
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, ExpressionSettings expressionSettings)Creates a new expression instance from an expression string with given settings.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 Static Methods Instance Methods Concrete Methods Modifier and Type Method Description FunctionaddFunction(Function function)Adds a function to the list of supported functionsLazyFunctionaddLazyFunction(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.Expressionand(java.lang.String variable, Expression.LazyNumber value)Sets a variable value.Expressionand(java.lang.String variable, java.lang.String value)Sets a variable value.Expressionand(java.lang.String variable, java.math.BigDecimal value)Sets a variable value.static voidassertNotNull(java.math.BigDecimal v1)Assert the single expected operand is not null.static voidassertNotNull(java.math.BigDecimal v1, java.math.BigDecimal v2)Assert the two expected operands are not null.protected Expression.LazyNumbercreateLazyNumber(java.math.BigDecimal bigDecimal)Construct a LazyNumber from a BigDecimalbooleanequals(java.lang.Object o)java.math.BigDecimaleval()Evaluates the expression.java.math.BigDecimaleval(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.StringgetExpression()java.util.Iterator<Expression.Token>getExpressionTokenizer()Get an iterator for this expression, allows iterating over an expression token by token.java.lang.StringgetOriginalExpression()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.inthashCode()java.util.List<java.lang.String>infixNotation()booleanisBoolean()Checks whether the expression is a boolean expression.protected booleanisNumber(java.lang.String st)Is the string a number?ExpressionsetFirstVariableCharacters(java.lang.String chars)Sets the characters other than letters and digits that are valid as the first character of a variable.ExpressionsetPrecision(int precision)Sets the precision for expression evaluation.ExpressionsetRoundingMode(java.math.RoundingMode roundingMode)Sets the rounding mode for expression evaluation.ExpressionsetVariable(java.lang.String variable, Expression.LazyNumber value)Sets a variable value.ExpressionsetVariable(java.lang.String variable, java.lang.String value)Sets a variable value.ExpressionsetVariable(java.lang.String variable, java.math.BigDecimal value)Sets a variable value.ExpressionsetVariableCharacters(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.StringtoRPN()Get a string representation of the RPN (Reverse Polish Notation) for this expression.java.lang.StringtoString()Expressionwith(java.lang.String variable, Expression.LazyNumber value)Sets a variable value.Expressionwith(java.lang.String variable, java.lang.String value)Sets a variable value.Expressionwith(java.lang.String variable, java.math.BigDecimal 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_POWER_HIGHER
public static final int OPERATOR_PRECEDENCE_POWER_HIGHER
An optional higher power operator precedence.ExpressionSettings- 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.
-
MISSING_PARAMETERS_FOR_OPERATOR
public static final java.lang.String MISSING_PARAMETERS_FOR_OPERATOR
Exception message for missing operators.- See Also:
- Constant Field Values
-
operators
protected java.util.Map<java.lang.String,LazyOperator> operators
All defined operators with name and implementation.
-
functions
protected java.util.Map<java.lang.String,LazyFunction> functions
All defined functions with name and implementation.
-
variables
protected java.util.Map<java.lang.String,Expression.LazyNumber> variables
All defined variables with name and value.
-
-
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- TheMathContextto use by default.
-
Expression
public Expression(java.lang.String expression, ExpressionSettings expressionSettings)Creates a new expression instance from an expression string with given settings.- Parameters:
expression- The expression. E.g."2.4*sin(3)/(2-4)"or"sin(y)>0 & max(z, 3)>3"expressionSettings- TheExpressionSettingsto use by default.
-
-
Method Detail
-
createLazyNumber
protected Expression.LazyNumber createLazyNumber(java.math.BigDecimal bigDecimal)
Construct a LazyNumber from a BigDecimal
-
assertNotNull
public static void assertNotNull(java.math.BigDecimal v1)
Assert the single expected operand is not null.
-
assertNotNull
public static void assertNotNull(java.math.BigDecimal v1, java.math.BigDecimal v2)Assert the two expected operands are not null.
-
isNumber
protected boolean isNumber(java.lang.String st)
Is the string a number?- Parameters:
st- The string.- Returns:
true, if the input string is a number.
-
eval
public java.math.BigDecimal eval()
Evaluates the expression.- Returns:
- The result of the expression. Trailing zeros are stripped.
-
eval
public java.math.BigDecimal eval(boolean stripTrailingZeros)
Evaluates the expression.- Parameters:
stripTrailingZeros- If set totruetrailing 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
nullif 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
nullif 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
nullif 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<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:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin 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:
trueif the last operator/function was a boolean.
-
infixNotation
public java.util.List<java.lang.String> infixNotation()
-
-