AggregateFunction Interface
To accomplish the aggregate functionality desired, jAgg uses a cache of objects
that implement the AggregateFunction interface to perform the actual
aggregation calculations. These objects are analogous to the Oracle object types
that can be created for custom aggregation functions. The AggregateFunction
interface does the following:
- Defines the interface that the Aggregation class expects.
- Calculates an aggregate value based on all values in the original List of Objects
that have equivalent “group by” properties.
Implementing classes implement the methods init,
iterate, merge, and terminate that do the actual
aggregation. They also implement the method replicate to return a
new AggregateFunction of the same type, but uninitialized.
Typically, AggregateFunction is not implemented directly. The abstract
class Aggregator implements this interface and
and supplies common functionality. All jAgg built-in aggregators subclass
Aggregator.
Methods
The following methods are declared by the AggregateFunction interface.
The example followed in the explanations for each method below are for a function that
computes an average of property values.
- void init() - Initialize the state of this AggregateFunction.
Example: reset an internal count and an internal sum to 0.
- void iterate(Object value) - Extract the property from the value
and add the property to the function's state. Example: add the property to the
running sum and increment the running count.
- void merge(AggregateFunction func) - Merge the state of another
AggregateFunction into this one. Example: Add the other function's sum and count
to the internal sum and count, respectively.
- Object terminate() - Return the result, based on the internal
state accumulated so far. Example: Divide the internal sum by the internal count,
returning a Double.
- AggregateFunction replicate() - Return an uninitialized copy of
this aggregate function.
- There are a couple more methods to implement that are typically provided by
the abstract implementing class Aggregator.