|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.silverpeas.util.PrefixedNotationExpressionEngine<R>
R
- the type the data the evaluation must result.public class PrefixedNotationExpressionEngine<R>
This engine reads a prefixed notation expression in order to evaluate it.
Each part of the expression is composed of an operator with one or several operands.
Each operand must be wrapped into parentheses.
An operand can be:
from(Function, OperatorFunction[])
) method. The value to convert is detected
when it has no parenthesisfrom(Function, OperatorFunction[])
method.from(Function, OperatorFunction[])
method, the caller defines the
behaviour of the operators of the expression to evaluate.
For example:
PrefixedNotationExpressionEngineSome errors can be thrown asengine = from( (aString) -> aString == null ? 0 : Integer.parseInt(aString), // the converter new OperatorFunction ("+", (a,b) -> (a == null ? 0 : a) + b), // ADD operator new OperatorFunction ("-", (a,b) -> (a == null ? 0 : a) - b) // SUBTRACT operator ) engine.evaluate("+(+(3)(4))(+(-(5))(2))"); // gives 4 // Decomposed treatment: // +(7)(+(-(5))(2)) // +(7)(+(-5)(2)) // +(7)(-3) // 4 engine.evaluate("+(+(3)(4))(+(-5)(2))"); // gives also 4, here the minus character from '-5' // is taken into account as part of the value and not // as an operator
IllegalArgumentException
with message containing an error
key. It is free to the caller to use these keys.
Nested Class Summary | |
---|---|
static class |
PrefixedNotationExpressionEngine.OperatorFunction<T>
Defines an operator behavior. |
Method Summary | ||
---|---|---|
boolean |
detectOperator(String expression)
Indicates if the given expression is contains an operator, and so, a potential expression to evaluate. |
|
R |
evaluate(String expression)
Evaluates the given expression. |
|
static
|
from(Function<String,R> converter,
PrefixedNotationExpressionEngine.OperatorFunction<R>... operationFunctions)
Initializes the instance. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static <R> PrefixedNotationExpressionEngine<R> from(Function<String,R> converter, PrefixedNotationExpressionEngine.OperatorFunction<R>... operationFunctions)
operationFunctions
-
public R evaluate(String expression)
expression
- the expression to evaluate.
public boolean detectOperator(String expression)
expression
- the expression to verify.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |