RollingColumn { @SuppressWarnings({"unchecked"}) public <INCOL extends Column<?>, OUT> Column<?> calc(AggregateFunction<INCOL, OUT> function) { Column<?> result = function.returnType().create(generateNewColumnName(function)); for (int i = 0; i < window - 1; i++) { result.appendMissing(); } for (int origColIndex = 0; origColIndex < column.size() - window + 1; origColIndex++) { Selection selection = new BitmapBackedSelection(); selection.addRange(origColIndex, origColIndex + window); INCOL subsetCol = (INCOL) column.subset(selection.toArray()); OUT answer = function.summarize(subsetCol); if (answer instanceof Number) { Number number = (Number) answer; ((DoubleColumn) result).append(number.doubleValue()); } else { result.appendObj(answer); } } return result; } RollingColumn(Column<?> column, int window); @SuppressWarnings({"unchecked"}) Column<?> calc(AggregateFunction<INCOL, OUT> function);  }
@Test public void testRollingMaxDate() { LocalDateTime[] data = new LocalDateTime[] { LocalDate.of(2011, 1, 1).atStartOfDay(), LocalDate.of(2011, 1, 3).atStartOfDay(), LocalDate.of(2011, 1, 5).atStartOfDay(), LocalDate.of(2011, 1, 7).atStartOfDay(), LocalDate.of(2011, 1, 9).atStartOfDay() }; LocalDateTime[] sma5 = new LocalDateTime[] { null, LocalDate.of(2011, 1, 3).atStartOfDay(), LocalDate.of(2011, 1, 5).atStartOfDay(), LocalDate.of(2011, 1, 7).atStartOfDay(), LocalDate.of(2011, 1, 9).atStartOfDay() }; DateTimeColumn result = (DateTimeColumn) DateTimeColumn.create("data", data).rolling(2).calc(latestDateTime); assertArrayEquals(sma5, result.asObjectArray()); }
@Test public void testRollingCountTrue() { Boolean[] data = new Boolean[] {true, false, false, true, true}; BooleanColumn booleanColumn = BooleanColumn.create("data", data); DoubleColumn result = (DoubleColumn) booleanColumn.rolling(2).calc(countTrue); assertEquals(Double.NaN, result.getDouble(0), 0.0); assertEquals(1, result.getDouble(1), 0.0); assertEquals(0, result.getDouble(2), 0.0); assertEquals(1, result.getDouble(3), 0.0); assertEquals(2, result.getDouble(4), 0.0); }