StdDeviation extends AbstractCollectionFunction<I,I> { @Override public I compute(Collector<I> collector) { I avg = averager.compute(collector); List<I> deviations = new ArrayList<I>(collector.collected()); for (I value : collector.all()) { deviations.add( summer.subtract(avg, value)); } List<I> squares = new ArrayList<I>(collector.collected()); for (I dev : deviations) { squares.add( multiplier.multiply(dev, dev)); } I variance = averager.compute( asFinalCollector(squares)); return rooter.squareRoot(variance); } private  StdDeviation(Summer<I> theSummer, Averager<I> theAverager, Multiply<I> theMult, SquareRoot<I> theRoot); @Override Collector<I> createCollector(); @Override I compute(Collector<I> collector); static final String Id; static final StdDeviation<Integer> IntDev; static final StdDeviation<Long> LongDev; static final StdDeviation<Float> FloatDev; static final StdDeviation<Double> DoubleDev; static final Map<Class<?>, Function<?,?>> StdDeviationByType; }
@Test public void computeInts() { for (String[] test : Tests) { Integer expectedResult = Integer.parseInt(test[0]); List<Integer> nums = intsOf(test); Collector<Integer> collector = asFinalCollector(nums); Integer result = StdDeviation.IntDev.compute(collector); Assert.assertEquals(result, expectedResult); } }
@Test public void computeLongs() { for (String[] test : Tests) { Long expectedResult = Long.parseLong(test[0]); List<Long> nums = longsOf(test); Collector<Long> collector = asFinalCollector(nums); Long result = StdDeviation.LongDev.compute(collector); Assert.assertEquals(result, expectedResult); } }