Skip to content

Commit

Permalink
Add IdentityRandomSampling class for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jun 29, 2018
1 parent 235b00c commit ebdee65
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/main/java/gr/james/sampling/IdentityRandomSampling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package gr.james.sampling;

import java.util.*;

/**
* A {@link RandomSampling} decorator that doesn't permit duplicate items.
*
* @param <T> the element type
* @param <RS> the {@link RandomSampling} implementation type
*/
@Deprecated
class IdentityRandomSampling<T, RS extends RandomSampling<T>> implements RandomSampling<T> {
private RS source;
private Set<T> set;

/**
* Decorates {@code source} as an {@link IdentityRandomSampling}.
* <p>
* The caller must ensure that {@code source} will not be accessed directly after this point.
*
* @param source the source {@link RandomSampling} implementation
* @throws NullPointerException if {@code source} is {@code null}
* @throws IllegalArgumentException if {@code source} already had some items feeded
*/
IdentityRandomSampling(RS source) {
if (source == null) {
throw new NullPointerException();
}
if (source.sample().size() != 0) {
throw new IllegalArgumentException();
}
this.source = source;
this.set = new HashSet<>();
}

@Override
public RS feed(T item) {
if (item == null) {
throw new NullPointerException();
}
if (!set.add(item)) {
throw new UnsupportedOperationException();
}
source.feed(item);
return source;
}

@Override
public RS feed(Iterator<T> items) {
source.feed(items);
return source;
}

@Override
public RS feed(Iterable<T> items) {
source.feed(items);
return source;
}

@Override
public int sampleSize() {
return source.sampleSize();
}

@Override
public long streamSize() {
return source.streamSize();
}

@Override
public Set<T> sample() {
assert source.sample().stream().distinct().count() == source.sample().stream().distinct().count();
return new AbstractSet<T>() {
final Collection<T> sample = source.sample();

@Override
public Iterator<T> iterator() {
return sample.iterator();
}

@Override
public int size() {
return sample.size();
}
};
}
}

0 comments on commit ebdee65

Please sign in to comment.