http://www.eteks.com

com.eteks.parser
Class WrapperInterpreter

java.lang.Object
  |
  +--com.eteks.parser.DoubleInterpreter
        |
        +--com.eteks.parser.WrapperInterpreter
All Implemented Interfaces:
Interpreter
Direct Known Subclasses:
JeksInterpreter

public class WrapperInterpreter
extends DoubleInterpreter

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 :

These features allow to compute the following functions :
 // 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)}));
The methods of this class are thread safe.

Since:
Jeks 1.0
Version:
1.0
Author:
Emmanuel Puybaret
See Also:
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

WrapperInterpreter

public WrapperInterpreter()
Creates an interpreter operating on instances of wrapping classes. By default concatenation, division on integers and operations on characters are supported.

WrapperInterpreter

public WrapperInterpreter(boolean concatenationSupported)
Creates an interpreter operating on instances of wrapping classes. By default division on integers and operations on characters are supported.
Parameters:
concatenationSupported - if equal to true, the binary operator of the addition performs string concatenation if one of its operands is an instance of String.

WrapperInterpreter

public WrapperInterpreter(boolean concatenationSupported,
                          boolean integerDivisionSupported)
Creates an interpreter operating on instances of wrapping classes. By default operations on characters are supported.
Parameters:
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).

WrapperInterpreter

public WrapperInterpreter(boolean concatenationSupported,
                          boolean integerDivisionSupported,
                          boolean characterOperationSupported)
Creates an interpreter operating on instances of wrapping classes.
Parameters:
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

getLiteralValue

public java.lang.Object getLiteralValue(java.lang.Object literal)
Returns the value of the literal literal. literal may be an instance of Number, String or Character.
Overrides:
getLiteralValue in class DoubleInterpreter
Parameters:
literal - an instance of Number, String or Character.
Returns:
the value of the literal. The returned value is the object itself.
Throws:
java.lang.IllegalArgumentException - if literal isn't an instance of Number, String or Character.

getParameterValue

public java.lang.Object getParameterValue(java.lang.Object parameter)
Returns the value of the parameter parameter. parameter may be an instance of Number, String, Character or Boolean.
Overrides:
getParameterValue in class DoubleInterpreter
Parameters:
parameter - an instance of Number, String, Character or Boolean.
Returns:
the value of the parameter. The returned value is the object itself.
Throws:
java.lang.IllegalArgumentException - if parameter isn't an instance of Number, String, Character or Boolean.

getConstantValue

public java.lang.Object getConstantValue(java.lang.Object constantKey)
Returns the value of the constant constantKey. constantKey may be the key of a constant of Syntax (one of CONSTANT_PI, CONSTANT_E, CONSTANT_FALSE, CONSTANT_TRUE).
Overrides:
getConstantValue in class DoubleInterpreter
Parameters:
constantKey - the key of a constant of Syntax.
Returns:
the value of the constant.
Throws:
java.lang.IllegalArgumentException - if constantKey isn't a key of a constant of Syntax.

getUnaryOperatorValue

public 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. unaryOperatorKey must be the key of an unary operator of Syntax (one of OPERATOR_POSITIVE, OPERATOR_OPPOSITE, OPERATOR_LOGICAL_NOT, OPERATOR_BITWISE_NOT).
Overrides:
getUnaryOperatorValue in class DoubleInterpreter
Parameters:
unaryOperatorKey - the key of an unary operator of Syntax.
operand - the operand.
Returns:
the result of the operation. The returned value is an instance of Double, Long or Boolean.
Throws:
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.

getBinaryOperatorValue

public 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. binaryOperatorKey must be the key of a binary operator of Syntax (one of OPERATOR_ADD, OPERATOR_SUBSTRACT, OPERATOR_MULTIPLY, OPERATOR_DIVIDE,...).
Overrides:
getBinaryOperatorValue in class DoubleInterpreter
Parameters:
binaryOperatorKey - the key of a binary operator of Syntax.
operand1 - the first operand.
operand2 - the second operand.
Returns:
the result of the operation. The returned value is an instance of Double, Long, Boolean or String.
Throws:
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.

getCommonFunctionValue

public java.lang.Object getCommonFunctionValue(java.lang.Object commonFunctionKey,
                                               java.lang.Object param)
Returns the value of the common function 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,...).
Overrides:
getCommonFunctionValue in class DoubleInterpreter
Parameters:
commonFunctionKey - the key of a common function of Syntax.
param - the parameter of the function.
Returns:
the result of the function. The returned value is an instance of Double, Long or Boolean.
Throws:
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.

isTrue

public boolean isTrue(java.lang.Object condition)
Returns true or false according to the value of condition.
Overrides:
isTrue in class DoubleInterpreter
Parameters:
condition - the value to test (instance of Boolean).
Returns:
the boolean value of condition.

&cp; 1998-2003 eTeks - All rights reserved