Here is an example of code that uses the jAgg API:
The Record class is an example class that stores data to aggregate:
public class Record { private String trialClass; private float testResult; public Record(String trialClass, float result) { this.trialClass = trialClass; testResult = result; } public String getTrialClass() { return trialClass; } public float getTestResult() { return testResult; } }
The following is code that generates a List of Records and calls the jAgg API:
List<Record> rawData = new ArrayList<Record>(); rawData.add(new Record("placebo", 60.5)); rawData.add(new Record("placebo", 62.5)); rawData.add(new Record("placebo", 58.5)); rawData.add(new Record("newDrug", 80.5)); rawData.add(new Record("newDrug", 83.5)); rawData.add(new Record("newDrug", 77.5)); List<String> properties = new ArrayList<String>(); properties.add("trialClass"); List<Aggregator> aggregators = new ArrayList<Aggregator>(); aggregators.add(new CountAggregator("*")); aggregators.add(new AvgAggregator("testResult")); aggregators.add(new StdDevAggregator("testResult")); List<AggregateValue<Record>> aggValues = Aggregations.groupBy( rawData, properties, aggregators); for (AggregateValue<Record> aggValue : aggValues ) { Record r = aggValue.getObject(); StringBuffer buf = new StringBuffer(); buf.append(r.getTrialClass()); buf.append(":"); for (Aggregator aggregator : aggregators) { buf.append(" "); buf.append(aggregator.toString()); buf.append("="); buf.append(aggValue.getAggregateValue(aggregator)); } System.out.println(buf.toString()); }
Alternatively, using Multiset Discrimination, one would replace the call to "groupBy" above with the following line:
List<AggregateValue<Record>> aggValues = Aggregations.groupBy( rawData, properties, aggregators, true);
This code would output the following:
newDrug: Count(*)=3 Avg(testResult)=80.5 StdDev(testResult)=3 placebo: Count(*)=3 Avg(testResult)=60.5 StdDev(testResult)=2