The Java Embedded Aggregator Class CompoundAggregate

CompoundAggregate implements the "compound aggregate": i.e. the combination of several aggregates. (Note that this can calculate more than one aggregate on one traversal).

For an overview see page Java Aggregator Class

Class Definition

 
    public static class CompoundAggregate implements Aggregate
    {
        public void initialize(Object val) 
        {
            for (Aggregate agg : aggregates) 
            {
                agg.initialize(val);
            }
        }
 
        public void accumulate(Object val) 
        {
            for (Aggregate agg : aggregates) 
            {
                agg.accumulate(val);
            }
        }
 
        public Object result() 
        {
            Object[] arr = new Object[aggregates.length];
            for (int i = 0; i < aggregates.length; i++) 
            {
                arr[i] = aggregates[i].result();
            }
            return arr;
        }
 
        public void merge(Aggregate other) 
        {
            Aggregate[] otherAggregates = ((CompoundAggregate)other).aggregates;
            for (int i = 0; i < otherAggregates.length; i++) 
            {
                aggregates[i].merge(otherAggregates[i]);
            }
        }
 
        public CompoundAggregate(Aggregate... aggs) 
        {
            aggregates = aggs;
        }
 
        Aggregate[] aggregates;
    }