From 145b19acc4e6b1a2b1812e49a93c4285f2e79c75 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Mon, 31 May 2004 16:43:17 +0000 Subject: [PATCH] Add PredicateDecorator to provide consistent access to predicates based on code from Brian Lloyd-Newberry git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131756 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections/functors/AllPredicate.java | 4 +- .../collections/functors/AndPredicate.java | 22 +++------- .../collections/functors/AnyPredicate.java | 4 +- .../collections/functors/NonePredicate.java | 4 +- .../collections/functors/NotPredicate.java | 12 +++--- .../functors/NullIsExceptionPredicate.java | 12 +++--- .../functors/NullIsFalsePredicate.java | 12 +++--- .../functors/NullIsTruePredicate.java | 12 +++--- .../collections/functors/OnePredicate.java | 4 +- .../collections/functors/OrPredicate.java | 21 +++------- .../functors/PredicateDecorator.java | 42 +++++++++++++++++++ .../functors/TransformedPredicate.java | 13 +++--- 12 files changed, 93 insertions(+), 69 deletions(-) create mode 100644 src/java/org/apache/commons/collections/functors/PredicateDecorator.java diff --git a/src/java/org/apache/commons/collections/functors/AllPredicate.java b/src/java/org/apache/commons/collections/functors/AllPredicate.java index 9b4341346..6186c2f39 100644 --- a/src/java/org/apache/commons/collections/functors/AllPredicate.java +++ b/src/java/org/apache/commons/collections/functors/AllPredicate.java @@ -24,11 +24,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns true if all the predicates return true. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class AllPredicate implements Predicate, Serializable { +public final class AllPredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = -3094696765038308799L; diff --git a/src/java/org/apache/commons/collections/functors/AndPredicate.java b/src/java/org/apache/commons/collections/functors/AndPredicate.java index 7b5a74d5b..462859762 100644 --- a/src/java/org/apache/commons/collections/functors/AndPredicate.java +++ b/src/java/org/apache/commons/collections/functors/AndPredicate.java @@ -23,11 +23,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns true if both the predicates return true. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class AndPredicate implements Predicate, Serializable { +public final class AndPredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = 4189014213763186912L; @@ -76,23 +76,13 @@ public final class AndPredicate implements Predicate, Serializable { } /** - * Gets the first predicate. + * Gets the two predicates being decorated as an array. * - * @return the predicate + * @return the predicates * @since Commons Collections 3.1 */ - public Predicate getPredicate1() { - return iPredicate1; - } - - /** - * Gets the second predicate. - * - * @return the predicate - * @since Commons Collections 3.1 - */ - public Predicate getPredicate2() { - return iPredicate2; + public Predicate[] getPredicates() { + return new Predicate[] {iPredicate1, iPredicate2}; } } diff --git a/src/java/org/apache/commons/collections/functors/AnyPredicate.java b/src/java/org/apache/commons/collections/functors/AnyPredicate.java index 83d14344e..8dfc2e356 100644 --- a/src/java/org/apache/commons/collections/functors/AnyPredicate.java +++ b/src/java/org/apache/commons/collections/functors/AnyPredicate.java @@ -24,11 +24,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns true if any of the predicates return true. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class AnyPredicate implements Predicate, Serializable { +public final class AnyPredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = 7429999530934647542L; diff --git a/src/java/org/apache/commons/collections/functors/NonePredicate.java b/src/java/org/apache/commons/collections/functors/NonePredicate.java index 19a7a99bd..e914ab8fa 100644 --- a/src/java/org/apache/commons/collections/functors/NonePredicate.java +++ b/src/java/org/apache/commons/collections/functors/NonePredicate.java @@ -24,11 +24,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns true if none of the predicates return true. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class NonePredicate implements Predicate, Serializable { +public final class NonePredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = 2007613066565892961L; diff --git a/src/java/org/apache/commons/collections/functors/NotPredicate.java b/src/java/org/apache/commons/collections/functors/NotPredicate.java index 34b9975b6..ecae2c914 100644 --- a/src/java/org/apache/commons/collections/functors/NotPredicate.java +++ b/src/java/org/apache/commons/collections/functors/NotPredicate.java @@ -23,11 +23,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns the opposite of the decorated predicate. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class NotPredicate implements Predicate, Serializable { +public final class NotPredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = -2654603322338049674L; @@ -71,13 +71,13 @@ public final class NotPredicate implements Predicate, Serializable { } /** - * Gets the predicate. + * Gets the predicate being decorated. * - * @return the predicate + * @return the predicate as the only element in an array * @since Commons Collections 3.1 */ - public Predicate getPredicate() { - return iPredicate; + public Predicate[] getPredicates() { + return new Predicate[] {iPredicate}; } } diff --git a/src/java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java b/src/java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java index f7a4a6d99..516198810 100644 --- a/src/java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java +++ b/src/java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java @@ -24,11 +24,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that throws an exception if the input is null. * * @since Commons Collections 3.0 - * @version $Revision: 1.6 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.7 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class NullIsExceptionPredicate implements Predicate, Serializable { +public final class NullIsExceptionPredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = 3243449850504576071L; @@ -77,13 +77,13 @@ public final class NullIsExceptionPredicate implements Predicate, Serializable { } /** - * Gets the predicate. + * Gets the predicate being decorated. * - * @return the predicate + * @return the predicate as the only element in an array * @since Commons Collections 3.1 */ - public Predicate getPredicate() { - return iPredicate; + public Predicate[] getPredicates() { + return new Predicate[] {iPredicate}; } } diff --git a/src/java/org/apache/commons/collections/functors/NullIsFalsePredicate.java b/src/java/org/apache/commons/collections/functors/NullIsFalsePredicate.java index 8abbb8a3d..27e48cbad 100644 --- a/src/java/org/apache/commons/collections/functors/NullIsFalsePredicate.java +++ b/src/java/org/apache/commons/collections/functors/NullIsFalsePredicate.java @@ -23,11 +23,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns false if the input is null. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class NullIsFalsePredicate implements Predicate, Serializable { +public final class NullIsFalsePredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = -2997501534564735525L; @@ -75,13 +75,13 @@ public final class NullIsFalsePredicate implements Predicate, Serializable { } /** - * Gets the predicate. + * Gets the predicate being decorated. * - * @return the predicate + * @return the predicate as the only element in an array * @since Commons Collections 3.1 */ - public Predicate getPredicate() { - return iPredicate; + public Predicate[] getPredicates() { + return new Predicate[] {iPredicate}; } } diff --git a/src/java/org/apache/commons/collections/functors/NullIsTruePredicate.java b/src/java/org/apache/commons/collections/functors/NullIsTruePredicate.java index 21d27154d..46078064e 100644 --- a/src/java/org/apache/commons/collections/functors/NullIsTruePredicate.java +++ b/src/java/org/apache/commons/collections/functors/NullIsTruePredicate.java @@ -23,11 +23,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns true if the input is null. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class NullIsTruePredicate implements Predicate, Serializable { +public final class NullIsTruePredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = -7625133768987126273L; @@ -75,13 +75,13 @@ public final class NullIsTruePredicate implements Predicate, Serializable { } /** - * Gets the predicate. + * Gets the predicate being decorated. * - * @return the predicate + * @return the predicate as the only element in an array * @since Commons Collections 3.1 */ - public Predicate getPredicate() { - return iPredicate; + public Predicate[] getPredicates() { + return new Predicate[] {iPredicate}; } } diff --git a/src/java/org/apache/commons/collections/functors/OnePredicate.java b/src/java/org/apache/commons/collections/functors/OnePredicate.java index 9e6793084..49772988a 100644 --- a/src/java/org/apache/commons/collections/functors/OnePredicate.java +++ b/src/java/org/apache/commons/collections/functors/OnePredicate.java @@ -24,11 +24,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns true if only one of the predicates return true. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class OnePredicate implements Predicate, Serializable { +public final class OnePredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = -8125389089924745785L; diff --git a/src/java/org/apache/commons/collections/functors/OrPredicate.java b/src/java/org/apache/commons/collections/functors/OrPredicate.java index a6bcdeb45..25164625f 100644 --- a/src/java/org/apache/commons/collections/functors/OrPredicate.java +++ b/src/java/org/apache/commons/collections/functors/OrPredicate.java @@ -23,11 +23,11 @@ import org.apache.commons.collections.Predicate; * Predicate implementation that returns true if either of the predicates return true. * * @since Commons Collections 3.0 - * @version $Revision: 1.5 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.6 $ $Date: 2004/05/31 16:43:17 $ * * @author Stephen Colebourne */ -public final class OrPredicate implements Predicate, Serializable { +public final class OrPredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = -8791518325735182855L; @@ -76,22 +76,13 @@ public final class OrPredicate implements Predicate, Serializable { } /** - * Gets the first predicate. + * Gets the two predicates being decorated as an array. * - * @return the predicate + * @return the predicates * @since Commons Collections 3.1 */ - public Predicate getPredicate1() { - return iPredicate1; - } - - /** - * Gets the second predicate. - * @return the predicate - * @since Commons Collections 3.1 - */ - public Predicate getPredicate2() { - return iPredicate2; + public Predicate[] getPredicates() { + return new Predicate[] {iPredicate1, iPredicate2}; } } diff --git a/src/java/org/apache/commons/collections/functors/PredicateDecorator.java b/src/java/org/apache/commons/collections/functors/PredicateDecorator.java new file mode 100644 index 000000000..b0f7eeb4e --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/PredicateDecorator.java @@ -0,0 +1,42 @@ +/* + * Copyright 2004 The Apache Software Foundation + * + * Licensed 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 org.apache.commons.collections.Predicate; + +/** + * Defines a predicate that decorates one or more other predicates. + *

+ * This interface enables tools to access the decorated predicates. + * + * @since Commons Collections 3.1 + * @version $Revision: 1.1 $ $Date: 2004/05/31 16:43:17 $ + * + * @author Stephen Colebourne + */ +public interface PredicateDecorator extends Predicate { + + /** + * Gets the predicates being decorated as an array. + *

+ * The array may be the internal data structure of the predicate and thus + * should not be altered. + * + * @return the predicates being decorated + */ + Predicate[] getPredicates(); + +} diff --git a/src/java/org/apache/commons/collections/functors/TransformedPredicate.java b/src/java/org/apache/commons/collections/functors/TransformedPredicate.java index c3970bdc1..d73c38619 100644 --- a/src/java/org/apache/commons/collections/functors/TransformedPredicate.java +++ b/src/java/org/apache/commons/collections/functors/TransformedPredicate.java @@ -25,11 +25,11 @@ import org.apache.commons.collections.Transformer; * another Predicate. * * @since Commons Collections 3.1 - * @version $Revision: 1.3 $ $Date: 2004/05/16 11:16:01 $ + * @version $Revision: 1.4 $ $Date: 2004/05/31 16:43:17 $ * @author Alban Peignier * @author Stephen Colebourne */ -public final class TransformedPredicate implements Predicate, Serializable { +public final class TransformedPredicate implements Predicate, PredicateDecorator, Serializable { /** Serial version UID */ static final long serialVersionUID = -5596090919668315834L; @@ -82,12 +82,13 @@ public final class TransformedPredicate implements Predicate, Serializable { } /** - * Gets the predicate in use. + * Gets the predicate being decorated. * - * @return the predicate + * @return the predicate as the only element in an array + * @since Commons Collections 3.1 */ - public Predicate getPredicate() { - return iPredicate; + public Predicate[] getPredicates() { + return new Predicate[] {iPredicate}; } /**