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