Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions: ------------------------------------------------------------------------ r471575 | scolebourne | 2006-11-05 15:58:08 -0800 (Sun, 05 Nov 2006) | 1 line Generify and remove AbstractSerializableCollectionDecorator ------------------------------------------------------------------------ r471202 | scolebourne | 2006-11-04 06:21:44 -0800 (Sat, 04 Nov 2006) | 1 line Remove getCollection() - use covariant decorated() ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815037 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4e30197607
commit
c0aa4fdd16
|
@ -17,7 +17,6 @@
|
||||||
package org.apache.commons.collections.collection;
|
package org.apache.commons.collections.collection;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
|
||||||
|
|
||||||
import org.apache.commons.collections.Predicate;
|
import org.apache.commons.collections.Predicate;
|
||||||
|
|
||||||
|
@ -34,19 +33,20 @@ import org.apache.commons.collections.Predicate;
|
||||||
* <p>
|
* <p>
|
||||||
* This class is Serializable from Commons Collections 3.1.
|
* This class is Serializable from Commons Collections 3.1.
|
||||||
*
|
*
|
||||||
|
* @param <E> the type of the elements in the collection
|
||||||
* @since Commons Collections 3.0
|
* @since Commons Collections 3.0
|
||||||
* @version $Revision$ $Date$
|
* @version $Revision$ $Date$
|
||||||
*
|
*
|
||||||
* @author Stephen Colebourne
|
* @author Stephen Colebourne
|
||||||
* @author Paul Jack
|
* @author Paul Jack
|
||||||
*/
|
*/
|
||||||
public class PredicatedCollection extends AbstractSerializableCollectionDecorator {
|
public class PredicatedCollection<E> extends AbstractCollectionDecorator<E> {
|
||||||
|
|
||||||
/** Serialization version */
|
/** Serialization version */
|
||||||
private static final long serialVersionUID = -5259182142076705162L;
|
private static final long serialVersionUID = -5259182142076705162L;
|
||||||
|
|
||||||
/** The predicate to use */
|
/** The predicate to use */
|
||||||
protected final Predicate predicate;
|
protected final Predicate<? super E> predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory method to create a predicated (validating) collection.
|
* Factory method to create a predicated (validating) collection.
|
||||||
|
@ -54,16 +54,17 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
|
||||||
* If there are any elements already in the collection being decorated, they
|
* If there are any elements already in the collection being decorated, they
|
||||||
* are validated.
|
* are validated.
|
||||||
*
|
*
|
||||||
|
* @param <T> the type of the elements in the collection
|
||||||
* @param coll the collection to decorate, must not be null
|
* @param coll the collection to decorate, must not be null
|
||||||
* @param predicate the predicate to use for validation, must not be null
|
* @param predicate the predicate to use for validation, must not be null
|
||||||
* @return a new predicated collection
|
* @return a new predicated collection
|
||||||
* @throws IllegalArgumentException if collection or predicate is null
|
* @throws IllegalArgumentException if collection or predicate is null
|
||||||
* @throws IllegalArgumentException if the collection contains invalid elements
|
* @throws IllegalArgumentException if the collection contains invalid elements
|
||||||
*/
|
*/
|
||||||
public static Collection decorate(Collection coll, Predicate predicate) {
|
public static <T> Collection<T> decorate(Collection<T> coll, Predicate<? super T> predicate) {
|
||||||
return new PredicatedCollection(coll, predicate);
|
return new PredicatedCollection<T>(coll, predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------
|
//-----------------------------------------------------------------------
|
||||||
/**
|
/**
|
||||||
* Constructor that wraps (not copies).
|
* Constructor that wraps (not copies).
|
||||||
|
@ -76,14 +77,14 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
|
||||||
* @throws IllegalArgumentException if collection or predicate is null
|
* @throws IllegalArgumentException if collection or predicate is null
|
||||||
* @throws IllegalArgumentException if the collection contains invalid elements
|
* @throws IllegalArgumentException if the collection contains invalid elements
|
||||||
*/
|
*/
|
||||||
protected PredicatedCollection(Collection coll, Predicate predicate) {
|
protected PredicatedCollection(Collection<E> coll, Predicate<? super E> predicate) {
|
||||||
super(coll);
|
super(coll);
|
||||||
if (predicate == null) {
|
if (predicate == null) {
|
||||||
throw new IllegalArgumentException("Predicate must not be null");
|
throw new IllegalArgumentException("Predicate must not be null");
|
||||||
}
|
}
|
||||||
this.predicate = predicate;
|
this.predicate = predicate;
|
||||||
for (Iterator it = coll.iterator(); it.hasNext(); ) {
|
for (E item : coll) {
|
||||||
validate(it.next());
|
validate(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,7 +97,7 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
|
||||||
* @param object the object being added
|
* @param object the object being added
|
||||||
* @throws IllegalArgumentException if the add is invalid
|
* @throws IllegalArgumentException if the add is invalid
|
||||||
*/
|
*/
|
||||||
protected void validate(Object object) {
|
protected void validate(E object) {
|
||||||
if (predicate.evaluate(object) == false) {
|
if (predicate.evaluate(object) == false) {
|
||||||
throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate '" + predicate + "' rejected it");
|
throw new IllegalArgumentException("Cannot add Object '" + object + "' - Predicate '" + predicate + "' rejected it");
|
||||||
}
|
}
|
||||||
|
@ -111,9 +112,9 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
|
||||||
* @return the result of adding to the underlying collection
|
* @return the result of adding to the underlying collection
|
||||||
* @throws IllegalArgumentException if the add is invalid
|
* @throws IllegalArgumentException if the add is invalid
|
||||||
*/
|
*/
|
||||||
public boolean add(Object object) {
|
public boolean add(E object) {
|
||||||
validate(object);
|
validate(object);
|
||||||
return getCollection().add(object);
|
return decorated().add(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -125,11 +126,11 @@ public class PredicatedCollection extends AbstractSerializableCollectionDecorato
|
||||||
* @return the result of adding to the underlying collection
|
* @return the result of adding to the underlying collection
|
||||||
* @throws IllegalArgumentException if the add is invalid
|
* @throws IllegalArgumentException if the add is invalid
|
||||||
*/
|
*/
|
||||||
public boolean addAll(Collection coll) {
|
public boolean addAll(Collection<? extends E> coll) {
|
||||||
for (Iterator it = coll.iterator(); it.hasNext(); ) {
|
for (E item : coll) {
|
||||||
validate(it.next());
|
validate(item);
|
||||||
}
|
}
|
||||||
return getCollection().addAll(coll);
|
return decorated().addAll(coll);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue