RFR: 8279598: Provide adapter from RandomGenerator to Random

Stuart Marks smarks at openjdk.java.net
Wed Jan 26 17:47:34 UTC 2022


On Wed, 26 Jan 2022 17:04:35 GMT, Yasser Bazzi <duke at openjdk.java.net> wrote:

>> src/java.base/share/classes/java/util/random/RandomGenerator.java line 144:
>> 
>>> 142:     /**
>>> 143:      * Returns a wrapper to use {@link java.util.Random}
>>> 144:      * @return {@link java.util.Random}
>> 
>> Possible reword:
>> "Returns a Random from this RandomGenerator."
>> 
>> It should be specified that the resulting Random acts in all respects except that
>> setSeed is not available.
>> 
>> Missing period at the end of the sentence.  Check all comments for grammar and punctuation.
>
> Is something like this better?
> /**
>      * Returns a {@link java.util.Random} from this {@link java.util.RandomGenerator}.
>      * The resulting Random acts in all respects except that setSeed is not available.
>      *
>      * @return {@link java.util.Random}
>      */

Suggest:

> Returns an instance of {@link java.util.Random} based on this {@code RandomGenerator}.
> If this generator is already an instance of {@code Random}, it is returned. Otherwise, this method
> returns an instance of {@code Random} that delegates all methods except `setSeed` to this
> generator. Its `setSeed` method always throws {@link UnsupportedOperationException}.

(Note no link is necessary for RandomGenerator since we are in that class already.)

>> src/java.base/share/classes/jdk/internal/util/random/RandomWrapper.java line 58:
>> 
>>> 56:      * setSeed does not exist in {@link java.util.random.RandomGenerator} so can't
>>> 57:      * use it
>>> 58:      */
>> 
>> Its debatable whether setSeed can ignore the argument or should throw an exception.
>> If it ignores the argument, it is explicitly violating the contract of the supertype Random.setSeed
>> that says the seed is set.  
>> I'd recommend throwing an UnsupportedOperationException because setSeed will not work as expected.
>> Other classes (ThreadLocalRandom) throws UnsupportedOperationException from setSeed.
>
> throwing UnsupportedOperationException was my first thought but when i call asRandom() it will always throw it, i could create something like what ThreadLocalRandom does (check if it initialized and throw)

Yes, probably following ThreadLocalRandom is the right thing. Pretty clunky but I think it's necessary.

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

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


More information about the core-libs-dev mailing list