59088
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package java.util.random;
|
|
27 |
|
|
28 |
import java.util.function.Function;
|
|
29 |
import java.util.Map;
|
|
30 |
import java.util.Objects;
|
|
31 |
import java.util.ServiceLoader;
|
|
32 |
import java.util.ServiceLoader.Provider;
|
|
33 |
import java.util.stream.Collectors;
|
|
34 |
import java.util.stream.Stream;
|
|
35 |
import java.util.stream.StreamSupport;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* This is a factory class for generating random number generators of a specific
|
|
39 |
* catagory and algorithm.
|
|
40 |
*/
|
|
41 |
public class RandomGeneratorFactory<T> {
|
|
42 |
/**
|
|
43 |
* Instance provider class of random number algorithm.
|
|
44 |
*/
|
|
45 |
private final Provider<T> provider;
|
|
46 |
|
|
47 |
/**
|
|
48 |
* Map of provider classes.
|
|
49 |
*/
|
|
50 |
private static Map<String, Provider<RandomGenerator>> providerMap;
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Private constructor.
|
|
54 |
*
|
|
55 |
* @param provider Provider class to wrap.
|
|
56 |
*/
|
|
57 |
private RandomGeneratorFactory(Provider<T> provider) {
|
|
58 |
this.provider = provider;
|
|
59 |
}
|
|
60 |
|
|
61 |
/**
|
|
62 |
* Returns the provider map, lazily constructing map on first call.
|
|
63 |
*
|
|
64 |
* @return Map of provider classes.
|
|
65 |
*/
|
|
66 |
private static Map<String, Provider<RandomGenerator>> getProviderMap() {
|
|
67 |
if (providerMap == null) {
|
|
68 |
synchronized (RandomGeneratorFactory.class) {
|
|
69 |
if (providerMap == null) {
|
|
70 |
providerMap =
|
|
71 |
ServiceLoader
|
|
72 |
.load(RandomGenerator.class)
|
|
73 |
.stream()
|
|
74 |
.filter(p -> !p.type().isInterface())
|
|
75 |
.collect(Collectors.toMap(p -> p.type().getSimpleName().toUpperCase(),
|
|
76 |
Function.identity()));
|
|
77 |
}
|
|
78 |
}
|
|
79 |
}
|
|
80 |
return providerMap;
|
|
81 |
}
|
|
82 |
|
|
83 |
private static Provider<RandomGenerator> findProvider(String name, Class<? extends RandomGenerator> category)
|
|
84 |
throws IllegalArgumentException {
|
|
85 |
Map<String, Provider<RandomGenerator>> pm = getProviderMap();
|
|
86 |
Provider<RandomGenerator> provider = pm.get(name.toUpperCase());
|
|
87 |
if (provider == null || provider.type().isAssignableFrom(category)) {
|
|
88 |
throw new IllegalArgumentException(name + " is an unknown random number generator");
|
|
89 |
}
|
|
90 |
return provider;
|
|
91 |
}
|
|
92 |
|
|
93 |
/**
|
|
94 |
* Returns a {@link RandomGenerator} that utilizes the {@code name} algorithm.
|
|
95 |
*
|
|
96 |
* @param name Name of random number algorithm to use
|
|
97 |
* @param category Sub-interface of {@link RandomGenerator} to type check
|
|
98 |
* @param <T> Sub-interface of {@link RandomGenerator} to produce
|
|
99 |
* @return An instance of {@link RandomGenerator}
|
|
100 |
* @throws IllegalArgumentException when either the name or category is null
|
|
101 |
*/
|
|
102 |
static <T> T of(String name, Class<? extends RandomGenerator> category)
|
|
103 |
throws IllegalArgumentException {
|
|
104 |
@SuppressWarnings("unchecked")
|
|
105 |
T uncheckedRandomGenerator = (T)findProvider(name, category).get();
|
|
106 |
return uncheckedRandomGenerator;
|
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Returns a {@link RandomGeneratorFactory} that will produce instances
|
|
111 |
* of {@link RandomGenerator} that utilizes the {@code name} algorithm.
|
|
112 |
*
|
|
113 |
* @param name Name of random number algorithm to use
|
|
114 |
* @param category Sub-interface of {@link RandomGenerator} to type check
|
|
115 |
* @param <T> Sub-interface of {@link RandomGenerator} to produce
|
|
116 |
* @return Factory of {@link RandomGenerator}
|
|
117 |
* @throws IllegalArgumentException when either the name or category is null
|
|
118 |
*/
|
|
119 |
static <T> RandomGeneratorFactory<T> factoryOf(String name, Class<? extends RandomGenerator> category)
|
|
120 |
throws IllegalArgumentException {
|
|
121 |
@SuppressWarnings("unchecked")
|
|
122 |
Provider<T> uncheckedProvider = (Provider<T>)findProvider(name, category);
|
|
123 |
return new RandomGeneratorFactory<T>(uncheckedProvider);
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* Create an instance of {@link RandomGenerator}.
|
|
128 |
*
|
|
129 |
* @return Instance of {@link RandomGenerator}.
|
|
130 |
*/
|
|
131 |
public T create() {
|
|
132 |
return provider.get();
|
|
133 |
}
|
|
134 |
}
|
|
135 |
|
|
136 |
|