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

Yasser Bazzi duke at openjdk.java.net
Fri Jan 28 00:42:09 UTC 2022


On Wed, 26 Jan 2022 17:43:09 GMT, Stuart Marks <smarks at openjdk.org> wrote:

>> 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.)

Commited the change on df78e05e3e692e2189c9d318fbd4892a4b96a55f

>> 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.

ok so i made the change to check if its initialized and throw if the user tries to use setSeet() c297d42f4505b03780bd9ab2e169546fd6cffcb9

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

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


More information about the core-libs-dev mailing list