Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
/ jdk17 Public archive

Commit

Permalink
8270075: SplittableRandom extends AbstractSplittableGenerator
Browse files Browse the repository at this point in the history
Reviewed-by: rriggs, bpb
  • Loading branch information
Jim Laskey committed Jul 14, 2021
1 parent 381bd62 commit 3bbd233
Showing 1 changed file with 47 additions and 19 deletions.
66 changes: 47 additions & 19 deletions src/java.base/share/classes/java/util/SplittableRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.math.BigInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.random.RandomGenerator;
import java.util.random.RandomGenerator.SplittableGenerator;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
Expand Down Expand Up @@ -86,13 +88,12 @@
* @author Doug Lea
* @since 1.8
*/
@SuppressWarnings("exports")
@RandomGeneratorProperties(
name = "SplittableRandom",
i = 64, j = 0, k = 0,
equidistribution = 1
)
public final class SplittableRandom extends AbstractSplittableGenerator {
public final class SplittableRandom implements RandomGenerator, SplittableGenerator {

/*
* Implementation Overview.
Expand Down Expand Up @@ -182,6 +183,7 @@ public final class SplittableRandom extends AbstractSplittableGenerator {
private SplittableRandom(long seed, long gamma) {
this.seed = seed;
this.gamma = gamma;
this.proxy = new AbstractSplittableGeneratorProxy();
}

/**
Expand Down Expand Up @@ -216,6 +218,31 @@ private static long mixGamma(long z) {
return (n < 24) ? z ^ 0xaaaaaaaaaaaaaaaaL : z;
}

/**
* Proxy class to non-public RandomSupportAbstractSplittableGenerator.
*/
private class AbstractSplittableGeneratorProxy extends AbstractSplittableGenerator {
@Override
public int nextInt() {
return SplittableRandom.this.nextInt();
}

@Override
public long nextLong() {
return SplittableRandom.this.nextLong();
}

@Override
public java.util.SplittableRandom split(SplittableGenerator source) {
return new SplittableRandom(source.nextLong(), mixGamma(source.nextLong()));
}
}

/**
* Proxy object to non-public RandomSupportAbstractSplittableGenerator.
*/
private AbstractSplittableGeneratorProxy proxy;

/**
* Adds gamma to seed.
*/
Expand Down Expand Up @@ -251,6 +278,7 @@ public SplittableRandom() { // emulate defaultGen.split()
long s = defaultGen.getAndAdd(2 * GOLDEN_GAMMA);
this.seed = mix64(s);
this.gamma = mixGamma(s + GOLDEN_GAMMA);
this.proxy = new AbstractSplittableGeneratorProxy();
}

/**
Expand Down Expand Up @@ -297,7 +325,7 @@ public long nextLong() {
*/
@Override
public void nextBytes(byte[] bytes) {
super.nextBytes(bytes);
proxy.nextBytes(bytes);
}

/**
Expand All @@ -307,7 +335,7 @@ public void nextBytes(byte[] bytes) {
*/
@Override
public Stream<SplittableGenerator> splits() {
return super.splits();
return proxy.splits();
}

/**
Expand All @@ -318,7 +346,7 @@ public Stream<SplittableGenerator> splits() {
*/
@Override
public Stream<SplittableGenerator> splits(long streamSize) {
return super.splits(streamSize, this);
return proxy.splits(streamSize, this);
}

/**
Expand All @@ -329,7 +357,7 @@ public Stream<SplittableGenerator> splits(long streamSize) {
*/
@Override
public Stream<SplittableGenerator> splits(SplittableGenerator source) {
return super.splits(Long.MAX_VALUE, source);
return proxy.splits(Long.MAX_VALUE, source);
}

/**
Expand All @@ -341,7 +369,7 @@ public Stream<SplittableGenerator> splits(SplittableGenerator source) {
*/
@Override
public Stream<SplittableGenerator> splits(long streamSize, SplittableGenerator source) {
return super.splits(streamSize, source);
return proxy.splits(streamSize, source);
}

/**
Expand All @@ -356,7 +384,7 @@ public Stream<SplittableGenerator> splits(long streamSize, SplittableGenerator s
*/
@Override
public IntStream ints(long streamSize) {
return super.ints(streamSize);
return proxy.ints(streamSize);
}

/**
Expand All @@ -370,7 +398,7 @@ public IntStream ints(long streamSize) {
*/
@Override
public IntStream ints() {
return super.ints();
return proxy.ints();
}

/**
Expand All @@ -390,7 +418,7 @@ public IntStream ints() {
*/
@Override
public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) {
return super.ints(streamSize, randomNumberOrigin, randomNumberBound);
return proxy.ints(streamSize, randomNumberOrigin, randomNumberBound);
}

/**
Expand All @@ -410,7 +438,7 @@ public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberB
*/
@Override
public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
return super.ints(randomNumberOrigin, randomNumberBound);
return proxy.ints(randomNumberOrigin, randomNumberBound);
}

/**
Expand All @@ -425,7 +453,7 @@ public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
*/
@Override
public LongStream longs(long streamSize) {
return super.longs(streamSize);
return proxy.longs(streamSize);
}

/**
Expand All @@ -439,7 +467,7 @@ public LongStream longs(long streamSize) {
*/
@Override
public LongStream longs() {
return super.longs();
return proxy.longs();
}

/**
Expand All @@ -459,7 +487,7 @@ public LongStream longs() {
*/
@Override
public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) {
return super.longs(streamSize, randomNumberOrigin, randomNumberBound);
return proxy.longs(streamSize, randomNumberOrigin, randomNumberBound);
}

/**
Expand All @@ -479,7 +507,7 @@ public LongStream longs(long streamSize, long randomNumberOrigin, long randomNum
*/
@Override
public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
return super.longs(randomNumberOrigin, randomNumberBound);
return proxy.longs(randomNumberOrigin, randomNumberBound);
}

/**
Expand All @@ -494,7 +522,7 @@ public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
*/
@Override
public DoubleStream doubles(long streamSize) {
return super.doubles(streamSize);
return proxy.doubles(streamSize);
}

/**
Expand All @@ -509,7 +537,7 @@ public DoubleStream doubles(long streamSize) {
*/
@Override
public DoubleStream doubles() {
return super.doubles();
return proxy.doubles();
}

/**
Expand All @@ -529,7 +557,7 @@ public DoubleStream doubles() {
*/
@Override
public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) {
return super.doubles(streamSize, randomNumberOrigin, randomNumberBound);
return proxy.doubles(streamSize, randomNumberOrigin, randomNumberBound);
}

/**
Expand All @@ -549,6 +577,6 @@ public DoubleStream doubles(long streamSize, double randomNumberOrigin, double r
*/
@Override
public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
return super.doubles(randomNumberOrigin, randomNumberBound);
return proxy.doubles(randomNumberOrigin, randomNumberBound);
}
}

1 comment on commit 3bbd233

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.