DateTimePeriodDataUtil { @SuppressWarnings("unchecked") public static <T extends DateTimePeriodData> void fill(Class<T> type, Map<DateTime,T> dataset, List<DateTimePeriod> periods) { for (DateTimePeriod period : periods) { if (dataset.containsKey(period.getStart())) { } else { try { Constructor ctor = type.getConstructor(DateTimePeriod.class); T data = (T)ctor.newInstance(period); dataset.put(period.getStart(), data); } catch (Exception e) { throw new RuntimeException("Failed to create default dataset entry", e); } } } } private  DateTimePeriodDataUtil(); @SuppressWarnings("unchecked") static void fill(Class<T> type, Map<DateTime,T> dataset, List<DateTimePeriod> periods);  }
@Test public void fill() throws Exception { DateTime start = new DateTime(2009, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC); DateTimePeriod period = new DateTimeHour(start, start.plusHours(4)); TreeMap<DateTime, SampleData> emptyMap = new TreeMap<DateTime, SampleData>(); DateTimePeriodDataUtil.fill(SampleData.class, emptyMap, period.toHours()); SampleData[] result0 = emptyMap.values().toArray(new SampleData[0]); Assert.assertArrayEquals(new SampleData[] { new SampleData(DateTimePeriod.createHour(2009, 1, 1, 0, DateTimeZone.UTC), null), new SampleData(DateTimePeriod.createHour(2009, 1, 1, 1, DateTimeZone.UTC), null), new SampleData(DateTimePeriod.createHour(2009, 1, 1, 2, DateTimeZone.UTC), null), new SampleData(DateTimePeriod.createHour(2009, 1, 1, 3, DateTimeZone.UTC), null) }, result0); SampleData hour0 = new SampleData(DateTimePeriod.createHour(2009, 1, 1, 0, DateTimeZone.UTC), "hour0"); SampleData hour2 = new SampleData(DateTimePeriod.createHour(2009, 1, 1, 2, DateTimeZone.UTC), "hour2"); TreeMap<DateTime, SampleData> partialMap = new TreeMap<DateTime, SampleData>(); partialMap.put(hour0.getPeriod().getStart(), hour0); partialMap.put(hour2.getPeriod().getStart(), hour2); DateTimePeriodDataUtil.fill(SampleData.class, partialMap, period.toHours()); result0 = partialMap.values().toArray(new SampleData[0]); Assert.assertArrayEquals(new SampleData[] { new SampleData(DateTimePeriod.createHour(2009, 1, 1, 0, DateTimeZone.UTC), "hour0"), new SampleData(DateTimePeriod.createHour(2009, 1, 1, 1, DateTimeZone.UTC), null), new SampleData(DateTimePeriod.createHour(2009, 1, 1, 2, DateTimeZone.UTC), "hour2"), new SampleData(DateTimePeriod.createHour(2009, 1, 1, 3, DateTimeZone.UTC), null) }, result0); }