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
18 package org.apache.commons.rng.sampling;
19
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.ArrayList;
23
24 import org.apache.commons.rng.UniformRandomProvider;
25
26 /**
27 * Sampling from a {@link Collection}.
28 *
29 * <p>Sampling uses {@link UniformRandomProvider#nextInt(int)}.</p>
30 *
31 * @param <T> Type of items in the collection.
32 *
33 * @since 1.0
34 */
35 public class CollectionSampler<T> implements SharedStateObjectSampler<T> {
36 /** Collection to be sampled from. */
37 private final List<T> items;
38 /** RNG. */
39 private final UniformRandomProvider rng;
40
41 /**
42 * Creates a sampler.
43 *
44 * @param rng Generator of uniformly distributed random numbers.
45 * @param collection Collection to be sampled.
46 * A (shallow) copy will be stored in the created instance.
47 * @throws IllegalArgumentException if {@code collection} is empty.
48 */
49 public CollectionSampler(UniformRandomProvider rng,
50 Collection<T> collection) {
51 this(rng, toList(collection));
52 }
53
54 /**
55 * @param rng Generator of uniformly distributed random numbers.
56 * @param collection Collection to be sampled.
57 */
58 private CollectionSampler(UniformRandomProvider rng,
59 List<T> collection) {
60 this.rng = rng;
61 items = collection;
62 }
63
64 /**
65 * Picks one of the items from the
66 * {@link #CollectionSampler(UniformRandomProvider,Collection)
67 * collection passed to the constructor}.
68 *
69 * @return a random sample.
70 */
71 @Override
72 public T sample() {
73 return items.get(rng.nextInt(items.size()));
74 }
75
76 /**
77 * {@inheritDoc}
78 *
79 * @since 1.3
80 */
81 @Override
82 public CollectionSampler<T> withUniformRandomProvider(UniformRandomProvider rng) {
83 return new CollectionSampler<>(rng, this.items);
84 }
85
86 /**
87 * Convert the collection to a list (shallow) copy.
88 *
89 * <p>This method exists to raise an exception before invocation of the
90 * private constructor; this mitigates Finalizer attacks
91 * (see SpotBugs CT_CONSTRUCTOR_THROW).
92 *
93 * @param <T> Type of items in the collection.
94 * @param collection Collection.
95 * @return the list copy
96 * @throws IllegalArgumentException if {@code collection} is empty.
97 */
98 private static <T> List<T> toList(Collection<T> collection) {
99 if (collection.isEmpty()) {
100 throw new IllegalArgumentException("Empty collection");
101 }
102 return new ArrayList<>(collection);
103 }
104 }