E
- the element typepublic 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<? super E> predicate)
Constructs a PredicatedCollectionBuilder with the specified Predicate.
|
Modifier and Type | Method and Description |
---|---|
PredicatedCollection.Builder<E> |
add(E item)
Adds the item to the builder.
|
PredicatedCollection.Builder<E> |
addAll(Collection<? extends E> items)
Adds all elements from the given collection to the builder.
|
Bag<E> |
createPredicatedBag()
Create a new predicated bag filled with the accepted elements.
|
Bag<E> |
createPredicatedBag(Bag<E> bag)
Decorates the given bag with validating behavior using the predicate.
|
List<E> |
createPredicatedList()
Create a new predicated list filled with the accepted elements.
|
List<E> |
createPredicatedList(List<E> list)
Decorates the given list with validating behavior using the predicate.
|
MultiSet<E> |
createPredicatedMultiSet()
Create a new predicated multiset filled with the accepted elements.
|
MultiSet<E> |
createPredicatedMultiSet(MultiSet<E> multiset)
Decorates the given multiset with validating behavior using the predicate.
|
Queue<E> |
createPredicatedQueue()
Create a new predicated queue filled with the accepted elements.
|
Queue<E> |
createPredicatedQueue(Queue<E> queue)
Decorates the given queue with validating behavior using the predicate.
|
Set<E> |
createPredicatedSet()
Create a new predicated set filled with the accepted elements.
|
Set<E> |
createPredicatedSet(Set<E> set)
Decorates the given list with validating behavior using the predicate.
|
Collection<E> |
rejectedElements()
Returns an unmodifiable collection containing all rejected elements.
|
public Builder(Predicate<? super E> predicate)
predicate
- the predicate to useNullPointerException
- if predicate is nullpublic 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 addpublic 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 builderpublic 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 nullNullPointerException
- if list is nullIllegalArgumentException
- if list contains invalid elementspublic 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 nullNullPointerException
- if set is nullIllegalArgumentException
- if set contains invalid elementspublic 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 nullNullPointerException
- if multiset is nullIllegalArgumentException
- if multiset contains invalid elementspublic 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 nullNullPointerException
- if bag is nullIllegalArgumentException
- if bag contains invalid elementspublic 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 nullNullPointerException
- if queue is nullIllegalArgumentException
- if queue contains invalid elementspublic Collection<E> rejectedElements()
Copyright © 2001–2018 The Apache Software Foundation. All rights reserved.