package org.terracotta.statistics.derived;
import org.terracotta.statistics.observer.ChainedEventObserver;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
public class OperationResultSampler<T extends Enum<T>> extends OperationResultFilter<T> {
private final int ceiling;
public OperationResultSampler(Set<T> targets, double sampling, ChainedEventObserver... observers) {
super(targets, observers);
if (sampling > 1.0 || sampling < 0.0) {
throw new IllegalArgumentException("Sampling must be between 0.0 and 1.0");
}
this.ceiling = (int) (Integer.MAX_VALUE * sampling);
}
@Override
public void end(long time, long latency, T result) {
if (!derivedStatistics.isEmpty() && targets.contains(result) && sample()) {
for (ChainedEventObserver derived : derivedStatistics) {
derived.event(time, latency);
}
}
}
private boolean sample() {
return ceiling == Integer.MAX_VALUE || ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE) < ceiling;
}
public int getCeiling() {
return ceiling;
}
}