### Question:
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);  }### Answer:
@Test public void init() { WeightedSnapshot.WeightedSample sample = new WeightedSnapshot.WeightedSample(1L, 0.0); WeightedSnapshot snapshot = new WeightedSnapshot(Collections.singletonList(sample)); assertFalse(Double.isNaN(snapshot.getMean())); }
@Test public void calculatesTheMeanValue() throws Exception { assertThat(snapshot.getMean()) .isEqualTo(2.7); }
@Test public void calculatesAMeanOfZeroForAnEmptySnapshot() throws Exception { final Snapshot emptySnapshot = new WeightedSnapshot( WeightedArray(new long[]{}, new double[]{}) ); assertThat(emptySnapshot.getMean()) .isZero(); }
@Test public void expectNoOverflowForLowWeights() throws Exception { 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); }