NegativeValueDoubleTimeSeriesFilter extends TimeSeriesFilter { @Override public FilteredTimeSeries evaluate(final LocalDateDoubleTimeSeries ts) { ArgumentChecker.notNull(ts, "ts"); if (ts.isEmpty()) { s_logger.info("Time series was empty"); return new FilteredTimeSeries(EMPTY_SERIES, EMPTY_SERIES); } final int n = ts.size(); final int[] filteredDates = new int[n]; final double[] filteredData = new double[n]; final int[] rejectedDates = new int[n]; final double[] rejectedData = new double[n]; final LocalDateDoubleEntryIterator it = ts.iterator(); int i = 0, j = 0; while (it.hasNext()) { int date = it.nextTimeFast(); double value = it.currentValue(); if (value < 0) { rejectedDates[j] = date; rejectedData[j++] = value; } else { filteredDates[i] = date; filteredData[i++] = value; } } return getFilteredSeries(filteredDates, filteredData, i, rejectedDates, rejectedData, j); } NegativeValueDoubleTimeSeriesFilter(); @Override FilteredTimeSeries evaluate(final LocalDateDoubleTimeSeries ts);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullTS() { FILTER.evaluate((LocalDateDoubleTimeSeries) null); }
@Test public void testEmptyTS() { final FilteredTimeSeries filtered = FILTER.evaluate(EMPTY_SERIES); assertEquals(filtered.getFilteredTS(), EMPTY_SERIES); assertEquals(filtered.getRejectedTS(), EMPTY_SERIES); }
@Test public void test() { final int n = 100; final LocalDate[] dates = new LocalDate[n]; final double[] data = new double[n]; final LocalDate[] filteredDates = new LocalDate[n]; final double[] filteredData = new double[n]; final LocalDate[] rejectedDates = new LocalDate[n]; final double[] rejectedData = new double[n]; Double d; int j = 0, k = 0; for (int i = 0; i < n; i++) { d = RANDOM.nextDouble(); dates[i] = LocalDate.ofEpochDay(i); if (d < 0.25) { data[i] = -d; rejectedDates[k] = LocalDate.ofEpochDay(i); rejectedData[k++] = -d; } else { data[i] = d; filteredDates[j] = LocalDate.ofEpochDay(i); filteredData[j++] = d; } } final FilteredTimeSeries result = FILTER.evaluate(ImmutableLocalDateDoubleTimeSeries.of(dates, data)); assertEquals(result, new FilteredTimeSeries(ImmutableLocalDateDoubleTimeSeries.of(Arrays.copyOf(filteredDates, j), Arrays.copyOf(filteredData, j)), ImmutableLocalDateDoubleTimeSeries.of(Arrays.copyOf(rejectedDates, k), Arrays.copyOf(rejectedData, k)))); }