org.josql.functions
Class CollectionFunctions

java.lang.Object
  extended by org.josql.functions.AbstractFunctionHandler
      extended by org.josql.functions.CollectionFunctions
All Implemented Interfaces:
FunctionHandler

public class CollectionFunctions
extends AbstractFunctionHandler

Defines a set of functions that operate on "collections" of objects in some way.


Field Summary
static String HANDLER_ID
          The id that can be used to get the "CollectionFunctions" handler object from the Query object.
 
Fields inherited from class org.josql.functions.AbstractFunctionHandler
q
 
Constructor Summary
CollectionFunctions()
           
 
Method Summary
 List collect(Expression exp)
           
 List collect(List allobjs, Expression exp)
           
 List collect(List objs, Expression exp, String saveValueName)
           
 int count(Expression exp)
           
 int count(List allobjs, Expression exp)
           
 int count(List objs, Expression exp, Object value)
          For each of the objects in the objs List get the value from each one using the accessor and compare it to the value parameter.
 List find(List objs, Expression exp)
          Find objects from the List based upon the expression passed in.
 List foreach(Expression exp)
           
 List foreach(Expression listFunction, Expression exp)
           
 List foreach(List allobjs, Expression exp)
           
 List foreach(List objs, String exp)
          Given a list of objects, execute the expression against each one and return those objects that return a true value for the expression.
 Object get(List l, Number n)
          Get a value from the specified List.
 Object get(Map m, Expression exp)
          Get a value from the specified Map.
 Map grp(List objs, Expression exp)
          Group objects from the List based upon the expression passed in.
 Map map(List objs, Expression exp)
          Create a map of the objects passed in, the key will be the object in the list and the value will be the result of calling the expression on the object.
 Map map(List objs, Expression keyExp, Expression valExp)
          Create a map of the objects passed in, the key will be the result of calling the keyExp expression on the object in the list and the value will be the result of calling the valExp expression on the object.
 List sort(List objs)
          Sort a list according to it's "natural" ordering (see Collections.sort(List)).
 List sort(Map m)
          Sort a Map by the keys in ascending order (for more optionality in the sort and ordering see: sort(Map,String,String)).
 List sort(Map m, String type)
          Sort a Map by it's keys or values in ascending order (for more optionality in the sort and ordering see: sort(Map,String,String)).
 List sort(Map m, String type, String dir)
          Sort a Map by either it's key or value.
 List toList(Expression exp)
           
 List toList(List allobjs, Expression exp)
           
 List toList(List allobjs, Expression exp, String saveValueName)
           
 List unique(Expression exp)
           
 List unique(List objs)
           
 List unique(List objs, Expression exp)
           
 
Methods inherited from class org.josql.functions.AbstractFunctionHandler
setQuery
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HANDLER_ID

public static final String HANDLER_ID
The id that can be used to get the "CollectionFunctions" handler object from the Query object.

See Also:
Constant Field Values
Constructor Detail

CollectionFunctions

public CollectionFunctions()
Method Detail

sort

public List sort(List objs)
Sort a list according to it's "natural" ordering (see Collections.sort(List)).

Parameters:
objs - The list of objects to sort.
Returns:
The sorted list, according to their natural ordering.

sort

public List sort(Map m)
Sort a Map by the keys in ascending order (for more optionality in the sort and ordering see: sort(Map,String,String)).

Parameters:
m - The map to sort.
Returns:
A List sorted according to the key in ascending order.

sort

public List sort(Map m,
                 String type)
Sort a Map by it's keys or values in ascending order (for more optionality in the sort and ordering see: sort(Map,String,String)).

Parameters:
m - The map to sort.
type - Should be either: "key" or "value" to indicate which item to sort on. Use null for key.
Returns:
A List sorted according to the key in ascending order.

sort

public List sort(Map m,
                 String type,
                 String dir)
Sort a Map by either it's key or value.

Parameters:
m - The map to sort.
type - Should be either: "key" or "value" to indicate which item to sort on. Use null for key.
dir - The direction you want to sort on, either "asc" or "desc". Use null for "asc".
Returns:
A List sorted according to the key or value.

get

public Object get(Map m,
                  Expression exp)
           throws QueryExecutionException
Get a value from the specified Map.

Parameters:
m - The map of objects.
exp - The expression is evaluated (in the context of the current object) and the value returned used as the key to the Map, the value it maps to (which may be null) is returned.
Returns:
The value that the exp value maps to, may be null.
Throws:
QueryExecutionException

get

public Object get(List l,
                  Number n)
Get a value from the specified List.

Parameters:
l - The list of objects.
n - The index, indices start at 0.
Returns:
The value of the ith element from the list of objects. Return null if n is out of range.

count

public int count(List objs,
                 Expression exp,
                 Object value)
          throws QueryExecutionException
For each of the objects in the objs List get the value from each one using the accessor and compare it to the value parameter. The value param is converted to a string and then to a Boolean value using: Boolean.valueOf(String).

Parameters:
objs - The list of objects to iterate over.
exp - The expression to use to get the value from the object in the List.
value - The value to compare the result of the accessor against. If the parm is null then it defaults to Boolean.FALSE.
Returns:
A count of how many times the accessor evaluated to the same value of the value parm.
Throws:
QueryExecutionException - If the value from the accessor cannot be gained or if the compare cannot be performed.

