jAgg Aggregations Algorithm
The Aggregations class uses the same general algorithm no matter which overloaded
“groupBy” method is called. This may help to understand what the Aggregator class's
abstract methods are meant to accomplish.
- Create a shallow copy of the given List of Ts.
- Sort the list copy according to either the “compare” method if T is Comparable,
using an internal adapter Comparator class ("ComparableComparator"), or by
using another internal Comparator class (“PropertiesComparator”) based on the
supplied “group by” properties. Call Collections.sort(Collection, Comparator).
Alternatively, if so specified, instead of sorting, perform
Multiset Discrimination based on the supplied "group by"
properties.
- If parallel processing is indicated and desired, then break down the list of
Ts into multiple sections that different Threads will process. Different
Threads will get different Aggregators, even for the same property, so that
Aggregators do not need to be thread-safe.
- For each run of Ts that compare equal, do the following:
- Get an Aggregator for each caller-specified Aggregator using “getAggregator”.
- Initialize each Aggregator by calling “init”.
- For each T in the run, call “iterate” on each Aggregator, passing in the T
object.
- If parallel processing is indicated and desired, call “merge” on some
Aggregators to merge other Aggregators’ states into one. This occurs
when a run of T objects that compare the same is split between threads.
- Call “terminate” on each Aggregator that’s left, to get the final aggregate
results.
- Create an object of type AggregateValue<T> and pass it the first T of
the run. Add aggregate results into the AggregateValue’s internal HashMap,
keying on the Aggregator object.
- Return a List of AggregateValues back to the caller.