Avoid object creation in predicate factory methods

bug 38703, from Matt Benson

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@382470 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2006-03-02 19:02:36 +00:00
parent 17e59448ea
commit f9588d4deb
5 changed files with 45 additions and 2 deletions

View File

@ -49,6 +49,12 @@ public final class AllPredicate implements Predicate, PredicateDecorator, Serial
*/
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 final class AllPredicate implements Predicate, PredicateDecorator, Serial
*/
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);
}

View File

@ -49,8 +49,13 @@ public final class AnyPredicate implements Predicate, PredicateDecorator, Serial
*/
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 final class AnyPredicate implements Predicate, PredicateDecorator, Serial
*/
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);
}

View File

@ -49,6 +49,9 @@ public final class NonePredicate implements Predicate, PredicateDecorator, Seria
*/
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 final class NonePredicate implements Predicate, PredicateDecorator, Seria
*/
public static Predicate getInstance(Collection predicates) {
Predicate[] preds = FunctorUtils.validate(predicates);
if (preds.length == 0) {
return TruePredicate.INSTANCE;
}
return new NonePredicate(preds);
}

View File

@ -49,6 +49,12 @@ public final class OnePredicate implements Predicate, PredicateDecorator, Serial
*/
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);
}

View File

@ -210,6 +210,8 @@ public class TestPredicateUtils extends junit.framework.TestCase {
//------------------------------------------------------------------
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 class TestPredicateUtils extends junit.framework.TestCase {
//------------------------------------------------------------------
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 class TestPredicateUtils extends junit.framework.TestCase {
//------------------------------------------------------------------
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 class TestPredicateUtils extends junit.framework.TestCase {
//------------------------------------------------------------------
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[] {