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 | Field and Description |
---|---|
protected Predicate<? super E> |
predicate
The predicate to use
|
private static long |
serialVersionUID
Serialization version
|
Modifier | Constructor and Description |
---|---|
protected |
PredicatedCollection(java.util.Collection<E> coll,
Predicate<? super E> predicate)
Constructor that wraps (not copies).
|
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.
|
protected void |
validate(E object)
Validates the object being added to ensure it matches the predicate.
|
clear, contains, containsAll, decorated, isEmpty, iterator, remove, removeAll, retainAll, setCollection, size, toArray, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
private static final long serialVersionUID
protected PredicatedCollection(java.util.Collection<E> coll, Predicate<? super E> predicate)
If there are any elements already in the collection being decorated, they are validated.
coll
- 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 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 elementsprotected void validate(E object)
The predicate itself should not throw an exception, but return false to indicate that the object cannot be added.
object
- the object being addedjava.lang.IllegalArgumentException
- if the add is invalidpublic 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