Refactor functors from inner classes to subpackage

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131370 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-11-23 22:05:24 +00:00
parent 29e4020d3f
commit ab2b44981c
10 changed files with 1069 additions and 240 deletions

View File

@ -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.13 2003/11/23 19:11:21 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.14 2003/11/23 22:05:24 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -57,25 +57,28 @@
*/ */
package org.apache.commons.collections; package org.apache.commons.collections;
import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.commons.collections.functors.AllPredicate;
import org.apache.commons.collections.functors.AndPredicate;
import org.apache.commons.collections.functors.AnyPredicate;
import org.apache.commons.collections.functors.EqualPredicate; import org.apache.commons.collections.functors.EqualPredicate;
import org.apache.commons.collections.functors.ExceptionPredicate; import org.apache.commons.collections.functors.ExceptionPredicate;
import org.apache.commons.collections.functors.FalsePredicate; 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.IdentityPredicate;
import org.apache.commons.collections.functors.InstanceofPredicate; import org.apache.commons.collections.functors.InstanceofPredicate;
import org.apache.commons.collections.functors.NonePredicate;
import org.apache.commons.collections.functors.NotNullPredicate; import org.apache.commons.collections.functors.NotNullPredicate;
import org.apache.commons.collections.functors.NotPredicate; import org.apache.commons.collections.functors.NotPredicate;
import org.apache.commons.collections.functors.NullIsExceptionPredicate; import org.apache.commons.collections.functors.NullIsExceptionPredicate;
import org.apache.commons.collections.functors.NullIsFalsePredicate; import org.apache.commons.collections.functors.NullIsFalsePredicate;
import org.apache.commons.collections.functors.NullIsTruePredicate; import org.apache.commons.collections.functors.NullIsTruePredicate;
import org.apache.commons.collections.functors.NullPredicate; import org.apache.commons.collections.functors.NullPredicate;
import org.apache.commons.collections.functors.OnePredicate;
import org.apache.commons.collections.functors.OrPredicate;
import org.apache.commons.collections.functors.TransformerPredicate;
import org.apache.commons.collections.functors.TruePredicate; import org.apache.commons.collections.functors.TruePredicate;
import org.apache.commons.collections.functors.UniquePredicate;
/** /**
* <code>PredicateUtils</code> provides reference implementations and utilities * <code>PredicateUtils</code> provides reference implementations and utilities
@ -102,7 +105,7 @@ import org.apache.commons.collections.functors.TruePredicate;
* All the supplied predicates are Serializable. * All the supplied predicates are Serializable.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.13 $ $Date: 2003/11/23 19:11:21 $ * @version $Revision: 1.14 $ $Date: 2003/11/23 22:05:24 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
* @author Ola Berg * @author Ola Berg
@ -211,7 +214,7 @@ public class PredicateUtils {
*/ */
public static Predicate uniquePredicate() { public static Predicate uniquePredicate() {
// must return new instance each time // must return new instance each time
return new UniquePredicate(); return UniquePredicate.getInstance();
} }
/** /**
@ -268,7 +271,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if either predicate is null * @throws IllegalArgumentException if either predicate is null
*/ */
public static Predicate andPredicate(Predicate predicate1, Predicate predicate2) { public static Predicate andPredicate(Predicate predicate1, Predicate predicate2) {
return allPredicate(new Predicate[] { predicate1, predicate2 }); return AndPredicate.getInstance(predicate1, predicate2);
} }
/** /**
@ -282,7 +285,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if any predicate in the array is null * @throws IllegalArgumentException if any predicate in the array is null
*/ */
public static Predicate allPredicate(Predicate[] predicates) { public static Predicate allPredicate(Predicate[] predicates) {
return new AllPredicate(validate(predicates)); return AllPredicate.getInstance(predicates);
} }
/** /**
@ -296,7 +299,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if any predicate in the collection is null * @throws IllegalArgumentException if any predicate in the collection is null
*/ */
public static Predicate allPredicate(Collection predicates) { public static Predicate allPredicate(Collection predicates) {
return new AllPredicate(validate(predicates)); return AllPredicate.getInstance(predicates);
} }
/** /**
@ -309,7 +312,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if either predicate is null * @throws IllegalArgumentException if either predicate is null
*/ */
public static Predicate orPredicate(Predicate predicate1, Predicate predicate2) { public static Predicate orPredicate(Predicate predicate1, Predicate predicate2) {
return anyPredicate(new Predicate[] { predicate1, predicate2 }); return OrPredicate.getInstance(predicate1, predicate2);
} }
/** /**
@ -323,7 +326,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if any predicate in the array is null * @throws IllegalArgumentException if any predicate in the array is null
*/ */
public static Predicate anyPredicate(Predicate[] predicates) { public static Predicate anyPredicate(Predicate[] predicates) {
return new AnyPredicate(validate(predicates)); return AnyPredicate.getInstance(predicates);
} }
/** /**
@ -337,7 +340,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if any predicate in the collection is null * @throws IllegalArgumentException if any predicate in the collection is null
*/ */
public static Predicate anyPredicate(Collection predicates) { public static Predicate anyPredicate(Collection predicates) {
return new AnyPredicate(validate(predicates)); return AnyPredicate.getInstance(predicates);
} }
/** /**
@ -364,7 +367,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if any predicate in the array is null * @throws IllegalArgumentException if any predicate in the array is null
*/ */
public static Predicate onePredicate(Predicate[] predicates) { public static Predicate onePredicate(Predicate[] predicates) {
return new OnePredicate(validate(predicates)); return OnePredicate.getInstance(predicates);
} }
/** /**
@ -378,7 +381,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if any predicate in the collection is null * @throws IllegalArgumentException if any predicate in the collection is null
*/ */
public static Predicate onePredicate(Collection predicates) { public static Predicate onePredicate(Collection predicates) {
return new OnePredicate(validate(predicates)); return OnePredicate.getInstance(predicates);
} }
/** /**
@ -405,11 +408,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if any predicate in the array is null * @throws IllegalArgumentException if any predicate in the array is null
*/ */
public static Predicate nonePredicate(Predicate[] predicates) { public static Predicate nonePredicate(Predicate[] predicates) {
Predicate[] preds = validate(predicates); return NonePredicate.getInstance(predicates);
for (int i = 0; i < preds.length; i++) {
preds[i] = notPredicate(preds[i]);
}
return new AllPredicate(preds);
} }
/** /**
@ -423,11 +422,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if any predicate in the collection is null * @throws IllegalArgumentException if any predicate in the collection is null
*/ */
public static Predicate nonePredicate(Collection predicates) { public static Predicate nonePredicate(Collection predicates) {
Predicate[] preds = validate(predicates); return NonePredicate.getInstance(predicates);
for (int i = 0; i < preds.length; i++) {
preds[i] = notPredicate(preds[i]);
}
return new AllPredicate(preds);
} }
/** /**
@ -455,10 +450,7 @@ public class PredicateUtils {
* @throws IllegalArgumentException if the transformer is null * @throws IllegalArgumentException if the transformer is null
*/ */
public static Predicate asPredicate(Transformer transformer) { public static Predicate asPredicate(Transformer transformer) {
if (transformer == null) { return TransformerPredicate.getInstance(transformer);
throw new IllegalArgumentException("The transformer to call must not be null");
}
return new TransformerPredicate(transformer);
} }
// Null handlers // Null handlers
@ -503,212 +495,4 @@ public class PredicateUtils {
return NullIsTruePredicate.getInstance(predicate); return NullIsTruePredicate.getInstance(predicate);
} }
/**
* Convert a collection to an array using the iterator.
*
* @param predicates the predicates to validate
* @return predicate array
*/
private static Predicate[] validate(Collection predicates) {
if (predicates == null) {
throw new IllegalArgumentException("The predicate collection must not be null");
}
if (predicates.size() < 2) {
throw new IllegalArgumentException(
"At least 2 predicates must be specified in the predicate collection, size was " + predicates.size());
}
// convert to array like this to guarantee iterator() ordering
Predicate[] preds = new Predicate[predicates.size()];
int i = 0;
for (Iterator it = predicates.iterator(); it.hasNext();) {
preds[i] = (Predicate) it.next();
if (preds[i] == null) {
throw new IllegalArgumentException("The predicate collection must not contain a null predicate, index " + i + " was null");
}
i++;
}
return preds;
}
/**
* Validate method shared amongst predicate implementations.
*
* @param predicates the predicates to validate
* @return predicate array (copy)
*/
private static Predicate[] validate(Predicate[] predicates) {
if (predicates == null) {
throw new IllegalArgumentException("The predicate array must not be null");
}
if (predicates.length < 2) {
throw new IllegalArgumentException(
"At least 2 predicates must be specified in the predicate array, size was " + predicates.length);
}
Predicate[] preds = new Predicate[predicates.length];
for (int i = 0; i < predicates.length; i++) {
if (predicates[i] == null) {
throw new IllegalArgumentException("The predicate array must not contain a null predicate, index " + i + " was null");
}
preds[i] = predicates[i];
}
return preds;
}
// AllPredicate
//----------------------------------------------------------------------------------
/**
* AllPredicate returns true if all predicates return true
*/
private static class AllPredicate implements Predicate, Serializable {
/** The array of predicates to call */
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;
}
}
// AnyPredicate
//----------------------------------------------------------------------------------
/**
* AnyPredicate returns true if one of the predicates return true
*/
private static class AnyPredicate implements Predicate, Serializable {
/** The array of predicates to call */
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 {
/** The array of predicates to call */
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;
}
}
// UniquePredicate
//----------------------------------------------------------------------------------
/**
* UniquePredicate returns true the first time an object is
* encountered, and false if the same object is received
* again using equals().
*/
private static class UniquePredicate implements Predicate, Serializable {
/** The set of previously seen objects */
private final Set iSet = new HashSet();
/**
* Constructor
*/
public UniquePredicate() {
super();
}
/**
* Return true the first time, and false subsequent times
* that an object is encountered, using equals().
*/
public boolean evaluate(Object object) {
return iSet.add(object);
}
}
// TransformerPredicate
//----------------------------------------------------------------------------------
/**
* TransformerPredicate returns the result of the Transformer as a boolean.
*/
private static class TransformerPredicate implements Predicate, Serializable {
/** The transformer to call */
private final Transformer iTransformer;
/**
* Constructor
*/
public TransformerPredicate(Transformer transformer) {
super();
iTransformer = transformer;
}
/**
* Return the boolean result of a Transformer
*/
public boolean evaluate(Object object) {
Object result = iTransformer.transform(object);
if (result instanceof Boolean == false) {
throw new FunctorException(
"TransformerPredicate: Transformer must return an instanceof Boolean, it was a "
+ (result == null ? "null object" : result.getClass().getName()));
}
return ((Boolean) result).booleanValue();
}
}
} }