count

public int count(Expression exp)
          throws QueryExecutionException
Throws:
QueryExecutionException

count

public int count(List allobjs,
                 Expression exp)
          throws QueryExecutionException
Throws:
QueryExecutionException

toList

public List toList(List allobjs,
                   Expression exp,
                   String saveValueName)
            throws QueryExecutionException
Throws:
QueryExecutionException

unique

public List unique(List objs)

unique

public List unique(Expression exp)
            throws QueryExecutionException
Throws:
QueryExecutionException

unique

public List unique(List objs,
                   Expression exp)
            throws QueryExecutionException
Throws:
QueryExecutionException

collect

public List collect(List objs,
                    Expression exp,
                    String saveValueName)
             throws QueryExecutionException
Throws:
QueryExecutionException

collect

public List collect(Expression exp)
             throws QueryExecutionException
Throws:
QueryExecutionException

collect

public List collect(List allobjs,
                    Expression exp)
             throws QueryExecutionException
Throws:
QueryExecutionException

toList

public List toList(Expression exp)
            throws QueryExecutionException
Throws:
QueryExecutionException

toList

public List toList(List allobjs,
                   Expression exp)
            throws QueryExecutionException
Throws:
QueryExecutionException

foreach

public List foreach(Expression exp)
             throws QueryExecutionException
Throws:
QueryExecutionException

foreach

public List foreach(List allobjs,
                    Expression exp)
             throws QueryExecutionException
Throws:
QueryExecutionException

foreach

public List foreach(List objs,
                    String exp)
             throws QueryExecutionException
Given a list of objects, execute the expression against each one and return those objects that return a true value for the expression. In effect this is equivalent to executing the WHERE clause of a JoSQL statement against each object (which in fact is what happens internally). The class for the objects if found by examining the list passed in.

Parameters:
objs - The list of objects.
exp - The expression (basically a where clause, it is ok for the expression to start with "WHERE", case-insensitive) to execute for each of the objects.
Returns:
The list of matching objects.
Throws:
QueryExecutionException

foreach

public List foreach(Expression listFunction,
                    Expression exp)
             throws QueryExecutionException
Throws:
QueryExecutionException

find

public List find(List objs,
                 Expression exp)
          throws QueryExecutionException
Find objects from the List based upon the expression passed in. If the expression evaluates to true then the object will be returned. Note: in accordance with the general operating methodology for the Query object, the ":_allobjs" special bind variable will be set to the the List passed in and the "_currobj" will be set to the relevant object in the List.

Parameters:
objs - The List of objects to search.
exp - The expression to evaulate against each object in the List.
Returns:
The List of matching objects, if none match then an empty list is returned.
Throws:
QueryExecutionException - If the expression cannot be evaulated against each object.

grp

public Map grp(List objs,
               Expression exp)
        throws QueryExecutionException
Group objects from the List based upon the expression passed in. The expression is evaulated for each object, by calling: Expression.getValue(Object,Query) and the return value used as the key to the Map. All objects with that value are added to a List held against the key. To maintain the ordering of the keys (if desirable) a LinkedHashMap is used as the return Map. Note: in accordance with the general operating methodology for the Query object, the ":_allobjs" special bind variable will be set to the the List passed in and the "_currobj" will be set to the relevant object in the List.

Parameters:
objs - The List of objects to search.
exp - The expression to evaulate against each object in the List.
Returns:
The LinkedHashMap of matching objects, grouped according to the return value of executing the expression against each object in the input List.
Throws:
QueryExecutionException - If the expression cannot be evaulated against each object.

map

public Map map(List objs,
               Expression exp)
        throws QueryExecutionException
Create a map of the objects passed in, the key will be the object in the list and the value will be the result of calling the expression on the object. The expression is evaulated for each object, by calling: Expression.getValue(Object,Query) and the return value used as the value to the Map. To maintain the ordering of the keys (if desirable) a LinkedHashMap is used as the return Map. Note: in accordance with the general operating methodology for the Query object, the ":_allobjs" special bind variable will be set to the the List passed in and the "_currobj" will be set to the relevant object in the List.

Parameters:
objs - The List of objects to map.
exp - The expression to evaulate against each object in the List.
Returns:
The LinkedHashMap of mapped objects.
Throws:
QueryExecutionException - If the expression cannot be evaulated against each object.

map

public Map map(List objs,
               Expression keyExp,
               Expression valExp)
        throws QueryExecutionException
Create a map of the objects passed in, the key will be the result of calling the keyExp expression on the object in the list and the value will be the result of calling the valExp expression on the object. The expression is evaulated for each object, by calling: Expression.getValue(Object,Query) and the return value used as the value to the Map. To maintain the ordering of the keys (if desirable) a LinkedHashMap is used as the return Map. Note: in accordance with the general operating methodology for the Query object, the ":_allobjs" special bind variable will be set to the the List passed in and the "_currobj" will be set to the relevant object in the List.

Parameters:
objs - The List of objects to map.
exp - The expression to evaulate against each object in the List.
Returns:
The LinkedHashMap of mapped objects.
Throws:
QueryExecutionException - If the expression cannot be evaulated against each object.


  Copyright © 2008 Gary Bentley. All Rights Reserved.