cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1296678 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/ framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/ framework/cayenne-jdk1.5-unpub...
Date Sat, 03 Mar 2012 18:55:37 GMT
Author: aadamchik
Date: Sat Mar  3 18:55:37 2012
New Revision: 1296678

URL: http://svn.apache.org/viewvc?rev=1296678&view=rev
Log:
CAY-1675 [PATCH] add .first(List<T>) method to Expression to enhance in-memory filtering

Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/Expression.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=1296678&r1=1296677&r2=1296678&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Sat Mar  3 18:55:37 2012
@@ -41,6 +41,7 @@ CAY-1666 Fix problem with tests on ingre
 CAY-1667 Expression parser performance optimization
 CAY-1669 RuntimeProperties use of System properties becomes a contention point when creating
query translators 
 CAY-1670 Non-blocking DataRowStore
+CAY-1675 [PATCH] add .first(List<T>) method to Expression to enhance in-memory filtering
 CAY-1679 A notion of default node
 CAY-1680 Get rid of shared locks in DataDomain metadata lookups
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/Expression.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/Expression.java?rev=1296678&r1=1296677&r2=1296678&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/Expression.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/exp/Expression.java
Sat Mar  3 18:55:37 2012
@@ -386,6 +386,21 @@ public abstract class Expression impleme
     public boolean match(Object o) {
         return ConversionUtil.toBoolean(evaluate(o));
     }
+    
+    /**
+     * Returns the first object in the list that matches the expression.
+     * 
+     * @since 3.1
+     */
+    public <T> T first(List<T> objects) {
+        for (T o : objects) {
+            if (match(o)) {
+                return o;
+            }
+        }
+
+        return null;
+    }
 
     /**
      * Returns a list of objects that match the expression.

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java?rev=1296678&r1=1296677&r2=1296678&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/CayenneDataObjectTest.java
Sat Mar  3 18:55:37 2012
@@ -37,7 +37,7 @@ public class CayenneDataObjectTest exten
 
     @Inject
     private ObjectContext context;
-    
+
     public void testSetObjectId() throws Exception {
         CayenneDataObject object = new CayenneDataObject();
         ObjectId oid = new ObjectId("T");

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java?rev=1296678&r1=1296677&r2=1296678&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/exp/ExpressionTest.java
Sat Mar  3 18:55:37 2012
@@ -21,6 +21,7 @@ package org.apache.cayenne.exp;
 
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
@@ -37,13 +38,33 @@ import org.apache.cayenne.unit.di.server
 
 @UseServerRuntime(ServerCase.TESTMAP_PROJECT)
 public class ExpressionTest extends ServerCase {
-    
+
     @Inject
     private ObjectContext context;
-    
+
     @Inject
     private ServerRuntime runtime;
 
+    @Override
+    protected void setUpAfterInjection() throws Exception {
+
+        SelectQuery query = new SelectQuery(Artist.class);
+        Expression qual = ExpressionFactory.likeExp("artistName", "Equals");
+        query.setQualifier(qual);
+        List<?> objects = context.performQuery(query);
+
+        if (objects.size() > 0) {
+            SelectQuery query1 = new SelectQuery(Painting.class);
+            Expression e = ExpressionFactory.matchExp(
+                    Painting.TO_ARTIST_PROPERTY,
+                    objects.get(0));
+            query.setQualifier(e);
+            objects.addAll(context.performQuery(query1));
+        }
+
+        context.deleteObjects(objects);
+    }
+
     public void testFromStringLong() {
         Expression e = Expression.fromString("216201000180L");
         assertEquals(new Long(216201000180L), e.evaluate(new Object()));
@@ -226,69 +247,76 @@ public class ExpressionTest extends Serv
 
         assertEquals("a = enum:org.apache.cayenne.exp.ExpEnum1.TWO", buffer.toString());
     }
-    
+
     public void testEqualsObjects() {
-        
+
         assertTrue(context instanceof DataContext);
-        
+
         DataContext context2 = (DataContext) runtime.getContext();
-        
+
         Artist a1 = context.newObject(Artist.class);
         a1.setArtistName("Equals");
         Painting p1 = context.newObject(Painting.class);
         p1.setToArtist(a1);
         p1.setPaintingTitle("painting1");
-        
+
         SelectQuery query = new SelectQuery(Painting.class);
-        Expression e = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a1); 
+        Expression e = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a1);
         query.setQualifier(e);
-        
+
         context.commitChanges();
-        
+
         assertNotSame(context2, context);
-        
+
         List<Artist> objects = context2.performQuery(query);
         assertEquals(1, objects.size());
-        
+
         // 2 same objects in different contexts
-        assertTrue(e.match(objects.get(0))); 
-        
-        // we change one object - so the objects are different now (PersistenceState different)
+        assertTrue(e.match(objects.get(0)));
+
+        // we change one object - so the objects are different now (PersistenceState
+        // different)
         a1.setArtistName("newName");
-        
+
         SelectQuery q2 = new SelectQuery(Painting.class);
-        Expression ex2 = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a1); 
+        Expression ex2 = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a1);
         q2.setQualifier(ex2);
-        
-        assertFalse(ex2.match(objects.get(0))); 
-        
+
+        assertFalse(ex2.match(objects.get(0)));
+
         Artist a2 = context.newObject(Artist.class);
         a1.setArtistName("Equals");
-        
+
         SelectQuery q = new SelectQuery(Painting.class);
-        Expression ex = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a2); 
+        Expression ex = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, a2);
         q.setQualifier(ex);
-        
+
         // 2 different objects in different contexts
-        assertFalse(ex.match(objects.get(0))); 
+        assertFalse(ex.match(objects.get(0)));
     }
-    
-    @Override
-    protected void setUpAfterInjection() throws Exception {
-        
-        SelectQuery query = new SelectQuery(Artist.class);
-        Expression qual = ExpressionFactory.likeExp("artistName", "Equals");
-        query.setQualifier(qual);
-        List<?> objects = context.performQuery(query);
-        
-        if (objects.size() > 0) {
-            SelectQuery query1 = new SelectQuery(Painting.class);
-            Expression e = ExpressionFactory.matchExp(Painting.TO_ARTIST_PROPERTY, objects.get(0));

-            query.setQualifier(e);
-            objects.addAll(context.performQuery(query1));
-        }
-        
-        
-        context.deleteObjects(objects);
+
+    public void testFirst() {
+        List<Painting> paintingList = new ArrayList<Painting>();
+        Painting p1 = context.newObject(Painting.class);
+        p1.setPaintingTitle("x1");
+        paintingList.add(p1);
+
+        Painting p2 = context.newObject(Painting.class);
+        p2.setPaintingTitle("x2");
+        paintingList.add(p2);
+
+        Painting p3 = context.newObject(Painting.class);
+        p3.setPaintingTitle("x3");
+        paintingList.add(p3);
+
+        Expression e1 = ExpressionFactory.likeExp("paintingTitle", "x%");
+        assertSame(p1, e1.first(paintingList));
+
+        Expression e3 = ExpressionFactory.matchExp("paintingTitle", "x3");
+        assertSame(p3, e3.first(paintingList));
+
+        Expression e4 = ExpressionFactory.matchExp("paintingTitle", "x4");
+        assertNull(e4.first(paintingList));
     }
+
 }



Mime
View raw message