WeightedSnapshot extends AbstractSnapshot { @Override public double getValue(double quantile) { if (quantile < 0.0 || quantile > 1.0 || Double.isNaN( quantile )) { throw new IllegalArgumentException(quantile + " is not in [0..1]"); } if (values.length == 0) { return 0.0; } int posx = Arrays.binarySearch(quantiles, quantile); if (posx < 0) posx = ((-posx) - 1) - 1; if (posx < 1) { return values[0]; } if (posx >= values.length) { return values[values.length - 1]; } return values[(int) posx]; } 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 smallQuantilesAreTheFirstValue() throws Exception { assertThat(snapshot.getValue(0.0)) .isEqualTo(1.0, offset(0.1)); }
@Test public void bigQuantilesAreTheLastValue() throws Exception { assertThat(snapshot.getValue(1.0)) .isEqualTo(5.0, offset(0.1)); }
@Test(expected = IllegalArgumentException.class) public void disallowsNotANumberQuantile() { snapshot.getValue( Double.NaN ); }
@Test(expected = IllegalArgumentException.class) public void disallowsNegativeQuantile() { snapshot.getValue( -0.5 ); }
@Test(expected = IllegalArgumentException.class) public void disallowsQuantileOverOne() { snapshot.getValue( 1.5 ); }