E - the type of the elements in the collection
public 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 for creating predicated collections.
|
| Modifier and Type | Field and Description |
|---|---|
protected Predicate |
predicate
The predicate to use
|
| Modifier | Constructor and Description |
|---|---|
protected |
PredicatedCollection(Collection
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(Collection
Override to validate the objects being added to ensure they match the predicate.
|
static <E> PredicatedCollection |
builder(Predicate
Returns a Builder with the given predicate.
|
static <E> PredicatedCollection |
notNullBuilder()
Returns a Builder with a NotNullPredicate.
|
static <T> PredicatedCollection |
predicatedCollection(Collection
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, toStringclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitequals, hashCode, parallelStream, removeIf, spliterator, streamprotected PredicatedCollection(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 null
predicate - the predicate to use for validation, must not be null
NullPointerException - if collection or predicate is null
IllegalArgumentException - if the collection contains invalid elements
public static <E> PredicatedCollection.Builder <E> builder(Predicate <? super E> predicate)
E - the element type
predicate - the predicate to use
public static <E> PredicatedCollection.Builder <E> notNullBuilder()
E - the element type
public static <T> PredicatedCollection<T> predicatedCollection(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 collection
coll - the collection to decorate, must not be null
predicate - the predicate to use for validation, must not be null
NullPointerException - if collection or predicate is null
IllegalArgumentException - if the collection contains invalid elements
protected 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 added
IllegalArgumentException - if the add is invalid
public boolean add(E object)
add in interface
Collection<E>
add in class
AbstractCollectionDecorator<E>
object - the object being added
IllegalArgumentException - if the add is invalid
public boolean addAll(Collection<? extends E> coll)
addAll in interface
Collection<E>
addAll in class
AbstractCollectionDecorator<E>
coll - the collection being added
IllegalArgumentException - if the add is invalid