RFR: 8279598: Provide adapter from RandomGenerator to Random [v6]

ExE Boss duke at openjdk.java.net
Mon Feb 28 19:21:35 UTC 2022


On Fri, 25 Feb 2022 22:59:05 GMT, liach <duke at openjdk.java.net> wrote:

>> src/java.base/share/classes/java/util/Random.java line 298:
>> 
>>> 296:      */
>>> 297:     public static Random from(RandomGenerator random) {
>>> 298:         return RandomWrapper.wrap(random);
>> 
>> Might be good to check here or in the called methods / constructors for `null`. Currently `null` would not be noticed until the first method is called on the created `Random`, which makes it difficult for the user to track down bugs in their code.
>
> Suggestion:
> 
>         Objects.requireNonNull(random);
>         return RandomWrapper.wrap(random);
> 
> fyi this is the original change suggested by marcono1234. Notice that this might need a csr update; maybe a javadoc update is needed too

Arguably, `RandomWrapper.wrap` should be inlined here, since it’s not used anywhere else:
Suggestion:

        if (Objects.requireNonNull(random, "random") instanceof Random) {
            return (Random) random;
        }
        return new RandomWrapper(random);

-------------

PR: https://git.openjdk.java.net/jdk/pull/7001


More information about the core-libs-dev mailing list