net.sf.jec
Class ExpressionEval

java.lang.Object
  extended bynet.sf.jec.ExpressionEval

public class ExpressionEval
extends java.lang.Object

ExpressionEval is an expression evaluator.

It works caching class+expr pairs with their associated compiled operator. Whenever someone calls ExpressionEval for evaluating a 'get' or 'set' expression on a context 'ctx', it checks for the pair (cxt.getClass() + expr) in the operator cache. If there's nothing, the expression is compiled on the context using a ExpressionCompiler and a new entry is created within the cache.

So, ExpressionEval grows over the time, filled with pairs of class plus expression, and its associated operator trees.

WARNING:Be careful with using ExpressionEval as a singleton in your application. For expressions that evaluate on fixed clases, an expression evaluated on a given context will have always the same operator tree. But that isn't true for classes with dynamic properties, as those returning java.util.Map. Anyway, I'm not absolutely sure about this and it would be great to have a criteria for choosing the singleton option when possible.

Author:
mballesteros

Constructor Summary
ExpressionEval(boolean throwExceptions)
          Creates a new instance of ExpressionEval
ExpressionEval(ExpressionCompiler expComp)
          Creates a new instance of ExpressionEval
 
Method Summary
 void clearOperatorCache()
          Removes all stored operators, so the garbage collector can collect them.
 void clearOperatorCache(java.lang.Class clazz)
          Removes all stored operator for objects of the given class
 java.lang.Object get(java.lang.Object ctx, java.lang.String expr)
          Evaluates expression expr over the context ctx
 Operator getCachedOperator(java.lang.Object ctx, java.lang.String expr)
          This method completely encapsulates the internal data representation.
static ExpressionEval getDefaultInstance(boolean throwExceptions)
          Returns a default expression evaluator that uses a default expression compiler (see ExpressionCompiler.getDefaultInstance(boolean)).
 void set(java.lang.Object ctx, java.lang.String expr, java.lang.Object value)
          Sets the expression expr over the context ctx to the specified value value, so assert(value==get(ctx, expr))
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionEval

public ExpressionEval(boolean throwExceptions)
Creates a new instance of ExpressionEval

Parameters:
throwExceptions - True if evaluation exceptions are desirable

ExpressionEval

public ExpressionEval(ExpressionCompiler expComp)
Creates a new instance of ExpressionEval

Parameters:
expComp - The ExpressionCompiler used to compile expressions into Operator objects.
Method Detail

getDefaultInstance

public static ExpressionEval getDefaultInstance(boolean throwExceptions)
Returns a default expression evaluator that uses a default expression compiler (see ExpressionCompiler.getDefaultInstance(boolean)).

Parameters:
throwExceptions -
Returns:
ExpressionEval

get

public java.lang.Object get(java.lang.Object ctx,
                            java.lang.String expr)
                     throws ParseException,
                            EvaluationException
Evaluates expression expr over the context ctx

Parameters:
ctx - Expression context
expr - Expression to evaluate
Returns:
Object Final evaluation result
Throws:
ParseException - Thrown when the expression is not well formed
EvaluationException - Thrown when, although the expression is valid, the evaluation cannot be performed.

set

public void set(java.lang.Object ctx,
                java.lang.String expr,
                java.lang.Object value)
         throws ParseException,
                EvaluationException
Sets the expression expr over the context ctx to the specified value value, so assert(value==get(ctx, expr))

Parameters:
ctx - Expression context
expr - Expression to evaluate
value - New expression value
Throws:
ParseException - Thrown when the expression is not well formed
EvaluationException - Thrown when, although the expression is valid, the evaluation cannot be performed.

getCachedOperator

public Operator getCachedOperator(java.lang.Object ctx,
                                  java.lang.String expr)
                           throws ParseException
This method completely encapsulates the internal data representation. Provides the function: f(ctx, expr) --> Operator

Throws:
ParseException

clearOperatorCache

public void clearOperatorCache()
Removes all stored operators, so the garbage collector can collect them.


clearOperatorCache

public void clearOperatorCache(java.lang.Class clazz)
Removes all stored operator for objects of the given class

Parameters:
clazz - The class whose Operator objects must be cleared


Copyright © 2003 SourceForge. All Rights Reserved.