T
- the type of objects to be collectedpublic class DataCollector<T> extends Object implements Consumer<T>
When asking methods to create or populate a List
it's easier to create and pass along a
Collector instead of having each method creating its own list and joining them afterwards.
By subclassing DataCollector one can also directly process the given values instead of just storing them in a list.
A typical use-case is:
DataCollector<String> collector = new DataCollector<String>();
computeStrings1(collector);
computeStrings2(collector);
collector.getData(); // use values
If a sorted list is required which does not depend on insertion order but on a given priority of each entry
PriorityCollector
can be used.
PriorityCollector
Constructor and Description |
---|
DataCollector() |
Modifier and Type | Method and Description |
---|---|
void |
accept(T t) |
void |
add(T entity)
Adds a value to the collector
|
void |
addAll(Collection<? extends T> entities)
Adds all values of the given collection to the collector
|
static <T> DataCollector<T> |
create()
Creates a new DataCollector.
|
List<T> |
getData()
Returns the List of values which where added to the collector so far.
|
String |
toString() |
public static <T> DataCollector<T> create()
Boilerplate method, so one doesn't need to re-type the type parameters.
T
- the type of objects created by the data collectorpublic void add(T entity)
entity
- contains the value to be added to the collector.public void addAll(@Nonnull Collection<? extends T> entities)
entities
- the collection of values added to the collector.@Nonnull public List<T> getData()
For the sake of simplicity, this returns the internally used list. Therefore modifying this list, modifies the collector.
Copyright © 2018. All rights reserved.