src/java.base/share/classes/java/util/AbstractSpliteratorRng.java
branchJDK-8193209-branch
changeset 57436 b0c958c0e6c6
parent 57435 9a4184201823
child 57437 f02ffcb61dce
equal deleted inserted replaced
57435:9a4184201823 57436:b0c958c0e6c6
     1 /*
       
     2  * Copyright (c) 2013, 2019, Oraclea and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package java.util;
       
    26 
       
    27 import java.util.Spliterator;
       
    28 import java.util.function.Consumer;
       
    29 import java.util.function.IntConsumer;
       
    30 import java.util.function.LongConsumer;
       
    31 import java.util.function.DoubleConsumer;
       
    32 import java.util.stream.StreamSupport;
       
    33 import java.util.stream.IntStream;
       
    34 import java.util.stream.LongStream;
       
    35 import java.util.stream.DoubleStream;
       
    36 
       
    37 /**
       
    38  * This class overrides the stream-producing methods (such as {@code ints()})
       
    39  * in class {@code AbstractRng} to provide {@code Spliterator}-based
       
    40  * implmentations that support potentially parallel execution.
       
    41  *
       
    42  * To implement a pseudorandom number generator, the programmer needs
       
    43  * only to extend this class and provide implementations for the methods
       
    44  * {@code nextInt()}, {@code nextLong()}, {@code makeIntsSpliterator},
       
    45  * {@code makeLongsSpliterator}, and {@code makeDoublesSpliterator}.
       
    46  *
       
    47  * This class is not public; it provides shared code to the public
       
    48  * classes {@code AbstractSplittableRng}, {@code AbstractSharedRng},
       
    49  * and {@code AbstractArbitrarilyJumpableRng}.
       
    50  *
       
    51  * @author  Guy Steele
       
    52  * @author  Doug Lea
       
    53  * @since   1.9
       
    54  */
       
    55 
       
    56 abstract class AbstractSpliteratorRng implements Rng {
       
    57     /*
       
    58      * Implementation Overview.
       
    59      *
       
    60      * This class provides most of the "user API" methods needed to
       
    61      * satisfy the interface java.util.Rng.  An implementation of this
       
    62      * interface need only extend this class and provide implementations
       
    63      * of six methods: nextInt, nextLong, and nextDouble (the versions
       
    64      * that take no arguments) and makeIntsSpliterator,
       
    65      * makeLongsSpliterator, and makeDoublesSpliterator.
       
    66      *
       
    67      * File organization: First the non-public abstract methods needed
       
    68      * to create spliterators, then the main public methods.
       
    69      */
       
    70 
       
    71     abstract Spliterator.OfInt makeIntsSpliterator(long index, long fence, int origin, int bound);
       
    72     abstract Spliterator.OfLong makeLongsSpliterator(long index, long fence, long origin, long bound);
       
    73     abstract Spliterator.OfDouble makeDoublesSpliterator(long index, long fence, double origin, double bound);
       
    74 	
       
    75     /* ---------------- public methods ---------------- */
       
    76 
       
    77     // stream methods, coded in a way intended to better isolate for
       
    78     // maintenance purposes the small differences across forms.
       
    79 
       
    80     /**
       
    81      * Returns a stream producing the given {@code streamSize} number
       
    82      * of pseudorandom {@code int} values from this generator and/or
       
    83      * one split from it.
       
    84      *
       
    85      * @param streamSize the number of values to generate
       
    86      * @return a stream of pseudorandom {@code int} values
       
    87      * @throws IllegalArgumentException if {@code streamSize} is
       
    88      *         less than zero
       
    89      */
       
    90     public IntStream ints(long streamSize) {
       
    91 	RngSupport.checkStreamSize(streamSize);
       
    92         return StreamSupport.intStream
       
    93             (makeIntsSpliterator(0L, streamSize, Integer.MAX_VALUE, 0),
       
    94              false);
       
    95     }
       
    96 
       
    97     /**
       
    98      * Returns an effectively unlimited stream of pseudorandomly chosen
       
    99      * {@code int} values.
       
   100      *
       
   101      * @implNote The implementation of this method is effectively
       
   102      * equivalent to {@code ints(Long.MAX_VALUE)}.
       
   103      *
       
   104      * @return a stream of pseudorandomly chosen {@code int} values
       
   105      */
       
   106     
       
   107     public IntStream ints() {
       
   108         return StreamSupport.intStream
       
   109             (makeIntsSpliterator(0L, Long.MAX_VALUE, Integer.MAX_VALUE, 0),
       
   110              false);
       
   111     }
       
   112 
       
   113     /**
       
   114      * Returns a stream producing the given {@code streamSize} number
       
   115      * of pseudorandom {@code int} values from this generator and/or one split
       
   116      * from it; each value conforms to the given origin (inclusive) and bound
       
   117      * (exclusive).
       
   118      *
       
   119      * @param streamSize the number of values to generate
       
   120      * @param randomNumberOrigin the origin (inclusive) of each random value
       
   121      * @param randomNumberBound the bound (exclusive) of each random value
       
   122      * @return a stream of pseudorandom {@code int} values,
       
   123      *         each with the given origin (inclusive) and bound (exclusive)
       
   124      * @throws IllegalArgumentException if {@code streamSize} is
       
   125      *         less than zero, or {@code randomNumberOrigin}
       
   126      *         is greater than or equal to {@code randomNumberBound}
       
   127      */
       
   128     public IntStream ints(long streamSize, int randomNumberOrigin,
       
   129 			   int randomNumberBound) {
       
   130 	RngSupport.checkStreamSize(streamSize);
       
   131 	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
       
   132         return StreamSupport.intStream
       
   133             (makeIntsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound),
       
   134              false);
       
   135     }
       
   136 
       
   137     /**
       
   138      * Returns an effectively unlimited stream of pseudorandom {@code
       
   139      * int} values from this generator and/or one split from it; each value
       
   140      * conforms to the given origin (inclusive) and bound (exclusive).
       
   141      *
       
   142      * @implNote This method is implemented to be equivalent to {@code
       
   143      * ints(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
       
   144      *
       
   145      * @param randomNumberOrigin the origin (inclusive) of each random value
       
   146      * @param randomNumberBound the bound (exclusive) of each random value
       
   147      * @return a stream of pseudorandom {@code int} values,
       
   148      *         each with the given origin (inclusive) and bound (exclusive)
       
   149      * @throws IllegalArgumentException if {@code randomNumberOrigin}
       
   150      *         is greater than or equal to {@code randomNumberBound}
       
   151      */
       
   152     public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
       
   153 	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
       
   154         return StreamSupport.intStream
       
   155             (makeIntsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
       
   156              false);
       
   157     }
       
   158 
       
   159     /**
       
   160      * Returns a stream producing the given {@code streamSize} number
       
   161      * of pseudorandom {@code long} values from this generator and/or
       
   162      * one split from it.
       
   163      *
       
   164      * @param streamSize the number of values to generate
       
   165      * @return a stream of pseudorandom {@code long} values
       
   166      * @throws IllegalArgumentException if {@code streamSize} is
       
   167      *         less than zero
       
   168      */
       
   169     public LongStream longs(long streamSize) {
       
   170 	RngSupport.checkStreamSize(streamSize);
       
   171         return StreamSupport.longStream
       
   172             (makeLongsSpliterator(0L, streamSize, Long.MAX_VALUE, 0L),
       
   173              false);
       
   174     }
       
   175 
       
   176     /**
       
   177      * Returns an effectively unlimited stream of pseudorandom {@code
       
   178      * long} values from this generator and/or one split from it.
       
   179      *
       
   180      * @implNote This method is implemented to be equivalent to {@code
       
   181      * longs(Long.MAX_VALUE)}.
       
   182      *
       
   183      * @return a stream of pseudorandom {@code long} values
       
   184      */
       
   185     public LongStream longs() {
       
   186         return StreamSupport.longStream
       
   187             (makeLongsSpliterator(0L, Long.MAX_VALUE, Long.MAX_VALUE, 0L),
       
   188              false);
       
   189     }
       
   190 
       
   191     /**
       
   192      * Returns a stream producing the given {@code streamSize} number of
       
   193      * pseudorandom {@code long} values from this generator and/or one split
       
   194      * from it; each value conforms to the given origin (inclusive) and bound
       
   195      * (exclusive).
       
   196      *
       
   197      * @param streamSize the number of values to generate
       
   198      * @param randomNumberOrigin the origin (inclusive) of each random value
       
   199      * @param randomNumberBound the bound (exclusive) of each random value
       
   200      * @return a stream of pseudorandom {@code long} values,
       
   201      *         each with the given origin (inclusive) and bound (exclusive)
       
   202      * @throws IllegalArgumentException if {@code streamSize} is
       
   203      *         less than zero, or {@code randomNumberOrigin}
       
   204      *         is greater than or equal to {@code randomNumberBound}
       
   205      */
       
   206     public LongStream longs(long streamSize, long randomNumberOrigin,
       
   207 			     long randomNumberBound) {
       
   208 	RngSupport.checkStreamSize(streamSize);
       
   209 	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
       
   210         return StreamSupport.longStream
       
   211             (makeLongsSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound),
       
   212              false);
       
   213     }
       
   214 
       
   215     /**
       
   216      * Returns an effectively unlimited stream of pseudorandom {@code
       
   217      * long} values from this generator and/or one split from it; each value
       
   218      * conforms to the given origin (inclusive) and bound (exclusive).
       
   219      *
       
   220      * @implNote This method is implemented to be equivalent to {@code
       
   221      * longs(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
       
   222      *
       
   223      * @param randomNumberOrigin the origin (inclusive) of each random value
       
   224      * @param randomNumberBound the bound (exclusive) of each random value
       
   225      * @return a stream of pseudorandom {@code long} values,
       
   226      *         each with the given origin (inclusive) and bound (exclusive)
       
   227      * @throws IllegalArgumentException if {@code randomNumberOrigin}
       
   228      *         is greater than or equal to {@code randomNumberBound}
       
   229      */
       
   230     public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
       
   231 	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
       
   232         return StreamSupport.longStream
       
   233             (makeLongsSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
       
   234              false);
       
   235     }
       
   236 
       
   237     /**
       
   238      * Returns a stream producing the given {@code streamSize} number of
       
   239      * pseudorandom {@code double} values from this generator and/or one split
       
   240      * from it; each value is between zero (inclusive) and one (exclusive).
       
   241      *
       
   242      * @param streamSize the number of values to generate
       
   243      * @return a stream of {@code double} values
       
   244      * @throws IllegalArgumentException if {@code streamSize} is
       
   245      *         less than zero
       
   246      */
       
   247     public DoubleStream doubles(long streamSize) {
       
   248 	RngSupport.checkStreamSize(streamSize);
       
   249         return StreamSupport.doubleStream
       
   250             (makeDoublesSpliterator(0L, streamSize, Double.MAX_VALUE, 0.0),
       
   251              false);
       
   252     }
       
   253 
       
   254     /**
       
   255      * Returns an effectively unlimited stream of pseudorandom {@code
       
   256      * double} values from this generator and/or one split from it; each value
       
   257      * is between zero (inclusive) and one (exclusive).
       
   258      *
       
   259      * @implNote This method is implemented to be equivalent to {@code
       
   260      * doubles(Long.MAX_VALUE)}.
       
   261      *
       
   262      * @return a stream of pseudorandom {@code double} values
       
   263      */
       
   264     public DoubleStream doubles() {
       
   265         return StreamSupport.doubleStream
       
   266             (makeDoublesSpliterator(0L, Long.MAX_VALUE, Double.MAX_VALUE, 0.0),
       
   267              false);
       
   268     }
       
   269 
       
   270     /**
       
   271      * Returns a stream producing the given {@code streamSize} number of
       
   272      * pseudorandom {@code double} values from this generator and/or one split
       
   273      * from it; each value conforms to the given origin (inclusive) and bound
       
   274      * (exclusive).
       
   275      *
       
   276      * @param streamSize the number of values to generate
       
   277      * @param randomNumberOrigin the origin (inclusive) of each random value
       
   278      * @param randomNumberBound the bound (exclusive) of each random value
       
   279      * @return a stream of pseudorandom {@code double} values,
       
   280      *         each with the given origin (inclusive) and bound (exclusive)
       
   281      * @throws IllegalArgumentException if {@code streamSize} is
       
   282      *         less than zero
       
   283      * @throws IllegalArgumentException if {@code randomNumberOrigin}
       
   284      *         is greater than or equal to {@code randomNumberBound}
       
   285      */
       
   286     public DoubleStream doubles(long streamSize, double randomNumberOrigin,
       
   287 				 double randomNumberBound) {
       
   288 	RngSupport.checkStreamSize(streamSize);
       
   289 	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
       
   290         return StreamSupport.doubleStream
       
   291             (makeDoublesSpliterator(0L, streamSize, randomNumberOrigin, randomNumberBound),
       
   292              false);
       
   293     }
       
   294 
       
   295     /**
       
   296      * Returns an effectively unlimited stream of pseudorandom {@code
       
   297      * double} values from this generator and/or one split from it; each value
       
   298      * conforms to the given origin (inclusive) and bound (exclusive).
       
   299      *
       
   300      * @implNote This method is implemented to be equivalent to {@code
       
   301      * doubles(Long.MAX_VALUE, randomNumberOrigin, randomNumberBound)}.
       
   302      *
       
   303      * @param randomNumberOrigin the origin (inclusive) of each random value
       
   304      * @param randomNumberBound the bound (exclusive) of each random value
       
   305      * @return a stream of pseudorandom {@code double} values,
       
   306      *         each with the given origin (inclusive) and bound (exclusive)
       
   307      * @throws IllegalArgumentException if {@code randomNumberOrigin}
       
   308      *         is greater than or equal to {@code randomNumberBound}
       
   309      */
       
   310     public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
       
   311 	RngSupport.checkRange(randomNumberOrigin, randomNumberBound);
       
   312         return StreamSupport.doubleStream
       
   313             (makeDoublesSpliterator(0L, Long.MAX_VALUE, randomNumberOrigin, randomNumberBound),
       
   314              false);
       
   315     }
       
   316 
       
   317 }