Class CollectionSampler<T>

    • Field Detail

      • items

        private final java.util.List<T> items
        Collection to be sampled from.
    • 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 - if collection 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

      • 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 interface SharedStateSampler<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 - if collection is empty.