src/java.base/share/classes/java/util/random/StreamableRNG.java
author jlaskey
Thu, 27 Jun 2019 18:02:51 -0300
branchJDK-8193209-branch
changeset 57436 b0c958c0e6c6
parent 57388 src/java.base/share/classes/java/util/StreamableRng.java@b1e6bc96af3d
child 57437 f02ffcb61dce
permissions -rw-r--r--
Move random number generators to new folder.

/*
 * Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package java.util;

import java.util.Rng;
import java.util.RngSupport;
import java.util.stream.Stream;


/**
 * The {@code StreamableRng} interface augments the {@code Rng} interface
 * to provide methods that return streams of {@code Rng} objects.
 * Ideally, such a stream of objects would have the property that the
 * behavior of each object is statistically independent of all the others.
 * In practice, one may have to settle for some approximation to this property.
 *
 * A generator that implements interface {@link java.util.SplittableRng}
 * may choose to use its {@code splits} method to implement the {@code rngs}
 * method required by this interface.
 *
 * A generator that implements interface {@link java.util.JumpableRng}
 * may choose to use its {@code jumps} method to implement the {@code rngs}
 * method required by this interface.
 *
 * A generator that implements interface {@link java.util.LeapableRng}
 * may choose to use its {@code leaps} method to implement the {@code rngs}
 * method required by this interface.
 *
 * <p>An implementation of the {@code StreamableRng} interface must provide
 * concrete definitions for the methods {@code nextInt()}, {@code nextLong},
 * {@code period()}, and {@code rngs()}.
 * Default implementations are provided for all other methods.
 *
 * <p>Objects that implement {@code java.util.StreamableRng} are typically
 * not cryptographically secure.  Consider instead using
 * {@link java.security.SecureRandom} to get a cryptographically
 * secure pseudo-random number generator for use by
 * security-sensitive applications.
 *
 * @author  Guy Steele
 * @since   1.9
 */

public interface StreamableRng extends Rng {
    /**
     * Returns an effectively unlimited stream of objects, each of
     * which implements the {@code Rng} interface.  Ideally the
     * generators in the stream will appear to be statistically
     * independent.  The new generators should be of the same kind
     * as this generator.
     *
     * @implNote It is permitted to implement this method in a manner
     * equivalent to {@code rngs(Long.MAX_VALUE)}.
     *
     * @return a stream of objects that implement the {@code Rng} interface
     */
    Stream<Rng> rngs();

    /**
     * Returns an effectively unlimited stream of objects, each of
     * which implements the {@code Rng} interface.  Ideally the
     * generators in the stream will appear to be statistically
     * independent.  The new generators should be of the same kind
     * as this generator.
     *
     * @implNote The default implementation calls {@code rngs()} and
     * then limits its length to {@code streamSize}.
     *
     * @param streamSize the number of generators to generate
     * @return a stream of objects that implement the {@code Rng} interface
     * @throws IllegalArgumentException if {@code streamSize} is
     *         less than zero
     */
    default Stream<Rng> rngs(long streamSize) {
	RngSupport.checkStreamSize(streamSize);
        return rngs().limit(streamSize);
    }
}