net.christopherschultz.evaluator
Class BinaryOperatorExpression

java.lang.Object
  extended by net.christopherschultz.evaluator.Expression
      extended by net.christopherschultz.evaluator.BinaryOperatorExpression

public class BinaryOperatorExpression
extends Expression

An expression to describe expressions with binary operators.

The operators supported are limited only by the parser's ability to recognize the operator as a binary (probably mathematical) expression, and the availability of a function which matches the operator.

Version:
$Revision: 1.2 $ $Date: 2008-06-20 22:58:48 $
Author:
Chris Schultz

Constructor Summary
BinaryOperatorExpression(Expression left, String operator, Expression right)
          Creates a new BinaryOperatorExpression with the specified sub expressions and operator.
 
Method Summary
 void acceptVisitor(ExpressionVisitor visitor)
          Accepts an ExpressionVisitor.
 boolean equals(Object o)
          Returns true if the specified object is equal to this one.
 Object evaluate(EvaluationContext ec)
          Evaluates this Expression by returning the appropriate combination of the left and right-hand sub expressions with the operator.
 Expression getLeftExpression()
          Gets the expression which is the left-hand operand of this binary operator.
 String getOperator()
           Gets the operator for this BinaryOperatorExpression.
 Expression getRightExpression()
          Gets the expression which is the right-hand operand of this binary operator.
 List getSubExpressions()
          Returns a List containing both sides of this binary BinaryOperatorExpression.
 int hashCode()
           
 String toString()
          Returns an infix String representation of this BinaryOperatorExpression.
 
Methods inherited from class net.christopherschultz.evaluator.Expression
getParent, setParent
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BinaryOperatorExpression

public BinaryOperatorExpression(Expression left,
                                String operator,
                                Expression right)
                         throws EvaluationException
Creates a new BinaryOperatorExpression with the specified sub expressions and operator.

Parameters:
left - The left-hand operand of this binary expresssion.
operator - The operator to be used (i.e. "+", "-", "&&", etc.)
right - The right-hand operand of this binary expresssion.
Throws:
IllegalArgumentException - If either of the sub-expressions are null, or the operator is not recognized.
EvaluationException
Method Detail

getLeftExpression

public Expression getLeftExpression()
Gets the expression which is the left-hand operand of this binary operator.

Returns:
The expression which is the left-hand operand of this binary operator.

getOperator

public String getOperator()

Gets the operator for this BinaryOperatorExpression.

Note that the operator is nothing magic: it's just the name of a function that will be looked-up in the EvaluationContext to determine which function to invoke on the two arguments.

Returns:
The operator for this BinaryOperatorExpression.

getRightExpression

public Expression getRightExpression()
Gets the expression which is the right-hand operand of this binary operator.

Returns:
The expression which is the right-hand operand of this binary operator.

getSubExpressions

public List getSubExpressions()
Returns a List containing both sides of this binary BinaryOperatorExpression.

Specified by:
getSubExpressions in class Expression
Returns:
A List containing both sides of this binary BinaryOperatorExpression.

acceptVisitor

public void acceptVisitor(ExpressionVisitor visitor)
Description copied from class: Expression
Accepts an ExpressionVisitor.

Specified by:
acceptVisitor in class Expression

evaluate

public Object evaluate(EvaluationContext ec)
                throws EvaluationException
Evaluates this Expression by returning the appropriate combination of the left and right-hand sub expressions with the operator. Mathmatical operators return the same type as their inputs (i.e. Integers). Logical and relational operators return objects of type Boolean.

Specified by:
evaluate in class Expression
Parameters:
ec - The EvaluationContext in which this expression should be evaluated.
Returns:
The value of this BinaryOperatorExpression.
Throws:
EvaluationException - If there is a problem performing the mathematical operation (which is a function).

toString

public String toString()
Returns an infix String representation of this BinaryOperatorExpression.

Overrides:
toString in class Object
Returns:
An infix String representation of this BinaryOperatorExpression.

equals

public boolean equals(Object o)
Returns true if the specified object is equal to this one.

Overrides:
equals in class Object
Parameters:
o - The object to test for equality.
Returns:
true if the specified object is equal to this one, false otherwise.

hashCode

public int hashCode()
Overrides:
hashCode in class Object