diff -r b0c958c0e6c6 -r f02ffcb61dce src/java.base/share/classes/java/util/Random.java --- a/src/java.base/share/classes/java/util/Random.java Thu Jun 27 18:02:51 2019 -0300 +++ b/src/java.base/share/classes/java/util/Random.java Thu Jun 27 18:30:27 2019 -0300 @@ -1,42 +1,42 @@ /* - * Copyright (c) 1995, 2013, 2019, Oracle and/or its affiliates. All rights reserved. - * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. - * - * + * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * - * - * - * - * - * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. * - * - * - * - * - * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). * - * - * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * - * - * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. */ package java.util; +import jdk.internal.misc.Unsafe; + import java.io.*; import java.math.BigInteger; +import java.util.Objects; +import java.util.ServiceLoader; +import java.util.ServiceLoader.Provider; import java.util.concurrent.atomic.AtomicLong; -import java.util.function.DoubleConsumer; -import java.util.function.IntConsumer; -import java.util.function.LongConsumer; -import java.util.stream.DoubleStream; -import java.util.stream.IntStream; -import java.util.stream.LongStream; -import java.util.stream.StreamSupport; - -import jdk.internal.misc.Unsafe; +import java.util.function.Function; +import java.util.random.AbstractSharedRNG; +import java.util.random.RandomNumberGenerator; +import java.util.stream.Collectors; /** * An instance of this class is used to generate a stream of @@ -76,7 +76,7 @@ * @since 1.0 */ public -class Random extends AbstractSharedRng implements java.io.Serializable { +class Random extends AbstractSharedRNG implements java.io.Serializable { /** use serialVersionUID from JDK 1.1 for interoperability */ static final long serialVersionUID = 3905348978240129619L; @@ -207,7 +207,10 @@ return (int)(nextseed >>> (48 - bits)); } - static final BigInteger thePeriod = BigInteger.valueOf(1L<<48); // Period is 2**48 + /* + * Period of Random is 2**48 + */ + private static final BigInteger PERIOD = BigInteger.valueOf(1L<<48); /** * Returns the period of this random number generator. @@ -215,11 +218,7 @@ * @return the period of this random number generator. */ public BigInteger period() { - // Here we also take care of checking for instances of class SecureRandom, - // just so as not to bother the implementors of that class. - // (Any specific instance of SecureRandom can of course override this method.) - // The cast to (Object) is of course needed only during development. - return ((Object)this instanceof java.security.SecureRandom) ? Rng.HUGE_PERIOD : thePeriod; + return PERIOD; } /** @@ -496,6 +495,47 @@ } /** + * Creates a new random number generator that uses the random number generator algorithm + * specified by name. The seed of the random number generator to a value very likely to be + * distinct from any other invocation. + * + * @param name name of random number generator algorithm to use. + * + * @return an instance of random number generator. + * + * @throws IllegalArgumentException if {@code name} is an unknown random number generator + * + * @since 14 + */ + public static RandomNumberGenerator byName(String name) throws IllegalArgumentException { + Objects.requireNonNull(name); + Map> rngs = getRNGMap(); + Provider provider = rngs.get(name.toUpperCase()); + if (provider == null) { + throw new IllegalArgumentException(name + " is an unknown random number generator"); + } + return provider.get(); + } + + private static Map> rngMap; + + private static Map> getRNGMap() { + if (rngMap == null) { + synchronized (Random.class) { + if (rngMap == null) { + rngMap = ServiceLoader + .load(RandomNumberGenerator.class) + .stream() + .filter(p -> !p.type().isInterface()) + .collect(Collectors.toMap(p -> p.type().getSimpleName().toUpperCase(), + Function.identity())); + } + } + } + return rngMap; + } + + /** * Serializable fields for Random. * * @serialField seed long