org.josql.utils
Class ExpressionEvaluator

java.lang.Object
  extended by org.josql.utils.ExpressionEvaluator

public class ExpressionEvaluator
extends Object

This class can be used as a convenient way of evaluating an expression without having to use the Query object itself.

In this way you can easily evaluate JoSQL expressions against objects.

Usage:

Use the static methods to evaluate the expression in one call, for instance to find out details about a file:

String exp = "path + ', size: ' + formatNumber(length) + ', last modified: ' + formatDate(lastModified)
String details = ExpressionEvaluator.getValue (exp, new File ('/home/me/myfile.txt'));


Constructor Summary
ExpressionEvaluator(String exp, Class cl)
          Create a new expression evaluator.
ExpressionEvaluator(String exp, Class cl, List fhs)
          Create a new expression evaluator.
 
Method Summary
 Query getQuery()
          Get the query associated with the expression, use this to setup bind variables, function handlers and so on, which of course must be setup prior to evaluating the expression.
 Object getValue(Object o)
          Evaluate the expression against the object passed in and return the value.
static Object getValue(String exp, Object o)
          Evaluate the expression against the object passed in and return the value.
 List getValues(List l)
          Evaluate the expression against the list of objects passed in and return the value.
static List getValues(String exp, List l)
          Evaluate the expression against the list of objects passed in and return the value.
 boolean isTrue(Object o)
          Evaluate the expression against the object passed in.
static boolean isTrue(String exp, Object o)
          Evaluate the expression against the object passed in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ExpressionEvaluator

public ExpressionEvaluator(String exp,
                           Class cl)
                    throws QueryParseException
Create a new expression evaluator.

Parameters:
exp - The expression to be evaluated.
cl - The class of the object(s) that the expression will be evaluated against.
Throws:
QueryParseException - If the expression cannot be parsed.

ExpressionEvaluator

public ExpressionEvaluator(String exp,
                           Class cl,
                           List fhs)
                    throws QueryParseException
Create a new expression evaluator.

Parameters:
exp - The expression to be evaluated.
cl - The class of the object(s) that the expression will be evaluated against.
fhs - A list of function handlers that contain functions that will be used by the expression, can be null.
Throws:
QueryParseException - If the expression cannot be parsed.
Method Detail

getQuery

public Query getQuery()
Get the query associated with the expression, use this to setup bind variables, function handlers and so on, which of course must be setup prior to evaluating the expression.

Returns:
The Query object.

isTrue

public boolean isTrue(Object o)
               throws QueryExecutionException
Evaluate the expression against the object passed in.

Parameters:
o - The object to evaluate the expression against.
Returns:
The value of calling Expression.isTrue (Query, Object).
Throws:
QueryExecutionException - If the expression cannot be executed.

getValues

public List getValues(List l)
               throws QueryExecutionException
Evaluate the expression against the list of objects passed in and return the value.

Parameters:
l - The list of objects to evaluate the expression against.
Returns:
The values gained when evaluating the expression against all the objects in the list.
Throws:
QueryExecutionException - If the expression cannot be executed.

getValue

public Object getValue(Object o)
                throws QueryExecutionException
Evaluate the expression against the object passed in and return the value.

Parameters:
o - The object to evaluate the expression against.
Returns:
The value gained when evaluating the expression against the object.
Throws:
QueryExecutionException - If the expression cannot be executed.

isTrue

public static boolean isTrue(String exp,
                             Object o)
                      throws QueryParseException,
                             QueryExecutionException
Evaluate the expression against the object passed in.

Parameters:
exp - A string representation of the expression to evaluate.
o - The object to evaluate the expression against.
Returns:
The value of calling Expression.isTrue (Query, Object).
Throws:
QueryParseException - If the expression cannot be parsed.
QueryExecutionException - If the expression cannot be executed.

getValues

public static List getValues(String exp,
                             List l)
                      throws QueryParseException,
                             QueryExecutionException
Evaluate the expression against the list of objects passed in and return the value.

Parameters:
exp - A string representation of the expression to evaluate.
l - The list of objects to evaluate the expression against.
Returns:
The values gained when evaluating the expression against all the objects in the list.
Throws:
QueryParseException - If the expression cannot be parsed.
QueryExecutionException - If the expression cannot be executed.

getValue

public static Object getValue(String exp,
                              Object o)
                       throws QueryParseException,
                              QueryExecutionException
Evaluate the expression against the object passed in and return the value.

Parameters:
exp - A string representation of the expression to evaluate.
o - The object to evaluate the expression against.
Returns:
The value gained when evaluating the expression against the object.
Throws:
QueryParseException - If the expression cannot be parsed.
QueryExecutionException - If the expression cannot be executed.


  Copyright © 2008 Gary Bentley. All Rights Reserved.