Average extends AbstractAggregator implements Aggregator { @Override public void accept(DataSetRow row, AggregationResult result) { final String key = row.get(groupBy); if (!result.contains(key)) { result.put(key, new AverageContext()); } final AverageContext context = (AverageContext) result.get(key); context.process(row.get(columnId)); } Average(String groupBy, String columnId); @Override void accept(DataSetRow row, AggregationResult result); @Override void normalize(AggregationResult result);  }
@Test public void shouldComputeAverage() { AggregationResult result = new AggregationResult(Operator.AVERAGE); aggregator.accept(getRow("toto", "10"), result); aggregator.accept(getRow("toto", "2"), result); aggregator.accept(getRow("toto", "3.6"), result); aggregator.accept(getRow("toto", ""), result); aggregator.accept(getRow("toto", "8.2"), result); aggregator.accept(getRow("tata", "10"), result); aggregator.accept(getRow("toto", "-8"), result); aggregator.accept(getRow("toto", "12.3"), result); aggregator.accept(getRow("tata", "5"), result); final Average.AverageContext toto = (Average.AverageContext) result.get("toto"); assertEquals(4.683d, toto.getValue(), 0.001d); final Average.AverageContext tata = (Average.AverageContext) result.get("tata"); assertEquals(7.5d, tata.getValue(), 0); }