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>>
Sampling from a
Collection.
Sampling uses UniformRandomProvider.nextInt(int).
- Since:
- 1.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionCollection to be sampled from.private final UniformRandomProviderRNG. -
Constructor Summary
ConstructorsModifierConstructorDescriptionCollectionSampler(UniformRandomProvider rng, Collection<T> collection) Creates a sampler.privateCollectionSampler(UniformRandomProvider rng, List<T> collection) -
Method Summary
Modifier and TypeMethodDescriptionsample()Picks one of the items from thecollection passed to the constructor.private static <T> List<T> toList(Collection<T> collection) Convert the collection to a list (shallow) copy.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 Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ObjectSampler
samples, samples
-
Field Details
-
items
-
rng
RNG.
-
-
Constructor Details
-
CollectionSampler
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:
IllegalArgumentException- ifcollectionis empty.
-
CollectionSampler
- Parameters:
rng- Generator of uniformly distributed random numbers.collection- Collection to be sampled.
-
-
Method Details
-
sample
Picks one of the items from thecollection passed to the constructor.- Specified by:
samplein interfaceObjectSampler<T>- Returns:
- a random sample.
-
withUniformRandomProvider
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:
withUniformRandomProviderin interfaceSharedStateSampler<T>- Parameters:
rng- Generator of uniformly distributed random numbers.- Returns:
- the sampler
- Since:
- 1.3
-
toList
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:
IllegalArgumentException- ifcollectionis empty.
-