diff --git a/src/main/java/org/apache/commons/collections/functors/AbstractQuantifierPredicate.java b/src/main/java/org/apache/commons/collections/functors/AbstractQuantifierPredicate.java new file mode 100644 index 000000000..1f34c9f30 --- /dev/null +++ b/src/main/java/org/apache/commons/collections/functors/AbstractQuantifierPredicate.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Abstract base class for quantification predicates, e.g. All, Any, None. + * + * @since 4.0 + * @version $Id$ + */ +public abstract class AbstractQuantifierPredicate implements Predicate, PredicateDecorator, Serializable { + + /** Serial version UID */ + private static final long serialVersionUID = -3094696765038308799L; + + /** The array of predicates to call */ + protected final Predicate[] iPredicates; + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param predicates the predicates to check, not cloned, not null + */ + public AbstractQuantifierPredicate(final Predicate ... predicates) { + iPredicates = predicates; + } + + /** + * Gets the predicates. + * + * @return a copy of the predicates + * @since 3.1 + */ + public Predicate[] getPredicates() { + return FunctorUtils.copy(iPredicates); + } + +} diff --git a/src/main/java/org/apache/commons/collections/functors/AllPredicate.java b/src/main/java/org/apache/commons/collections/functors/AllPredicate.java index 28e6d7347..8d5c8f18f 100644 --- a/src/main/java/org/apache/commons/collections/functors/AllPredicate.java +++ b/src/main/java/org/apache/commons/collections/functors/AllPredicate.java @@ -36,14 +36,11 @@ import org.apache.commons.collections.Predicate; * @since 3.0 * @version $Id$ */ -public final class AllPredicate implements Predicate, PredicateDecorator, Serializable { +public final class AllPredicate extends AbstractQuantifierPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = -3094696765038308799L; - /** The array of predicates to call */ - private final Predicate[] iPredicates; - /** * Factory to create the predicate. *

@@ -98,8 +95,7 @@ public final class AllPredicate implements Predicate, PredicateDecorator ... predicates) { - super(); - iPredicates = predicates; + super(predicates); } /** @@ -117,14 +113,4 @@ public final class AllPredicate implements Predicate, PredicateDecorator[] getPredicates() { - return iPredicates; - } - } diff --git a/src/main/java/org/apache/commons/collections/functors/AnyPredicate.java b/src/main/java/org/apache/commons/collections/functors/AnyPredicate.java index 637857a54..bd17ca2a2 100644 --- a/src/main/java/org/apache/commons/collections/functors/AnyPredicate.java +++ b/src/main/java/org/apache/commons/collections/functors/AnyPredicate.java @@ -32,14 +32,11 @@ import org.apache.commons.collections.Predicate; * @since 3.0 * @version $Id$ */ -public final class AnyPredicate implements Predicate, PredicateDecorator, Serializable { +public final class AnyPredicate extends AbstractQuantifierPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = 7429999530934647542L; - /** The array of predicates to call */ - private final Predicate[] iPredicates; - /** * Factory to create the predicate. *

@@ -95,8 +92,7 @@ public final class AnyPredicate implements Predicate, PredicateDecorator[] predicates) { - super(); - iPredicates = predicates; + super(predicates); } /** @@ -114,14 +110,4 @@ public final class AnyPredicate implements Predicate, PredicateDecorator[] getPredicates() { - return iPredicates; - } - } diff --git a/src/main/java/org/apache/commons/collections/functors/NonePredicate.java b/src/main/java/org/apache/commons/collections/functors/NonePredicate.java index 55b5aa39d..7f3a615ba 100644 --- a/src/main/java/org/apache/commons/collections/functors/NonePredicate.java +++ b/src/main/java/org/apache/commons/collections/functors/NonePredicate.java @@ -32,14 +32,11 @@ import org.apache.commons.collections.Predicate; * @since 3.0 * @version $Id$ */ -public final class NonePredicate implements Predicate, PredicateDecorator, Serializable { +public final class NonePredicate extends AbstractQuantifierPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = 2007613066565892961L; - /** The array of predicates to call */ - private final Predicate[] iPredicates; - /** * Factory to create the predicate. *

@@ -85,8 +82,7 @@ public final class NonePredicate implements Predicate, PredicateDecorator< * @param predicates the predicates to check, not cloned, not null */ public NonePredicate(final Predicate[] predicates) { - super(); - iPredicates = predicates; + super(predicates); } /** @@ -104,14 +100,4 @@ public final class NonePredicate implements Predicate, PredicateDecorator< return true; } - /** - * Gets the predicates, do not modify the array. - * - * @return the predicates - * @since 3.1 - */ - public Predicate[] getPredicates() { - return iPredicates; - } - } diff --git a/src/main/java/org/apache/commons/collections/functors/OnePredicate.java b/src/main/java/org/apache/commons/collections/functors/OnePredicate.java index 28c1c3a7b..c03a8ab28 100644 --- a/src/main/java/org/apache/commons/collections/functors/OnePredicate.java +++ b/src/main/java/org/apache/commons/collections/functors/OnePredicate.java @@ -32,14 +32,11 @@ import org.apache.commons.collections.Predicate; * @since 3.0 * @version $Id$ */ -public final class OnePredicate implements Predicate, PredicateDecorator, Serializable { +public final class OnePredicate extends AbstractQuantifierPredicate implements Serializable { /** Serial version UID */ private static final long serialVersionUID = -8125389089924745785L; - /** The array of predicates to call */ - private final Predicate[] iPredicates; - /** * Factory to create the predicate. *

@@ -85,8 +82,7 @@ public final class OnePredicate implements Predicate, PredicateDecorator[] predicates) { - super(); - iPredicates = predicates; + super(predicates); } /** @@ -109,14 +105,4 @@ public final class OnePredicate implements Predicate, PredicateDecorator[] getPredicates() { - return iPredicates; - } - }