Distribution { public int getPercentile(double percentile) { Preconditions.checkArgument(percentile > 0.0); Preconditions.checkArgument(percentile <= 100.0); long targetRank = (long) Math.ceil(percentile * count.get() / 100); long rank = 0; for (int i = 0; i < buckets.length(); i++) { rank += buckets.get(i); if (rank >= targetRank) { return i; } } return buckets.length(); } Distribution(int endValue); @Deprecated long getNthPercentile(double percentile); int getPercentile(double percentile); void record(int value); @Override String toString();  }
@Test public void testPercentile() { Distribution dist; dist = of(15, 20, 35, 40, 50); assertThat(dist.getPercentile(5)).isEqualTo(15); assertThat(dist.getPercentile(30)).isEqualTo(20); assertThat(dist.getPercentile(40)).isEqualTo(20); assertThat(dist.getPercentile(50)).isEqualTo(35); assertThat(dist.getPercentile(100)).isEqualTo(50); dist = of(3, 6, 7, 8, 8, 10, 13, 15, 16, 20); assertThat(dist.getPercentile(25)).isEqualTo(7); assertThat(dist.getPercentile(50)).isEqualTo(8); assertThat(dist.getPercentile(75)).isEqualTo(15); assertThat(dist.getPercentile(100)).isEqualTo(20); dist = of(3, 6, 7, 8, 8, 9, 10, 13, 15, 16, 20); assertThat(dist.getPercentile(25)).isEqualTo(7); assertThat(dist.getPercentile(50)).isEqualTo(9); assertThat(dist.getPercentile(75)).isEqualTo(15); assertThat(dist.getPercentile(100)).isEqualTo(20); }