Package net.objecthunter.exp4j
Class Expression
java.lang.Object
net.objecthunter.exp4j.Expression
- All Implemented Interfaces:
Serializable
This class represents a valid mathematical expression.
- Author:
- Frank Asseg
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionboolean
containsVariable
(String name) Tells if a variable exists in the expressioncopy()
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 ifvalidate()
returns aValidationResult
different thatValidationResult.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.evaluateAsync
(ExecutorService executor) Simple wrapper forExecutorService.submit(java.util.concurrent.Callable)
.
Expressions are NOT thread safe (and most likely will never be).Retrieves aSet
containing all the variable namessetVariable
(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.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.toString()
Retrieves the internal representation of the expression.
This method is mostly useless for most users.validate()
Alias forExpression#validate(true)
validate
(boolean checkVariablesSet) Validates an expression.
Building an expression is not the only metric of correctness, this method will generate aValidationResult
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).
-
Method Details
-
copy
Creates a new expression that is a copy of the existing one.- Returns:
- copy of this
Expression
-
setVariable
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 callingevaluate()
- Parameters:
name
- variable name as passed toExpressionBuilder
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
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 callingevaluate()
- Parameters:
variables
- aMap<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
Retrieves aSet
containing all the variable names- Returns:
- variable names
-
containsVariable
Tells if a variable exists in the expression- Parameters:
name
- variable name- Returns:
true
if the variable exists andfalse
otherwise
-
validate
Validates an expression.
Building an expression is not the only metric of correctness, this method will generate aValidationResult
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 andfalse
otherwise- Returns:
ValidationResult
-
validate
Alias forExpression#validate(true)
- Returns:
ValidationResult
- See Also:
-
evaluateAsync
Simple wrapper forExecutorService.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 ifvalidate()
returns aValidationResult
different thatValidationResult.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
-
toTokenString
Retrieves the internal representation of the expression.
This method is mostly useless for most users.- Returns:
- RPN of the expression
-