java.lang.Object
de.cuioss.tools.collect.CollectionBuilder<E>
- Type Parameters:
E- The type of the containedCollection
- All Implemented Interfaces:
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 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
-
Constructor Summary
ConstructorsConstructorDescriptionDefault Constructor initializing the collector with anArrayListCollectionBuilder(Collection<E> collector) Constructor for CollectionBuilder. -
Method Summary
Modifier and TypeMethodDescriptionadd.final CollectionBuilder<E>add.add.add(Collection<E> elements) add.add.add.addNullValues(boolean addNullValues) addNullValues.clear()Clears the elements in the collectorbooleancontains.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>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>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>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)booleanisEmpty()isEmpty.iterator()intsize()size.sort(Comparator<? super E> comparator) Sorts the contained Collection.stream()stream.E[]toArray.toConcurrentList.toConcurrentNavigableSet.toConcurrentSet.toImmutableList.toImmutableNavigableSet.toImmutableSet.toMutableList.toMutableNavigableSet.toMutableSet.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
CollectionBuilder
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 Details
-
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
size.
- Returns:
- the size of the contained Collection
-
isEmpty
isEmpty.
- Returns:
- see
Collection.isEmpty()
-
contains
contains.
- Parameters:
o- aObjectobject- Returns:
- see
Collection.isEmpty()
-
iterator
-
stream
stream.
- Returns:
- a
Streamon the contained objects
-
add
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
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
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
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
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
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
Sorts the contained Collection.- Parameters:
comparator- must not be null.- Returns:
- the instance itself in order to use it in a fluent way.
-
toMutableList
toMutableList.
-
toImmutableList
toImmutableList.
- Returns:
- an immutable
Listrepresentation of the builders content, the actual implementation callsCollections.unmodifiableList(List). The underlyingCollectionwill be copied by callingtoMutableList()
-
toMutableSet
toMutableSet.
-
toImmutableSet
toImmutableSet.
- Returns:
- an immutable
Setrepresentation of the builders content, the actual implementation callsCollections.unmodifiableList(List). The underlyingCollectionwill be copied by callingtoMutableSet()
-
toConcurrentList
toConcurrentList.
- Returns:
- a concurrent mutable
Listrepresentation of the builders content, the actual implementation is anCopyOnWriteArrayList
-
toConcurrentSet
toConcurrentSet.
- Returns:
- a concurrent mutable
Setrepresentation of the builders content, the actual implementation is anCopyOnWriteArraySet
-
toArray
toArray.
- Parameters:
targetType- identifying the concrete ArrayType- Returns:
- an array representation of the builders content
-
clear
Clears the elements in the collector- Returns:
- the instance itself in order to use it in a fluent way.
-
copyFrom
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
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
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
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
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
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
-