Package io.cui.tools.collect
Class CollectionBuilder<E>
- java.lang.Object
-
- io.cui.tools.collect.CollectionBuilder<E>
-
- Type Parameters:
E- The type of the containedCollection
- All Implemented Interfaces:
Iterable<E>
public final class CollectionBuilder<E> extends Object implements Iterable<E>
Overview
Builder for creatingCollections providing some convenience methods. The class writes everything through into the contained collector. Using the default constructor a newly createdArrayListwill 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
nullvalues are ignored. This behavior can be changed by calladdNullValues(boolean). Caution: In case of using one of thecopyFrom(Collection)methods for instantiation thenullvalues will not be checked in that wayStandard Usage
List<String> result = new CollectionBuilder<String>().add("this").add("that") .add(mutableList("on", "or an other")).toImmutableList();orSet<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 copyList<String> result = CollectionBuilder.copyFrom(mutableList("on", "or an other")).add("element").toMutableList();Sorting
The contained
Collectioncan be sorted any time by callingsort(Comparator)- Author:
- Oliver Wolff
-
-
Constructor Summary
Constructors Constructor Description CollectionBuilder()Default Constructor initializing the collector with anArrayListCollectionBuilder(Collection<E> collector)Constructor for CollectionBuilder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CollectionBuilder<E>add(E e)add.CollectionBuilder<E>add(E... elements)add.CollectionBuilder<E>add(Iterable<E> elements)add.CollectionBuilder<E>add(Collection<E> elements)add.CollectionBuilder<E>add(Optional<E> element)add.CollectionBuilder<E>add(Stream<E> elements)add.CollectionBuilder<E>addNullValues(boolean addNullValues)addNullValues.CollectionBuilder<E>clear()Clears the elements in the collectorbooleancontains(Object o)contains.static <E> CollectionBuilder<E>copyFrom(E source)Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)static <E> CollectionBuilder<E>copyFrom(E... source)Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)static <E> CollectionBuilder<E>copyFrom(Iterable<? extends E> source)Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)static <E> CollectionBuilder<E>copyFrom(Collection<? extends E> source)Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)static <E> CollectionBuilder<E>copyFrom(Iterator<? extends E> source)Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)static <E> CollectionBuilder<E>copyFrom(Stream<? extends E> source)Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)booleanequals(Object o)inthashCode()booleanisAddNullValues()If set totruenullelements are added to the contained collector, iffalse, default value,nullvalues are ignored.booleanisEmpty()isEmpty.Iterator<E>iterator()intsize()size.CollectionBuilder<E>sort(Comparator<? super E> comparator)Sorts the contained Collection.Stream<E>stream()stream.E[]toArray(Class<? super E> targetType)toArray.List<E>toConcurrentList()toConcurrentList.NavigableSet<E>toConcurrentNavigableSet()toConcurrentNavigableSet.Set<E>toConcurrentSet()toConcurrentSet.List<E>toImmutableList()toImmutableList.NavigableSet<E>toImmutableNavigableSet()toImmutableNavigableSet.Set<E>toImmutableSet()toImmutableSet.List<E>toMutableList()toMutableList.NavigableSet<E>toMutableNavigableSet()toMutableNavigableSet.Set<E>toMutableSet()toMutableSet.StringtoString()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
-
-
-
Constructor Detail
-
CollectionBuilder
public CollectionBuilder(Collection<E> collector)
Constructor for CollectionBuilder.
- Parameters:
collector- to be used for storage. Must not be null
-
CollectionBuilder
public CollectionBuilder()
Default Constructor initializing the collector with anArrayList
-
-
Method Detail
-
addNullValues
public CollectionBuilder<E> addNullValues(boolean addNullValues)
addNullValues.
- Parameters:
addNullValues- If set totruenullelements are added to the contained collector, iffalse, default value,nullvalues are ignored.- Returns:
- the instance itself in order to use it in a fluent way.
-
size
public int size()
size.
- Returns:
- the size of the contained Collection
-
isEmpty
public boolean isEmpty()
isEmpty.
- Returns:
- see
Collection.isEmpty()
-
contains
public boolean contains(Object o)
contains.
- Parameters:
o- aObjectobject- Returns:
- see
Collection.isEmpty()
-
add
public CollectionBuilder<E> add(E e)
add.
- Parameters:
e- the element to be added- Returns:
- the instance itself in order to use it in a fluent way. Caution: with this
call the return value of
Collection.add(Object)will be ignored.
-
add
public CollectionBuilder<E> add(E... elements)
add.
- Parameters:
elements- to be added- Returns:
- the instance itself in order to use it in a fluent way. Caution: with this
call the return value of
Collection.add(Object)will be ignored.
-
add
public CollectionBuilder<E> add(Iterable<E> elements)
add.
- Parameters:
elements- to be added- Returns:
- the instance itself in order to use it in a fluent way. Caution: with this
call the return value of
Collection.add(Object)will be ignored.
-
add
public CollectionBuilder<E> add(Collection<E> elements)
add.
- Parameters:
elements- to be added- Returns:
- the instance itself in order to use it in a fluent way. Caution: with this
call the return value of
Collection.add(Object)will be ignored.
-
add
public CollectionBuilder<E> add(Optional<E> element)
add.
- Parameters:
element- to be added if present. Caution: passing anOptionalparameter is a not a good thing to do, I know, but in this context it is quite convenient: Don't do this at home- Returns:
- the instance itself in order to use it in a fluent way. Caution: with this
call the return value of
Collection.add(Object)will be ignored.
-
add
public CollectionBuilder<E> add(Stream<E> elements)
add.
- Parameters:
elements- to be added- Returns:
- the instance itself in order to use it in a fluent way. Caution: with this
call the return value of
Collection.add(Object)will be ignored.
-
sort
public CollectionBuilder<E> sort(Comparator<? super E> comparator)
Sorts the contained Collection.- Parameters:
comparator- must not be null.- Returns:
- the instance itself in order to use it in a fluent way.
-
toMutableList
public List<E> toMutableList()
toMutableList.
-
toImmutableList
public List<E> toImmutableList()
toImmutableList.
- Returns:
- an immutable
Listrepresentation of the builders content, the actual implementation callsCollections.unmodifiableList(List). The underlyingCollectionwill be copied by callingtoMutableList()
-
toMutableSet
public Set<E> toMutableSet()
toMutableSet.
-
toImmutableSet
public Set<E> toImmutableSet()
toImmutableSet.
- Returns:
- an immutable
Setrepresentation of the builders content, the actual implementation callsCollections.unmodifiableList(List). The underlyingCollectionwill be copied by callingtoMutableSet()
-
toConcurrentList
public List<E> toConcurrentList()
toConcurrentList.
- Returns:
- a concurrent mutable
Listrepresentation of the builders content, the actual implementation is anCopyOnWriteArrayList
-
toConcurrentSet
public Set<E> toConcurrentSet()
toConcurrentSet.
- Returns:
- a concurrent mutable
Setrepresentation of the builders content, the actual implementation is anCopyOnWriteArraySet
-
toMutableNavigableSet
public NavigableSet<E> toMutableNavigableSet()
toMutableNavigableSet.
- Returns:
- a mutable
NavigableSetrepresentation of the builders content, the actual implementation is anTreeSet. The assumption is that the Actual type is at leastComparable
-
toImmutableNavigableSet
public NavigableSet<E> toImmutableNavigableSet()
toImmutableNavigableSet.
- Returns:
- an immutable
NavigableSetrepresentation of the builders content, the actual implementation is anTreeSetwrapped byCollections.unmodifiableNavigableSet(NavigableSet). The assumption is that the Actual type is at leastComparable
-
toConcurrentNavigableSet
public NavigableSet<E> toConcurrentNavigableSet()
toConcurrentNavigableSet.
- Returns:
- a mutable
NavigableSetrepresentation of the builders content, the actual implementation is anConcurrentSkipListSet. The assumption is that the actual type is at leastComparable
-
toArray
public E[] toArray(Class<? super E> targetType)
toArray.
- Parameters:
targetType- identifying the concrete ArrayType- Returns:
- an array representation of the builders content
-
clear
public CollectionBuilder<E> clear()
Clears the elements in the collector- Returns:
- the instance itself in order to use it in a fluent way.
-
copyFrom
public static <E> CollectionBuilder<E> copyFrom(Iterable<? extends E> source)
Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)- Type Parameters:
E- a E class- Parameters:
source- may be null- Returns:
- the newly created
CollectionBuilder
-
copyFrom
public static <E> CollectionBuilder<E> copyFrom(Iterator<? extends E> source)
Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)- Type Parameters:
E- a E class- Parameters:
source- may be null- Returns:
- the newly created
CollectionBuilder
-
copyFrom
public static <E> CollectionBuilder<E> copyFrom(Collection<? extends E> source)
Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)- Type Parameters:
E- a E class- Parameters:
source- may be null- Returns:
- the newly created
CollectionBuilder
-
copyFrom
public static <E> CollectionBuilder<E> copyFrom(Stream<? extends E> source)
Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)- Type Parameters:
E- a E class- Parameters:
source- may be null- Returns:
- the newly created
CollectionBuilder
-
copyFrom
@SafeVarargs public static <E> CollectionBuilder<E> copyFrom(E... source)
Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)- Type Parameters:
E- a E class- Parameters:
source- may be null- Returns:
- the newly created
CollectionBuilder
-
copyFrom
public static <E> CollectionBuilder<E> copyFrom(E source)
Creates an Instance ofCollectionBuilderby copying the content of the given source Caution: The given source will be used as it is, there will be no filtering as defined withinaddNullValues(boolean)- Type Parameters:
E- a E class- Parameters:
source- may be null- Returns:
- the newly created
CollectionBuilder
-
isAddNullValues
public boolean isAddNullValues()
If set totruenullelements are added to the contained collector, iffalse, default value,nullvalues are ignored.
-
-