@Test @SuppressWarnings("unchecked") public void supportsDifferentAmountOfCandleSticksToAggregate() { final Stream<Price> inputPrices = stream(new double[] { 1.0, 2.0, 3.0, 52.0, 16.0 }) .mapToObj(p -> new Price(p)); final SimpleMovingAveragePrice cut = new SimpleMovingAveragePrice(4); final List<Optional<Price>> prices = inputPrices.map(p -> cut.smooth(p)).collect(toList()); assertThat(prices).containsExactly(empty(), empty(), empty(), opt(new Price(14.5)), opt(new Price(18.25))); }
@Override public Optional<Price> smooth(final Price dataPoint) { prices.add(dataPoint.asPipette()); sum += dataPoint.asPipette(); if (prices.size() < aggregationCount) { return Optional.empty(); } final long returnVal = sum / aggregationCount; sum -= prices.poll(); return Optional.of(new Price(returnVal)); }
SimpleMovingAveragePrice implements Smoother<Price> { @Override public Optional<Price> smooth(final Price dataPoint) { prices.add(dataPoint.asPipette()); sum += dataPoint.asPipette(); if (prices.size() < aggregationCount) { return Optional.empty(); } final long returnVal = sum / aggregationCount; sum -= prices.poll(); return Optional.of(new Price(returnVal)); } }
SimpleMovingAveragePrice implements Smoother<Price> { @Override public Optional<Price> smooth(final Price dataPoint) { prices.add(dataPoint.asPipette()); sum += dataPoint.asPipette(); if (prices.size() < aggregationCount) { return Optional.empty(); } final long returnVal = sum / aggregationCount; sum -= prices.poll(); return Optional.of(new Price(returnVal)); } SimpleMovingAveragePrice(final int aggregationCount); }
SimpleMovingAveragePrice implements Smoother<Price> { @Override public Optional<Price> smooth(final Price dataPoint) { prices.add(dataPoint.asPipette()); sum += dataPoint.asPipette(); if (prices.size() < aggregationCount) { return Optional.empty(); } final long returnVal = sum / aggregationCount; sum -= prices.poll(); return Optional.of(new Price(returnVal)); } SimpleMovingAveragePrice(final int aggregationCount); @Override Optional<Price> smooth(final Price dataPoint); }
SimpleMovingAveragePrice implements Smoother<Price> { @Override public Optional<Price> smooth(final Price dataPoint) { prices.add(dataPoint.asPipette()); sum += dataPoint.asPipette(); if (prices.size() < aggregationCount) { return Optional.empty(); } final long returnVal = sum / aggregationCount; sum -= prices.poll(); return Optional.of(new Price(returnVal)); } SimpleMovingAveragePrice(final int aggregationCount); @Override Optional<Price> smooth(final Price dataPoint);  }