CountingSampler implements Sampler<T> { static <T> Sampler<T> create(float probability) { final int percent = (int) (probability * 100.0f); checkArgument(percent >= 0 && percent <= 100, "probability: %s (expected: 0.0 <= probability <= 1.0)", probability); if (percent == 0) { return Sampler.never(); } if (percent == 100) { return Sampler.always(); } return new CountingSampler<>(percent); } CountingSampler(int percent);   CountingSampler(int percent, Random random); @Override boolean isSampled(Object ignored); @Override String toString();  }
@Test public void testSamplingRateMinimumLimit() { assertThatThrownBy(() -> CountingSampler.create(-1.99f)).isInstanceOf(IllegalArgumentException.class); }
@Test public void testSamplingRateMaximumLimit() { assertThatThrownBy(() -> CountingSampler.create(1.01f)).isInstanceOf(IllegalArgumentException.class); }
@Test public void testSamplingRatePercentageRounding() { assertThat(CountingSampler.create(0.01f)).isInstanceOfSatisfying(CountingSampler.class, sampler -> { assertThat(sampler.sampleDecisions.cardinality()).isOne(); }); }
@Test public void testNeverSampledSampler() { assertThat(CountingSampler.create(0.0f)).isSameAs(Sampler.never()); }
@Test public void testAlwaysSampledSampler() { assertThat(CountingSampler.create(1.0f)).isSameAs(Sampler.always()); }