PreviousAndFirstValuePaddingTimeSeriesSamplingFunction 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 double[] scheduledData = new double[schedule.length]; int dateIndex = 0; for (int i = 0; i < schedule.length; i++) { final LocalDate localDate = schedule[i]; if (dateIndex < tsDates.length) { if (tsDates[dateIndex].equals(localDate)) { scheduledData[i] = values[dateIndex]; dateIndex++; continue; } while (dateIndex < tsDates.length && tsDates[dateIndex].isBefore(localDate)) { dateIndex++; } if (dateIndex < tsDates.length && tsDates[dateIndex].equals(localDate)) { scheduledData[i] = values[dateIndex]; dateIndex++; continue; } } if (dateIndex > 0) { scheduledData[i] = values[dateIndex - 1]; } else { scheduledData[i] = values[0]; } } return ImmutableLocalDateDoubleTimeSeries.of(schedule, 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 testMissingFirstData() { final LocalDate start = START.minusDays(21); final LocalDate[] daily = HOLIDAY_REMOVER.getStrippedSchedule(DAILY.getSchedule(start, END, true, true), WEEKEND_CALENDAR); final List<LocalDate> t = new ArrayList<>(); final List<Double> d = new ArrayList<>(); for (int i = 0; i < daily.length; i++) { if (daily[i].isAfter(START)) { t.add(daily[i]); d.add(Double.valueOf(i)); } } final LocalDateDoubleTimeSeries ts = ImmutableLocalDateDoubleTimeSeries.of(t, d); final LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(ts, daily); assertEquals(result.size(), daily.length); final int offset = 16; int i = 0; for (final Entry<LocalDate, Double> entry : result) { assertEquals(entry.getKey(), daily[i]); if (i < 16) { assertEquals(entry.getValue(), offset, 0); } else { assertEquals(entry.getValue(), i, 0); } i++; } }
@Test public void testNoMissingDataDaily() { final LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_NO_MISSING_DATA, DAILY_SCHEDULE); assertEquals(result.size(), DAILY_SCHEDULE.length); int i = 0; for (final Entry<LocalDate, Double> entry : result) { assertEquals(entry.getKey(), DAILY_SCHEDULE[i]); assertEquals(entry.getValue(), i++, 0); } }
@Test public void testMissingDataWeekly() { final LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_TWO_MISSING_DATA_POINTS, MONDAY_SCHEDULE); assertEquals(result.size(), MONDAY_SCHEDULE.length); int i = 0; int j = 2; for (final Entry<LocalDate, Double> entry : result) { assertEquals(entry.getKey(), MONDAY_SCHEDULE[i++]); if (entry.getKey().equals(MISSING_DAY_MONDAY_1) || entry.getKey().equals(MISSING_DAY_MONDAY_2)) { assertEquals(entry.getValue(), j - 1, 0); } else { assertEquals(entry.getValue(), j, 0); } j += 5; } }
@Test public void testDaysMissingDataDaily() { final LocalDateDoubleTimeSeries result = F.getSampledTimeSeries(TS_TWO_MISSING_DATA_POINTS, 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_MONDAY_1) || entry.getKey().equals(MISSING_DAY_MONDAY_2)) { assertEquals(entry.getValue(), i - 1, 0); } else { assertEquals(entry.getValue(), i, 0); } i++; } }