commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Keith Bennett <krbennet...@yahoo.com>
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
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


Mime
View raw message