commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Keith Bennett <>
Subject [collections] NegatingPredicate
Date Mon, 01 Dec 2003 22:59:48 GMT
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

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message