Package org.apache.commons.rng.sampling
Class CollectionSampler<T>
- java.lang.Object
-
- org.apache.commons.rng.sampling.CollectionSampler<T>
-
- Type Parameters:
T
- Type of items in the collection.
- All Implemented Interfaces:
ObjectSampler<T>
,SharedStateObjectSampler<T>
,SharedStateSampler<SharedStateObjectSampler<T>>
public class CollectionSampler<T> extends java.lang.Object implements SharedStateObjectSampler<T>
Sampling from aCollection
.Sampling uses
UniformRandomProvider.nextInt(int)
.- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<T>
items
Collection to be sampled from.private UniformRandomProvider
rng
RNG.
-
Constructor Summary
Constructors Modifier Constructor Description CollectionSampler(UniformRandomProvider rng, java.util.Collection<T> collection)
Creates a sampler.private
CollectionSampler(UniformRandomProvider rng, java.util.List<T> collection)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description T
sample()
Picks one of the items from thecollection passed to the constructor
.private static <T> java.util.List<T>
toList(java.util.Collection<T> collection)
Convert the collection to a list (shallow) copy.CollectionSampler<T>
withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.rng.sampling.ObjectSampler
samples, samples
-
-
-
-
Field Detail
-
items
private final java.util.List<T> items
Collection to be sampled from.
-
rng
private final UniformRandomProvider rng
RNG.
-
-
Constructor Detail
-
CollectionSampler
public CollectionSampler(UniformRandomProvider rng, java.util.Collection<T> collection)
Creates a sampler.- Parameters:
rng
- Generator of uniformly distributed random numbers.collection
- Collection to be sampled. A (shallow) copy will be stored in the created instance.- Throws:
java.lang.IllegalArgumentException
- ifcollection
is empty.
-
CollectionSampler
private CollectionSampler(UniformRandomProvider rng, java.util.List<T> collection)
- Parameters:
rng
- Generator of uniformly distributed random numbers.collection
- Collection to be sampled.
-
-
Method Detail
-
sample
public T sample()
Picks one of the items from thecollection passed to the constructor
.- Specified by:
sample
in interfaceObjectSampler<T>
- Returns:
- a random sample.
-
withUniformRandomProvider
public CollectionSampler<T> withUniformRandomProvider(UniformRandomProvider rng)
Create a new instance of the sampler with the same underlying state using the given uniform random provider as the source of randomness.- Specified by:
withUniformRandomProvider
in interfaceSharedStateSampler<T>
- Parameters:
rng
- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
toList
private static <T> java.util.List<T> toList(java.util.Collection<T> collection)
Convert the collection to a list (shallow) copy.This method exists to raise an exception before invocation of the private constructor; this mitigates Finalizer attacks (see SpotBugs CT_CONSTRUCTOR_THROW).
- Type Parameters:
T
- Type of items in the collection.- Parameters:
collection
- Collection.- Returns:
- the list copy
- Throws:
java.lang.IllegalArgumentException
- ifcollection
is empty.
-
-