author | jlaskey |
Thu, 27 Jun 2019 18:30:27 -0300 | |
branch | JDK-8193209-branch |
changeset 57437 | f02ffcb61dce |
parent 57436 | b0c958c0e6c6 |
permissions | -rw-r--r-- |
57388 | 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 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
26 |
package java.util.random; |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
27 |
|
57388 | 28 |
import java.util.stream.Stream; |
29 |
||
30 |
||
31 |
/** |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
32 |
* The {@link StreamableRNG} interface augments the {@link RandomNumberGenerator} interface |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
33 |
* to provide methods that return streams of {@link RandomNumberGenerator} objects. |
57388 | 34 |
* Ideally, such a stream of objects would have the property that the |
35 |
* behavior of each object is statistically independent of all the others. |
|
36 |
* In practice, one may have to settle for some approximation to this property. |
|
37 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
38 |
* A generator that implements interface {@link SplittableRNG} |
57388 | 39 |
* may choose to use its {@code splits} method to implement the {@code rngs} |
40 |
* method required by this interface. |
|
41 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
42 |
* A generator that implements interface {@link JumpableRNG} |
57388 | 43 |
* may choose to use its {@code jumps} method to implement the {@code rngs} |
44 |
* method required by this interface. |
|
45 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
46 |
* A generator that implements interface {@link LeapableRNG} |
57388 | 47 |
* may choose to use its {@code leaps} method to implement the {@code rngs} |
48 |
* method required by this interface. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
49 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
50 |
* An implementation of the {@link StreamableRNG} interface must provide |
57388 | 51 |
* concrete definitions for the methods {@code nextInt()}, {@code nextLong}, |
52 |
* {@code period()}, and {@code rngs()}. |
|
53 |
* Default implementations are provided for all other methods. |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
54 |
* <p> |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
55 |
* Objects that implement {@link StreamableRNG} are typically |
57388 | 56 |
* not cryptographically secure. Consider instead using |
57 |
* {@link java.security.SecureRandom} to get a cryptographically |
|
58 |
* secure pseudo-random number generator for use by |
|
59 |
* security-sensitive applications. |
|
60 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
61 |
* @since 14 |
57388 | 62 |
*/ |
63 |
||
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
64 |
public interface StreamableRNG extends RandomNumberGenerator { |
57388 | 65 |
/** |
66 |
* Returns an effectively unlimited stream of objects, each of |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
67 |
* which implements the {@link RandomNumberGenerator} interface. Ideally the |
57388 | 68 |
* generators in the stream will appear to be statistically |
69 |
* independent. The new generators should be of the same kind |
|
70 |
* as this generator. |
|
71 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
72 |
* @return a stream of objects that implement the {@link RandomNumberGenerator} interface |
57388 | 73 |
* |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
74 |
* @implNote It is permitted to implement this method in a manner |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
75 |
* equivalent to {@code rngs(Long.MAX_VALUE)}. |
57388 | 76 |
*/ |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
77 |
Stream<RandomNumberGenerator> rngs(); |
57388 | 78 |
|
79 |
/** |
|
80 |
* Returns an effectively unlimited stream of objects, each of |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
81 |
* which implements the {@link RandomNumberGenerator} interface. Ideally the |
57388 | 82 |
* generators in the stream will appear to be statistically |
83 |
* independent. The new generators should be of the same kind |
|
84 |
* as this generator. |
|
85 |
* |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
86 |
* @param streamSize the number of generators to generate |
57388 | 87 |
* |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
88 |
* @return a stream of objects that implement the {@link RandomNumberGenerator} interface |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
89 |
* |
57388 | 90 |
* @throws IllegalArgumentException if {@code streamSize} is |
91 |
* less than zero |
|
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
92 |
* |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
93 |
* @implNote The default implementation calls {@code rngs()} and |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
94 |
* then limits its length to {@code streamSize}. |
57388 | 95 |
*/ |
57437
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
96 |
default Stream<RandomNumberGenerator> rngs(long streamSize) { |
f02ffcb61dce
Rename Rng.java to RandomNumberGenerator.java, clean up javadoc and misc code changes
jlaskey
parents:
57436
diff
changeset
|
97 |
RNGSupport.checkStreamSize(streamSize); |
57388 | 98 |
return rngs().limit(streamSize); |
99 |
} |
|
100 |
} |