Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8265137: java.util.Random suddenly has new public methods nowhere documented #3469

Closed
wants to merge 10 commits into from
55 changes: 35 additions & 20 deletions src/java.base/share/classes/java/util/Random.java
Expand Up @@ -27,15 +27,12 @@

import java.io.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import jdk.internal.util.random.RandomSupport.*;

import jdk.internal.util.random.RandomSupport.AbstractSpliteratorGenerator;
import jdk.internal.util.random.RandomSupport.RandomGeneratorProperties;
import jdk.internal.util.random.RandomSupport.RandomIntsSpliterator;
import jdk.internal.util.random.RandomSupport.RandomLongsSpliterator;
import jdk.internal.util.random.RandomSupport.RandomDoublesSpliterator;
import static jdk.internal.util.random.RandomSupport.*;

import jdk.internal.misc.Unsafe;
Expand Down Expand Up @@ -617,28 +614,46 @@ private void resetSeed(long seedVal) {

// Methods required by class AbstractSpliteratorGenerator

/**
* @hidden
*/
private static final class ThreadLocalRandomProxy extends Random {
@java.io.Serial
static final long serialVersionUID = 0L;

static final AbstractSpliteratorGenerator proxy = new ThreadLocalRandomProxy();

public int nextInt() {
return ThreadLocalRandom.current().nextInt();
}

public long nextLong() {
return ThreadLocalRandom.current().nextLong();
}
}

@Override
public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
return new RandomIntsSpliterator(this, index, fence, origin, bound);
final protected Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
if (this instanceof ThreadLocalRandom) {
return new RandomIntsSpliterator(ThreadLocalRandomProxy.proxy, index, fence, origin, bound);
} else {
return new RandomIntsSpliterator(this, index, fence, origin, bound);
}
}

/**
* @hidden
*/
@Override
public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
return new RandomLongsSpliterator(this, index, fence, origin, bound);
final protected Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
if (this instanceof ThreadLocalRandom) {
return new RandomLongsSpliterator(ThreadLocalRandomProxy.proxy, index, fence, origin, bound);
} else {
return new RandomLongsSpliterator(this, index, fence, origin, bound);
}
}

/**
* @hidden
*/
@Override
public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
return new RandomDoublesSpliterator(this, index, fence, origin, bound);
final protected Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
if (this instanceof ThreadLocalRandom) {
return new RandomDoublesSpliterator(ThreadLocalRandomProxy.proxy, index, fence, origin, bound);
} else {
return new RandomDoublesSpliterator(this, index, fence, origin, bound);
}
}

/**
Expand Down
24 changes: 0 additions & 24 deletions src/java.base/share/classes/java/util/SplittableRandom.java
Expand Up @@ -280,30 +280,6 @@ public SplittableRandom split(SplittableGenerator source) {
return new SplittableRandom(source.nextLong(), mixGamma(source.nextLong()));
}

/**
* @hidden
*/
@Override
public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
return super.makeIntsSpliterator(index, fence, origin, bound);
}

/**
* @hidden
*/
@Override
public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
return super.makeLongsSpliterator(index, fence, origin, bound);
}

/**
* @hidden
*/
@Override
public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
return super.makeDoublesSpliterator(index, fence, origin, bound);
}

@Override
public int nextInt() {
return mix32(nextSeed());
Expand Down
Expand Up @@ -51,11 +51,7 @@
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import jdk.internal.util.random.RandomSupport;
import jdk.internal.util.random.RandomSupport.AbstractSpliteratorGenerator;
import jdk.internal.util.random.RandomSupport.RandomIntsSpliterator;
import jdk.internal.util.random.RandomSupport.RandomLongsSpliterator;
import jdk.internal.util.random.RandomSupport.RandomDoublesSpliterator;
import jdk.internal.util.random.RandomSupport.RandomGeneratorProperties;
import jdk.internal.util.random.RandomSupport.*;
import jdk.internal.misc.Unsafe;
import jdk.internal.misc.VM;

Expand Down Expand Up @@ -396,48 +392,6 @@ private Object readResolve() {
/** The common ThreadLocalRandom */
private static final ThreadLocalRandom instance = new ThreadLocalRandom();

private static final class ThreadLocalRandomProxy extends Random {
@java.io.Serial
static final long serialVersionUID = 0L;


static final AbstractSpliteratorGenerator proxy = new ThreadLocalRandomProxy();


public int nextInt() {
return ThreadLocalRandom.current().nextInt();
}

public long nextLong() {
return ThreadLocalRandom.current().nextLong();
}
}

// Methods required by class AbstractSpliteratorGenerator
/**
* @hidden
*/
@Override
public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
return new RandomIntsSpliterator(ThreadLocalRandomProxy.proxy, index, fence, origin, bound);
}

/**
* @hidden
*/
@Override
public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
return new RandomLongsSpliterator(ThreadLocalRandomProxy.proxy, index, fence, origin, bound);
}

