forked from gstamatelat/random-sampling
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IdentityRandomSampling class for testing
- Loading branch information
1 parent
235b00c
commit ebdee65
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/gr/james/sampling/IdentityRandomSampling.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
} | ||
} |