E - the element type
public static class PredicatedCollection.Builder<E> extends Object
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.
| Constructor and Description |
|---|
Builder(Predicate
Constructs a PredicatedCollectionBuilder with the specified Predicate.
|
| Modifier and Type | Method and Description |
|---|---|
PredicatedCollection |
add(E item)
Adds the item to the builder.
|
PredicatedCollection |
addAll(Collection
Adds all elements from the given collection to the builder.
|
Bag |
createPredicatedBag()
Create a new predicated bag filled with the accepted elements.
|
Bag |
createPredicatedBag(Bag
Decorates the given bag with validating behavior using the predicate.
|
List |
createPredicatedList()
Create a new predicated list filled with the accepted elements.
|
List |
createPredicatedList(List
Decorates the given list with validating behavior using the predicate.
|
MultiSet |
createPredicatedMultiSet()
Create a new predicated multiset filled with the accepted elements.
|
MultiSet |
createPredicatedMultiSet(MultiSet
Decorates the given multiset with validating behavior using the predicate.
|
Queue |
createPredicatedQueue()
Create a new predicated queue filled with the accepted elements.
|
Queue |
createPredicatedQueue(Queue
Decorates the given queue with validating behavior using the predicate.
|
Set |
createPredicatedSet()
Create a new predicated set filled with the accepted elements.
|
Set |
createPredicatedSet(Set
Decorates the given list with validating behavior using the predicate.
|
Collection |
rejectedElements()
Returns an unmodifiable collection containing all rejected elements.
|
public Builder(Predicate<? super E> predicate)
predicate - the predicate to use
NullPointerException - if predicate is null
public PredicatedCollection.Builder <E> add(E item)
If the predicate is true, it is added to the list of accepted elements, otherwise it is added to the rejected list.
item - the element to add
public PredicatedCollection.Builder <E> addAll(Collection <? extends E> items)
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.
items - the elements to add to the builder
public List<E> createPredicatedList()
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.
public List<E> createPredicatedList(List <E> list)
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.
list - the List to decorate, must not be null
NullPointerException - if list is null
IllegalArgumentException - if list contains invalid elements
public Set<E> createPredicatedSet()
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.
public Set<E> createPredicatedSet(Set <E> set)
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.
set - the set to decorate, must not be null
NullPointerException - if set is null
IllegalArgumentException - if set contains invalid elements
public MultiSet<E> createPredicatedMultiSet()
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.
public MultiSet<E> createPredicatedMultiSet(MultiSet <E> multiset)
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.
multiset - the multiset to decorate, must not be null
NullPointerException - if multiset is null
IllegalArgumentException - if multiset contains invalid elements
public Bag<E> createPredicatedBag()
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.
public Bag<E> createPredicatedBag(Bag <E> bag)
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.
bag - the bag to decorate, must not be null
NullPointerException - if bag is null
IllegalArgumentException - if bag contains invalid elements
public Queue<E> createPredicatedQueue()
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.
public Queue<E> createPredicatedQueue(Queue <E> queue)
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.
queue - the queue to decorate, must not be null
NullPointerException - if queue is null
IllegalArgumentException - if queue contains invalid elements
public Collection<E> rejectedElements()