net.christopherschultz.evaluator
Class Expression

java.lang.Object
  extended by net.christopherschultz.evaluator.Expression
Direct Known Subclasses:
ArrayReferenceExpression, BinaryOperatorExpression, ConstantExpression, FunctionCallExpression, NegatedExpression, UnaryMinusExpression

public abstract class Expression
extends Object

An evaluatable Expression. To obtain an Expression, you should use the ExpressionParser class:

 Expression exp = ExpressionParser.parseExpression("1 + 2 + 3 * 5");
 
This expression can later be evaluated:
 EvaluationContext ctx = ...;
 Object result = exp.evaluate(ctx);
 
The expression in the example will return an Integer result.

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

Constructor Summary
Expression()
           
 
Method Summary
abstract  void acceptVisitor(ExpressionVisitor visitor)
          Accepts an ExpressionVisitor.
abstract  Object evaluate(EvaluationContext ec)
          Evaluates this expression and returns the result.
 Expression getParent()
           
abstract  List getSubExpressions()
          Gets a flattened, depth-first traversal of the expressions in this Expression (including this Expression).
protected  void setParent(Expression parent)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Expression

public Expression()
Method Detail

evaluate

public abstract Object evaluate(EvaluationContext ec)
                         throws EvaluationException
Evaluates this expression and returns the result. This method is not threadsafe, but is idempotent (repeatable with the same result).

Parameters:
ec - The context in which this expression should be evaluated.
Returns:
The result of evaluating this Expression. This may by an object of any type, including null.
Throws:
EvaluationException - If there is an error evaluating this Expression.

getSubExpressions

public abstract List getSubExpressions()
Gets a flattened, depth-first traversal of the expressions in this Expression (including this Expression).

Returns:
A List of sub-expressions for this Expression.

acceptVisitor

public abstract void acceptVisitor(ExpressionVisitor visitor)
Accepts an ExpressionVisitor.


setParent

protected void setParent(Expression parent)
                  throws EvaluationException
Throws:
EvaluationException

getParent

public Expression getParent()