|
http://www.eteks.com | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.eteks.parser.DoubleInterpreter | +--com.eteks.parser.WrapperInterpreter
Runtime interpreter operating on instances of wrapping classes. This class overrides the
implementation of its DoubleInterpreter
super class to
return values, instances of the classes Double
, Long
, Boolean
,
String
or Character
according to the operated literal, operator or function.
The type required for parameters of the methods of this interpreter depends on
the computed operation. Most of them compute numbers and thus require their parameters
value to be instances of Number
(Double
, Integer
,...).
As no type checking is done during the parsing of functions and expressions, this interpreter
suits particularly to fill this lack and compute expressions supposed to respect the Java syntax :
getLiteralValue ()
is an instance of Boolean
for the keys Syntax.CONSTANT_FALSE
and Syntax.CONSTANT_TRUE
.isTrue ()
method used for conditions accepts only a Boolean
parameter.Boolean
.Boolean
operands.OPERATOR_ADD
, OPERATOR_SUBSTRACT
,
OPERATOR_MULTIPLY
, OPERATOR_DIVIDE
, OPERATOR_POWER
,
OPERATOR_MODULO
and OPERATOR_REMAINDER
) returns
an instance of Long
only if both operators are integers (instances
of Integer
or Long
), otherwise the returned value is
an instance of Double
.
OPERATOR_BITWISE_...
)
accepts only Integer
, Long
or Boolean
operands.OPERATOR_SHIFT_...
)
accepts only Integer
or Long
operands.getLiteralValue ()
accepts to return instances of String
or Character
.
WrapperInterpreter
interpreter is created with its parameter
concatenationSupported
equal to true
, the binary operator of the
addition performs string concatenation if one of its operands is an instance of String
.WrapperInterpreter
interpreter is created with its parameter
integerDivisionSupported
equal to true
, the binary operator of the
division returns an integer if the type of its operand are integers with possible loss of precision.WrapperInterpreter
interpreter is created with its parameter
characterOperationSupported
equal to true
, the unary and
binary operators and the common functions (excepted the logical ones) accepts a character
(instance of Character
) as an integer parameter.The methods of this class are thread safe.// Create a parser using a Java syntax that allows boolean literals and strings FunctionParser parser = new FunctionParser (new JavaSyntax (true)); // Compile different types of functions Function function1 = parser.compileFunction("f(x) = \"Value of x = \" + x"); Function function2 = parser.compileFunction("fact (x) = x <= 0 ? 1 : x * fact (x - 1)"); Function function3 = parser.compileFunction("multPow2 (x,power2) = x << power2"); // Compute these functions Interpreter interpreter = new WrapperInterpreter (); System.out.println (function1.computeFunction (interpreter, new Object [] {new Integer (2001)})); System.out.println (function2.computeFunction (interpreter, new Object [] {new Double (20)})); System.out.println (function2.computeFunction (interpreter, new Object [] {new Long (5)})); System.out.println (function3.computeFunction (interpreter, new Object [] {new Integer (51), new Integer (3)}));
JavaSyntax
,
Syntax
Fields inherited from class com.eteks.parser.DoubleInterpreter |
FALSE_DOUBLE, TRUE_DOUBLE |
Constructor Summary | |
WrapperInterpreter()
Creates an interpreter operating on instances of wrapping classes. |
|
WrapperInterpreter(boolean concatenationSupported)
Creates an interpreter operating on instances of wrapping classes. |
|
WrapperInterpreter(boolean concatenationSupported,
boolean integerDivisionSupported)
Creates an interpreter operating on instances of wrapping classes. |
|
WrapperInterpreter(boolean concatenationSupported,
boolean integerDivisionSupported,
boolean characterOperationSupported)
Creates an interpreter operating on instances of wrapping classes. |
Method Summary | |
java.lang.Object |
getBinaryOperatorValue(java.lang.Object binaryOperatorKey,
java.lang.Object operand1,
java.lang.Object operand2)
Returns the value of the operation of the binary operator binaryOperatorKey applied on
the two operands operand1 and operand2 . |
java.lang.Object |
getCommonFunctionValue(java.lang.Object commonFunctionKey,
java.lang.Object param)
Returns the value of the common function commonFunctionKey with
the parameter param . |
java.lang.Object |
getConstantValue(java.lang.Object constantKey)
Returns the value of the constant constantKey . |
java.lang.Object |
getLiteralValue(java.lang.Object literal)
Returns the value of the literal literal . |
java.lang.Object |
getParameterValue(java.lang.Object parameter)
Returns the value of the parameter parameter . |
java.lang.Object |
getUnaryOperatorValue(java.lang.Object unaryOperatorKey,
java.lang.Object operand)
Returns the value of the operation of the unary operator unaryOperatorKey applied
on the operand operand . |
boolean |
isTrue(java.lang.Object condition)
Returns true or false according to the value of condition . |
Methods inherited from class com.eteks.parser.DoubleInterpreter |
getConditionValue, getFunctionValue, supportsRecursiveCall |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
public WrapperInterpreter()
public WrapperInterpreter(boolean concatenationSupported)
concatenationSupported
- if equal to true
, the binary operator of the
addition performs string concatenation if one of its operands is an instance of String
.public WrapperInterpreter(boolean concatenationSupported, boolean integerDivisionSupported)
concatenationSupported
- if equal to true
, the binary operator of the
addition performs string concatenation if one of its operands is an instance of String
.integerDivisionSupported
- if equal to true
, the binary operator of the
division returns an integer if the type of its operand are integers (meaning 3 / 2
will return 1).public WrapperInterpreter(boolean concatenationSupported, boolean integerDivisionSupported, boolean characterOperationSupported)
concatenationSupported
- if equal to true
, the binary operator of the
addition performs string concatenation if one of its operands is an instance of String
.integerDivisionSupported
- if equal to true
, the binary operator of the
division returns an integer if the type of its operand are integers (meaning 3 / 2
will return 1).characterOperationSupported
- if equal to true
, the unary and
binary operators and the common functions (excepted the logical ones) accepts
a character (instance of the class Character
) as an integer parameter.Method Detail |
public java.lang.Object getLiteralValue(java.lang.Object literal)
literal
. literal
may be an instance of Number
, String
or Character
.getLiteralValue
in class DoubleInterpreter
literal
- an instance of Number
, String
or Character
.java.lang.IllegalArgumentException
- if literal
isn't an instance of Number
,
String
or Character
.public java.lang.Object getParameterValue(java.lang.Object parameter)
parameter
. parameter
may be an instance of Number
, String
, Character
or Boolean
.getParameterValue
in class DoubleInterpreter
parameter
- an instance of Number
, String
, Character
or Boolean
.java.lang.IllegalArgumentException
- if parameter
isn't an instance of Number
,
String
, Character
or Boolean
.public java.lang.Object getConstantValue(java.lang.Object constantKey)
constantKey
. constantKey
may be the key of a constant of Syntax
(one of CONSTANT_PI
,
CONSTANT_E
, CONSTANT_FALSE
, CONSTANT_TRUE
).getConstantValue
in class DoubleInterpreter
constantKey
- the key of a constant of Syntax
.java.lang.IllegalArgumentException
- if constantKey
isn't a key of a constant of
Syntax
.public java.lang.Object getUnaryOperatorValue(java.lang.Object unaryOperatorKey, java.lang.Object operand)
unaryOperatorKey
applied
on the operand operand
. unaryOperatorKey
must be the key
of an unary operator of Syntax
(one of OPERATOR_POSITIVE
,
OPERATOR_OPPOSITE
, OPERATOR_LOGICAL_NOT
, OPERATOR_BITWISE_NOT
).getUnaryOperatorValue
in class DoubleInterpreter
unaryOperatorKey
- the key of an unary operator of Syntax
.operand
- the operand.Double
,
Long
or Boolean
.java.lang.IllegalArgumentException
- if operand
isn't of the good type for the requested
operator or if unaryOperatorKey
isn't the key of an unary
operator of Syntax
.public java.lang.Object getBinaryOperatorValue(java.lang.Object binaryOperatorKey, java.lang.Object operand1, java.lang.Object operand2)
binaryOperatorKey
applied on
the two operands operand1
and operand2
. binaryOperatorKey
must be the key of a binary operator of Syntax
(one of OPERATOR_ADD
,
OPERATOR_SUBSTRACT
, OPERATOR_MULTIPLY
, OPERATOR_DIVIDE
,...).getBinaryOperatorValue
in class DoubleInterpreter
binaryOperatorKey
- the key of a binary operator of Syntax
.operand1
- the first operand.operand2
- the second operand.Double
,
Long
, Boolean
or String
.java.lang.IllegalArgumentException
- if operand1
and operand2
aren't of
the good type for requested operator or if binaryOperatorKey
isn't the key
of a binary operator of Syntax
.public java.lang.Object getCommonFunctionValue(java.lang.Object commonFunctionKey, java.lang.Object param)
commonFunctionKey
with
the parameter param
. commonFunctionKey
must be the key
of a commomon function of Syntax
(one of FUNCTION_LN
,
FUNCTION_LOG
, FUNCTION_EXP
, FUNCTION_SQR
,...).getCommonFunctionValue
in class DoubleInterpreter
commonFunctionKey
- the key of a common function of Syntax
.param
- the parameter of the function.Double
,
Long
or Boolean
.java.lang.IllegalArgumentException
- if param
isn't of the good type for the
requested function or if commonFunctionKey
isn't the key of a
commomon function of Syntax
.public boolean isTrue(java.lang.Object condition)
true
or false
according to the value of condition
.isTrue
in class DoubleInterpreter
condition
- the value to test (instance of Boolean
).condition
.
|
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |