You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Brian Lloyd-Newberry <ne...@yahoo.com> on 2004/05/14 19:38:21 UTC

[PATCH] Commons Collections - Wrapper and Composite Predicates

Commons Collections developers,

   In looking at the 3.1-dev source I see that some implementations of 
the Predicate objects have:
   Predicate getPredicate()
   or
   Predicate[] getPredicates()
methods. This is a good thing for me, as I am developing a search 
interface and would like to be able to use predicates in the 
search/filter process, and be able to do a display of the predicate 
expression structure.

   I observe that there are basic types of predicates. Ones which 
operate on Objects, ones that operate on a single (sub)Predicate and 
ones which operate on more than one Predicate. I propose that the single 
and multiple cases be tagged using an interface so that the appropriate 
getPredicate() or getPredicates() method can be found.

   I have attached a patch and 2 new interfaces (WrapperPredicate.java 
and CompositePredicate.java) which provide the functionality I propose. 
WrapperPredicate declares Predicate getPredicate(). CompositePredicate 
declares Iterator predicates(), shielding the CompositePredicate from 
modification (the Iterator returned is Unmodifiable) that the 
Predicate[] getPredicates[] implementation would allow unless a 
defensive copy was made before the return took place.

   If you have any questions or concerns about my implementation please 
let me know. This is my first patch on a Jakarta project, so I hope I 
have  done everything correctly (cvs diff -u).

   Thank you for your work and for providing such a useful library of 
collections to the community.

-Brian

Brian S. Lloyd-Newberry
Software Engineer
RBS, Inc
http://www.rbs2000.com

Re: [collections] [PATCH] Commons Collections - Wrapper and Composite Predicates

Posted by Stephen Colebourne <sc...@btopenworld.com>.
Thank you for your patch.

I'm not certain as to how to procede on this one. There is nothing wrong
with your patch or idea. The problem is that it opens the door to accessing
other functors such as Transformer and Closure. How far does this go?

One idea I have considered is to have a single generic Decorator interface
that specifies one method which returns a predicate.

Another idea is to use reflection, with a single method on PredicateUtils to
access this functionality. This might be less intrusive, but perhaps is also
less obvious.

Not sure, any opinions?

Stephen

----- Original Message -----
From: "Brian Lloyd-Newberry" <ne...@yahoo.com>
To: <co...@jakarta.apache.org>
Sent: Friday, May 14, 2004 6:38 PM
Subject: [PATCH] Commons Collections - Wrapper and Composite Predicates


> Commons Collections developers,
>
>    In looking at the 3.1-dev source I see that some implementations of
> the Predicate objects have:
>    Predicate getPredicate()
>    or
>    Predicate[] getPredicates()
> methods. This is a good thing for me, as I am developing a search
> interface and would like to be able to use predicates in the
> search/filter process, and be able to do a display of the predicate
> expression structure.
>
>    I observe that there are basic types of predicates. Ones which
> operate on Objects, ones that operate on a single (sub)Predicate and
> ones which operate on more than one Predicate. I propose that the single
> and multiple cases be tagged using an interface so that the appropriate
> getPredicate() or getPredicates() method can be found.
>
>    I have attached a patch and 2 new interfaces (WrapperPredicate.java
> and CompositePredicate.java) which provide the functionality I propose.
> WrapperPredicate declares Predicate getPredicate(). CompositePredicate
> declares Iterator predicates(), shielding the CompositePredicate from
> modification (the Iterator returned is Unmodifiable) that the
> Predicate[] getPredicates[] implementation would allow unless a
> defensive copy was made before the return took place.
>
>    If you have any questions or concerns about my implementation please
> let me know. This is my first patch on a Jakarta project, so I hope I
> have  done everything correctly (cvs diff -u).
>
>    Thank you for your work and for providing such a useful library of
> collections to the community.
>
> -Brian
>
> Brian S. Lloyd-Newberry
> Software Engineer
> RBS, Inc
> http://www.rbs2000.com
>


----------------------------------------------------------------------------
----


> /*
>  *  Copyright 2001-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 functor interface implemented by classes that perform a
predicate
>  * test on an object.
>  * <p>
>  * A <code>WrapperPredicate</code> extends the <code>Predicate</code>
interface to support the notion that one <code>Predicate</code> can wrap
another.
>  *
>  * Examples of <code>WrapperPredicates</code> include: not,
nullisexception, nullisfalse, nullistrue.
>  *
>  * <p>
>  * @since Commons Collections 3.1
>  * @version $Revision:$ $Date:$
>  *
>  * @author Brian S. Lloyd-Newberry
>  */
> public interface WrapperPredicate extends Predicate {
>
>     /**
>      * Retrieves the <code>Predicate</code> that this
<code>Predicate</code> is wrapping.
>      *
>      * @return the <code>Predicate</code>.
>      */
>     public Predicate getPredicate( );
> }
>


