diff -r b0c958c0e6c6 -r f02ffcb61dce src/java.base/share/classes/java/util/random/AbstractSpliteratorRNG.java --- a/src/java.base/share/classes/java/util/random/AbstractSpliteratorRNG.java Thu Jun 27 18:02:51 2019 -0300 +++ b/src/java.base/share/classes/java/util/random/AbstractSpliteratorRNG.java Thu Jun 27 18:30:27 2019 -0300 @@ -22,21 +22,19 @@ * or visit www.oracle.com if you need additional information or have any * questions. */ -package java.util; + +package java.util.random; import java.util.Spliterator; -import java.util.function.Consumer; -import java.util.function.IntConsumer; -import java.util.function.LongConsumer; -import java.util.function.DoubleConsumer; -import java.util.stream.StreamSupport; +import java.util.stream.Stream; +import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; -import java.util.stream.DoubleStream; +import java.util.stream.StreamSupport; /** * This class overrides the stream-producing methods (such as {@code ints()}) - * in class {@code AbstractRng} to provide {@code Spliterator}-based + * in class {@link AbstractRNG} to provide {@link Spliterator}-based * implmentations that support potentially parallel execution. * * To implement a pseudorandom number generator, the programmer needs @@ -45,20 +43,18 @@ * {@code makeLongsSpliterator}, and {@code makeDoublesSpliterator}. * * This class is not public; it provides shared code to the public - * classes {@code AbstractSplittableRng}, {@code AbstractSharedRng}, - * and {@code AbstractArbitrarilyJumpableRng}. + * classes {@link AbstractSplittableRNG}, {@link AbstractSharedRNG}, + * and {@link AbstractArbitrarilyJumpableRNG}. * - * @author Guy Steele - * @author Doug Lea - * @since 1.9 + * @since 14 */ -abstract class AbstractSpliteratorRng implements Rng { +abstract class AbstractSpliteratorRNG implements RandomNumberGenerator { /* * Implementation Overview. * * This class provides most of the "user API" methods needed to - * satisfy the interface java.util.Rng. An implementation of this + * satisfy the interface RandomNumberGenerator. An implementation of this * interface need only extend this class and provide implementations * of six methods: nextInt, nextLong, and nextDouble (the versions * that take no arguments) and makeIntsSpliterator, @@ -71,27 +67,37 @@ abstract Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound); abstract Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound); abstract Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound); - + /* ---------------- public methods ---------------- */ // stream methods, coded in a way intended to better isolate for // maintenance purposes the small differences across forms. + private static IntStream intStream(Spliterator.OfInt srng) { + return StreamSupport.intStream(srng, false); + } + + private static LongStream longStream(Spliterator.OfLong srng) { + return StreamSupport.longStream(srng, false); + } + + private static DoubleStream doubleStream(Spliterator.OfDouble srng) { + return StreamSupport.doubleStream(srng, false); + } + /** - * Returns a stream producing the given {@code streamSize} number - * of pseudorandom {@code int} values from this generator and/or - * one split from it. + * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code int} + * values from this generator and/or one split from it. * * @param streamSize the number of values to generate + * * @return a stream of pseudorandom {@code int} values - * @throws IllegalArgumentException if {@code streamSize} is - * less than zero + * + * @throws IllegalArgumentException if {@code streamSize} is less than zero */ public IntStream ints(long streamSize) { - RngSupport.checkStreamSize(streamSize); - return StreamSupport.intStream - (makeIntsSpliterator(0L, streamSize, Integer.MAX_VALUE, 0), - false); + RNGSupport.checkStreamSize(streamSize); + return intStream(makeIntsSpliterator(0L, streamSize, Integer.MAX_VALUE, 0)); } /** @@ -103,215 +109,202 @@ * * @return a stream of pseudorandomly chosen {@code int} values */ - + public IntStream ints() { - return StreamSupport.intStream - (makeIntsSpliterator(0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0), - false); + return intStream(makeIntsSpliterator(0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0)); } /** - * Returns a stream producing the given {@code streamSize} number - * of pseudorandom {@code int} values from this generator and/or one split - * from it; each value conforms to the given origin (inclusive) and bound - * (exclusive). + * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code int} + * values from this generator and/or one split from it; each value conforms to the given origin + * (inclusive) and bound (exclusive). * - * @param streamSize the number of values to generate + * @param streamSize the number of values to generate * @param randomNumberOrigin the origin (inclusive) of each random value - * @param randomNumberBound the bound (exclusive) of each random value - * @return a stream of pseudorandom {@code int} values, - * each with the given origin (inclusive) and bound (exclusive) - * @throws IllegalArgumentException if {@code streamSize} is - * less than zero, or {@code randomNumberOrigin} - * is greater than or equal to {@code randomNumberBound} + * @param randomNumberBound the bound (exclusive) of each random value + * + * @return a stream of pseudorandom {@code int} values, each with the given origin (inclusive) + * and bound (exclusive) + * + * @throws IllegalArgumentException if {@code streamSize} is less than zero, or {@code + * randomNumberOrigin} is greater than or equal to {@code + * randomNumberBound} */ - public IntStream ints(long streamSize, int randomNumberOrigin, - int randomNumberBound) { - RngSupport.checkStreamSize(streamSize); - RngSupport.checkRange(randomNumberOrigin, randomNumberBound); - return StreamSupport.intStream - (makeIntsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound), - false); + public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) { + RNGSupport.checkStreamSize(streamSize); + RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); + return intStream(makeIntsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound)); } /** - * Returns an effectively unlimited stream of pseudorandom {@code - * int} values from this generator and/or one split from it; each value - * conforms to the given origin (inclusive) and bound (exclusive). - * - * @implNote This method is implemented to be equivalent to {@code - * ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. + * Returns an effectively unlimited stream of pseudorandom {@code int} values from this + * generator and/or one split from it; each value conforms to the given origin (inclusive) and + * bound (exclusive). * * @param randomNumberOrigin the origin (inclusive) of each random value - * @param randomNumberBound the bound (exclusive) of each random value - * @return a stream of pseudorandom {@code int} values, - * each with the given origin (inclusive) and bound (exclusive) - * @throws IllegalArgumentException if {@code randomNumberOrigin} - * is greater than or equal to {@code randomNumberBound} + * @param randomNumberBound the bound (exclusive) of each random value + * + * @return a stream of pseudorandom {@code int} values, each with the given origin (inclusive) + * and bound (exclusive) + * + * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to + * {@code randomNumberBound} + * + * @implNote This method is implemented to be equivalent to {@code ints(Long.MAX_VALUE, + * randomNumberOrigin, randomNumberBound)}. */ public IntStream ints(int randomNumberOrigin, int randomNumberBound) { - RngSupport.checkRange(randomNumberOrigin, randomNumberBound); - return StreamSupport.intStream - (makeIntsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), - false); + RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); + return intStream(makeIntsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)); } /** - * Returns a stream producing the given {@code streamSize} number - * of pseudorandom {@code long} values from this generator and/or - * one split from it. + * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code long} + * values from this generator and/or one split from it. * * @param streamSize the number of values to generate + * * @return a stream of pseudorandom {@code long} values - * @throws IllegalArgumentException if {@code streamSize} is - * less than zero + * + * @throws IllegalArgumentException if {@code streamSize} is less than zero */ public LongStream longs(long streamSize) { - RngSupport.checkStreamSize(streamSize); - return StreamSupport.longStream - (makeLongsSpliterator(0L, streamSize, Long.MAX_VALUE, 0L), - false); + RNGSupport.checkStreamSize(streamSize); + return longStream(makeLongsSpliterator(0L, streamSize, Long.MAX_VALUE, 0L)); } /** - * Returns an effectively unlimited stream of pseudorandom {@code - * long} values from this generator and/or one split from it. + * Returns an effectively unlimited stream of pseudorandom {@code long} values from this + * generator and/or one split from it. + * + * @return a stream of pseudorandom {@code long} values * * @implNote This method is implemented to be equivalent to {@code - * longs(Long.MAX_VALUE)}. - * - * @return a stream of pseudorandom {@code long} values + * longs(Long.MAX_VALUE)}. */ public LongStream longs() { - return StreamSupport.longStream - (makeLongsSpliterator(0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L), - false); + return longStream(makeLongsSpliterator(0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L)); } /** - * Returns a stream producing the given {@code streamSize} number of - * pseudorandom {@code long} values from this generator and/or one split - * from it; each value conforms to the given origin (inclusive) and bound - * (exclusive). + * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code long} + * values from this generator and/or one split from it; each value conforms to the given origin + * (inclusive) and bound (exclusive). * - * @param streamSize the number of values to generate + * @param streamSize the number of values to generate * @param randomNumberOrigin the origin (inclusive) of each random value - * @param randomNumberBound the bound (exclusive) of each random value - * @return a stream of pseudorandom {@code long} values, - * each with the given origin (inclusive) and bound (exclusive) - * @throws IllegalArgumentException if {@code streamSize} is - * less than zero, or {@code randomNumberOrigin} - * is greater than or equal to {@code randomNumberBound} + * @param randomNumberBound the bound (exclusive) of each random value + * + * @return a stream of pseudorandom {@code long} values, each with the given origin (inclusive) + * and bound (exclusive) + * + * @throws IllegalArgumentException if {@code streamSize} is less than zero, or {@code + * randomNumberOrigin} is greater than or equal to {@code + * randomNumberBound} */ public LongStream longs(long streamSize, long randomNumberOrigin, - long randomNumberBound) { - RngSupport.checkStreamSize(streamSize); - RngSupport.checkRange(randomNumberOrigin, randomNumberBound); - return StreamSupport.longStream - (makeLongsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound), - false); + long randomNumberBound) { + RNGSupport.checkStreamSize(streamSize); + RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); + return longStream(makeLongsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound)); } /** - * Returns an effectively unlimited stream of pseudorandom {@code - * long} values from this generator and/or one split from it; each value - * conforms to the given origin (inclusive) and bound (exclusive). - * - * @implNote This method is implemented to be equivalent to {@code - * longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. + * Returns an effectively unlimited stream of pseudorandom {@code long} values from this + * generator and/or one split from it; each value conforms to the given origin (inclusive) and + * bound (exclusive). * * @param randomNumberOrigin the origin (inclusive) of each random value - * @param randomNumberBound the bound (exclusive) of each random value - * @return a stream of pseudorandom {@code long} values, - * each with the given origin (inclusive) and bound (exclusive) - * @throws IllegalArgumentException if {@code randomNumberOrigin} - * is greater than or equal to {@code randomNumberBound} + * @param randomNumberBound the bound (exclusive) of each random value + * + * @return a stream of pseudorandom {@code long} values, each with the given origin (inclusive) + * and bound (exclusive) + * + * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to + * {@code randomNumberBound} + * + * @implNote This method is implemented to be equivalent to {@code longs(Long.MAX_VALUE, + * randomNumberOrigin, randomNumberBound)}. */ public LongStream longs(long randomNumberOrigin, long randomNumberBound) { - RngSupport.checkRange(randomNumberOrigin, randomNumberBound); + RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); return StreamSupport.longStream (makeLongsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), false); } /** - * Returns a stream producing the given {@code streamSize} number of - * pseudorandom {@code double} values from this generator and/or one split - * from it; each value is between zero (inclusive) and one (exclusive). + * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code double} + * values from this generator and/or one split from it; each value is between zero (inclusive) + * and one (exclusive). * * @param streamSize the number of values to generate + * * @return a stream of {@code double} values - * @throws IllegalArgumentException if {@code streamSize} is - * less than zero + * + * @throws IllegalArgumentException if {@code streamSize} is less than zero */ public DoubleStream doubles(long streamSize) { - RngSupport.checkStreamSize(streamSize); - return StreamSupport.doubleStream - (makeDoublesSpliterator(0L, streamSize, Double.MAX_VALUE, 0.0), - false); + RNGSupport.checkStreamSize(streamSize); + return doubleStream(makeDoublesSpliterator(0L, streamSize, Double.MAX_VALUE, 0.0)); } /** - * Returns an effectively unlimited stream of pseudorandom {@code - * double} values from this generator and/or one split from it; each value - * is between zero (inclusive) and one (exclusive). + * Returns an effectively unlimited stream of pseudorandom {@code double} values from this + * generator and/or one split from it; each value is between zero (inclusive) and one + * (exclusive). + * + * @return a stream of pseudorandom {@code double} values * * @implNote This method is implemented to be equivalent to {@code - * doubles(Long.MAX_VALUE)}. - * - * @return a stream of pseudorandom {@code double} values + * doubles(Long.MAX_VALUE)}. */ public DoubleStream doubles() { - return StreamSupport.doubleStream - (makeDoublesSpliterator(0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0), - false); + return doubleStream(makeDoublesSpliterator(0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0)); } /** - * Returns a stream producing the given {@code streamSize} number of - * pseudorandom {@code double} values from this generator and/or one split - * from it; each value conforms to the given origin (inclusive) and bound - * (exclusive). + * Returns a stream producing the given {@code streamSize} number of pseudorandom {@code double} + * values from this generator and/or one split from it; each value conforms to the given origin + * (inclusive) and bound (exclusive). * - * @param streamSize the number of values to generate + * @param streamSize the number of values to generate * @param randomNumberOrigin the origin (inclusive) of each random value - * @param randomNumberBound the bound (exclusive) of each random value - * @return a stream of pseudorandom {@code double} values, - * each with the given origin (inclusive) and bound (exclusive) - * @throws IllegalArgumentException if {@code streamSize} is - * less than zero - * @throws IllegalArgumentException if {@code randomNumberOrigin} - * is greater than or equal to {@code randomNumberBound} + * @param randomNumberBound the bound (exclusive) of each random value + * + * @return a stream of pseudorandom {@code double} values, each with the given origin + * (inclusive) and bound (exclusive) + * + * @throws IllegalArgumentException if {@code streamSize} is less than zero + * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to + * {@code randomNumberBound} */ - public DoubleStream doubles(long streamSize, double randomNumberOrigin, - double randomNumberBound) { - RngSupport.checkStreamSize(streamSize); - RngSupport.checkRange(randomNumberOrigin, randomNumberBound); - return StreamSupport.doubleStream - (makeDoublesSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound), - false); + public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) { + RNGSupport.checkStreamSize(streamSize); + RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); + return doubleStream(makeDoublesSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound)); } /** - * Returns an effectively unlimited stream of pseudorandom {@code - * double} values from this generator and/or one split from it; each value - * conforms to the given origin (inclusive) and bound (exclusive). - * - * @implNote This method is implemented to be equivalent to {@code - * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. + * Returns an effectively unlimited stream of pseudorandom {@code double} values from this + * generator and/or one split from it; each value conforms to the given origin (inclusive) and + * bound (exclusive). * * @param randomNumberOrigin the origin (inclusive) of each random value - * @param randomNumberBound the bound (exclusive) of each random value - * @return a stream of pseudorandom {@code double} values, - * each with the given origin (inclusive) and bound (exclusive) - * @throws IllegalArgumentException if {@code randomNumberOrigin} - * is greater than or equal to {@code randomNumberBound} + * @param randomNumberBound the bound (exclusive) of each random value + * + * @return a stream of pseudorandom {@code double} values, each with the given origin + * (inclusive) and bound (exclusive) + * + * @throws IllegalArgumentException if {@code randomNumberOrigin} is greater than or equal to + * {@code randomNumberBound} + * + * @implNote This method is implemented to be equivalent to {@code + * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}. */ public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) { - RngSupport.checkRange(randomNumberOrigin, randomNumberBound); - return StreamSupport.doubleStream - (makeDoublesSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound), - false); + RNGSupport.checkRange(randomNumberOrigin, randomNumberBound); + return doubleStream(makeDoublesSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)); } }