newrandom/AbstractSharedRng.java
branchbriangoetz-test-branch
changeset 57369 6d87e9f7a1ec
equal deleted inserted replaced
57366:c646b256fbcc 57369:6d87e9f7a1ec
       
     1 /*
       
     2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
       
     4  *
       
     5  *
       
     6  *
       
     7  *
       
     8  *
       
     9  *
       
    10  *
       
    11  *
       
    12  *
       
    13  *
       
    14  *
       
    15  *
       
    16  *
       
    17  *
       
    18  *
       
    19  *
       
    20  *
       
    21  *
       
    22  *
       
    23  *
       
    24  */
       
    25 
       
    26 // package java.util;
       
    27 
       
    28 import java.util.Spliterator;
       
    29 import java.util.function.Consumer;
       
    30 import java.util.function.IntConsumer;
       
    31 import java.util.function.LongConsumer;
       
    32 import java.util.function.DoubleConsumer;
       
    33 import java.util.stream.StreamSupport;
       
    34 import java.util.stream.Stream;
       
    35 
       
    36 /**
       
    37  * This class provides much of the implementation of the {@code Rng}
       
    38  * interface, to minimize the effort required to implement that interface.
       
    39  *
       
    40  * To implement a pseudorandom number generator, the programmer needs
       
    41  * only to extend this class and provide implementations for the
       
    42  * {@code nextInt()} and {@code nextLong()} methods.  In order for
       
    43  * the implementations of other methods in this class to operate
       
    44  * correctly, it must be safe for multiple threads to call these
       
    45  * methods on that same object.  The principal purpose of this class
       
    46  * is to support the implementations of {@code java.util.Random}
       
    47  * and {@code java.util.concurrent.ThreadLocalRandom}, but it could
       
    48  * in principle be used to implement others as well.
       
    49  *
       
    50  * (If the pseudorandom number generator has the ability to split or
       
    51  * jump, then the programmer may wish to consider instead extending
       
    52  * another abstract class, such as {@code AbstractSplittableRng},
       
    53  * {@code AbstractJumpableRng}, {@code AbstractArbitrarilyJumpableRng},
       
    54  * {@code AbstractSplittableJumpableRng}, or
       
    55  * {@code AbstractSplittableArbitrarilyJumpableRng}.)
       
    56  *
       
    57  * The programmer should generally provide at least three constructors:
       
    58  * one that takes no arguments, one that accepts a {@code long}
       
    59  * seed value, and one that accepts an array of seed {@code byte} values.
       
    60  * This class provides a public {@code initialSeed()} method that may
       
    61  * be useful in initializing some static state from which to derive
       
    62  * defaults seeds for use by the no-argument constructor.
       
    63  *
       
    64  * For the stream methods (such as {@code ints()} and {@code splits()}),
       
    65  * this class provides {@code Spliterator}-based implementations that
       
    66  * allow parallel execution when appropriate.
       
    67  *
       
    68  * The documentation for each non-abstract method in this class
       
    69  * describes its implementation in detail. Each of these methods may
       
    70  * be overridden if the pseudorandom number generator being
       
    71  * implemented admits a more efficient implementation.
       
    72  *
       
    73  * @author  Guy Steele
       
    74  * @author  Doug Lea
       
    75  * @since   1.9
       
    76  */
       
    77 public abstract class AbstractSharedRng extends AbstractSpliteratorRng {
       
    78 
       
    79     /*
       
    80      * Implementation Overview.
       
    81      *
       
    82      * This class provides most of the "user API" methods needed to
       
    83      * satisfy the interface java.util.Rng.  Most of these methods
       
    84      * are in turn inherited from AbstractRng and the non-public class
       
    85      * AbstractSpliteratorRng; this file implements methods and spliterators
       
    86      * necessary to support the latter.
       
    87      *
       
    88      * File organization: First some non-public methods, followed by
       
    89      * some custom spliterator classes needed for stream methods.
       
    90      */
       
    91 
       
    92     // Methods required by class AbstractSpliteratorRng
       
    93     Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound) {
       
    94 	return new RandomIntsSpliterator(this, index, fence, origin, bound);
       
    95     }
       
    96     Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound) {
       
    97 	return new RandomLongsSpliterator(this, index, fence, origin, bound);
       
    98     }
       
    99     Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound) {
       
   100 	return new RandomDoublesSpliterator(this, index, fence, origin, bound);
       
   101     }
       
   102 
       
   103     // Spliterators for producing streams. These are based on abstract
       
   104     // spliterator classes provided by class AbstractSpliteratorRng.
       
   105     // Each one needs to define only a constructor and two methods.
       
   106 
       
   107     static class RandomIntsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfInt {
       
   108 	final AbstractSharedRng generatingRng;
       
   109         final int origin;
       
   110         final int bound;
       
   111 
       
   112         RandomIntsSpliterator(AbstractSharedRng generatingRng, long index, long fence, int origin, int bound) {
       
   113 	    super(index, fence);
       
   114 	    this.generatingRng = generatingRng;
       
   115             this.origin = origin; this.bound = bound;
       
   116         }
       
   117 	
       
   118         public Spliterator.OfInt trySplit() {
       
   119             long i = index, m = (i + fence) >>> 1;
       
   120 	    if (m <= i) return null;
       
   121 	    index = m;
       
   122 	    // The same generatingRng is used, with no splitting or copying.
       
   123 	    return new RandomIntsSpliterator(generatingRng, i, m, origin, bound);
       
   124         }
       
   125 
       
   126         public boolean tryAdvance(IntConsumer consumer) {
       
   127             if (consumer == null) throw new NullPointerException();
       
   128             long i = index, f = fence;
       
   129             if (i < f) {
       
   130                 consumer.accept(RngSupport.boundedNextInt(generatingRng, origin, bound));
       
   131                 index = i + 1;
       
   132                 return true;
       
   133             }
       
   134             else return false;
       
   135         }
       
   136 
       
   137         public void forEachRemaining(IntConsumer consumer) {
       
   138             if (consumer == null) throw new NullPointerException();
       
   139             long i = index, f = fence;
       
   140             if (i < f) {
       
   141                 index = f;
       
   142                 Rng r = generatingRng;
       
   143                 int o = origin, b = bound;
       
   144                 do {
       
   145                     consumer.accept(RngSupport.boundedNextInt(r, o, b));
       
   146                 } while (++i < f);
       
   147             }
       
   148         }
       
   149     }
       
   150 
       
   151     /**
       
   152      * Spliterator for long streams.
       
   153      */
       
   154     static class RandomLongsSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfLong {
       
   155 	final AbstractSharedRng generatingRng;
       
   156         final long origin;
       
   157         final long bound;
       
   158 
       
   159         RandomLongsSpliterator(AbstractSharedRng generatingRng, long index, long fence, long origin, long bound) {
       
   160 	    super(index, fence);
       
   161 	    this.generatingRng = generatingRng;
       
   162             this.origin = origin; this.bound = bound;
       
   163         }
       
   164 	
       
   165         public Spliterator.OfLong trySplit() {
       
   166             long i = index, m = (i + fence) >>> 1;
       
   167 	    if (m <= i) return null;
       
   168 	    index = m;
       
   169 	    // The same generatingRng is used, with no splitting or copying.
       
   170 	    return new RandomLongsSpliterator(generatingRng, i, m, origin, bound);
       
   171         }
       
   172 
       
   173         public boolean tryAdvance(LongConsumer consumer) {
       
   174             if (consumer == null) throw new NullPointerException();
       
   175             long i = index, f = fence;
       
   176             if (i < f) {
       
   177                 consumer.accept(RngSupport.boundedNextLong(generatingRng, origin, bound));
       
   178                 index = i + 1;
       
   179                 return true;
       
   180             }
       
   181             else return false;
       
   182         }
       
   183 
       
   184         public void forEachRemaining(LongConsumer consumer) {
       
   185             if (consumer == null) throw new NullPointerException();
       
   186             long i = index, f = fence;
       
   187             if (i < f) {
       
   188                 index = f;
       
   189                 Rng r = generatingRng;
       
   190                 long o = origin, b = bound;
       
   191                 do {
       
   192                     consumer.accept(RngSupport.boundedNextLong(r, o, b));
       
   193                 } while (++i < f);
       
   194             }
       
   195         }
       
   196     }
       
   197 
       
   198     /**
       
   199      * Spliterator for double streams.
       
   200      */
       
   201     static class RandomDoublesSpliterator extends RngSupport.RandomSpliterator implements Spliterator.OfDouble {
       
   202 	final AbstractSharedRng generatingRng;
       
   203         final double origin;
       
   204         final double bound;
       
   205 
       
   206         RandomDoublesSpliterator(AbstractSharedRng generatingRng, long index, long fence, double origin, double bound) {
       
   207 	    super(index, fence);
       
   208 	    this.generatingRng = generatingRng;
       
   209             this.origin = origin; this.bound = bound;
       
   210         }
       
   211 	
       
   212         public Spliterator.OfDouble trySplit() {
       
   213             long i = index, m = (i + fence) >>> 1;
       
   214 	    if (m <= i) return null;
       
   215 	    index = m;
       
   216 	    // The same generatingRng is used, with no splitting or copying.
       
   217 	    return new RandomDoublesSpliterator(generatingRng, i, m, origin, bound);
       
   218         }
       
   219 
       
   220         public boolean tryAdvance(DoubleConsumer consumer) {
       
   221             if (consumer == null) throw new NullPointerException();
       
   222             long i = index, f = fence;
       
   223             if (i < f) {
       
   224                 consumer.accept(RngSupport.boundedNextDouble(generatingRng, origin, bound));
       
   225                 index = i + 1;
       
   226                 return true;
       
   227             }
       
   228             else return false;
       
   229         }
       
   230 
       
   231         public void forEachRemaining(DoubleConsumer consumer) {
       
   232             if (consumer == null) throw new NullPointerException();
       
   233             long i = index, f = fence;
       
   234             if (i < f) {
       
   235                 index = f;
       
   236                 Rng r = generatingRng;
       
   237                 double o = origin, b = bound;
       
   238                 do {
       
   239                     consumer.accept(RngSupport.boundedNextDouble(r, o, b));
       
   240                 } while (++i < f);
       
   241             }
       
   242         }
       
   243     }
       
   244 
       
   245 }