E
- the type of the elements in the collectionpublic class PredicatedCollection<E> extends AbstractCollectionDecorator<E>
Collection
to validate that additions
match a specified predicate.
This collection exists to provide validation for the decorated collection. It is normally created to decorate an empty collection. If an object cannot be added to the collection, an IllegalArgumentException is thrown.
One usage would be to ensure that no null entries are added to the collection:
Collection coll = PredicatedCollection.predicatedCollection(new ArrayList(), NotNullPredicate.INSTANCE);
This class is Serializable from Commons Collections 3.1.
Modifier and Type | Class and Description |
---|---|
static class |
PredicatedCollection.Builder<E>
Builder for creating predicated collections.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Override to validate the object being added to ensure it matches
the predicate.
|
boolean |
addAll(java.util.Collection<? extends E> coll)
Override to validate the objects being added to ensure they match
the predicate.
|
static <E> PredicatedCollection.Builder<E> |
builder(Predicate<? super E> predicate)
Returns a Builder with the given predicate.
|
static <E> PredicatedCollection.Builder<E> |
notNullBuilder()
Returns a Builder with a NotNullPredicate.
|
static <T> PredicatedCollection<T> |
predicatedCollection(java.util.Collection<T> coll,
Predicate<? super T> predicate)
Factory method to create a predicated (validating) collection.
|
clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
public static <E> PredicatedCollection.Builder<E> builder(Predicate<? super E> predicate)
E
- the element typepredicate
- the predicate to usepublic static <E> PredicatedCollection.Builder<E> notNullBuilder()
E
- the element typepublic static <T> PredicatedCollection<T> predicatedCollection(java.util.Collection<T> coll, Predicate<? super T> predicate)
If there are any elements already in the collection being decorated, they are validated.
T
- the type of the elements in the collectioncoll
- the collection to decorate, must not be nullpredicate
- the predicate to use for validation, must not be nulljava.lang.NullPointerException
- if collection or predicate is nulljava.lang.IllegalArgumentException
- if the collection contains invalid elementspublic boolean add(E object)
add
in interface java.util.Collection<E>
add
in class AbstractCollectionDecorator<E>
object
- the object being addedjava.lang.IllegalArgumentException
- if the add is invalidpublic boolean addAll(java.util.Collection<? extends E> coll)
addAll
in interface java.util.Collection<E>
addAll
in class AbstractCollectionDecorator<E>
coll
- the collection being addedjava.lang.IllegalArgumentException
- if the add is invalid"Copyright © 2010 - 2020 Adobe Systems Incorporated. All Rights Reserved"