Class PredicatedCollection.Builder<E>

  • Type Parameters:
    E - the element type
    Enclosing class:
    PredicatedCollection<E>

    public static class PredicatedCollection.Builder<E>
    extends java.lang.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.

    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.

    Since:
    4.1
    • Constructor Detail

      • Builder

        public Builder​(Predicate<? super E> predicate)
        Constructs a PredicatedCollectionBuilder with the specified Predicate.
        Parameters:
        predicate - the predicate to use
        Throws:
        java.lang.NullPointerException - if predicate is null
    • Method Detail

      • 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​(java.util.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.
      • createPredicatedList

        public java.util.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 java.util.List<E> createPredicatedList​(java.util.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:
        java.lang.NullPointerException - if list is null
        java.lang.IllegalArgumentException - if list contains invalid elements
      • createPredicatedSet

        public java.util.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 java.util.Set<E> createPredicatedSet​(java.util.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:
        java.lang.NullPointerException - if set is null
        java.lang.IllegalArgumentException - if set 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:
        java.lang.NullPointerException - if multiset is null
        java.lang.IllegalArgumentException - if multiset contains invalid elements
      • createPredicatedBag

        public Bag<E> createPredicatedBag()
        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

        public Bag<E> createPredicatedBag​(Bag<E> 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.

        Parameters:
        bag - the bag to decorate, must not be null
        Returns:
        the decorated bag.
        Throws:
        java.lang.NullPointerException - if bag is null
        java.lang.IllegalArgumentException - if bag contains invalid elements
      • createPredicatedQueue

        public java.util.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 java.util.Queue<E> createPredicatedQueue​(java.util.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:
        java.lang.NullPointerException - if queue is null
        java.lang.IllegalArgumentException - if queue contains invalid elements
      • rejectedElements

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