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