Package com.fathzer.soft.javaluator
Class AbstractEvaluator<T>
java.lang.Object
com.fathzer.soft.javaluator.AbstractEvaluator<T>
- Type Parameters:
T- The type of values handled by the evaluator
- Direct Known Subclasses:
DoubleEvaluator
An abstract evaluator, able to evaluate infix expressions.
Some standard evaluators are included in the library, you can define your own by subclassing this class.
This class is thread safe.
Some standard evaluators are included in the library, you can define your own by subclassing this class.
This class is thread safe.
- Author:
- Jean-Marc Astesana
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected TEvaluates a constant.protected TEvaluates a function.protected TEvaluates an operation.Evaluates an expression.Evaluates an expression that contains variables.Gets the constants supported by this evaluator.Gets the functions supported by this evaluator.Gets the operators supported by this evaluator.protected OperatorguessOperator(Token previous, List<Operator> candidates) When a token can be more than one operator (homonym operators), this method guesses the right operator.Converts the evaluated expression into tokens.protected abstract TEvaluates a literal (Converts it to a value).protected voidvalidateHomonyms(List<Operator> operators) Validates that homonym operators are valid.
-
Constructor Details
-
AbstractEvaluator
Constructor.- Parameters:
parameters- The evaluator parameters.
Please note that there's no side effect between the evaluator and the parameters. So, changes made to the parameters after the call to this constructor are ignored by the instance.
-
-
Method Details
-
validateHomonyms
Validates that homonym operators are valid.
Homonym operators are operators with the same name (like the unary - and the binary - operators)
This method is called when homonyms are passed to the constructor.
This default implementation only allows the case where there's two operators, one binary and one unary. Subclasses can override this method in order to accept others configurations.- Parameters:
operators- The operators to validate.- Throws:
IllegalArgumentException- if the homonyms are not compatibles.- See Also:
-
guessOperator
When a token can be more than one operator (homonym operators), this method guesses the right operator.
A very common case is the - sign in arithmetic computation which can be an unary or a binary operator, depending on what was the previous token.
Warning: maybe the arguments of this function are not enough to deal with all the cases. So, this part of the evaluation is in alpha state (method may change in the future).- Parameters:
previous- The last parsed tokens (the previous token in the infix expression we are evaluating).candidates- The candidate tokens.- Returns:
- A token
- See Also:
-
evaluate
Evaluates a constant.
Subclasses that support constants must override this method (The default implementation throws a UnsupportedOperationException).- Parameters:
constant- The constantevaluationContext- The context of the evaluation- Returns:
- The constant's value
- Throws:
UnsupportedOperationException- if implementor of a subclass defined constants but forgot to override this method
-
evaluate
Evaluates an operation.
Subclasses that support operators must override this method (The default implementation throws a UnsupportedOperationException).- Parameters:
operator- The operatoroperands- The operandsevaluationContext- The context of the evaluation- Returns:
- The result of the operation
- Throws:
UnsupportedOperationException- if implementor of a subclass defined operators but forgot to override this method
-
evaluate
Evaluates a function.
Subclasses that support functions must override this method (The default implementation throws a UnsupportedOperationException).- Parameters:
function- The functionarguments- The function's argumentsevaluationContext- The context of the evaluation- Returns:
- The result of the function
- Throws:
UnsupportedOperationException- if implementor of a subclass defined functions but forgot to override this method
-
toValue
Evaluates a literal (Converts it to a value).- Parameters:
literal- The literal to evaluate.evaluationContext- The context of the evaluation- Returns:
- an instance of T.
- Throws:
IllegalArgumentException- if the literal can't be converted to a value.
-
evaluate
Evaluates an expression.- Parameters:
expression- The expression to evaluate.- Returns:
- the result of the evaluation.
- Throws:
IllegalArgumentException- if the expression is not correct.
-
evaluate
Evaluates an expression that contains variables.- Parameters:
expression- The expression to evaluate.evaluationContext- The context of the evaluation.
This context is an object that can contain useful dynamic data, for example the values of the variables used in the expression (Use an AbstractVariableSet to do that).
The context is not limited to variable values but can be used for any dynamic information. A good example is the BooleanSetEvaluator one.- Returns:
- the result of the evaluation.
- Throws:
IllegalArgumentException- if the expression is not correct.- See Also:
-
getOperators
Gets the operators supported by this evaluator.- Returns:
- a collection of operators.
-
getFunctions
Gets the functions supported by this evaluator.- Returns:
- a collection of functions.
-
getConstants
Gets the constants supported by this evaluator.- Returns:
- a collection of constants.
-
tokenize
Converts the evaluated expression into tokens.
Example: The result for the expression "-1+min(10,3)" is an iterator on "-", "1", "+", "min", "(", "10", ",", "3", ")".
By default, the operators symbols, the brackets and the function argument separator are used as delimiter in the string.- Parameters:
expression- The expression that is evaluated- Returns:
- A string iterator.
-