View File

@ -0,0 +1,133 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/AllPredicate.java,v 1.1 2003/11/23 22:05:24 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import java.util.Collection;
import org.apache.commons.collections.Predicate;
/**
* Predicate implementation that returns true if all the predicates return true.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 22:05:24 $
*
* @author Stephen Colebourne
*/
public final class AllPredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = -3094696765038308799L;
/** The array of predicates to call */
private final Predicate[] iPredicates;
/**
* Factory to create the predicate.
*
* @param predicates the predicates to check, cloned, not null
* @return the <code>all</code> 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 getInstance(Predicate[] predicates) {
FunctorUtils.validateMin2(predicates);
predicates = FunctorUtils.copy(predicates);
return new AllPredicate(predicates);
}
/**
* Factory to create the predicate.
*
* @param predicates the predicates to check, cloned, not null
* @return the <code>all</code> predicate
* @throws IllegalArgumentException if the predicates array is null
* @throws IllegalArgumentException if any predicate in the array is null
* @throws IllegalArgumentException if the predicates array has less than 2 elements
*/
public static Predicate getInstance(Collection predicates) {
Predicate[] preds = FunctorUtils.validate(predicates);
return new AllPredicate(preds);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*
* @param predicates the predicates to check, not cloned, not null
*/
public AllPredicate(Predicate[] predicates) {
super();
iPredicates = predicates;
}
/**
* Return the predicate result.
*/
public boolean evaluate(Object object) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(object) == false) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,117 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/AndPredicate.java,v 1.1 2003/11/23 22:05:24 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import org.apache.commons.collections.Predicate;
/**
* Predicate implementation that returns true if both the predicates return true.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 22:05:24 $
*
* @author Stephen Colebourne
*/
public final class AndPredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = 4189014213763186912L;
/** The array of predicates to call */
private final Predicate iPredicate1;
/** The array of predicates to call */
private final Predicate iPredicate2;
/**
* Factory to create the predicate.
*
* @param predicate1 the first predicate to check, not null
* @param predicate2 the second predicate to check, not null
* @return the <code>and</code> predicate
* @throws IllegalArgumentException if either predicate is null
*/
public static Predicate getInstance(Predicate predicate1, Predicate predicate2) {
if (predicate1 == null || predicate2 == null) {
throw new IllegalArgumentException("Predicate must not be null");
}
return new AndPredicate(predicate1, predicate2);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*
* @param predicate1 the first predicate to check, not null
* @param predicate2 the second predicate to check, not null
*/
public AndPredicate(Predicate predicate1, Predicate predicate2) {
super();
iPredicate1 = predicate1;
iPredicate2 = predicate2;
}
/**
* Return the predicate result.
*/
public boolean evaluate(Object object) {
return (iPredicate1.evaluate(object) && iPredicate2.evaluate(object));
}
}

