PreviousValuePaddingTimeSeriesSamplingFunction implements TimeSeriesSamplingFunction { @Override public LocalDateDoubleTimeSeries getSampledTimeSeries(final DateDoubleTimeSeries<?> ts, final LocalDate[] schedule) { ArgumentChecker.notNull(ts, "time series"); ArgumentChecker.notNull(schedule, "schedule"); final LocalDateDoubleTimeSeries localDateTS = ImmutableLocalDateDoubleTimeSeries.of(ts); final LocalDate[] tsDates = localDateTS.timesArray(); final double[] values = localDateTS.valuesArrayFast(); final List<LocalDate> scheduledDates = new ArrayList<>(); final List<Double> scheduledData = new ArrayList<>(); int dateIndex = 0; for (final LocalDate localDate : schedule) { if (dateIndex < tsDates.length) { if (tsDates[dateIndex].equals(localDate)) { scheduledDates.add(localDate); scheduledData.add(values[dateIndex]); dateIndex++; continue; } while (dateIndex < tsDates.length && tsDates[dateIndex].isBefore(localDate)) { dateIndex++; } if (dateIndex < tsDates.length && tsDates[dateIndex].equals(localDate)) { scheduledDates.add(localDate); scheduledData.add(values[dateIndex]); dateIndex++; continue; } } if (dateIndex > 0) { scheduledDates.add(localDate); scheduledData.add(values[dateIndex - 1]); } } return ImmutableLocalDateDoubleTimeSeries.of(scheduledDates, scheduledData); }  @Override LocalDateDoubleTimeSeries getSampledTimeSeries(final DateDoubleTimeSeries<?> ts, final LocalDate[] schedule);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullTS() { F.getSampledTimeSeries(null, DAILY_SCHEDULE); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullSchedule() { F.getSampledTimeSeries(TS_NO_MISSING_DATA, null); }
@Test public void testNoMissingDataDaily() { final LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_NO_MISSING_DATA, DAILY_SCHEDULE); assertEquals(TS_NO_MISSING_DATA.size(), result.size()); int i = 0; for (final Entry<LocalDate, Double> entry : result) { assertEquals(TS_NO_MISSING_DATA.getTimeAtIndex(i), entry.getKey()); assertEquals(TS_NO_MISSING_DATA.getValueAtIndex(i++), entry.getValue(), 0); } }
@Test public void testNoMissingDataWeekly() { final LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_NO_MISSING_DATA, TUESDAY_SCHEDULE); assertEquals(TUESDAY_SCHEDULE.length, result.size()); int i = 0, j = 3; for (final Entry<LocalDate, Double> entry : result) { assertEquals(TUESDAY_SCHEDULE[i++], entry.getKey()); assertEquals(j, entry.getValue(), 0); j += 5; } }
@Test public void testOneDayMissingDataDaily() { LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_ONE_MISSING_DAY, DAILY_SCHEDULE); assertEquals(TS_NO_MISSING_DATA.size(), result.size()); int i = 0; result = F.getSampledTimeSeries(TS_ONE_MISSING_DAY, DAILY_SCHEDULE); assertEquals(TS_NO_MISSING_DATA.size(), result.size()); i = 0; for (final Entry<LocalDate, Double> entry : result) { assertEquals(TS_NO_MISSING_DATA.getTimeAtIndex(i), entry.getKey()); if (entry.getKey().equals(MISSING_DAY_FRIDAY)) { assertEquals(TS_NO_MISSING_DATA.getValueAtIndex(i - 1), entry.getValue(), 0); } else { assertEquals(TS_NO_MISSING_DATA.getValueAtIndex(i), entry.getValue(), 0); } i++; } }
@Test public void testMissingDataWeekly() { LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_ONE_MISSING_DAY, FRIDAY_SCHEDULE); assertEquals(FRIDAY_SCHEDULE.length, result.size()); int i = 0, j = 1; for (final Entry<LocalDate, Double> entry : result) { assertEquals(FRIDAY_SCHEDULE[i++], entry.getKey()); if (entry.getKey().equals(MISSING_DAY_FRIDAY)) { assertEquals(j - 1, entry.getValue(), 0); } else { assertEquals(j, entry.getValue(), 0); } j += 5; } result = F.getSampledTimeSeries(TS_THREE_MISSING_DAYS, MONDAY_SCHEDULE); assertEquals(MONDAY_SCHEDULE.length, result.size()); i = 0; j = 2; for (final Entry<LocalDate, Double> entry : result) { assertEquals(MONDAY_SCHEDULE[i++], entry.getKey()); if (entry.getKey().equals(MISSING_DAY_MONDAY_1)) { assertEquals(j - 2, entry.getValue(), 0); } else if (entry.getKey().equals(MISSING_DAY_MONDAY_2)) { assertEquals(j - 1, entry.getValue(), 0); } else { assertEquals(j, entry.getValue(), 0); } j += 5; } }
@Test public void testThreeDaysMissingDataDaily() { final LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_THREE_MISSING_DAYS, DAILY_SCHEDULE); assertEquals(result.size(), DAILY_SCHEDULE.length); int i = 0; for (final Entry<LocalDate, Double> entry : result) { assertEquals(entry.getKey(), DAILY_SCHEDULE[i]); if (entry.getKey().equals(MISSING_DAY_FRIDAY)) { assertEquals(entry.getValue(), i - 1, 0); } else if (entry.getKey().equals(MISSING_DAY_MONDAY_1)) { assertEquals(entry.getValue(), i - 2, 0); } else if (entry.getKey().equals(MISSING_DAY_MONDAY_2)) { assertEquals(entry.getValue(), i - 1, 0); } else { assertEquals(entry.getValue(), i, 0); } i++; } }
@Test public void testMissingMonth() { final LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_MISSING_MONTH, DAILY_SCHEDULE); assertEquals(result.size(), DAILY_SCHEDULE.length); int i = 0; for (final Entry<LocalDate, Double> entry : result) { assertEquals(entry.getKey(), DAILY_SCHEDULE[i]); if (entry.getKey().getMonth() == Month.FEBRUARY && entry.getKey().getYear() == 2009) { assertEquals(entry.getValue(), 21, 0); } else { assertEquals(entry.getValue(), i, 0); } i++; } }