Skip to content

Commit

Permalink
Allow subclassing of AbstractThreadSafeRandomSampling (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Sep 1, 2022
1 parent 806d3ae commit 68683b9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,40 @@
* @author Michael Böckling
*/
public abstract class AbstractThreadSafeRandomSampling<T> implements RandomSampling<T>, 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<T> sample;
private final AtomicInteger samplesCount;
private final Collection<T> unmodifiableSample;
private AtomicLong streamSize;
private AtomicLong skip;
/**
* The internal reservoir maintained by the algorithm.
*/
protected final AtomicReferenceArray<T> sample;

/**
* The size of the internal reservoir maintained by the algorithm.
*/
protected final AtomicInteger samplesCount;

/**
* The unmodifiable decorator around the sample.
*/
protected final Collection<T> 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
Expand All @@ -33,7 +59,7 @@ public abstract class AbstractThreadSafeRandomSampling<T> 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");
}
Expand Down Expand Up @@ -175,7 +201,7 @@ public final Collection<T> 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.
Expand All @@ -185,7 +211,7 @@ public final Collection<T> 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<T> extends AbstractList<T> implements List<T>, RandomAccess {
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/gr/james/sampling/LiLSamplingThreadSafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,29 @@ public static <E> RandomSamplingCollector<E> 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());
Expand Down

0 comments on commit 68683b9

Please sign in to comment.