de.congrace.exp4j
Class CustomOperator

java.lang.Object
  extended by de.congrace.exp4j.CustomOperator

public abstract class CustomOperator
extends Object

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

 
      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:
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

Author:
frank asseg

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

CustomOperator

protected CustomOperator(String symbol,
                         boolean leftAssociative,
                         int precedence)
Create a new CustomOperator for two operands

Parameters:
symbol - the symbol to be used in expressions to identify this operation
leftAssociative - true is the operation is left associative
precedence - the precedence of the operation

CustomOperator

protected CustomOperator(String symbol,
                         boolean leftAssociative,
                         int precedence,
                         int operandCount)
Create a new CustomOperator

Parameters:
symbol - the symbol to be used in expressions to identify this operation
leftAssociative - true is the operation is left associative
precedence - the precedence of the operation
operandCount - 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.

CustomOperator

protected CustomOperator(String symbol)
Create a left associative CustomOperator with precedence value of 1 that uses two operands

Parameters:
symbol - the String to use a symbol for this operation

CustomOperator

protected CustomOperator(String symbol,
                         int precedence)
Create a left associative CustomOperator for two operands

Parameters:
symbol - the String to use a symbol for this operation
precedence - the precedence of the operation
Method Detail

applyOperation

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:
 
       double applyOperation(double[] values) {
           return values[0]*values[1];
       }
 

Parameters:
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.
Returns:
the result of the operation


Copyright © 2012. All Rights Reserved.