StatisticsService { public static Collection<Statistics> aggregateHistoricalStatistics(List<List<Statistics>> statisticsLists){ if(statisticsLists.isEmpty()) return Collections.emptyList(); if(statisticsLists.size()==1){ return statisticsLists.iterator().next(); } List<Statistics> aggregatedStatistics = new ArrayList<>(); int numberOfRecords = statisticsLists.get(0).size(); List<Statistics> unaggregatedStatistics; for (int i=0; i<numberOfRecords; i++){ unaggregatedStatistics = new ArrayList<>(); for (List<Statistics> orgStats : statisticsLists){ if(orgStats.isEmpty()) continue; unaggregatedStatistics.add(orgStats.get(i)); } Statistics aggregatedRecord = aggregateStatistics(unaggregatedStatistics); aggregatedStatistics.add(aggregatedRecord); } return aggregatedStatistics; }  static Collection<Statistics> getStatistics(StatisticsRepository repository, Collection<String> organizations, String startDate, String endDate); static Statistics aggregateStatistics(Collection<Statistics> statistics); static Collection<Statistics> aggregateHistoricalStatistics(List<List<Statistics>> statisticsLists);  }
@Test public void testAggregateHistoricalStatistics(){ List<List<Statistics>> history = generateStatisticHistory(); Collection<Statistics> aggregatedHistory = StatisticsService.aggregateHistoricalStatistics(history); Assert.assertNotNull(aggregatedHistory); Assert.assertEquals(3, aggregatedHistory.size()); Iterator<Statistics> iter = aggregatedHistory.iterator(); checkStatisticsRecord(history, 0, iter.next()); checkStatisticsRecord(history, 1, iter.next()); checkStatisticsRecord(history, 2, iter.next()); }