Class CollectionBuilder<E>

  • Type Parameters:
    E - The type of the contained Collection
    All Implemented Interfaces:
    Iterable<E>

    public final class CollectionBuilder<E>
    extends Object
    implements Iterable<E>

    Overview

    Builder for creating Collections providing some convenience methods. The class writes everything through into the contained collector. Using the default constructor a newly created ArrayList will be used as collector, but you can pass you own collector as constructor-argument. Of course this should be mutable in order to work.

    Handling of null-values

    As default null values are ignored. This behavior can be changed by call addNullValues(boolean). Caution: In case of using one of the copyFrom(Collection) methods for instantiation the null values will not be checked in that way

    Standard Usage

    
     List<String> result = new CollectionBuilder<String>().add("this").add("that")
             .add(mutableList("on", "or an other")).toImmutableList();
     
    or
    
     Set<String> result = new CollectionBuilder<String>().add("this").add("that")
             .add(mutableList("on", "or an other")).toMutableSet();
     

    Copy From

    This methods can be used for ensuring a real copy
    
     List<String> result =
         CollectionBuilder.copyFrom(mutableList("on", "or an other")).add("element").toMutableList();
    
     

    Sorting

    The contained Collection can be sorted any time by calling sort(Comparator)

    Author:
    Oliver Wolff