WeightedSnapshot extends Snapshot { @Override public double getMean() { if (values.length == 0) { return 0; } double sum = 0; for (int i = 0; i < values.length; i++) { sum += values[i] * normWeights[i]; } return sum; } WeightedSnapshot(Collection<WeightedSample> 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 calculatesTheMeanValue() { assertThat(snapshot.getMean()) .isEqualTo(2.7); }
@Test public void calculatesAMeanOfZeroForAnEmptySnapshot() { final Snapshot emptySnapshot = new WeightedSnapshot( weightedArray(new long[]{}, new double[]{})); assertThat(emptySnapshot.getMean()) .isZero(); }
@Test public void expectNoOverflowForLowWeights() { final Snapshot scatteredSnapshot = new WeightedSnapshot( weightedArray( new long[]{1, 2, 3}, new double[]{Double.MIN_VALUE, Double.MIN_VALUE, Double.MIN_VALUE} ) ); assertThat(scatteredSnapshot.getMean()) .isEqualTo(2); }
@Test public void doesNotProduceNaNValues() { WeightedSnapshot weightedSnapshot = new WeightedSnapshot( weightedArray(new long[]{1, 2, 3}, new double[]{0, 0, 0})); assertThat(weightedSnapshot.getMean()).isEqualTo(0); }