Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
8270075: SplittableRandom extends AbstractSplittableGenerator
Reviewed-by: rriggs, bpb
  • Loading branch information
JimLaskey committed Jul 14, 2021
1 parent 381bd62 commit 3bbd2332bd4876b5529ccdf90e5e5d6c515e9d58
Showing with 47 additions and 19 deletions.
  1. +47 −19 src/java.base/share/classes/java/util/SplittableRandom.java
@@ -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;
@@ -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.
@@ -182,6 +183,7 @@
private SplittableRandom(long seed, long gamma) {
this.seed = seed;
this.gamma = gamma;
this.proxy = new AbstractSplittableGeneratorProxy();
}

/**
@@ -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.
*/
@@ -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();
}

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

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

/**
@@ -318,7 +346,7 @@ public void nextBytes(byte[] bytes) {
*/
@Override
public Stream<SplittableGenerator> splits(long streamSize) {
return super.splits(streamSize, this);
return proxy.splits(streamSize, this);
}

/**
@@ -329,7 +357,7 @@ public void nextBytes(byte[] bytes) {
*/
@Override
public Stream<SplittableGenerator> splits(SplittableGenerator source) {
return super.splits(Long.MAX_VALUE, source);
return proxy.splits(Long.MAX_VALUE, source);
}

/**
@@ -341,7 +369,7 @@ public void nextBytes(byte[] bytes) {
*/
@Override
public Stream<SplittableGenerator> splits(long streamSize, SplittableGenerator source) {
return super.splits(streamSize, source);
return proxy.splits(streamSize, source);
}

/**
@@ -356,7 +384,7 @@ public void nextBytes(byte[] bytes) {
*/
@Override
public IntStream ints(long streamSize) {
return super.ints(streamSize);
return proxy.ints(streamSize);
}

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

/**
@@ -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);
}

/**
@@ -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);
}

/**
@@ -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);
}

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

/**
@@ -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);
}

/**
@@ -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);
}

/**
@@ -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);
}

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

/**
@@ -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);
}

/**
@@ -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

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 3bbd233 Jul 14, 2021

Please sign in to comment.