|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.congrace.exp4j.CustomOperator
public abstract class CustomOperator
This class is used to create custom operators for use in expressions
The applyOperation(double[] values) will have to be implemented by users of this class.
Example
When constructing
CustomOperator greaterEq = new CustomOperator(">=", true, 4, 2) {
double applyOperation(double[] values) {
if (values[0] >= values[1]){
return 1d;
}else{
return 0d;
}
}
};
Calculable calc = new ExpressionBuilder("1>=2").withOperation(greaterEq).build();
assertTrue(0d == calc.calculate());
CustomOperator
special attention has to be given to the precedence of the
operation. see http://en.wikipedia.org/wiki/Order_of_operations. The precendence values for the builtin operators are
as follows:
Addition and Subtraction (+,-) have precedence 1
Division Multiplication, and Modulo (/,*,%) have precedence 3
Exponentiation (^) has precendence 5
Unary minus and plus (+1,-1) have precedence 7
Constructor Summary | |
---|---|
protected |
CustomOperator(String symbol)
Create a left associative CustomOperator with precedence value of 1 that uses two operands |
protected |
CustomOperator(String symbol,
boolean leftAssociative,
int precedence)
Create a new CustomOperator for two operands |
protected |
CustomOperator(String symbol,
boolean leftAssociative,
int precedence,
int operandCount)
Create a new CustomOperator |
protected |
CustomOperator(String symbol,
int precedence)
Create a left associative CustomOperator for two operands |
Method Summary | |
---|---|
protected abstract double |
applyOperation(double[] values)
Apply the custom operation on the two operands and return the result as an double An example implementation for a multiplication could look like this: |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected CustomOperator(String symbol, boolean leftAssociative, int precedence)
CustomOperator
for two operands
symbol
- the symbol to be used in expressions to identify this operationleftAssociative
- true is the operation is left associativeprecedence
- the precedence of the operationprotected CustomOperator(String symbol, boolean leftAssociative, int precedence, int operandCount)
CustomOperator
symbol
- the symbol to be used in expressions to identify this operationleftAssociative
- true is the operation is left associativeprecedence
- the precedence of the operationoperandCount
- the number of operands of the operation. A value of 1 means the operation takes one operand. Any other
value means the operation takes 2 arguments.protected CustomOperator(String symbol)
CustomOperator
with precedence value of 1 that uses two operands
symbol
- the String
to use a symbol for this operationprotected CustomOperator(String symbol, int precedence)
CustomOperator
for two operands
symbol
- the String
to use a symbol for this operationprecedence
- the precedence of the operationMethod Detail |
---|
protected abstract double applyOperation(double[] values)
double applyOperation(double[] values) {
return values[0]*values[1];
}
values
- the operands for the operation. If the CustomOperator
uses only one operand such as a
factorial the operation has to be applied to the first element of the values array. If the
CustomOperator
uses two operands the operation has to be applied to the first two items in the
values array, with special care given to the operator associativity. The operand to the left of the
symbol is the first element in the array while the operand to the right is the second element of the
array.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |