diff --git a/src/java/org/apache/commons/collections/PredicateUtils.java b/src/java/org/apache/commons/collections/PredicateUtils.java index cc197c7a6..5c32b3f8a 100644 --- a/src/java/org/apache/commons/collections/PredicateUtils.java +++ b/src/java/org/apache/commons/collections/PredicateUtils.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/PredicateUtils.java,v 1.12 2003/11/23 17:01:36 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/PredicateUtils.java,v 1.13 2003/11/23 19:11:21 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -63,8 +63,19 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Set; +import org.apache.commons.collections.functors.EqualPredicate; import org.apache.commons.collections.functors.ExceptionPredicate; +import org.apache.commons.collections.functors.FalsePredicate; import org.apache.commons.collections.functors.FunctorException; +import org.apache.commons.collections.functors.IdentityPredicate; +import org.apache.commons.collections.functors.InstanceofPredicate; +import org.apache.commons.collections.functors.NotNullPredicate; +import org.apache.commons.collections.functors.NotPredicate; +import org.apache.commons.collections.functors.NullIsExceptionPredicate; +import org.apache.commons.collections.functors.NullIsFalsePredicate; +import org.apache.commons.collections.functors.NullIsTruePredicate; +import org.apache.commons.collections.functors.NullPredicate; +import org.apache.commons.collections.functors.TruePredicate; /** * PredicateUtils provides reference implementations and utilities @@ -91,30 +102,13 @@ import org.apache.commons.collections.functors.FunctorException; * All the supplied predicates are Serializable. * * @since Commons Collections 3.0 - * @version $Revision: 1.12 $ $Date: 2003/11/23 17:01:36 $ + * @version $Revision: 1.13 $ $Date: 2003/11/23 19:11:21 $ * * @author Stephen Colebourne * @author Ola Berg */ public class PredicateUtils { - /** - * A predicate that always returns true - */ - private static final Predicate TRUE_PREDICATE = new ConstantPredicate(true); - /** - * A predicate that always returns false - */ - private static final Predicate FALSE_PREDICATE = new ConstantPredicate(false); - /** - * A predicate that returns true if the object is null - */ - private static final Predicate NULL_PREDICATE = new IdentityPredicate(null); - /** - * A predicate that returns true if the object is not null - */ - private static final Predicate NOT_NULL_PREDICATE = new NotPredicate(NULL_PREDICATE); - /** * This class is not normally instantiated. */ @@ -141,7 +135,7 @@ public class PredicateUtils { * @return the predicate */ public static Predicate truePredicate() { - return TRUE_PREDICATE; + return TruePredicate.INSTANCE; } /** @@ -150,7 +144,7 @@ public class PredicateUtils { * @return the predicate */ public static Predicate falsePredicate() { - return FALSE_PREDICATE; + return FalsePredicate.INSTANCE; } /** @@ -159,7 +153,7 @@ public class PredicateUtils { * @return the predicate */ public static Predicate nullPredicate() { - return NULL_PREDICATE; + return NullPredicate.INSTANCE; } /** @@ -168,7 +162,7 @@ public class PredicateUtils { * @return the predicate */ public static Predicate notNullPredicate() { - return NOT_NULL_PREDICATE; + return NotNullPredicate.INSTANCE; } /** @@ -179,10 +173,7 @@ public class PredicateUtils { * @return the predicate */ public static Predicate equalPredicate(Object value) { - if (value == null) { - return NULL_PREDICATE; - } - return new EqualPredicate(value); + return EqualPredicate.getInstance(value); } /** @@ -193,10 +184,7 @@ public class PredicateUtils { * @return the predicate */ public static Predicate identityPredicate(Object value) { - if (value == null) { - return NULL_PREDICATE; - } - return new IdentityPredicate(value); + return IdentityPredicate.getInstance(value); } /** @@ -209,10 +197,7 @@ public class PredicateUtils { * @throws IllegalArgumentException if the class is null */ public static Predicate instanceofPredicate(Class type) { - if (type == null) { - throw new IllegalArgumentException("The type to check instanceof must not be null"); - } - return new InstanceofPredicate(type); + return InstanceofPredicate.getInstance(type); } /** @@ -454,10 +439,7 @@ public class PredicateUtils { * @throws IllegalArgumentException if the predicate is null */ public static Predicate notPredicate(Predicate predicate) { - if (predicate == null) { - throw new IllegalArgumentException("The predicate must not be null"); - } - return new NotPredicate(predicate); + return NotPredicate.getInstance(predicate); } // Adaptors @@ -492,10 +474,7 @@ public class PredicateUtils { * @throws IllegalArgumentException if the predicate is null. */ public static Predicate nullIsExceptionPredicate(Predicate predicate){ - if (predicate == null) { - throw new IllegalArgumentException("The predicate must not be null"); - } - return new NullIsExceptionPredicate( predicate); + return NullIsExceptionPredicate.getInstance(predicate); } /** @@ -508,10 +487,7 @@ public class PredicateUtils { * @throws IllegalArgumentException if the predicate is null. */ public static Predicate nullIsFalsePredicate(Predicate predicate){ - if (predicate == null) { - throw new IllegalArgumentException("The predicate must not be null"); - } - return new NullIsFalsePredicate(predicate); + return NullIsFalsePredicate.getInstance(predicate); } /** @@ -524,10 +500,7 @@ public class PredicateUtils { * @throws IllegalArgumentException if the predicate is null. */ public static Predicate nullIsTruePredicate(Predicate predicate){ - if (predicate == null) { - throw new IllegalArgumentException("The predicate must not be null"); - } - return new NullIsTruePredicate(predicate); + return NullIsTruePredicate.getInstance(predicate); } /** @@ -581,32 +554,6 @@ public class PredicateUtils { return preds; } - // ConstantPredicate - //---------------------------------------------------------------------------------- - - /** - * ConstantPredicate returns the same instance each time. - */ - private static class ConstantPredicate implements Predicate, Serializable { - /** The constant value to return each time */ - private final boolean iConstant; - - /** - * Constructor to store constant - */ - private ConstantPredicate(boolean constant) { - super(); - iConstant = constant; - } - - /** - * Always return constant - */ - public boolean evaluate(Object object) { - return iConstant; - } - } - // AllPredicate //---------------------------------------------------------------------------------- @@ -704,110 +651,6 @@ public class PredicateUtils { } } - // NotPredicate - //---------------------------------------------------------------------------------- - - /** - * NotPredicate returns the opposite of the wrapped predicate - */ - private static class NotPredicate implements Predicate, Serializable { - /** The predicate to call */ - private final Predicate iPredicate; - - /** - * Constructor - */ - private NotPredicate(Predicate predicate) { - super(); - iPredicate = predicate; - } - - /** - * Return true if the wrapped predicate returns false, and vice versa - */ - public boolean evaluate(Object object) { - return !iPredicate.evaluate(object); - } - } - - // InstanceofPredicate - //---------------------------------------------------------------------------------- - - /** - * InstanceofPredicate checks the type of an object - */ - private static class InstanceofPredicate implements Predicate, Serializable { - /** The type to check for */ - private final Class iType; - - /** - * Constructor - */ - public InstanceofPredicate(Class type) { - super(); - iType = type; - } - - /** - * Return true if the object is an instanceof the type of the predicate. - */ - public boolean evaluate(Object object) { - return iType.isInstance(object); - } - } - - // EqualPredicate - //---------------------------------------------------------------------------------- - - /** - * EqualPredicate that checks if the object is a particular value by equals(). - */ - private static class EqualPredicate implements Predicate, Serializable { - /** The object to compare to */ - private final Object iValue; - - /** - * Constructor - */ - public EqualPredicate(Object value) { - super(); - iValue = value; - } - - /** - * Return true if the object is equals() to the value stored in the predicate. - */ - public boolean evaluate(Object object) { - return iValue.equals(object); - } - } - - // IdentityPredicate - //---------------------------------------------------------------------------------- - - /** - * IdentityPredicate that checks if the object is a particular value by identity. - */ - private static class IdentityPredicate implements Predicate, Serializable { - /** The object to compare identity to */ - private final Object iValue; - - /** - * Constructor - */ - public IdentityPredicate(Object value) { - super(); - iValue = value; - } - - /** - * Return true if the object is equals() to the value stored in the predicate. - */ - public boolean evaluate(Object object) { - return iValue == object; - } - } - // UniquePredicate //---------------------------------------------------------------------------------- @@ -868,91 +711,4 @@ public class PredicateUtils { } } - // NullIsExceptionPredicate - //---------------------------------------------------------------------------------- - - /** - * NullIsExceptionPredicate returns an exception if null is passed in. - */ - private static class NullIsExceptionPredicate implements Predicate, Serializable { - /** The predicate to call */ - private final Predicate iPredicate; - - /** - * Constructor - */ - private NullIsExceptionPredicate(Predicate predicate){ - super(); - iPredicate = predicate; - } - - /** - * Return an exception if null - */ - public boolean evaluate(Object object){ - if (object == null) { - throw new FunctorException("NullIsExceptionPredicate: Input Object must not be null"); - } - return iPredicate.evaluate(object); - } - } - - // NullIsFalsePredicate - //---------------------------------------------------------------------------------- - - /** - * NullIsFalsePredicate returns false if null is passed in. - */ - private static class NullIsFalsePredicate implements Predicate, Serializable { - /** The predicate to call */ - private final Predicate iPredicate; - - /** - * Constructor - */ - private NullIsFalsePredicate(Predicate predicate){ - super(); - iPredicate = predicate; - } - - /** - * Return false if null - */ - public boolean evaluate(Object object){ - if (object == null) { - return false; - } - return iPredicate.evaluate(object); - } - } - - // NullIsTruePredicate - //---------------------------------------------------------------------------------- - - /** - * NullIsTruePredicate returns true if null is passed in. - */ - private static class NullIsTruePredicate implements Predicate, Serializable { - /** The predicate to call */ - private final Predicate iPredicate; - - /** - * Constructor - */ - private NullIsTruePredicate(Predicate predicate){ - super(); - iPredicate = predicate; - } - - /** - * Return true if null - */ - public boolean evaluate(Object object){ - if (object == null) { - return true; - } - return iPredicate.evaluate(object); - } - } - } diff --git a/src/java/org/apache/commons/collections/functors/EqualPredicate.java b/src/java/org/apache/commons/collections/functors/EqualPredicate.java new file mode 100644 index 000000000..fb4190148 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/EqualPredicate.java @@ -0,0 +1,113 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/EqualPredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that returns true if the input is the same object + * as the one stored in this predicate by equals. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class EqualPredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 5633766978029907089L; + + /** The value to compare to */ + private final Object iValue; + + /** + * Factory to create the identity predicate. + * + * @param object the object to compare to + * @return the predicate + * @throws IllegalArgumentException if the predicate is null + */ + public static Predicate getInstance(Object object) { + if (object == null) { + return NullPredicate.INSTANCE; + } + return new EqualPredicate(object); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param object the object to compare to + */ + public EqualPredicate(Object object) { + super(); + iValue = object; + } + + /** + * Return the predicate result. + */ + public boolean evaluate(Object object) { + return (iValue.equals(object)); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/FalsePredicate.java b/src/java/org/apache/commons/collections/functors/FalsePredicate.java new file mode 100644 index 000000000..1a44e2d5c --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/FalsePredicate.java @@ -0,0 +1,94 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/FalsePredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that always returns false. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class FalsePredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 7533784454832764388L; + + /** Singleton predicate instance */ + public static final Predicate INSTANCE = new FalsePredicate(); + + /** + * Restricted constructor. + */ + private FalsePredicate() { + super(); + } + + /** + * Always return true. + */ + public boolean evaluate(Object object) { + return false; + } + +} diff --git a/src/java/org/apache/commons/collections/functors/IdentityPredicate.java b/src/java/org/apache/commons/collections/functors/IdentityPredicate.java new file mode 100644 index 000000000..1e1d4cfb4 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/IdentityPredicate.java @@ -0,0 +1,114 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/IdentityPredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that returns true if the input is the same object + * as the one stored in this predicate. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class IdentityPredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = -89901658494523293L; + + + /** The value to compare to */ + private final Object iValue; + + /** + * Factory to create the identity predicate. + * + * @param object the object to compare to + * @return the predicate + * @throws IllegalArgumentException if the predicate is null + */ + public static Predicate getInstance(Object object) { + if (object == null) { + return NullPredicate.INSTANCE; + } + return new IdentityPredicate(object); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param object the object to compare to + */ + public IdentityPredicate(Object object) { + super(); + iValue = object; + } + + /** + * Return the predicate result. + */ + public boolean evaluate(Object object) { + return (iValue == object); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/InstanceofPredicate.java b/src/java/org/apache/commons/collections/functors/InstanceofPredicate.java new file mode 100644 index 000000000..1d29b837c --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/InstanceofPredicate.java @@ -0,0 +1,113 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/InstanceofPredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that returns true if the input is an instanceof + * the type stored in this predicate. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class InstanceofPredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = -6682656911025165584L; + + /** The type to compare to */ + private final Class iType; + + /** + * Factory to create the identity predicate. + * + * @param type the type to check for, may not be null + * @return the predicate + * @throws IllegalArgumentException if the class is null + */ + public static Predicate getInstance(Class type) { + if (type == null) { + throw new IllegalArgumentException("The type to check instanceof must not be null"); + } + return new InstanceofPredicate(type); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param object the object to compare to + */ + public InstanceofPredicate(Class type) { + super(); + iType = type; + } + + /** + * Return the predicate result. + */ + public boolean evaluate(Object object) { + return (iType.isInstance(object)); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/NotNullPredicate.java b/src/java/org/apache/commons/collections/functors/NotNullPredicate.java new file mode 100644 index 000000000..02f1b5285 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/NotNullPredicate.java @@ -0,0 +1,94 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/NotNullPredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that returns true if the input is not null. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class NotNullPredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 7533784454832764388L; + + /** Singleton predicate instance */ + public static final Predicate INSTANCE = new NotNullPredicate(); + + /** + * Restricted constructor. + */ + private NotNullPredicate() { + super(); + } + + /** + * Return true if the object equals null. + */ + public boolean evaluate(Object object) { + return (object != null); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/NotPredicate.java b/src/java/org/apache/commons/collections/functors/NotPredicate.java new file mode 100644 index 000000000..8f75e35ef --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/NotPredicate.java @@ -0,0 +1,112 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/NotPredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that returns the opposite of the decorated predicate. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class NotPredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = -2654603322338049674L; + + /** The predicate to decorate */ + private final Predicate iPredicate; + + /** + * Factory to create the not predicate. + * + * @param predicate the predicate to decorate, not null + * @return the predicate + * @throws IllegalArgumentException if the predicate is null + */ + public static Predicate getInstance(Predicate predicate) { + if (predicate == null) { + throw new IllegalArgumentException("Predicate must not be null"); + } + return new NotPredicate(predicate); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param predicate the predicate to call after the null check + */ + public NotPredicate(Predicate predicate) { + super(); + iPredicate = predicate; + } + + /** + * Return the negated predicate result. + */ + public boolean evaluate(Object object) { + return !(iPredicate.evaluate(object)); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java b/src/java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java new file mode 100644 index 000000000..60551e768 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java @@ -0,0 +1,115 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/NullIsExceptionPredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +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.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class NullIsExceptionPredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 3243449850504576071L; + + /** The predicate to decorate */ + private final Predicate iPredicate; + + /** + * Factory to create the null exception predicate. + * + * @param predicate the predicate to decorate, not null + * @return the predicate + * @throws IllegalArgumentException if the predicate is null + */ + public static Predicate getInstance(Predicate predicate) { + if (predicate == null) { + throw new IllegalArgumentException("Predicate must not be null"); + } + return new NullIsExceptionPredicate(predicate); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param predicate the predicate to call after the null check + */ + public NullIsExceptionPredicate(Predicate predicate) { + super(); + iPredicate = predicate; + } + + /** + * Return true if the object equals null else call the decorated predicate. + */ + public boolean evaluate(Object object) { + if (object == null) { + throw new FunctorException("Input Object must not be null"); + } + return iPredicate.evaluate(object); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/NullIsFalsePredicate.java b/src/java/org/apache/commons/collections/functors/NullIsFalsePredicate.java new file mode 100644 index 000000000..c9a0874b9 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/NullIsFalsePredicate.java @@ -0,0 +1,115 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/NullIsFalsePredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that returns false if the input is null. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class NullIsFalsePredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = -2997501534564735525L; + + /** The predicate to decorate */ + private final Predicate iPredicate; + + /** + * Factory to create the null false predicate. + * + * @param predicate the predicate to decorate, not null + * @return the predicate + * @throws IllegalArgumentException if the predicate is null + */ + public static Predicate getInstance(Predicate predicate) { + if (predicate == null) { + throw new IllegalArgumentException("Predicate must not be null"); + } + return new NullIsFalsePredicate(predicate); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param predicate the predicate to call after the null check + */ + public NullIsFalsePredicate(Predicate predicate) { + super(); + iPredicate = predicate; + } + + /** + * Return false if the object equals null else call the decorated predicate. + */ + public boolean evaluate(Object object) { + if (object == null) { + return false; + } + return iPredicate.evaluate(object); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/NullIsTruePredicate.java b/src/java/org/apache/commons/collections/functors/NullIsTruePredicate.java new file mode 100644 index 000000000..337f85ccc --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/NullIsTruePredicate.java @@ -0,0 +1,115 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/NullIsTruePredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that returns true if the input is null. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class NullIsTruePredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = -7625133768987126273L; + + /** The predicate to decorate */ + private final Predicate iPredicate; + + /** + * Factory to create the null true predicate. + * + * @param predicate the predicate to decorate, not null + * @return the predicate + * @throws IllegalArgumentException if the predicate is null + */ + public static Predicate getInstance(Predicate predicate) { + if (predicate == null) { + throw new IllegalArgumentException("Predicate must not be null"); + } + return new NullIsTruePredicate(predicate); + } + + /** + * Constructor that performs no validation. + * Use getInstance if you want that. + * + * @param predicate the predicate to call after the null check + */ + public NullIsTruePredicate(Predicate predicate) { + super(); + iPredicate = predicate; + } + + /** + * Return true if the object equals null else call the decorated predicate. + */ + public boolean evaluate(Object object) { + if (object == null) { + return true; + } + return iPredicate.evaluate(object); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/NullPredicate.java b/src/java/org/apache/commons/collections/functors/NullPredicate.java new file mode 100644 index 000000000..0f8624896 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/NullPredicate.java @@ -0,0 +1,94 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/NullPredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that returns true if the input is null. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class NullPredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 7533784454832764388L; + + /** Singleton predicate instance */ + public static final Predicate INSTANCE = new NullPredicate(); + + /** + * Restricted constructor. + */ + private NullPredicate() { + super(); + } + + /** + * Return true if the object equals null. + */ + public boolean evaluate(Object object) { + return (object == null); + } + +} diff --git a/src/java/org/apache/commons/collections/functors/TruePredicate.java b/src/java/org/apache/commons/collections/functors/TruePredicate.java new file mode 100644 index 000000000..968634829 --- /dev/null +++ b/src/java/org/apache/commons/collections/functors/TruePredicate.java @@ -0,0 +1,94 @@ +/* + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/TruePredicate.java,v 1.1 2003/11/23 19:11:21 scolebourne Exp $ + * ==================================================================== + * + * The Apache Software License, Version 1.1 + * + * Copyright (c) 2001-2003 The Apache Software Foundation. All rights + * reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. The end-user documentation included with the redistribution, if + * any, must include the following acknowledgement: + * "This product includes software developed by the + * Apache Software Foundation (http://www.apache.org/)." + * Alternately, this acknowledgement may appear in the software itself, + * if and wherever such third-party acknowledgements normally appear. + * + * 4. The names "The Jakarta Project", "Commons", and "Apache Software + * Foundation" must not be used to endorse or promote products derived + * from this software without prior written permission. For written + * permission, please contact apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Software Foundation. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT + * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.commons.collections.functors; + +import java.io.Serializable; + +import org.apache.commons.collections.Predicate; + +/** + * Predicate implementation that always returns true. + * + * @since Commons Collections 3.0 + * @version $Revision: 1.1 $ $Date: 2003/11/23 19:11:21 $ + * + * @author Stephen Colebourne + */ +public final class TruePredicate implements Predicate, Serializable { + + /** Serial version UID */ + static final long serialVersionUID = 3374767158756189740L; + + /** Singleton predicate instance */ + public static final Predicate INSTANCE = new TruePredicate(); + + /** + * Restricted constructor. + */ + private TruePredicate() { + super(); + } + + /** + * Always return true. + */ + public boolean evaluate(Object object) { + return true; + } + +}