UniformSnapshot extends AbstractSnapshot { @Override public double getStdDev() { if (values.length <= 1) { return 0; } final double mean = getMean(); double sum = 0; for (long value : values) { final double diff = value - mean; sum += diff * diff; } final double variance = sum / (values.length - 1); return Math.sqrt(variance); } UniformSnapshot(Collection<Long> values);  UniformSnapshot(long[] values); @Override double getValue(double quantile); @Override int size(); @Override long[] getValues(); @Override long getMax(); @Override long getMin(); @Override double getMean(); @Override double getStdDev(); @Override void dump(OutputStream output);  }
@Test public void calculatesTheStdDev() throws Exception { assertThat(snapshot.getStdDev()) .isEqualTo(1.5811, offset(0.0001)); }
@Test public void calculatesAStdDevOfZeroForAnEmptySnapshot() throws Exception { final Snapshot emptySnapshot = new UniformSnapshot(new long[]{ }); assertThat(emptySnapshot.getStdDev()) .isZero(); }
@Test public void calculatesAStdDevOfZeroForASingletonSnapshot() throws Exception { final Snapshot singleItemSnapshot = new UniformSnapshot(new long[]{ 1 }); assertThat(singleItemSnapshot.getStdDev()) .isZero(); }