In additions to providing a database-independent WHERE clause for SQL queries, expressions support in-memory evaluation. An expressions can be evaluated with any type of objects that follow Java Beans method naming convention. Of course this includes DataObjects. The following API is used for expressions evaluation:

  • public Object evaluate(Object object)
    Evaluates expression with object, returning the result.
  • public boolean match(Object object)
    Returns true if an object "matches" expression criteria.
  • public java.util.List filterObjects(java.util.List objects)
    Returns a list of objects from the original list that match expression criteria.
Limitation of In-Memory Expressions
Current limitation of in-memory expressions is that no collections are permitted in the object property path. In case of DataObjects that means that path containing to-many relationships may not work for in-memory evaluation.

Here is an example of evaluating expression with a single object:

public class User extends CayenneDataObject {
     public String getName() {
         ...
     }
}
... 
public class NonPersistentUser extends Object {
     protected String name;
     public String getName() {
         return name;
     }
     ...
}
... 
Expression exp = ExpressionFactory.inExp("name", new Object[] {"John", "Bob"});  
User persistentObject;
NonPersistentUser nonPersistentBean;
... 
// evaluate with DataObject
if(exp.match(persistentObject)) {
    // do something
}

if(exp.match(nonPersistentBean)) {
    // do something else
}

Another example - using expression to filter a list objects:

Expression exp = ExpressionFactory.likeExp("artistName", "A%");  
List startWithA = exp.filterObjects(artists);