commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r382470 - in /jakarta/commons/proper/collections/trunk/src: java/org/apache/commons/collections/functors/ test/org/apache/commons/collections/
Date Thu, 02 Mar 2006 19:02:38 GMT
Author: scolebourne
Date: Thu Mar  2 11:02:36 2006
New Revision: 382470

URL: http://svn.apache.org/viewcvs?rev=382470&view=rev
Log:
Avoid object creation in predicate factory methods
bug 38703, from Matt Benson

Modified:
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AllPredicate.java
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AnyPredicate.java
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NonePredicate.java
    jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/OnePredicate.java
    jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AllPredicate.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AllPredicate.java?rev=382470&r1=382469&r2=382470&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AllPredicate.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AllPredicate.java
Thu Mar  2 11:02:36 2006
@@ -49,6 +49,12 @@
      */
     public static Predicate getInstance(Predicate[] predicates) {
         FunctorUtils.validate(predicates);
+        if (predicates.length == 0) {
+            return TruePredicate.INSTANCE;
+        }
+        if (predicates.length == 1) {
+            return predicates[0];
+        }
         predicates = FunctorUtils.copy(predicates);
         return new AllPredicate(predicates);
     }
@@ -63,6 +69,12 @@
      */
     public static Predicate getInstance(Collection predicates) {
         Predicate[] preds = FunctorUtils.validate(predicates);
+        if (preds.length == 0) {
+            return TruePredicate.INSTANCE;
+        }
+        if (preds.length == 1) {
+            return preds[0];
+        }
         return new AllPredicate(preds);
     }
 

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AnyPredicate.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AnyPredicate.java?rev=382470&r1=382469&r2=382470&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AnyPredicate.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/AnyPredicate.java
Thu Mar  2 11:02:36 2006
@@ -49,8 +49,13 @@
      */
     public static Predicate getInstance(Predicate[] predicates) {
         FunctorUtils.validate(predicates);
-        predicates = FunctorUtils.copy(predicates);
-        return new AnyPredicate(predicates);
+        if (predicates.length == 0) {
+            return FalsePredicate.INSTANCE;
+        }
+        if (predicates.length == 1) {
+            return predicates[0];
+        }
+        return new AnyPredicate(FunctorUtils.copy(predicates));
     }
 
     /**
@@ -63,6 +68,12 @@
      */
     public static Predicate getInstance(Collection predicates) {
         Predicate[] preds = FunctorUtils.validate(predicates);
+        if (preds.length == 0) {
+            return FalsePredicate.INSTANCE;
+        }
+        if (preds.length == 1) {
+            return preds[0];
+        }
         return new AnyPredicate(preds);
     }
 

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NonePredicate.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NonePredicate.java?rev=382470&r1=382469&r2=382470&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NonePredicate.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/NonePredicate.java
Thu Mar  2 11:02:36 2006
@@ -49,6 +49,9 @@
      */
     public static Predicate getInstance(Predicate[] predicates) {
         FunctorUtils.validate(predicates);
+        if (predicates.length == 0) {
+            return TruePredicate.INSTANCE;
+        }
         predicates = FunctorUtils.copy(predicates);
         return new NonePredicate(predicates);
     }
@@ -63,6 +66,9 @@
      */
     public static Predicate getInstance(Collection predicates) {
         Predicate[] preds = FunctorUtils.validate(predicates);
+        if (preds.length == 0) {
+            return TruePredicate.INSTANCE;
+        }
         return new NonePredicate(preds);
     }
 

Modified: jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/OnePredicate.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/OnePredicate.java?rev=382470&r1=382469&r2=382470&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/OnePredicate.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/java/org/apache/commons/collections/functors/OnePredicate.java
Thu Mar  2 11:02:36 2006
@@ -49,6 +49,12 @@
      */
     public static Predicate getInstance(Predicate[] predicates) {
         FunctorUtils.validate(predicates);
+        if (predicates.length == 0) {
+            return FalsePredicate.INSTANCE;
+        }
+        if (predicates.length == 1) {
+            return predicates[0];
+        }
         predicates = FunctorUtils.copy(predicates);
         return new OnePredicate(predicates);
     }

Modified: jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java?rev=382470&r1=382469&r2=382470&view=diff
==============================================================================
--- jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java
(original)
+++ jakarta/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestPredicateUtils.java
Thu Mar  2 11:02:36 2006
@@ -210,6 +210,8 @@
     //------------------------------------------------------------------
 
     public void testAllPredicate() {
+        assertTrue(PredicateUtils.allPredicate(
+            new Predicate[] {}).evaluate(null));
         assertEquals(true, PredicateUtils.allPredicate(new Predicate[] {
             PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
         assertEquals(false, PredicateUtils.allPredicate(new Predicate[] {
@@ -323,6 +325,8 @@
     //------------------------------------------------------------------
 
     public void testAnyPredicate() {
+        assertFalse(PredicateUtils.anyPredicate(
+            new Predicate[] {}).evaluate(null));
         assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] {
             PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
         assertEquals(true, PredicateUtils.anyPredicate(new Predicate[] {
@@ -436,6 +440,8 @@
     //------------------------------------------------------------------
 
     public void testOnePredicate() {
+        assertFalse(PredicateUtils.onePredicate(
+            new Predicate[] {}).evaluate(null));
         assertEquals(false, PredicateUtils.onePredicate(new Predicate[] {
             PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
         assertEquals(false, PredicateUtils.onePredicate(new Predicate[] {
@@ -553,6 +559,8 @@
     //------------------------------------------------------------------
 
     public void testNonePredicate() {
+        assertTrue(PredicateUtils.nonePredicate(
+            new Predicate[] {}).evaluate(null));
         assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] {
             PredicateUtils.truePredicate(), PredicateUtils.truePredicate(), PredicateUtils.truePredicate()}).evaluate(null));
         assertEquals(false, PredicateUtils.nonePredicate(new Predicate[] {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message