newrandom/StreamableRng.java
author briangoetz
Thu, 23 May 2019 16:45:56 -0400
branchbriangoetz-test-branch
changeset 57369 6d87e9f7a1ec
permissions -rwxr-xr-x
Initial comment in newrandom/
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57369
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     1
/*
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     3
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     4
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     5
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     6
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     7
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     8
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
     9
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    10
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    11
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    12
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    13
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    14
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    15
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    16
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    17
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    18
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    19
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    20
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    21
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    22
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    23
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    24
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    25
// package java.util;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    26
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    27
import java.math.BigInteger;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    28
import java.util.stream.DoubleStream;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    29
import java.util.stream.IntStream;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    30
import java.util.stream.LongStream;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    31
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    32
/**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    33
 * The {@code StreamableRng} interface augments the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    34
 * to provide methods that return streams of {@code Rng} objects.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    35
 * Ideally, such a stream of objects would have the property that the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    36
 * behavior of each object is statistically independent of all the others.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    37
 * In practice, one may have to settle for some approximation to this property.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    38
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    39
 * A generator that implements interface {@link java.util.SplittableRng}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    40
 * may choose to use its {@code splits} method to implement the {@code rngs}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    41
 * method required by this interface.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    42
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    43
 * A generator that implements interface {@link java.util.JumpableRng}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    44
 * may choose to use its {@code jumps} method to implement the {@code rngs}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    45
 * method required by this interface.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    46
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    47
 * A generator that implements interface {@link java.util.LeapableRng}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    48
 * may choose to use its {@code leaps} method to implement the {@code rngs}
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    49
 * method required by this interface.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    50
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    51
 * <p>An implementation of the {@code StreamableRng} interface must provide
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    52
 * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    53
 * {@code period()}, and {@code rngs()}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    54
 * Default implementations are provided for all other methods.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    55
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    56
 * <p>Objects that implement {@code java.util.StreamableRng} are typically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    57
 * not cryptographically secure.  Consider instead using
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    58
 * {@link java.security.SecureRandom} to get a cryptographically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    59
 * secure pseudo-random number generator for use by
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    60
 * security-sensitive applications.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    61
 *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    62
 * @author  Guy Steele
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    63
 * @since   1.9
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    64
 */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    65
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    66
import java.util.stream.Stream;
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    67
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    68
interface StreamableRng extends Rng {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    69
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    70
     * Returns an effectively unlimited stream of objects, each of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    71
     * which implements the {@code Rng} interface.  Ideally the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    72
     * generators in the stream will appear to be statistically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    73
     * independent.  The new generators should be of the same kind
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    74
     * as this generator.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    75
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    76
     * @implNote It is permitted to implement this method in a manner
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    77
     * equivalent to {@code rngs(Long.MAX_VALUE)}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    78
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    79
     * @return a stream of objects that implement the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    80
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    81
    Stream<Rng> rngs();
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    82
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    83
    /**
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    84
     * Returns an effectively unlimited stream of objects, each of
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    85
     * which implements the {@code Rng} interface.  Ideally the
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    86
     * generators in the stream will appear to be statistically
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    87
     * independent.  The new generators should be of the same kind
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    88
     * as this generator.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    89
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    90
     * @implNote The default implementation calls {@code rngs()} and
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    91
     * then limits its length to {@code streamSize}.
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    92
     *
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    93
     * @param streamSize the number of generators to generate
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    94
     * @return a stream of objects that implement the {@code Rng} interface
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    95
     * @throws IllegalArgumentException if {@code streamSize} is
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    96
     *         less than zero
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    97
     */
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    98
    default Stream<Rng> rngs(long streamSize) {
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
    99
	RngSupport.checkStreamSize(streamSize);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   100
        return rngs().limit(streamSize);
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   101
    }
6d87e9f7a1ec Initial comment in newrandom/
briangoetz
parents:
diff changeset
   102
}