You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2002/06/29 14:50:19 UTC
cvs commit: jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/predicate Predicate.java PredicateException.java PredicateUtils.java
scolebourne 2002/06/29 05:50:19
Added: pattern/src/java/org/apache/commons/pattern/predicate
Predicate.java PredicateException.java
PredicateUtils.java
Log:
Initial checkin
Revision Changes Path
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/predicate/Predicate.java
Index: Predicate.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements 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
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.predicate;
/**
* <code>Predicate</code> defines an interface implemented by classes that
* perform a predicate test on an object. Predicate instances can be used
* to implement queries or to do filtering.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: Predicate.java,v 1.1 2002/06/29 12:50:19 scolebourne Exp $
*/
public interface Predicate {
/**
* Use the specified parameter to perform a test that returns true or false.
*
* @return true or false
* @throws PredicateException (runtime) if the predicate encounters a problem
*/
public boolean evaluate(Object object);
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/predicate/PredicateException.java
Index: PredicateException.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements 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
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.predicate;
import org.apache.commons.lang.exception.NestableRuntimeException;
/**
* Exception thrown when the a Predicate encounters a problem.
* If required, a root cause error can be wrapped within this one.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: PredicateException.java,v 1.1 2002/06/29 12:50:19 scolebourne Exp $
*/
public class PredicateException extends NestableRuntimeException {
/**
* Constructs a new <code>PredicateException</code> without specified
* detail message.
*/
public PredicateException() {
super();
}
/**
* Constructs a new <code>PredicateException</code> with specified
* detail message.
*
* @param msg the error message.
*/
public PredicateException(String msg) {
super(msg);
}
/**
* Constructs a new <code>PredicateException</code> with specified
* nested <code>Throwable</code> root cause.
*
* @param rootCause the exception or error that caused this exception
* to be thrown.
*/
public PredicateException(Throwable rootCause) {
super(rootCause);
}
/**
* Constructs a new <code>PredicateException</code> with specified
* detail message and nested <code>Throwable</code> root cause.
*
* @param msg the error message.
* @param rootCause the exception or error that caused this exception
* to be thrown.
*/
public PredicateException(String msg, Throwable rootCause) {
super(msg, rootCause);
}
}
1.1 jakarta-commons-sandbox/pattern/src/java/org/apache/commons/pattern/predicate/PredicateUtils.java
Index: PredicateUtils.java
===================================================================
/* =========/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002 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 acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements 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
* <http://www.apache.org/>.
*/
package org.apache.commons.pattern.predicate;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* <code>PredicateUtils</code> provides reference implementations and utilities
* for the Predicate pattern interface. The supplied predicates are:
* <ul>
* <li>True - always return true
* <li>False - always return false
* <li>And/All - true if all of the predicates are true
* <li>Or/Any - true if any of the predicates is true
* <li>Either/One - true if only one of the predicate is true
* <li>Neither/None - true if none of the predicates are true
* <li>Not - true if the predicate is false, and vice versa
* <li>InstanceOf - true if the object is an instanceof a class
* <li>Equal - true if the object equals() a specified object
* <li>Identity - true if the object == a specified object
* <li>Null - true if the object is null
* <li>NotNull - true if the object is not null
* <li>Unique - true if the object has not already been evaluated
* </ul>
* All the supplied predicates are Serializable.
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: PredicateUtils.java,v 1.1 2002/06/29 12:50:19 scolebourne Exp $
*/
public class PredicateUtils {
/**
* A predicate that always returns true
*/
public static final Predicate TRUE_PREDICATE = new ConstantPredicate(true);
/**
* A predicate that always returns false
*/
public static final Predicate FALSE_PREDICATE = new ConstantPredicate(false);
/**
* A predicate that returns true if the object is null
*/
public static final Predicate NULL_PREDICATE = new IdentityPredicate(null);
/**
* A predicate that returns true if the object is not null
*/
public static final Predicate NOT_NULL_PREDICATE = new NotPredicate(NULL_PREDICATE);
/**
* Restructive constructor
*/
private PredicateUtils() {
super();
}
/**
* Gets a predicate that always returns true.
*
* @return the predicate
*/
public static Predicate truePredicate() {
return TRUE_PREDICATE;
}
/**
* Gets a predicate that always returns false.
*
* @return the predicate
*/
public static Predicate falsePredicate() {
return FALSE_PREDICATE;
}
/**
* Gets a predicate that checks if the object passed in is null.
*
* @return the predicate
*/
public static Predicate nullPredicate() {
return NULL_PREDICATE;
}
/**
* Gets a predicate that checks if the object passed in is not null.
*
* @return the predicate
*/
public static Predicate notNullPredicate() {
return NOT_NULL_PREDICATE;
}
/**
* Create a predicate that checks if the object passed in equals to the
* specified object using equals().
*
* @param value the value to compare against
* @return the predicate
*/
public static Predicate equalPredicate(Object value) {
if (value == null) {
return NULL_PREDICATE;
}
return new EqualPredicate(value);
}
/**
* Create a predicate that checks if the object passed in equals to the
* specified object by identity.
*
* @param value the value to compare against
* @return the predicate
*/
public static Predicate identityPredicate(Object value) {
if (value == null) {
return NULL_PREDICATE;
}
return new IdentityPredicate(value);
}
/**
* Create a new predicate that returns true only if both of the specified
* predicates are true.
*
* @param predicate1 the first predicate
* @param predicate2 the second predicate
* @return the predicate
* @throws IllegalArgumentException if either predicate is null
*/
public static Predicate andPredicate(Predicate predicate1, Predicate predicate2) {
return allPredicate(new Predicate[] { predicate1, predicate2 });
}
/**
* Create a new predicate that returns true only if all of the specified
* predicates are true.
*
* @param predicates an array of predicates to check
* @return the predicate
* @throws IllegalArgumentException if the predicates array is null
* @throws IllegalArgumentException if the predicates array has less than 2 elements
* @throws IllegalArgumentException if any predicate in the array is null
*/
public static Predicate allPredicate(Predicate[] predicates) {
validate(predicates);
return new AllPredicate(predicates);
}
/**
* Create a new predicate that returns true if either of the specified
* predicates are true.
*
* @param predicate1 the first predicate
* @param predicate2 the second predicate
* @return the predicate
* @throws IllegalArgumentException if either predicate is null
*/
public static Predicate orPredicate(Predicate predicate1, Predicate predicate2) {
return anyPredicate(new Predicate[] { predicate1, predicate2 });
}
/**
* Create a new predicate that returns true if any of the specified
* predicates are true.
*
* @param predicates an array of predicates to check
* @return the predicate
* @throws IllegalArgumentException if the predicates array is null
* @throws IllegalArgumentException if the predicates array has less than 2 elements
* @throws IllegalArgumentException if any predicate in the array is null
*/
public static Predicate anyPredicate(Predicate[] predicates) {
validate(predicates);
return new AnyPredicate(predicates);
}
/**
* Create a new predicate that returns true if one, but not both, of the
* specified predicates are true.
*
* @param predicate1 the first predicate
* @param predicate2 the second predicate
* @return the predicate
* @throws IllegalArgumentException if either predicate is null
*/
public static Predicate eitherPredicate(Predicate predicate1, Predicate predicate2) {
return onePredicate(new Predicate[] { predicate1, predicate2 });
}
/**
* Create a new predicate that returns true if only one of the specified
* predicates are true.
*
* @param predicates an array of predicates to check
* @return the predicate
* @throws IllegalArgumentException if the predicates array is null
* @throws IllegalArgumentException if the predicates array has less than 2 elements
* @throws IllegalArgumentException if any predicate in the array is null
*/
public static Predicate onePredicate(Predicate[] predicates) {
validate(predicates);
return new OnePredicate(predicates);
}
/**
* Create a new predicate that returns true if neither of the specified
* predicates are true.
*
* @param predicate1 the first predicate
* @param predicate2 the second predicate
* @return the predicate
* @throws IllegalArgumentException if either predicate is null
*/
public static Predicate neitherPredicate(Predicate predicate1, Predicate predicate2) {
return nonePredicate(new Predicate[] { predicate1, predicate2 });
}
/**
* Create a new predicate that returns true if none of the specified
* predicates are true.
*
* @param predicates an array of predicates to check
* @return the predicate
* @throws IllegalArgumentException if the predicates array is null
* @throws IllegalArgumentException if the predicates array has less than 2 elements
* @throws IllegalArgumentException if any predicate in the array is null
*/
public static Predicate nonePredicate(Predicate[] predicates) {
validate(predicates);
for (int i = 0; i < predicates.length; i++) {
predicates[i] = notPredicate(predicates[i]);
}
return new AllPredicate(predicates);
}
/**
* Create a new predicate that returns true if the specified predicate
* returns false and vice versa.
*
* @param predicate the predicate to not
* @return the predicate
* @throws IllegalArgumentException if the predicate is null
*/
public static Predicate notPredicate(Predicate predicate) {
if (predicate == null) {
throw new IllegalArgumentException("Predicate: The predicate must not be null");
}
return new NotPredicate(predicate);
}
/**
* Create a new predicate that checks if the object passed in is of
* a particular type, using instanceof.
*
* @param type the type to check for
* @return the predicate
* @throws IllegalArgumentException if the class is null
*/
public static Predicate instanceofPredicate(Class type) {
if (type == null) {
throw new IllegalArgumentException("Predicate: The type to check instanceof must not be null");
}
return new InstanceofPredicate(type);
}
/**
* Gets a predicate that returns true the first time an object is
* encoutered, and false if the same object is received
* again. The comparison is by equals().
*
* @return the predicate
*/
public static Predicate uniquePredicate() {
return new UniquePredicate();
}
/**
* Validate method shared amongst predicate implementations
*
* @param predicates the predicates to validate
*/
private static void validate(Predicate[] predicates) {
if (predicates == null) {
throw new IllegalArgumentException("Predicate: The predicate array must not be null");
}
if (predicates.length < 2) {
throw new IllegalArgumentException(
"Predicate: At least 2 predicates must be specified in the predicate array, size was "
+ predicates.length);
}
for (int i = 0; i < predicates.length; i++) {
if (predicates[i] == null) {
throw new IllegalArgumentException(
"Predicate: The predicate array must not contain a null predicate, index "
+ i
+ " was null");
}
}
}
// ConstantPredicate
//----------------------------------------------------------------------------------
/**
* ConstantPredicate returns the same instance each time.
*/
private static class ConstantPredicate implements Predicate, Serializable {
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;
}
}
// AndPredicate
//----------------------------------------------------------------------------------
/**
* AndPredicate returns true if all predicates return true
*/
private static class AllPredicate implements Predicate, Serializable {
private final Predicate[] iPredicates;
/**
* Constructor
*/
private AllPredicate(Predicate[] predicates) {
super();
iPredicates = predicates;
}
/**
* Return true if all predicates return true
*/
public boolean evaluate(Object object) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(object) == false) {
return false;
}
}
return true;
}
}
// OrPredicate
//----------------------------------------------------------------------------------
/**
* OrPredicate returns true if one of the predicates return true
*/
private static class AnyPredicate implements Predicate, Serializable {
private final Predicate[] iPredicates;
/**
* Constructor
*/
private AnyPredicate(Predicate[] predicates) {
super();
iPredicates = predicates;
}
/**
* Return true if one of the predicates returns true
*/
public boolean evaluate(Object object) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(object)) {
return true;
}
}
return false;
}
}
// OnePredicate
//----------------------------------------------------------------------------------
/**
* OnePredicate returns true if only one of the predicates return true
*/
private static class OnePredicate implements Predicate, Serializable {
private final Predicate[] iPredicates;
/**
* Constructor
*/
private OnePredicate(Predicate[] predicates) {
super();
iPredicates = predicates;
}
/**
* Return true if only one of the predicates returns true
*/
public boolean evaluate(Object object) {
boolean match = false;
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(object)) {
if (match) {
return false;
}
match = true;
}
}
return match;
}
}
// NotPredicate
//----------------------------------------------------------------------------------
/**
* NotPredicate returns the opposite of the wrapped predicate
*/
private static class NotPredicate implements Predicate, Serializable {
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 {
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 {
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 {
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
//----------------------------------------------------------------------------------
/**
* UniquePredicate returns true the first time an object is
* encoutered, and false if the same object is received
* again using equals().
*/
private static class UniquePredicate implements Predicate, Serializable {
private final Set iSet = new HashSet();
/**
* Constructor
*/
public UniquePredicate() {
super();
}
/**
* Return true the first time, and false subsequant times
* that an object is encountered, using equals().
*/
public boolean evaluate(Object object) {
return iSet.add(object);
}
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>