/**
* @hidden
*/
@Override
public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
return new RandomDoublesSpliterator(ThreadLocalRandomProxy.proxy, index, fence, origin, bound);
}

/**
* The next seed for default constructors.
*/
Expand Down
Expand Up @@ -1438,59 +1438,9 @@ public abstract static class AbstractSpliteratorGenerator implements RandomGener
protected AbstractSpliteratorGenerator() {
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is wrong now since there are instances of this class (really, subclasses). The constructor should probably simply be removed.

/**
* Create an instance of {@link Spliterator.OfInt} that for each
* traversal position between the specified index (inclusive) and the
* specified fence (exclusive) generates a pseudorandomly chosen
* {@code int} value between the specified origin (inclusive) and the
* specified bound (exclusive).
*
* @param index the (inclusive) lower bound on traversal positions
* @param fence the (exclusive) upper bound on traversal positions
* @param origin the (inclusive) lower bound on the pseudorandom values to be generated
* @param bound the (exclusive) upper bound on the pseudorandom values to be generated
*
* @return an instance of {@link Spliterator.OfInt}
*
* @hidden
*/
public abstract Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound);

/**
* Create an instance of {@link Spliterator.OfLong} that for each
* traversal position between the specified index (inclusive) and the
* specified fence (exclusive) generates a pseudorandomly chosen
* {@code long} value between the specified origin (inclusive) and the
* specified bound (exclusive).
*
* @param index the (inclusive) lower bound on traversal positions
* @param fence the (exclusive) upper bound on traversal positions
* @param origin the (inclusive) lower bound on the pseudorandom values to be generated
* @param bound the (exclusive) upper bound on the pseudorandom values to be generated
*
* @return an instance of {@link Spliterator.OfLong}
*
* @hidden
*/
public abstract Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound);

/**
* Create an instance of {@link Spliterator.OfDouble} that for each
* traversal position between the specified index (inclusive) and the
* specified fence (exclusive) generates a pseudorandomly chosen
* {@code double} value between the specified origin (inclusive) and the
* specified bound (exclusive).
*
* @param index the (inclusive) lower bound on traversal positions
* @param fence the (exclusive) upper bound on traversal positions
* @param origin the (inclusive) lower bound on the pseudorandom values to be generated
* @param bound the (exclusive) upper bound on the pseudorandom values to be generated
*
* @return an instance of {@link Spliterator.OfDouble}
*
* @hidden
*/
public abstract Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound);
protected abstract Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound);
protected abstract Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound);
protected abstract Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound);

/* ---------------- public methods ---------------- */

Expand Down Expand Up @@ -1663,15 +1613,15 @@ protected AbstractArbitrarilyJumpableGenerator() {

// Methods required by class AbstractSpliteratorGenerator

public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
protected Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
JimLaskey marked this conversation as resolved.
Show resolved Hide resolved
return new RandomIntsSpliterator(this, index, fence, origin, bound);
}

public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
protected Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
return new RandomLongsSpliterator(this, index, fence, origin, bound);
}

public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
protected Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
return new RandomDoublesSpliterator(this, index, fence, origin, bound);
}

Expand Down Expand Up @@ -2103,15 +2053,15 @@ public abstract static class AbstractSplittableGenerator extends AbstractSpliter
protected AbstractSplittableGenerator() {
}

public Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
protected Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
return new RandomIntsSpliterator(this, index, fence, origin, bound);
}

public Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
protected Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
return new RandomLongsSpliterator(this, index, fence, origin, bound);
}

public Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
protected Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
return new RandomDoublesSpliterator(this, index, fence, origin, bound);
}

Expand Down