public abstract class CustomOperator
extends java.lang.Object
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());
When constructing 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: Modifier | Constructor and Description |
---|---|
protected |
CustomOperator(java.lang.String symbol)
Create a left associative
CustomOperator with precedence value of 1 that uses two operands |
protected |
CustomOperator(java.lang.String symbol,
boolean leftAssociative,
int precedence)
Create a new
CustomOperator for two operands |
protected |
CustomOperator(java.lang.String symbol,
boolean leftAssociative,
int precedence,
int operandCount)
Create a new
CustomOperator |
protected |
CustomOperator(java.lang.String symbol,
int precedence)
Create a left associative
CustomOperator for two operands |
Modifier and Type | Method and Description |
---|---|
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:
|
protected CustomOperator(java.lang.String symbol, boolean leftAssociative, int precedence)
CustomOperator
for two operandssymbol
- the symbol to be used in expressions to identify this operationleftAssociative
- true is the operation is left associativeprecedence
- the precedence of the operationprotected CustomOperator(java.lang.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(java.lang.String symbol)
CustomOperator
with precedence value of 1 that uses two operandssymbol
- the String
to use a symbol for this operationprotected CustomOperator(java.lang.String symbol, int precedence)
CustomOperator
for two operandssymbol
- the String
to use a symbol for this operationprecedence
- the precedence of the operationprotected 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.