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 |
|---|---|
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| protected final Predicate | predicate
The predicate to use |
| private static final long | serialVersionUID
Hides org. |
| Access | Constructor and Description |
|---|---|
| protected | PredicatedCollection(final Collection<E>
the collection to decorate, must not be null coll, final Predicate<? super E> the predicate to use for validation, must not be null predicate)Constructor that wraps (not copies). |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: the result of adding to the underlying collectionthe object being added object)Overrides org. Implements java. |
| public boolean | Returns: the result of adding to the underlying collectionthe collection being added coll)Overrides org. Implements java. |
| public static < the element type E> PredicatedCollection. | |
| public static < the element type E> PredicatedCollection. | Returns: a new Builder for predicated collections that ignores null values.Returns a Builder with a NotNullPredicate. |
| public static < the type of the elements in the collection T> PredicatedCollection | Returns: a new predicated collectionthe collection to decorate, must not be null coll, final Predicate<? super T> the predicate to use for validation, must not be null predicate)Factory method to create a predicated (validating) collection. |
| protected void | validate(final E
the object being added object)Validates the object being added to ensure it matches the predicate. |
| predicate | back to summary |
|---|---|
| protected final Predicate<? super E> predicate The predicate to use | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides org. Serialization version | |
| PredicatedCollection | back to summary |
|---|---|
| protected PredicatedCollection(final Collection<E> coll, final Predicate<? super E> predicate) Constructor that wraps (not copies). If there are any elements already in the collection being decorated, they are validated.
| |
| add | back to summary |
|---|---|
| public boolean add(final E object) Overrides org. Implements java. Override to validate the object being added to ensure it matches the predicate.
| |
| addAll | back to summary |
|---|---|
| public boolean addAll(final Collection<? extends E> coll) Overrides org. Implements java. Override to validate the objects being added to ensure they match the predicate. If any one fails, no update is made to the underlying collection.
| |
| builder | back to summary |
|---|---|
| public static <E> PredicatedCollection. Returns a Builder with the given predicate.
| |
| notNullBuilder | back to summary |
|---|---|
| public static <E> PredicatedCollection. Returns a Builder with a NotNullPredicate.
| |
| predicatedCollection | back to summary |
|---|---|
| public static <T> PredicatedCollection Factory method to create a predicated (validating) collection. If there are any elements already in the collection being decorated, they are validated.
| |
| validate | back to summary |
|---|---|
| protected void validate(final E object) Validates the object being added to ensure it matches the predicate. The predicate itself should not throw an exception, but return false to indicate that the object cannot be added.
| |
Create a Builder with a predicate to validate elements against, then add any elements to the builder. Elements that fail the predicate will be added to a rejected list. Finally create or decorate a collection using the createPredicated[List,Set,Bag,Queue] methods.
An example:
Predicate<String> predicate = NotNullPredicate.notNullPredicate();
PredicatedCollectionBuilder<String> builder = PredicatedCollection.builder(predicate);
builder.add("item1");
builder.add(null);
builder.add("item2");
List<String> predicatedList = builder.createPredicatedList();
At the end of the code fragment above predicatedList is protected by the predicate supplied to the builder and it contains item1 and item2.
More elements can be added to the builder once a predicated collection has been created, but these elements will not be reflected in already created collections.
| Modifier and Type | Field and Description |
|---|---|
| private final List | accepted
The buffer containing valid elements. |
| private final Predicate | predicate
The predicate to use. |
| private final List | rejected
The buffer containing rejected elements. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public PredicatedCollection. | Returns: the PredicatedCollectionBuilder.the element to add item)Adds the item to the builder. |
| public PredicatedCollection. | Returns: the PredicatedCollectionBuilder.the elements to add to the builder items)Adds all elements from the given collection to the builder. |
| public Bag | Returns: a new predicated bag.Create a new predicated bag filled with the accepted elements. |
| public Bag | Returns: the decorated bag.the bag to decorate, must not be null bag)Decorates the given bag with validating behavior using the predicate. |
| public List | Returns: a new predicated list.Create a new predicated list filled with the accepted elements. |
| public List | Returns: the decorated list.the List to decorate, must not be null list)Decorates the given list with validating behavior using the predicate. |
| public MultiSet | Returns: a new predicated multiset.Create a new predicated multiset filled with the accepted elements. |
| public MultiSet | Returns: the decorated multiset.the multiset to decorate, must not be null multiset)Decorates the given multiset with validating behavior using the predicate. |
| public Queue | Returns: a new predicated queue.Create a new predicated queue filled with the accepted elements. |
| public Queue | Returns: the decorated queue.the queue to decorate, must not be null queue)Decorates the given queue with validating behavior using the predicate. |
| public Set | Returns: a new predicated set.Create a new predicated set filled with the accepted elements. |
| public Set | Returns: the decorated set.the set to decorate, must not be null set)Decorates the given list with validating behavior using the predicate. |
| public Collection | Returns: an unmodifiable collectionReturns an unmodifiable collection containing all rejected elements. |
| accepted | back to summary |
|---|---|
| private final List<E> accepted The buffer containing valid elements. | |
| predicate | back to summary |
|---|---|
| private final Predicate<? super E> predicate The predicate to use. | |
| rejected | back to summary |
|---|---|
| private final List<E> rejected The buffer containing rejected elements. | |
| Builder | back to summary |
|---|---|
| public Builder(final Predicate<? super E> predicate) Constructs a PredicatedCollectionBuilder with the specified Predicate.
| |
| add | back to summary |
|---|---|
| public PredicatedCollection. Adds the item to the builder. If the predicate is true, it is added to the list of accepted elements, otherwise it is added to the rejected list.
| |
| addAll | back to summary |
|---|---|
| public PredicatedCollection. Adds all elements from the given collection to the builder. All elements for which the predicate evaluates to true will be added to the list of accepted elements, otherwise they are added to the rejected list.
| |
| createPredicatedBag | back to summary |
|---|---|
| public Bag Create a new predicated bag filled with the accepted elements. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned bag.
| |
| createPredicatedBag | back to summary |
|---|---|
| public Bag Decorates the given bag with validating behavior using the predicate. All accepted elements are appended to the bag. If the bag already contains elements, they are validated. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned bag.
| |
| createPredicatedList | back to summary |
|---|---|
| public List Create a new predicated list filled with the accepted elements. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned list.
| |
| createPredicatedList | back to summary |
|---|---|
| public List Decorates the given list with validating behavior using the predicate. All accepted elements are appended to the list. If the list already contains elements, they are validated. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned list.
| |
| createPredicatedMultiSet | back to summary |
|---|---|
| public MultiSet Create a new predicated multiset filled with the accepted elements. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned multiset.
| |
| createPredicatedMultiSet | back to summary |
|---|---|
| public MultiSet Decorates the given multiset with validating behavior using the predicate. All accepted elements are appended to the multiset. If the multiset already contains elements, they are validated. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned multiset.
| |
| createPredicatedQueue | back to summary |
|---|---|
| public Queue Create a new predicated queue filled with the accepted elements. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned queue.
| |
| createPredicatedQueue | back to summary |
|---|---|
| public Queue Decorates the given queue with validating behavior using the predicate. All accepted elements are appended to the queue. If the queue already contains elements, they are validated. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned queue.
| |
| createPredicatedSet | back to summary |
|---|---|
| public Set Create a new predicated set filled with the accepted elements. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned set.
| |
| createPredicatedSet | back to summary |
|---|---|
| public Set Decorates the given list with validating behavior using the predicate. All accepted elements are appended to the set. If the set already contains elements, they are validated. The builder is not modified by this method, so it is possible to create more collections or add more elements afterwards. Further changes will not propagate to the returned set.
| |
| rejectedElements | back to summary |
|---|---|
| public Collection Returns an unmodifiable collection containing all rejected elements.
| |