----------------------------------------------------------------------------
----


> /*
>  *  Copyright 2001-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 java.util.Iterator;
>
> import org.apache.commons.collections.Predicate;
>
> /**
>  * Defines a functor interface implemented by classes that perform a
predicate
>  * test on an object.
>  *
>  * <p>
>  * A <code>CompoundPredicate</code> extends the <code>Predicate</code>
interface to support the notion that one <code>Predicate</code> can wrap a
collection of other predicates.
>  *
>  * Examples of <code>CompoundPredicates</code> include: and, or, all, any,
none.
>  *
>  * <p>
>  * @since Commons Collections 3.1
>  * @version $Revision:$ $Date:$
>  *
>  * @author Brian S. Lloyd-Newberry
>  */
> public interface CompoundPredicate extends Predicate
> {
>     /**
>      * Retrieves an <code>Iterator</code> over the <code>Predicates</code>
that this <code>Predicate</code> contains.
>      *
>      * @return an <code>Iterator</code> over the <code>Predicates</code>.
>      */
>     public Iterator predicates( );
> }
>


----------------------------------------------------------------------------
----


> ? java/org/apache/commons/collections/functors/CompoundPredicate.java
> ? java/org/apache/commons/collections/functors/WrapperPredicate.java
> Index: java/org/apache/commons/collections/functors/AllPredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/AllPredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 AllPredicate.java
> --- java/org/apache/commons/collections/functors/AllPredicate.java 13 Mar
2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/AllPredicate.java 14 May
2004 17:36:58 -0000
> @@ -17,8 +17,11 @@
>
>  import java.io.Serializable;
>  import java.util.Collection;
> +import java.util.Iterator;
>
>  import org.apache.commons.collections.Predicate;
> +import org.apache.commons.collections.iterators.ObjectArrayIterator;
> +import org.apache.commons.collections.iterators.UnmodifiableIterator;
>
>  /**
>   * Predicate implementation that returns true if all the predicates
return true.
> @@ -27,8 +30,9 @@
>   * @version $Revision: 1.4 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class AllPredicate implements Predicate, Serializable {
> +public final class AllPredicate implements CompoundPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = -3094696765038308799L;
> @@ -97,4 +101,12 @@
>          return iPredicates;
>      }
>
> +    /**
> +     * <code>Iterator</code> over predicates. Not modifiable.
> +     * @return an <code>Iterator</code> over the predicates.
> +     * @since Commons Collection 3.1
> +     */
> +    public Iterator predicates() {
> +        return UnmodifiableIterator.decorate(new
ObjectArrayIterator(iPredicates));
> +    }
>  }
> Index: java/org/apache/commons/collections/functors/AndPredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/AndPredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 AndPredicate.java
> --- java/org/apache/commons/collections/functors/AndPredicate.java 13 Mar
2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/AndPredicate.java 14 May
2004 17:36:58 -0000
> @@ -16,8 +16,11 @@
>  package org.apache.commons.collections.functors;
>
>  import java.io.Serializable;
> +import java.util.Iterator;
>
>  import org.apache.commons.collections.Predicate;
> +import org.apache.commons.collections.iterators.ObjectArrayIterator;
> +import org.apache.commons.collections.iterators.UnmodifiableIterator;
>
>  /**
>   * Predicate implementation that returns true if both the predicates
return true.
> @@ -26,8 +29,9 @@
>   * @version $Revision: 1.4 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class AndPredicate implements Predicate, Serializable {
> +public final class AndPredicate implements CompoundPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = 4189014213763186912L;
> @@ -90,4 +94,12 @@
>          return iPredicate2;
>      }
>
> +    /**
> +     * <code>Iterator</code> over predicates. Not modifiable.
> +     * @return an <code>Iterator</code> over the predicates.
> +     * @since Commons Collection 3.1
> +     */
> +    public Iterator predicates() {
> +        return UnmodifiableIterator.decorate(new ObjectArrayIterator(new
Object[] {iPredicate1, iPredicate2}));
> +    }
>  }
> Index: java/org/apache/commons/collections/functors/AnyPredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/AnyPredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 AnyPredicate.java
> --- java/org/apache/commons/collections/functors/AnyPredicate.java 13 Mar
2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/AnyPredicate.java 14 May
2004 17:36:58 -0000
> @@ -17,8 +17,11 @@
>
>  import java.io.Serializable;
>  import java.util.Collection;
> +import java.util.Iterator;
>
>  import org.apache.commons.collections.Predicate;
> +import org.apache.commons.collections.iterators.ObjectArrayIterator;
> +import org.apache.commons.collections.iterators.UnmodifiableIterator;
>
>  /**
>   * Predicate implementation that returns true if any of the predicates
return true.
> @@ -27,8 +30,9 @@
>   * @version $Revision: 1.4 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class AnyPredicate implements Predicate, Serializable {
> +public final class AnyPredicate implements CompoundPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = 7429999530934647542L;
> @@ -97,4 +101,12 @@
>          return iPredicates;
>      }
>
> +    /**
> +     * <code>Iterator</code> over predicates. Not modifiable.
> +     * @return an <code>Iterator</code> over the predicates.
> +     * @since Commons Collection 3.1
> +     */
> +    public Iterator predicates() {
> +        return UnmodifiableIterator.decorate(new
ObjectArrayIterator(iPredicates));
> +    }
>  }
> Index: java/org/apache/commons/collections/functors/NonePredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/NonePredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 NonePredicate.java
> --- java/org/apache/commons/collections/functors/NonePredicate.java 13 Mar
2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/NonePredicate.java 14 May
2004 17:36:58 -0000
> @@ -17,8 +17,11 @@
>
>  import java.io.Serializable;
>  import java.util.Collection;
> +import java.util.Iterator;
>
>  import org.apache.commons.collections.Predicate;
> +import org.apache.commons.collections.iterators.ObjectArrayIterator;
> +import org.apache.commons.collections.iterators.UnmodifiableIterator;
>
>  /**
>   * Predicate implementation that returns true if none of the predicates
return true.
> @@ -27,8 +30,9 @@
>   * @version $Revision: 1.4 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class NonePredicate implements Predicate, Serializable {
> +public final class NonePredicate implements CompoundPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = 2007613066565892961L;
> @@ -97,4 +101,12 @@
>          return iPredicates;
>      }
>
> +    /**
> +     * <code>Iterator</code> over predicates. Not modifiable.
> +     * @return an <code>Iterator</code> over the predicates.
> +     * @since Commons Collection 3.1
> +     */
> +    public Iterator predicates() {
> +        return UnmodifiableIterator.decorate(new
ObjectArrayIterator(iPredicates));
> +    }
>  }
> Index: java/org/apache/commons/collections/functors/NotPredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/NotPredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 NotPredicate.java
> --- java/org/apache/commons/collections/functors/NotPredicate.java 13 Mar
2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/NotPredicate.java 14 May
2004 17:36:58 -0000
> @@ -26,8 +26,9 @@
>   * @version $Revision: 1.4 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class NotPredicate implements Predicate, Serializable {
> +public final class NotPredicate implements WrapperPredicate, Serializable
{
>
>      /** Serial version UID */
>      static final long serialVersionUID = -2654603322338049674L;
> Index:
java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/NullIsExceptionPredicate.java,v
> retrieving revision 1.5
> diff -u -r1.5 NullIsExceptionPredicate.java
> ---
java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java
13 Mar 2004 17:17:03 -0000 1.5
> +++
java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java
14 May 2004 17:36:59 -0000
> @@ -27,8 +27,9 @@
>   * @version $Revision: 1.5 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class NullIsExceptionPredicate implements Predicate,
Serializable {
> +public final class NullIsExceptionPredicate implements WrapperPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = 3243449850504576071L;
> Index:
java/org/apache/commons/collections/functors/NullIsFalsePredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/NullIsFalsePredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 NullIsFalsePredicate.java
> --- java/org/apache/commons/collections/functors/NullIsFalsePredicate.java
13 Mar 2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/NullIsFalsePredicate.java
14 May 2004 17:36:59 -0000
> @@ -26,8 +26,9 @@
>   * @version $Revision: 1.4 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class NullIsFalsePredicate implements Predicate,
Serializable {
> +public final class NullIsFalsePredicate implements WrapperPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = -2997501534564735525L;
> Index:
java/org/apache/commons/collections/functors/NullIsTruePredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/NullIsTruePredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 NullIsTruePredicate.java
> --- java/org/apache/commons/collections/functors/NullIsTruePredicate.java
13 Mar 2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/NullIsTruePredicate.java
14 May 2004 17:36:59 -0000
> @@ -27,7 +27,7 @@
>   *
>   * @author Stephen Colebourne
>   */
> -public final class NullIsTruePredicate implements Predicate, Serializable
{
> +public final class NullIsTruePredicate implements WrapperPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = -7625133768987126273L;
> Index: java/org/apache/commons/collections/functors/OnePredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/OnePredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 OnePredicate.java
> --- java/org/apache/commons/collections/functors/OnePredicate.java 13 Mar
2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/OnePredicate.java 14 May
2004 17:36:59 -0000
> @@ -17,8 +17,11 @@
>
>  import java.io.Serializable;
>  import java.util.Collection;
> +import java.util.Iterator;
>
>  import org.apache.commons.collections.Predicate;
> +import org.apache.commons.collections.iterators.ObjectArrayIterator;
> +import org.apache.commons.collections.iterators.UnmodifiableIterator;
>
>  /**
>   * Predicate implementation that returns true if only one of the
predicates return true.
> @@ -27,8 +30,9 @@
>   * @version $Revision: 1.4 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class OnePredicate implements Predicate, Serializable {
> +public final class OnePredicate implements CompoundPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = -8125389089924745785L;
> @@ -101,4 +105,12 @@
>          return iPredicates;
>      }
>
> +    /**
> +     * <code>Iterator</code> over predicates. Not modifiable.
> +     * @return an <code>Iterator</code> over the predicates.
> +     * @since Commons Collection 3.1
> +     */
> +    public Iterator predicates() {
> +        return UnmodifiableIterator.decorate(new
ObjectArrayIterator(iPredicates));
> +    }
>  }
> Index: java/org/apache/commons/collections/functors/OrPredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/OrPredicate.java,v
> retrieving revision 1.4
> diff -u -r1.4 OrPredicate.java
> --- java/org/apache/commons/collections/functors/OrPredicate.java 13 Mar
2004 17:17:03 -0000 1.4
> +++ java/org/apache/commons/collections/functors/OrPredicate.java 14 May
2004 17:36:59 -0000
> @@ -16,8 +16,11 @@
>  package org.apache.commons.collections.functors;
>
>  import java.io.Serializable;
> +import java.util.Iterator;
>
>  import org.apache.commons.collections.Predicate;
> +import org.apache.commons.collections.iterators.ObjectArrayIterator;
> +import org.apache.commons.collections.iterators.UnmodifiableIterator;
>
>  /**
>   * Predicate implementation that returns true if either of the predicates
return true.
> @@ -26,8 +29,9 @@
>   * @version $Revision: 1.4 $ $Date: 2004/03/13 17:17:03 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
> -public final class OrPredicate implements Predicate, Serializable {
> +public final class OrPredicate implements CompoundPredicate, Serializable
{
>
>      /** Serial version UID */
>      static final long serialVersionUID = -8791518325735182855L;
> @@ -90,4 +94,12 @@
>          return iPredicate2;
>      }
>
> +    /**
> +     * <code>Iterator</code> over predicates. Not modifiable.
> +     * @return an <code>Iterator</code> over the predicates.
> +     * @since Commons Collection 3.1
> +     */
> +    public Iterator predicates() {
> +        return UnmodifiableIterator.decorate(new ObjectArrayIterator(new
Object[] {iPredicate1, iPredicate2}));
> +    }
>  }
> Index:
java/org/apache/commons/collections/functors/TransformedPredicate.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/java/org/apache/commons/coll
ections/functors/TransformedPredicate.java,v
> retrieving revision 1.2
> diff -u -r1.2 TransformedPredicate.java
> --- java/org/apache/commons/collections/functors/TransformedPredicate.java
13 Mar 2004 17:15:17 -0000 1.2
> +++ java/org/apache/commons/collections/functors/TransformedPredicate.java
14 May 2004 17:36:59 -0000
> @@ -29,7 +29,7 @@
>   * @author Alban Peignier
>   * @author Stephen Colebourne
>   */
> -public final class TransformedPredicate implements Predicate,
Serializable {
> +public final class TransformedPredicate implements WrapperPredicate,
Serializable {
>
>      /** Serial version UID */
>      static final long serialVersionUID = -5596090919668315834L;
> Index: test/org/apache/commons/collections/TestPredicateUtils.java
> ===================================================================
> RCS file:
/home/cvspublic/jakarta-commons/collections/src/test/org/apache/commons/coll
ections/TestPredicateUtils.java,v
> retrieving revision 1.8
> diff -u -r1.8 TestPredicateUtils.java
> --- test/org/apache/commons/collections/TestPredicateUtils.java 13 Mar
2004 16:34:46 -0000 1.8
> +++ test/org/apache/commons/collections/TestPredicateUtils.java 14 May
2004 17:37:00 -0000
> @@ -19,6 +19,7 @@
>  import java.util.Collection;
>  import java.util.Collections;
>  import java.util.HashMap;
> +import java.util.Iterator;
>  import java.util.List;
>  import java.util.Map;
>
> @@ -27,6 +28,9 @@
>  import junit.framework.TestSuite;
>  import junit.textui.TestRunner;
>
> +import org.apache.commons.collections.functors.CompoundPredicate;
> +import org.apache.commons.collections.functors.WrapperPredicate;
> +
>  /**
>   * Tests the org.apache.commons.collections.PredicateUtils class.
>   *
> @@ -34,6 +38,7 @@
>   * @version $Revision: 1.8 $ $Date: 2004/03/13 16:34:46 $
>   *
>   * @author Stephen Colebourne
> + * @author Brian S. Lloyd-Newberry
>   */
>  public class TestPredicateUtils extends junit.framework.TestCase {
>
> @@ -175,6 +180,9 @@
>          assertEquals(false,
PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cObject
));
>          assertEquals(false,
PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cString
));
>          assertEquals(false,
PredicateUtils.notPredicate(PredicateUtils.truePredicate()).evaluate(cIntege
r));
> +
> +        Predicate true_predicate = PredicateUtils.truePredicate();
> +        assertEquals(true_predicate, ((WrapperPredicate)
PredicateUtils.notPredicate(true_predicate)).getPredicate());
>      }
>
>      public void testNotPredicateEx() {
> @@ -194,6 +202,13 @@
>          assertEquals(false,
PredicateUtils.andPredicate(PredicateUtils.truePredicate(),
PredicateUtils.falsePredicate()).evaluate(null));
>          assertEquals(false,
PredicateUtils.andPredicate(PredicateUtils.falsePredicate(),
PredicateUtils.truePredicate()).evaluate(null));
>          assertEquals(false,
PredicateUtils.andPredicate(PredicateUtils.falsePredicate(),
PredicateUtils.falsePredicate()).evaluate(null));
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate and_predicate =
PredicateUtils.andPredicate(predicate_one,predicate_two);
> +        Iterator and_predicate_i = ((CompoundPredicate)
and_predicate).predicates();
> +        assertEquals(predicate_one, and_predicate_i.next());
> +        assertEquals(predicate_two, and_predicate_i.next());
> +        assertEquals(false, and_predicate_i.hasNext());
>      }
>
>      public void testAndPredicateEx() {
> @@ -205,6 +220,21 @@
>          fail();
>      }
>
> +    public void testAndPredicateEx2() {
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate and_predicate =
PredicateUtils.andPredicate(predicate_one,predicate_two);
> +        Iterator and_predicate_i = ((CompoundPredicate)
and_predicate).predicates();
> +
> +        try {
> +            and_predicate_i.next( );
> +            and_predicate_i.remove( );
> +            fail("CompoundPredicate.predicates() should not return a
modifiable Iterator." );
> +        } catch(UnsupportedOperationException uoe) {
> +        }
> +
> +    }
> +
>      // allPredicate
>      //------------------------------------------------------------------
>
> @@ -237,6 +267,19 @@
>          coll.add(PredicateUtils.falsePredicate());
>          coll.add(PredicateUtils.falsePredicate());
>          assertEquals(false,
PredicateUtils.allPredicate(coll).evaluate(null));
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate predicate_three = PredicateUtils.truePredicate();
> +        coll.clear();
> +        coll.add(predicate_one);
> +        coll.add(predicate_two);
> +        coll.add(predicate_three);
> +        Predicate all_predicate = PredicateUtils.allPredicate(coll);
> +        Iterator all_predicate_i = ((CompoundPredicate)
all_predicate).predicates();
> +        assertEquals(predicate_one, all_predicate_i.next());
> +        assertEquals(predicate_two, all_predicate_i.next());
> +        assertEquals(predicate_three, all_predicate_i.next());
> +        assertEquals(false, all_predicate_i.hasNext());
>      }
>
>      public void testAllPredicateEx1() {
> @@ -295,6 +338,26 @@
>          }
>          fail();
>      }
> +
> +
> +    public void testAllPredicateEx7() {
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Collection coll = new ArrayList();
> +        coll.add( predicate_one );
> +        coll.add( predicate_two );
> +        Predicate all_predicate = PredicateUtils.allPredicate(coll);
> +        Iterator all_predicate_i = ((CompoundPredicate)
all_predicate).predicates();
> +
> +        try {
> +            all_predicate_i.next( );
> +            all_predicate_i.remove( );
> +            fail("CompoundPredicate.predicates() should not return a
modifiable Iterator." );
> +        } catch(UnsupportedOperationException uoe) {
> +        }
> +
> +    }
> +
>
>      // orPredicate
>      //------------------------------------------------------------------
> @@ -304,6 +367,13 @@
>          assertEquals(true,
PredicateUtils.orPredicate(PredicateUtils.truePredicate(),
PredicateUtils.falsePredicate()).evaluate(null));
>          assertEquals(true,
PredicateUtils.orPredicate(PredicateUtils.falsePredicate(),
PredicateUtils.truePredicate()).evaluate(null));
>          assertEquals(false,
PredicateUtils.orPredicate(PredicateUtils.falsePredicate(),
PredicateUtils.falsePredicate()).evaluate(null));
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate or_predicate =
PredicateUtils.orPredicate(predicate_one,predicate_two);
> +        Iterator or_predicate_i = ((CompoundPredicate)
or_predicate).predicates();
> +        assertEquals(predicate_one, or_predicate_i.next());
> +        assertEquals(predicate_two, or_predicate_i.next());
> +        assertEquals(false, or_predicate_i.hasNext());
>      }
>
>      public void testOrPredicateEx() {
> @@ -314,6 +384,22 @@
>          }
>          fail();
>      }
> +
> +    public void testOrPredicateEx2() {
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate or_predicate =
PredicateUtils.orPredicate(predicate_one,predicate_two);
> +        Iterator or_predicate_i = ((CompoundPredicate)
or_predicate).predicates();
> +
> +        try {
> +            or_predicate_i.next( );
> +            or_predicate_i.remove( );
> +            fail("CompoundPredicate.predicates() should not return a
modifiable Iterator." );
> +        } catch(UnsupportedOperationException uoe) {
> +        }
> +
> +    }
> +
>
>      // anyPredicate
>      //------------------------------------------------------------------
> @@ -347,6 +433,19 @@
>          coll.add(PredicateUtils.falsePredicate());
>          coll.add(PredicateUtils.falsePredicate());
>          assertEquals(false,
PredicateUtils.anyPredicate(coll).evaluate(null));
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate predicate_three = PredicateUtils.truePredicate();
> +        coll.clear();
> +        coll.add(predicate_one);
> +        coll.add(predicate_two);
> +        coll.add(predicate_three);
> +        Predicate any_predicate = PredicateUtils.anyPredicate(coll);
> +        Iterator any_predicate_i = ((CompoundPredicate)
any_predicate).predicates();
> +        assertEquals(predicate_one, any_predicate_i.next());
> +        assertEquals(predicate_two, any_predicate_i.next());
> +        assertEquals(predicate_three, any_predicate_i.next());
> +        assertEquals(false, any_predicate_i.hasNext());
>      }
>
>      public void testAnyPredicateEx1() {
> @@ -405,7 +504,26 @@
>          }
>          fail();
>      }
> -
> +
> +    public void testAnyPredicateEx7() {
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Collection coll = new ArrayList();
> +        coll.add( predicate_one );
> +        coll.add( predicate_two );
> +        Predicate any_predicate = PredicateUtils.anyPredicate(coll);
> +        Iterator any_predicate_i = ((CompoundPredicate)
any_predicate).predicates();
> +
> +        try {
> +            any_predicate_i.next( );
> +            any_predicate_i.remove( );
> +            fail("CompoundPredicate.predicates() should not return a
modifiable Iterator." );
> +        } catch(UnsupportedOperationException uoe) {
> +        }
> +
> +    }
> +
> +
>      // eitherPredicate
>      //------------------------------------------------------------------
>
> @@ -414,6 +532,13 @@
>          assertEquals(true,
PredicateUtils.eitherPredicate(PredicateUtils.truePredicate(),
PredicateUtils.falsePredicate()).evaluate(null));
>          assertEquals(true,
PredicateUtils.eitherPredicate(PredicateUtils.falsePredicate(),
PredicateUtils.truePredicate()).evaluate(null));
>          assertEquals(false,
PredicateUtils.eitherPredicate(PredicateUtils.falsePredicate(),
PredicateUtils.falsePredicate()).evaluate(null));
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate either_predicate =
PredicateUtils.eitherPredicate(predicate_one,predicate_two);
> +        Iterator either_predicate_i = ((CompoundPredicate)
either_predicate).predicates();
> +        assertEquals(predicate_one, either_predicate_i.next());
> +        assertEquals(predicate_two, either_predicate_i.next());
> +        assertEquals(false, either_predicate_i.hasNext());
>      }
>
>      public void testEitherPredicateEx() {
> @@ -425,6 +550,22 @@
>          fail();
>      }
>
> +    public void testEitherPredicateEx2() {
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate either_predicate =
PredicateUtils.eitherPredicate(predicate_one,predicate_two);
> +        Iterator either_predicate_i = ((CompoundPredicate)
either_predicate).predicates();
> +
> +        try {
> +            either_predicate_i.next( );
> +            either_predicate_i.remove( );
> +            fail("CompoundPredicate.predicates() should not return a
modifiable Iterator." );
> +        } catch(UnsupportedOperationException uoe) {
> +        }
> +
> +    }
> +
> +
>      // onePredicate
>      //------------------------------------------------------------------
>
> @@ -461,6 +602,19 @@
>          coll.add(PredicateUtils.falsePredicate());
>          coll.add(PredicateUtils.falsePredicate());
>          assertEquals(false,
PredicateUtils.onePredicate(coll).evaluate(null));
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate predicate_three = PredicateUtils.truePredicate();
> +        coll.clear();
> +        coll.add(predicate_one);
> +        coll.add(predicate_two);
> +        coll.add(predicate_three);
> +        Predicate one_predicate = PredicateUtils.onePredicate(coll);
> +        Iterator one_predicate_i = ((CompoundPredicate)
one_predicate).predicates();
> +        assertEquals(predicate_one, one_predicate_i.next());
> +        assertEquals(predicate_two, one_predicate_i.next());
> +        assertEquals(predicate_three, one_predicate_i.next());
> +        assertEquals(false, one_predicate_i.hasNext());
>      }
>
>      public void testOnePredicateEx1() {
> @@ -519,6 +673,25 @@
>          }
>          fail();
>      }
> +
> +    public void testOnePredicateEx7() {
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Collection coll = new ArrayList();
> +        coll.add( predicate_one );
> +        coll.add( predicate_two );
> +        Predicate one_predicate = PredicateUtils.onePredicate(coll);
> +        Iterator one_predicate_i = ((CompoundPredicate)
one_predicate).predicates();
> +
> +        try {
> +            one_predicate_i.next( );
> +            one_predicate_i.remove( );
> +            fail("CompoundPredicate.predicates() should not return a
modifiable Iterator." );
> +        } catch(UnsupportedOperationException uoe) {
> +        }
> +
> +    }
> +
>
>      // neitherPredicate
>      //------------------------------------------------------------------
> @@ -528,6 +701,13 @@
>          assertEquals(false,
PredicateUtils.neitherPredicate(PredicateUtils.truePredicate(),
PredicateUtils.falsePredicate()).evaluate(null));
>          assertEquals(false,
PredicateUtils.neitherPredicate(PredicateUtils.falsePredicate(),
PredicateUtils.truePredicate()).evaluate(null));
>          assertEquals(true,
PredicateUtils.neitherPredicate(PredicateUtils.falsePredicate(),
PredicateUtils.falsePredicate()).evaluate(null));
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate neither_predicate =
PredicateUtils.neitherPredicate(predicate_one,predicate_two);
> +        Iterator neither_predicate_i = ((CompoundPredicate)
neither_predicate).predicates();
> +        assertEquals(predicate_one, neither_predicate_i.next());
> +        assertEquals(predicate_two, neither_predicate_i.next());
> +        assertEquals(false, neither_predicate_i.hasNext());
>      }
>
>      public void testNeitherPredicateEx() {
> @@ -538,6 +718,22 @@
>          }
>          fail();
>      }
> +
> +    public void testNeitherPredicateEx2() {
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate neither_predicate =
PredicateUtils.neitherPredicate(predicate_one,predicate_two);
> +        Iterator neither_predicate_i = ((CompoundPredicate)
neither_predicate).predicates();
> +
> +        try {
> +            neither_predicate_i.next( );
> +            neither_predicate_i.remove( );
> +            fail("CompoundPredicate.predicates() should not return a
modifiable Iterator." );
> +        } catch(UnsupportedOperationException uoe) {
> +        }
> +
> +    }
> +
>
>      // nonePredicate
>      //------------------------------------------------------------------
> @@ -571,6 +767,19 @@
>          coll.add(PredicateUtils.falsePredicate());
>          coll.add(PredicateUtils.falsePredicate());
>          assertEquals(true,
PredicateUtils.nonePredicate(coll).evaluate(null));
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Predicate predicate_three = PredicateUtils.truePredicate();
> +        coll.clear();
> +        coll.add(predicate_one);
> +        coll.add(predicate_two);
> +        coll.add(predicate_three);
> +        Predicate none_predicate = PredicateUtils.nonePredicate(coll);
> +        Iterator none_predicate_i = ((CompoundPredicate)
none_predicate).predicates();
> +        assertEquals(predicate_one, none_predicate_i.next());
> +        assertEquals(predicate_two, none_predicate_i.next());
> +        assertEquals(predicate_three, none_predicate_i.next());
> +        assertEquals(false, none_predicate_i.hasNext());
>      }
>
>      public void testNonePredicateEx1() {
> @@ -629,6 +838,25 @@
>          }
>          fail();
>      }
> +
> +    public void testNonePredicateEx7() {
> +        Predicate predicate_one = PredicateUtils.truePredicate();
> +        Predicate predicate_two = PredicateUtils.falsePredicate();
> +        Collection coll = new ArrayList();
> +        coll.add( predicate_one );
> +        coll.add( predicate_two );
> +        Predicate none_predicate = PredicateUtils.nonePredicate(coll);
> +        Iterator none_predicate_i = ((CompoundPredicate)
none_predicate).predicates();
> +
> +        try {
> +            none_predicate_i.next( );
> +            none_predicate_i.remove( );
> +            fail("CompoundPredicate.predicates() should not return a
modifiable Iterator." );
> +        } catch(UnsupportedOperationException uoe) {
> +        }
> +
> +    }
> +
>
>      // instanceofPredicate
>      //------------------------------------------------------------------
> @@ -762,6 +990,9 @@
>
>      public void testNullIsExceptionPredicate() {
>          assertEquals(true,
PredicateUtils.nullIsExceptionPredicate(PredicateUtils.truePredicate()).eval
uate(new Object()));
> +        Predicate true_predicate = PredicateUtils.truePredicate();
> +        assertEquals(true_predicate, ((WrapperPredicate)
PredicateUtils.nullIsExceptionPredicate(true_predicate)).getPredicate());
> +
>          try {
>
PredicateUtils.nullIsExceptionPredicate(PredicateUtils.truePredicate()).eval
uate(null);
>          } catch (FunctorException ex) {
> @@ -786,6 +1017,9 @@
>          assertEquals(true,
PredicateUtils.nullIsTruePredicate(PredicateUtils.truePredicate()).evaluate(
null));
>          assertEquals(true,
PredicateUtils.nullIsTruePredicate(PredicateUtils.truePredicate()).evaluate(
new Object()));
>          assertEquals(false,
PredicateUtils.nullIsTruePredicate(PredicateUtils.falsePredicate()).evaluate
(new Object()));
> +        Predicate true_predicate = PredicateUtils.truePredicate();
> +        assertEquals(true_predicate, ((WrapperPredicate)
PredicateUtils.nullIsTruePredicate(true_predicate)).getPredicate());
> +
>      }
>
>      public void testNullIsTruePredicateEx1() {
> @@ -804,6 +1038,9 @@
>          assertEquals(false,
PredicateUtils.nullIsFalsePredicate(PredicateUtils.truePredicate()).evaluate
(null));
>          assertEquals(true,
PredicateUtils.nullIsFalsePredicate(PredicateUtils.truePredicate()).evaluate
(new Object()));
>          assertEquals(false,
PredicateUtils.nullIsFalsePredicate(PredicateUtils.falsePredicate()).evaluat
e(new Object()));
> +        Predicate true_predicate = PredicateUtils.truePredicate();
> +        assertEquals(true_predicate, ((WrapperPredicate)
PredicateUtils.nullIsFalsePredicate(true_predicate)).getPredicate());
> +
>      }
>
>      public void testNullIsFalsePredicateEx1() {
> @@ -833,6 +1070,10 @@
>              PredicateUtils.transformedPredicate(null, null);
>              fail();
>          } catch (IllegalArgumentException ex) {}
> +
> +        Predicate transformed_predicate =
PredicateUtils.transformedPredicate(t, p);
> +        assertEquals(p, ((WrapperPredicate)
transformed_predicate).getPredicate());
> +
>      }
>
>  }
>
>


----------------------------------------------------------------------------
----


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


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