Package org.apache.commons.collections4.collection

# Class PredicatedCollection.Builder<E>

java.lang.Object
org.apache.commons.collections4.collection.PredicatedCollection.Builder<E>

Type Parameters:
  `E` - the element type.

Enclosing class:
  `PredicatedCollection<E>`

public static class PredicatedCollection.Builder<E> extends Object
Builder for creating predicated collections.

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.

For 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.

Since:
  4.1

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Builder(Predicate<? super E> predicate)`
  Constructs a PredicatedCollectionBuilder with the specified Predicate.

- ## Method Summary

  Modifier and Type
  Method
  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()`
  Deprecated.
  Since 4.6.0, use `createPredicatedMultiSet()` instead.
  `Bag<E>`
  `createPredicatedBag(Bag<E> bag)`
  Deprecated.
  Since 4.6.0, use `createPredicatedMultiSet(MultiSet)` instead.
  `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.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Builder

    public Builder(Predicate<? super E> predicate)
    Constructs a PredicatedCollectionBuilder with the specified Predicate.
    Parameters:
      `predicate` - The predicate to use.
    Throws:
      `NullPointerException` - if predicate is null.

- ## Method Details

  - ### add

    public PredicatedCollection.Builder<E> add(E item)
    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.

    Parameters:
      `item` - The element to add.
    Returns:
      The PredicatedCollectionBuilder.

  - ### addAll

    public PredicatedCollection.Builder<E> addAll(Collection<? extends E> items)
    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.

    Parameters:
      `items` - The elements to add to the builder.
    Returns:
      The PredicatedCollectionBuilder.

  - ### createPredicatedBag

    @Deprecated public Bag<E> createPredicatedBag()
    Deprecated.
    Since 4.6.0, use `createPredicatedMultiSet()` instead.
    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.

    Returns:
      A new predicated bag.

  - ### createPredicatedBag

    @Deprecated public Bag<E> createPredicatedBag(Bag<E> bag)
    Deprecated.
    Since 4.6.0, use `createPredicatedMultiSet(MultiSet)` instead.
    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.

    Parameters:
      `bag` - The bag to decorate, must not be null.
    Returns:
      The decorated bag.
    Throws:
      `NullPointerException` - if bag is null.
      `IllegalArgumentException` - if bag contains invalid elements.

  - ### createPredicatedList

    public List<E> createPredicatedList()
    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.

    Returns:
      A new predicated list.

  - ### createPredicatedList

    public List<E> createPredicatedList(List<E> 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.

    Parameters:
      `list` - The List to decorate, must not be null.
    Returns:
      The decorated list.
    Throws:
      `NullPointerException` - if list is null.
      `IllegalArgumentException` - if list contains invalid elements.

  - ### createPredicatedMultiSet

    public MultiSet<E> createPredicatedMultiSet()
    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.

    Returns:
      A new predicated multiset.

  - ### createPredicatedMultiSet

    public MultiSet<E> createPredicatedMultiSet(MultiSet<E> 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.

    Parameters:
      `multiset` - The multiset to decorate, must not be null.
    Returns:
      The decorated multiset.
    Throws:
      `NullPointerException` - if multiset is null.
      `IllegalArgumentException` - if multiset contains invalid elements.

  - ### createPredicatedQueue

    public Queue<E> createPredicatedQueue()
    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.

    Returns:
      A new predicated queue.

  - ### createPredicatedQueue

    public Queue<E> createPredicatedQueue(Queue<E> 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.

    Parameters:
      `queue` - The queue to decorate, must not be null.
    Returns:
      The decorated queue.
    Throws:
      `NullPointerException` - if queue is null.
      `IllegalArgumentException` - if queue contains invalid elements.

  - ### createPredicatedSet

    public Set<E> createPredicatedSet()
    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.

    Returns:
      A new predicated set.

  - ### createPredicatedSet

    public Set<E> createPredicatedSet(Set<E> 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.

    Parameters:
      `set` - The set to decorate, must not be null.
    Returns:
      The decorated set.
    Throws:
      `NullPointerException` - if set is null.
      `IllegalArgumentException` - if set contains invalid elements.

  - ### rejectedElements

    public Collection<E> rejectedElements()
    Returns an unmodifiable collection containing all rejected elements.
    Returns:
      An unmodifiable collection.