diff --git a/src/main/java/gr/james/sampling/AbstractThreadSafeRandomSampling.java b/src/main/java/gr/james/sampling/AbstractThreadSafeRandomSampling.java index 4a3b761..9188b3a 100644 --- a/src/main/java/gr/james/sampling/AbstractThreadSafeRandomSampling.java +++ b/src/main/java/gr/james/sampling/AbstractThreadSafeRandomSampling.java @@ -14,14 +14,40 @@ * @author Michael Böckling */ public abstract class AbstractThreadSafeRandomSampling implements RandomSampling, ThreadSafeRandomSampling { + /** + * The given sample size from the constructor. + */ + protected final int sampleSize; + + /** + * The given Random instance from the constructor. + */ + protected final Random random; - private final int sampleSize; - private final Random random; - private final AtomicReferenceArray sample; - private final AtomicInteger samplesCount; - private final Collection unmodifiableSample; - private AtomicLong streamSize; - private AtomicLong skip; + /** + * The internal reservoir maintained by the algorithm. + */ + protected final AtomicReferenceArray sample; + + /** + * The size of the internal reservoir maintained by the algorithm. + */ + protected final AtomicInteger samplesCount; + + /** + * The unmodifiable decorator around the sample. + */ + protected final Collection unmodifiableSample; + + /** + * The counted stream size. + */ + protected AtomicLong streamSize; + + /** + * The next skip size. + */ + protected AtomicLong skip; /** * Construct a new instance of this class using the specified sample size and RNG. The implementation assumes that @@ -33,7 +59,7 @@ public abstract class AbstractThreadSafeRandomSampling implements RandomSampl * @throws NullPointerException if {@code random} is {@code null} * @throws IllegalArgumentException if {@code sampleSize} is less than 1 */ - AbstractThreadSafeRandomSampling(int sampleSize, Random random) { + protected AbstractThreadSafeRandomSampling(int sampleSize, Random random) { if (random == null) { throw new NullPointerException("Random was null"); } @@ -175,7 +201,7 @@ public final Collection sample() { * @param random the {@link Random} instance to use * @return how many items to skip */ - abstract long skipLength(long streamSize, int sampleSize, Random random); + protected abstract long skipLength(long streamSize, int sampleSize, Random random); /** * Performs initialization logic. @@ -185,7 +211,7 @@ public final Collection sample() { * @param sampleSize expected sample size * @param random the {@link Random} instance assigned to this instance */ - void init(int sampleSize, Random random) { + protected void init(int sampleSize, Random random) { } static class AtomicReferenceArrayList extends AbstractList implements List, RandomAccess { diff --git a/src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java b/src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java index 84d0ab9..13b8467 100644 --- a/src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java +++ b/src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java @@ -66,15 +66,29 @@ public static RandomSamplingCollector collector(int sampleSize, Random ra return new RandomSamplingCollector<>(() -> new LiLSamplingThreadSafe<>(sampleSize, random)); } + /** + * {@inheritDoc} + * + * @param sampleSize {@inheritDoc} + * @param random {@inheritDoc} + */ @Override - void init(int sampleSize, Random random) { + protected void init(int sampleSize, Random random) { //W = Math.pow(RandomSamplingUtils.randomExclusive(random), 1.0 / sampleSize); W = new AtomicLong(); W.set(Double.doubleToLongBits(Math.pow(RandomSamplingUtils.randomExclusive(random), 1.0 / sampleSize))); } + /** + * {@inheritDoc} + * + * @param streamSize {@inheritDoc} + * @param sampleSize {@inheritDoc} + * @param random {@inheritDoc} + * @return {@inheritDoc} + */ @Override - long skipLength(long streamSize, int sampleSize, Random random) { + protected long skipLength(long streamSize, int sampleSize, Random random) { final double random1 = RandomSamplingUtils.randomExclusive(random); final double random2 = RandomSamplingUtils.randomExclusive(random); double w = Double.longBitsToDouble(W.get());