CollectionSampler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.rng.sampling;

  18. import java.util.Collection;
  19. import java.util.List;
  20. import java.util.ArrayList;

  21. import org.apache.commons.rng.UniformRandomProvider;

  22. /**
  23.  * Sampling from a {@link Collection}.
  24.  *
  25.  * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)}.</p>
  26.  *
  27.  * @param <T> Type of items in the collection.
  28.  *
  29.  * @since 1.0
  30.  */
  31. public class CollectionSampler<T> implements SharedStateObjectSampler<T> {
  32.     /** Collection to be sampled from. */
  33.     private final List<T> items;
  34.     /** RNG. */
  35.     private final UniformRandomProvider rng;

  36.     /**
  37.      * Creates a sampler.
  38.      *
  39.      * @param rng Generator of uniformly distributed random numbers.
  40.      * @param collection Collection to be sampled.
  41.      * A (shallow) copy will be stored in the created instance.
  42.      * @throws IllegalArgumentException if {@code collection} is empty.
  43.      */
  44.     public CollectionSampler(UniformRandomProvider rng,
  45.                              Collection<T> collection) {
  46.         this(rng, toList(collection));
  47.     }

  48.     /**
  49.      * @param rng Generator of uniformly distributed random numbers.
  50.      * @param collection Collection to be sampled.
  51.      */
  52.     private CollectionSampler(UniformRandomProvider rng,
  53.                               List<T> collection) {
  54.         this.rng = rng;
  55.         items = collection;
  56.     }

  57.     /**
  58.      * Picks one of the items from the
  59.      * {@link #CollectionSampler(UniformRandomProvider,Collection)
  60.      * collection passed to the constructor}.
  61.      *
  62.      * @return a random sample.
  63.      */
  64.     @Override
  65.     public T sample() {
  66.         return items.get(rng.nextInt(items.size()));
  67.     }

  68.     /**
  69.      * {@inheritDoc}
  70.      *
  71.      * @since 1.3
  72.      */
  73.     @Override
  74.     public CollectionSampler<T> withUniformRandomProvider(UniformRandomProvider rng) {
  75.         return new CollectionSampler<>(rng, this.items);
  76.     }

  77.     /**
  78.      * Convert the collection to a list (shallow) copy.
  79.      *
  80.      * <p>This method exists to raise an exception before invocation of the
  81.      * private constructor; this mitigates Finalizer attacks
  82.      * (see SpotBugs CT_CONSTRUCTOR_THROW).
  83.      *
  84.      * @param <T> Type of items in the collection.
  85.      * @param collection Collection.
  86.      * @return the list copy
  87.      * @throws IllegalArgumentException if {@code collection} is empty.
  88.      */
  89.     private static <T> List<T> toList(Collection<T> collection) {
  90.         if (collection.isEmpty()) {
  91.             throw new IllegalArgumentException("Empty collection");
  92.         }
  93.         return new ArrayList<>(collection);
  94.     }
  95. }