src/java.base/share/classes/java/util/random/AbstractSharedRNG.java
branchJDK-8193209-branch
changeset 57547 56cbdc3ea079
parent 57546 1ca1cfdcb451
child 57671 6a4be8bf8990
--- a/src/java.base/share/classes/java/util/random/AbstractSharedRNG.java	Fri Jul 26 15:20:31 2019 -0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,238 +0,0 @@
-/*
- * Copyright (c) 2013, 2019, Oracle 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.function.DoubleConsumer;
-import java.util.function.IntConsumer;
-import java.util.function.LongConsumer;
-
-/**
- * This class provides much of the implementation of the {@link RandomNumberGenerator}
- * interface, to minimize the effort required to implement that interface.
- *
- * To implement a pseudorandom number generator, the programmer needs
- * only to extend this class and provide implementations for the
- * {@code nextInt()} and {@code nextLong()} methods.  In order for
- * the implementations of other methods in this class to operate
- * correctly, it must be safe for multiple threads to call these
- * methods on that same object.  The principal purpose of this class
- * is to support the implementations of {@link java.util.Random}
- * and {@code java.util.concurrent.ThreadLocalRandom}, but it could
- * in principle be used to implement others as well.
- *
- * (If the pseudorandom number generator has the ability to split or
- * jump, then the programmer may wish to consider instead extending
- * another abstract class, such as {@link AbstractSplittableRNG},
- * {@link ArbitrarilyJumpableRNG}, or {@link AbstractArbitrarilyJumpableRNG}.)
- *
- * The programmer should generally provide at least three constructors:
- * one that takes no arguments, one that accepts a {@code long}
- * seed value, and one that accepts an array of seed {@code byte} values.
- * This class provides a public {@code initialSeed()} method that may
- * be useful in initializing some static state from which to derive
- * defaults seeds for use by the no-argument constructor.
- *
- * For the stream methods (such as {@code ints()} and {@code splits()}),
- * this class provides {@link Spliterator}-based implementations that
- * allow parallel execution when appropriate.
- *
- * The documentation for each non-abstract method in this class
- * describes its implementation in detail. Each of these methods may
- * be overridden if the pseudorandom number generator being
- * implemented admits a more efficient implementation.
- *
- * @since 14
- */
-public abstract class AbstractSharedRNG extends AbstractSpliteratorRNG {
-    /*
-     * Implementation Overview.
-     *
-     * This class provides most of the "user API" methods needed to
-     * satisfy the interface RandomNumberGenerator.  Most of these methods
-     * are in turn inherited from AbstractRNG and the non-public class
-     * AbstractSpliteratorRNG; this file implements methods and spliterators
-     * necessary to support the latter.
-     *
-     * File organization: First some non-public methods, followed by
-     * some custom spliterator classes needed for stream methods.
-     */
-
-    // Methods required by class AbstractSpliteratorRNG
-    Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
-        return new RandomIntsSpliterator(this, index, fence, origin, bound);
-    }
-    Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
-        return new RandomLongsSpliterator(this, index, fence, origin, bound);
-    }
-    Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
-        return new RandomDoublesSpliterator(this, index, fence, origin, bound);
-    }
-
-    /**
-     * Spliterators for producing streams. These are based on abstract spliterator classes provided
-     * by class AbstractSpliteratorRNG. Each one needs to define only a constructor and two
-     * methods.
-     */
-    static class RandomIntsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator.OfInt {
-        final AbstractSharedRNG generatingRNG;
-        final int origin;
-        final int bound;
-
-        RandomIntsSpliterator(AbstractSharedRNG generatingRNG, long index, long fence, int origin, int bound) {
-            super(index, fence);
-            this.generatingRNG = generatingRNG;
-            this.origin = origin; this.bound = bound;
-        }
-
-        public Spliterator.OfInt trySplit() {
-            long i = index, m = (i + fence) >>> 1;
-            if (m <= i) return null;
-            index = m;
-            // The same generatingRNG is used, with no splitting or copying.
-            return new RandomIntsSpliterator(generatingRNG, i, m, origin, bound);
-        }
-
-        public boolean tryAdvance(IntConsumer consumer) {
-            if (consumer == null) throw new NullPointerException();
-            long i = index, f = fence;
-            if (i < f) {
-                consumer.accept(RNGSupport.boundedNextInt(generatingRNG, origin, bound));
-                index = i + 1;
-                return true;
-            }
-            else return false;
-        }
-
-        public void forEachRemaining(IntConsumer consumer) {
-            if (consumer == null) throw new NullPointerException();
-            long i = index, f = fence;
-            if (i < f) {
-                index = f;
-                RandomNumberGenerator r = generatingRNG;
-                int o = origin, b = bound;
-                do {
-                    consumer.accept(RNGSupport.boundedNextInt(r, o, b));
-                } while (++i < f);
-            }
-        }
-    }
-
-    /**
-     * Spliterator for long streams.
-     */
-    static class RandomLongsSpliterator extends RNGSupport.RandomSpliterator implements Spliterator.OfLong {
-        final AbstractSharedRNG generatingRNG;
-        final long origin;
-        final long bound;
-
-        RandomLongsSpliterator(AbstractSharedRNG generatingRNG, long index, long fence, long origin, long bound) {
-            super(index, fence);
-            this.generatingRNG = generatingRNG;
-            this.origin = origin; this.bound = bound;
-        }
-
-        public Spliterator.OfLong trySplit() {
-            long i = index, m = (i + fence) >>> 1;
-            if (m <= i) return null;
-            index = m;
-            // The same generatingRNG is used, with no splitting or copying.
-            return new RandomLongsSpliterator(generatingRNG, i, m, origin, bound);
-        }
-
-        public boolean tryAdvance(LongConsumer consumer) {
-            if (consumer == null) throw new NullPointerException();
-            long i = index, f = fence;
-            if (i < f) {
-                consumer.accept(RNGSupport.boundedNextLong(generatingRNG, origin, bound));
-                index = i + 1;
-                return true;
-            }
-            else return false;
-        }
-
-        public void forEachRemaining(LongConsumer consumer) {
-            if (consumer == null) throw new NullPointerException();
-            long i = index, f = fence;
-            if (i < f) {
-                index = f;
-                RandomNumberGenerator r = generatingRNG;
-                long o = origin, b = bound;
-                do {
-                    consumer.accept(RNGSupport.boundedNextLong(r, o, b));
-                } while (++i < f);
-            }
-        }
-    }
-
-    /**
-     * Spliterator for double streams.
-     */
-    static class RandomDoublesSpliterator extends RNGSupport.RandomSpliterator implements Spliterator.OfDouble {
-        final AbstractSharedRNG generatingRNG;
-        final double origin;
-        final double bound;
-
-        RandomDoublesSpliterator(AbstractSharedRNG generatingRNG, long index, long fence, double origin, double bound) {
-            super(index, fence);
-            this.generatingRNG = generatingRNG;
-            this.origin = origin; this.bound = bound;
-        }
-
-        public Spliterator.OfDouble trySplit() {
-            long i = index, m = (i + fence) >>> 1;
-            if (m <= i) return null;
-            index = m;
-            // The same generatingRNG is used, with no splitting or copying.
-            return new RandomDoublesSpliterator(generatingRNG, i, m, origin, bound);
-        }
-
-        public boolean tryAdvance(DoubleConsumer consumer) {
-            if (consumer == null) throw new NullPointerException();
-            long i = index, f = fence;
-            if (i < f) {
-                consumer.accept(RNGSupport.boundedNextDouble(generatingRNG, origin, bound));
-                index = i + 1;
-                return true;
-            }
-            else return false;
-        }
-
-        public void forEachRemaining(DoubleConsumer consumer) {
-            if (consumer == null) throw new NullPointerException();
-            long i = index, f = fence;
-            if (i < f) {
-                index = f;
-                RandomNumberGenerator r = generatingRNG;
-                double o = origin, b = bound;
-                do {
-                    consumer.accept(RNGSupport.boundedNextDouble(r, o, b));
-                } while (++i < f);
-            }
-        }
-    }
-
-}