src/java.base/share/classes/java/util/random/AbstractSpliteratorRNG.java
branchJDK-8193209-branch
changeset 57547 56cbdc3ea079
parent 57546 1ca1cfdcb451
child 57671 6a4be8bf8990
--- a/src/java.base/share/classes/java/util/random/AbstractSpliteratorRNG.java	Fri Jul 26 15:20:31 2019 -0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,310 +0,0 @@
-/*
- * Copyright (c) 2013, 2019, Oraclea and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-package java.util.random;
-
-import java.util.Spliterator;
-import java.util.stream.Stream;
-import java.util.stream.DoubleStream;
-import java.util.stream.IntStream;
-import java.util.stream.LongStream;
-import java.util.stream.StreamSupport;
-
-/**
- * This class overrides the stream-producing methods (such as {@code ints()})
- * in class {@link AbstractRNG} to provide {@link Spliterator}-based
- * implmentations that support potentially parallel execution.
- *
- * To implement a pseudorandom number generator, the programmer needs
- * only to extend this class and provide implementations for the methods
- * {@code nextInt()}, {@code nextLong()}, {@code makeIntsSpliterator},
- * {@code makeLongsSpliterator}, and {@code makeDoublesSpliterator}.
- *
- * This class is not public; it provides shared code to the public
- * classes {@link AbstractSplittableRNG}, {@link AbstractSharedRNG},
- * and {@link AbstractArbitrarilyJumpableRNG}.
- *
- * @since 14
- */
-
-abstract class AbstractSpliteratorRNG implements RandomNumberGenerator {
-    /*
-     * Implementation Overview.
-     *
-     * This class provides most of the "user API" methods needed to
-     * 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,
-     * makeLongsSpliterator, and makeDoublesSpliterator.
-     *
-     * File organization: First the non-public abstract methods needed
-     * to create spliterators, then the main public methods.
-     */
-
-    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.
-     *
-     * @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
-     */
-    public IntStream ints(long streamSize) {
-        RNGSupport.checkStreamSize(streamSize);
-        return intStream(makeIntsSpliterator(0L, streamSize, Integer.MAX_VALUE, 0));
-    }
-
-    /**
-     * Returns an effectively unlimited stream of pseudorandomly chosen
-     * {@code int} values.
-     *
-     * @implNote The implementation of this method is effectively
-     * equivalent to {@code ints(Long.MAX_VALUE)}.
-     *
-     * @return a stream of pseudorandomly chosen {@code int} values
-     */
-
-    public IntStream ints() {
-        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).
-     *
-     * @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}
-     */
-    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).
-     *
-     * @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}
-     *
-     * @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 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.
-     *
-     * @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
-     */
-    public LongStream longs(long streamSize) {
-        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.
-     *
-     * @return a stream of pseudorandom {@code long} values
-     *
-     * @implNote This method is implemented to be equivalent to {@code
-     *         longs(Long.MAX_VALUE)}.
-     */
-    public LongStream longs() {
-        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).
-     *
-     * @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}
-     */
-    public LongStream longs(long streamSize, long randomNumberOrigin,
-                             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).
-     *
-     * @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}
-     *
-     * @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);
-        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).
-     *
-     * @param streamSize the number of values to generate
-     *
-     * @return a stream of {@code double} values
-     *
-     * @throws IllegalArgumentException if {@code streamSize} is less than zero
-     */
-    public DoubleStream doubles(long streamSize) {
-        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).
-     *
-     * @return a stream of pseudorandom {@code double} values
-     *
-     * @implNote This method is implemented to be equivalent to {@code
-     *         doubles(Long.MAX_VALUE)}.
-     */
-    public DoubleStream doubles() {
-        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).
-     *
-     * @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}
-     */
-    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).
-     *
-     * @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}
-     *
-     * @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 doubleStream(makeDoublesSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound));
-    }
-
-}