T
- the type of values to be collectedpublic class PriorityCollector<T> extends Object
DataCollector
with an externally supplied order.
When asking methods to create or populate a List
it's easier to create and pass along a
PriorityCollector instead of having each method creating its own list and joining them afterwards.
Using a PriorityCollector, several methods can be supplied with the same instance and generate a list or a
custom (externally) ordering. This greatly simplifies creating extensible systems which are enhanced by
sub-components and where order of elements matter. The final list is sorted by comparing the priority.
Therefore if a.priority < b.priority
then a will occur before b in the list.
If the order of the provided elements does not matter, a DataCollector
can be used.
DataCollector
Modifier and Type | Field and Description |
---|---|
protected List<ComparableTuple<Integer,T>> |
data |
static int |
DEFAULT_PRIORITY
Provides a default constant which can be used if a collector is pre-populated with standard values and then
enhanced by sub modules.
|
Constructor and Description |
---|
PriorityCollector() |
Modifier and Type | Method and Description |
---|---|
void |
add(int priority,
T entity)
Adds the given entity with the given priority.
|
void |
addDefault(T entity)
Adds the given entity with the default priority.
|
static <T> PriorityCollector<T> |
create()
Creates a new PriorityCollector.
|
List<T> |
getData()
Returns the list of added entities sorted by priority.
|
int |
size()
Returns the number of items added to this collector.
|
String |
toString() |
public static final int DEFAULT_PRIORITY
Using an agreed upon standard value makes it easy for component creators to provide values which will be inserted before or after the default value.
protected List<ComparableTuple<Integer,T>> data
public static <T> PriorityCollector<T> create()
Boilerplate method, so one doesn't need to re-type the type parameters.
T
- the type of value collect by the collectorpublic void add(int priority, T entity)
priority
- the priority to use for this element.entity
- the entity added to the collector.public void addDefault(T entity)
entity
- the entity added to the collector.DEFAULT_PRIORITY
public List<T> getData()
The comparator used is < - therefore if a.priority < b.priority
then a will occur before b in the list.
public int size()
Copyright © 2018. All rights reserved.