View File

@ -0,0 +1,133 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/AnyPredicate.java,v 1.1 2003/11/23 22:05:24 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import java.util.Collection;
import org.apache.commons.collections.Predicate;
/**
* Predicate implementation that returns true if any of the predicates return true.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 22:05:24 $
*
* @author Stephen Colebourne
*/
public final class AnyPredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = 7429999530934647542L;
/** The array of predicates to call */
private final Predicate[] iPredicates;
/**
* Factory to create the predicate.
*
* @param predicates the predicates to check, cloned, not null
* @return the <code>any</code> 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 getInstance(Predicate[] predicates) {
FunctorUtils.validateMin2(predicates);
predicates = FunctorUtils.copy(predicates);
return new AnyPredicate(predicates);
}
/**
* Factory to create the predicate.
*
* @param predicates the predicates to check, cloned, not null
* @return the <code>all</code> predicate
* @throws IllegalArgumentException if the predicates array is null
* @throws IllegalArgumentException if any predicate in the array is null
* @throws IllegalArgumentException if the predicates array has less than 2 elements
*/
public static Predicate getInstance(Collection predicates) {
Predicate[] preds = FunctorUtils.validate(predicates);
return new AnyPredicate(preds);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*
* @param predicates the predicates to check, not cloned, not null
*/
public AnyPredicate(Predicate[] predicates) {
super();
iPredicates = predicates;
}
/**
* Return the predicate result.
*/
public boolean evaluate(Object object) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(object)) {
return true;
}
}
return false;
}
}

