Apache CXF API

org.apache.cxf.jaxrs.ext.search
Class SimpleSearchCondition<T>

java.lang.Object
  extended by org.apache.cxf.jaxrs.ext.search.SimpleSearchCondition<T>
Type Parameters:
T - type of search condition.
All Implemented Interfaces:
SearchCondition<T>

public class SimpleSearchCondition<T>
extends Object
implements SearchCondition<T>

Simple search condition comparing primitive objects or complex object by its getters. For details see isMet(Object) description.


Constructor Summary
SimpleSearchCondition(ConditionType cType, T condition)
          Creates search condition with same operator (equality, inequality) applied in all comparison; see isMet(Object) for details of comparison.
SimpleSearchCondition(Map<String,ConditionType> getters2operators, T condition)
          Creates search condition with different operators (equality, inequality etc) specified for each getter; see isMet(Object) for details of comparison.
 
Method Summary
 void accept(SearchConditionVisitor<T> visitor)
          Provides a visitor which will convert this SearchCondition into a custom expression, for example, into the SQL statement, etc
 List<T> findAll(Collection<T> pojos)
          Returns a list of pojos matching the condition
 T getCondition()
          Some SearchConditions may use instance of T to capture the actual search criteria thus making it simpler to implement isMet(T).
 ConditionType getConditionType()
          Returns the type of the condition this SearchCondition represents
 List<SearchCondition<T>> getSearchConditions()
          List of conditions this SearchCondition may represent.
 PrimitiveStatement getStatement()
          Primitive statement such a > b, i < 5, etc this condition may represent.
 boolean isMet(T pojo)
          Compares given object against template condition object.
 String toSQL(String table, String... columns)
          This method is now deprecated and will be removed soon.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleSearchCondition

public SimpleSearchCondition(ConditionType cType,
                             T condition)
Creates search condition with same operator (equality, inequality) applied in all comparison; see isMet(Object) for details of comparison.

Parameters:
cType - shared condition type
condition - template object

SimpleSearchCondition

public SimpleSearchCondition(Map<String,ConditionType> getters2operators,
                             T condition)
Creates search condition with different operators (equality, inequality etc) specified for each getter; see isMet(Object) for details of comparison. Cannot be used for primitive T type due to per-getter comparison strategy.

Parameters:
getters2operators - getters names and operators to be used with them during comparison
condition - template object
Method Detail

getCondition

public T getCondition()
Description copied from interface: SearchCondition
Some SearchConditions may use instance of T to capture the actual search criteria thus making it simpler to implement isMet(T). In some cases, the code which is given SearchCondition may find it more efficient to directly deal with the captured state for a more efficient lookup of matching data/records as opposed to calling SearchCondition.isMet for every instance of T it knows about.

Specified by:
getCondition in interface SearchCondition<T>
Returns:
T the captured search criteria, can be null

getConditionType

public ConditionType getConditionType()
Returns the type of the condition this SearchCondition represents

When constructor with map is used it returns null.

Specified by:
getConditionType in interface SearchCondition<T>
Returns:
condition type

getSearchConditions

public List<SearchCondition<T>> getSearchConditions()
Description copied from interface: SearchCondition
List of conditions this SearchCondition may represent. Composite SearchConditions will return a list of conditions they are composed from, primitive ones will return null

Specified by:
getSearchConditions in interface SearchCondition<T>
Returns:
list of conditions, can be null

isMet

public boolean isMet(T pojo)
Compares given object against template condition object.

For built-in type T like String, Number (precisely, from type T located in subpackage of "java.lang.*") given object is directly compared with template object. Comparison for ConditionType.EQUALS requires correct implementation of Object.equals(Object), using inequalities requires type T implementing Comparable.

For other types the comparison of given object against template object is done using its getters; Value returned by isMet(Object) operation is conjunction ('and' operator) of comparisons of each getter accessible in object of type T. Getters of template object that return null or throw exception are not used in comparison. Finally, if all getters return nulls (are excluded) it is interpreted as no filter (match every pojo).

If constructor with shared operator was used, then getters are compared using the same operator. If constructor with map of operators was used then for every getter specified operator is used (getters for missing mapping are ignored). The way that comparison per-getter is done depending on operator type per getter - comparison for ConditionType.EQUALS requires correct implementation of Object.equals(Object), using inequalities requires that getter type implements Comparable.

For equality comparison and String type in template object (either being built-in or getter from client provided type) it is allowed to used asterisk at the beginning or at the end of text as wild card (zero or more of any characters) e.g. "foo*", "*foo" or "*foo*". Inner asterisks are not interpreted as wild cards.

Example:

 SimpleSearchCondition<Integer> ssc = new SimpleSearchCondition<Integer>(
   ConditionType.GREATER_THAN, 10);    
 ssc.isMet(20);
 // true since 20>10 
 
 class Entity {
   public String getName() {...
   public int getLevel() {...
   public String getMessage() {...
 }
 
 Entity template = new Entity("bbb", 10, null);
 ssc = new SimpleSearchCondition<Entity>(
   ConditionType.GREATER_THAN, template);    
 
 ssc.isMet(new Entity("aaa", 20, "some mesage")); 
 // false: is not met, expression '"aaa">"bbb" and 20>10' is not true  
 // since "aaa" is not greater than "bbb"; not that message is null in template hence ingored
 
 ssc.isMet(new Entity("ccc", 30, "other message"));
 // true: is met, expression '"ccc">"bbb" and 30>10' is true
 
 Map<String,ConditionType> map;
 map.put("name", ConditionType.EQUALS);
 map.put("level", ConditionType.GREATER_THAN);
 ssc = new SimpleSearchCondition<Entity>(
   ConditionType.GREATER_THAN, template);
   
 ssc.isMet(new Entity("ccc", 30, "other message"));
 // false due to expression '"aaa"=="ccc" and 30>10"' (note different operators)
 
 

Specified by:
isMet in interface SearchCondition<T>
Parameters:
pojo - the object which will be checked
Returns:
true if the pojo meets this search condition, false - otherwise
Throws:
IllegalAccessException - when security manager disallows reflective call of getters.

findAll

public List<T> findAll(Collection<T> pojos)
Description copied from interface: SearchCondition
Returns a list of pojos matching the condition

Specified by:
findAll in interface SearchCondition<T>
Parameters:
pojos - list of pojos
Returns:
list of the matching pojos or null if none have been found

toSQL

public String toSQL(String table,
                    String... columns)
Description copied from interface: SearchCondition
This method is now deprecated and will be removed soon. Utility method for converting this condition into an SQL expression

Specified by:
toSQL in interface SearchCondition<T>
Parameters:
table - table name
columns - column names, a wildcard as in 'SELECT * from table' will be used if names are not provided
Returns:
SQL expression

getStatement

public PrimitiveStatement getStatement()
Description copied from interface: SearchCondition
Primitive statement such a > b, i < 5, etc this condition may represent. Complex conditions will return null.

Specified by:
getStatement in interface SearchCondition<T>
Returns:
primitive search statement, can be null

accept

public void accept(SearchConditionVisitor<T> visitor)
Description copied from interface: SearchCondition
Provides a visitor which will convert this SearchCondition into a custom expression, for example, into the SQL statement, etc

Specified by:
accept in interface SearchCondition<T>

Apache CXF API

Apache CXF