You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Keith Bennett <kr...@yahoo.com> on 2003/12/01 23:59:48 UTC

[collections] NegatingPredicate

When using the Predicate interface, I thought it would be useful to be
able to negate a predicate rather than writing a new one with the
opposite behavior.  Soon afterwards, I encountered a real need for
such a thing, so I wrote it.  If you think this would be useful to add
to the library (as I do IMHO), feel free to do so, or let me know how
I can be of help in doing so.  Here it is:

-----

import org.apache.commons.collections.Predicate;

/**
 * Wraps a Predicate and reverses its return value.
 */
public class NegatingPredicate implements Predicate
{
    
    private Predicate predicate;

    /**
     * Creates an instance with the specified Predicate.
     *
     * @param p the predicate to wrap and reverse
     */
    NegatingPredicate(Predicate p) {
        predicate = p;
    }

    /**
     * Gets the result of the wrapped Predicate and negates it.
     *
     * @param o the object to evaluate
     */
    public boolean evaluate(Object o) {
        return ! predicate.evaluate(o);
    }
}

-----

- Keith Bennett


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

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


Re: [collections] NegatingPredicate

Posted by Stephen Colebourne <sc...@btopenworld.com>.
On the CVS you will find a whole package full of predicate implementations,
including this one - NotPredicate. Also accessible through PredicateUtils.

Still working towards a release....

Stephen

----- Original Message -----
From: "Keith Bennett" <kr...@yahoo.com>
> When using the Predicate interface, I thought it would be useful to be
> able to negate a predicate rather than writing a new one with the
> opposite behavior.  Soon afterwards, I encountered a real need for
> such a thing, so I wrote it.  If you think this would be useful to add
> to the library (as I do IMHO), feel free to do so, or let me know how
> I can be of help in doing so.  Here it is:
>
> -----
>
> import org.apache.commons.collections.Predicate;
>
> /**
>  * Wraps a Predicate and reverses its return value.
>  */
> public class NegatingPredicate implements Predicate
> {
>
>     private Predicate predicate;
>
>     /**
>      * Creates an instance with the specified Predicate.
>      *
>      * @param p the predicate to wrap and reverse
>      */
>     NegatingPredicate(Predicate p) {
>         predicate = p;
>     }
>
>     /**
>      * Gets the result of the wrapped Predicate and negates it.
>      *
>      * @param o the object to evaluate
>      */
>     public boolean evaluate(Object o) {
>         return ! predicate.evaluate(o);
>     }
> }
>
> -----
>
> - Keith Bennett
>
>
> __________________________________
> Do you Yahoo!?
> Free Pop-Up Blocker - Get it now
> http://companion.yahoo.com/
>
> ---------------------------------------------------------------------
> 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