View File

@ -1,5 +1,5 @@
/* /*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/FunctorUtils.java,v 1.1 2003/11/23 17:01:35 scolebourne Exp $ * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/FunctorUtils.java,v 1.2 2003/11/23 22:05:24 scolebourne Exp $
* ==================================================================== * ====================================================================
* *
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
@ -57,6 +57,9 @@
*/ */
package org.apache.commons.collections.functors; package org.apache.commons.collections.functors;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.collections.Closure; import org.apache.commons.collections.Closure;
import org.apache.commons.collections.Predicate; import org.apache.commons.collections.Predicate;
@ -64,7 +67,7 @@ import org.apache.commons.collections.Predicate;
* Internal utilities for functors. * Internal utilities for functors.
* *
* @since Commons Collections 3.0 * @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 17:01:35 $ * @version $Revision: 1.2 $ $Date: 2003/11/23 22:05:24 $
* *
* @author Stephen Colebourne * @author Stephen Colebourne
*/ */
@ -100,6 +103,54 @@ class FunctorUtils {
} }
} }
/**
* Validate the predicates to ensure that all is well.
*
* @param predicates the predicates to validate
* @return the validated predicates
*/
static void validateMin2(Predicate[] predicates) {
if (predicates == null) {
throw new IllegalArgumentException("The predicate array must not be null");
}
if (predicates.length < 2) {
throw new IllegalArgumentException(
"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("The predicate array must not contain a null predicate, index " + i + " was null");
}
}
}
/**
* Validate the predicates to ensure that all is well.
*
* @param predicates the predicates to validate
* @return predicate array
*/
static Predicate[] validate(Collection predicates) {
if (predicates == null) {
throw new IllegalArgumentException("The predicate collection must not be null");
}
if (predicates.size() < 2) {
throw new IllegalArgumentException(
"At least 2 predicates must be specified in the predicate collection, size was " + predicates.size());
}
// convert to array like this to guarantee iterator() ordering
Predicate[] preds = new Predicate[predicates.size()];
int i = 0;
for (Iterator it = predicates.iterator(); it.hasNext();) {
preds[i] = (Predicate) it.next();
if (preds[i] == null) {
throw new IllegalArgumentException("The predicate collection must not contain a null predicate, index " + i + " was null");
}
i++;
}
return preds;
}
/** /**
* Clone the closures to ensure that the internal reference can't be messed with. * Clone the closures to ensure that the internal reference can't be messed with.
* *

View File

@ -0,0 +1,133 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/NonePredicate.java,v 1.1 2003/11/23 22:05:24 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import java.util.Collection;
import org.apache.commons.collections.Predicate;
/**
* Predicate implementation that returns true if none of the predicates return true.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 22:05:24 $
*
* @author Stephen Colebourne
*/
public final class NonePredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = 2007613066565892961L;
/** The array of predicates to call */
private final Predicate[] iPredicates;
/**
* Factory to create the predicate.
*
* @param predicates the predicates to check, cloned, not null
* @return the <code>any</code> 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 getInstance(Predicate[] predicates) {
FunctorUtils.validateMin2(predicates);
predicates = FunctorUtils.copy(predicates);
return new NonePredicate(predicates);
}
/**
* Factory to create the predicate.
*
* @param predicates the predicates to check, cloned, not null
* @return the <code>one</code> predicate
* @throws IllegalArgumentException if the predicates array is null
* @throws IllegalArgumentException if any predicate in the array is null
* @throws IllegalArgumentException if the predicates array has less than 2 elements
*/
public static Predicate getInstance(Collection predicates) {
Predicate[] preds = FunctorUtils.validate(predicates);
return new NonePredicate(preds);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*
* @param predicates the predicates to check, not cloned, not null
*/
public NonePredicate(Predicate[] predicates) {
super();
iPredicates = predicates;
}
/**
* Return the predicate result.
*/
public boolean evaluate(Object object) {
for (int i = 0; i < iPredicates.length; i++) {
if (iPredicates[i].evaluate(object)) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,137 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/OnePredicate.java,v 1.1 2003/11/23 22:05:24 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import java.util.Collection;
import org.apache.commons.collections.Predicate;
/**
* Predicate implementation that returns true if only one of the predicates return true.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 22:05:24 $
*
* @author Stephen Colebourne
*/
public final class OnePredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = -8125389089924745785L;
/** The array of predicates to call */
private final Predicate[] iPredicates;
/**
* Factory to create the predicate.
*
* @param predicates the predicates to check, cloned, not null
* @return the <code>any</code> 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 getInstance(Predicate[] predicates) {
FunctorUtils.validateMin2(predicates);
predicates = FunctorUtils.copy(predicates);
return new OnePredicate(predicates);
}
/**
* Factory to create the predicate.
*
* @param predicates the predicates to check, cloned, not null
* @return the <code>one</code> predicate
* @throws IllegalArgumentException if the predicates array is null
* @throws IllegalArgumentException if any predicate in the array is null
* @throws IllegalArgumentException if the predicates array has less than 2 elements
*/
public static Predicate getInstance(Collection predicates) {
Predicate[] preds = FunctorUtils.validate(predicates);
return new OnePredicate(preds);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*
* @param predicates the predicates to check, not cloned, not null
*/
public OnePredicate(Predicate[] predicates) {
super();
iPredicates = predicates;
}
/**
* Return the predicate result.
*/
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;
}
}

View File

@ -0,0 +1,117 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/OrPredicate.java,v 1.1 2003/11/23 22:05:24 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import org.apache.commons.collections.Predicate;
/**
* Predicate implementation that returns true if either of the predicates return true.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 22:05:24 $
*
* @author Stephen Colebourne
*/
public final class OrPredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = -8791518325735182855L;
/** The array of predicates to call */
private final Predicate iPredicate1;
/** The array of predicates to call */
private final Predicate iPredicate2;
/**
* Factory to create the predicate.
*
* @param predicate1 the first predicate to check, not null
* @param predicate2 the second predicate to check, not null
* @return the <code>and</code> predicate
* @throws IllegalArgumentException if either predicate is null
*/
public static Predicate getInstance(Predicate predicate1, Predicate predicate2) {
if (predicate1 == null || predicate2 == null) {
throw new IllegalArgumentException("Predicate must not be null");
}
return new OrPredicate(predicate1, predicate2);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*
* @param predicate1 the first predicate to check, not null
* @param predicate2 the second predicate to check, not null
*/
public OrPredicate(Predicate predicate1, Predicate predicate2) {
super();
iPredicate1 = predicate1;
iPredicate2 = predicate2;
}
/**
* Return the predicate result.
*/
public boolean evaluate(Object object) {
return (iPredicate1.evaluate(object) || iPredicate2.evaluate(object));
}
}

View File

@ -0,0 +1,116 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/TransformerPredicate.java,v 1.1 2003/11/23 22:05:24 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.Transformer;
/**
* Predicate implementation that returns the result of a transformer.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 22:05:24 $
*
* @author Stephen Colebourne
*/
public final class TransformerPredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = -2407966402920578741L;
/** The transformer to call */
private final Transformer iTransformer;
/**
* Factory to create the predicate.
*
* @return the transformer
* @throws IllegalArgumentException if the transformer is null
*/
public static Predicate getInstance(Transformer transformer) {
if (transformer == null) {
throw new IllegalArgumentException("The transformer to call must not be null");
}
return new TransformerPredicate(transformer);
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*/
public TransformerPredicate(Transformer transformer) {
super();
iTransformer = transformer;
}
/**
* Return the predicate result.
*/
public boolean evaluate(Object object) {
Object result = iTransformer.transform(object);
if (result instanceof Boolean == false) {
throw new FunctorException(
"Transformer must return an instanceof Boolean, it was a "
+ (result == null ? "null object" : result.getClass().getName()));
}
return ((Boolean) result).booleanValue();
}
}

View File

@ -0,0 +1,108 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/functors/UniquePredicate.java,v 1.1 2003/11/23 22:05:24 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
* <http://www.apache.org/>.
*
*/
package org.apache.commons.collections.functors;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.collections.Predicate;
/**
* Predicate implementation that returns true the first time an object is
* passed into the predicate.
*
* @since Commons Collections 3.0
* @version $Revision: 1.1 $ $Date: 2003/11/23 22:05:24 $
*
* @author Stephen Colebourne
*/
public final class UniquePredicate implements Predicate, Serializable {
/** Serial version UID */
static final long serialVersionUID = -3319417438027438040L;
/** The set of previously seen objects */
private final Set iSet = new HashSet();
/**
* Factory to create the predicate.
*
* @return the predicate
* @throws IllegalArgumentException if the predicate is null
*/
public static Predicate getInstance() {
return new UniquePredicate();
}
/**
* Constructor that performs no validation.
* Use <code>getInstance</code> if you want that.
*/
public UniquePredicate() {
super();
}
/**
* Return the predicate result.
*/
public boolean evaluate(Object object) {
return iSet.add(object);
}
}