001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.rng;
018
019import java.util.stream.Stream;
020
021/**
022 * Applies to generators that can be advanced a large number of
023 * steps of the output sequence in a single operation.
024 *
025 * @since 1.3
026 */
027public interface JumpableUniformRandomProvider extends UniformRandomProvider {
028    /**
029     * Creates a copy of the UniformRandomProvider and then advances the
030     * state of the current instance. The copy is returned.
031     *
032     * <p>The current state will be advanced in a single operation by the equivalent of a
033     * number of sequential calls to a method that updates the state of the provider. The
034     * size of the jump is implementation dependent.</p>
035     *
036     * <p>Repeat invocations of this method will create a series of generators
037     * that are uniformly spaced at intervals of the output sequence. Each generator provides
038     * non-overlapping output for the length of the jump for use in parallel computations.</p>
039     *
040     * @return A copy of the current state.
041     */
042    UniformRandomProvider jump();
043
044    /**
045     * Returns an effectively unlimited stream of new random generators, each of which
046     * implements the {@link UniformRandomProvider} interface.
047     *
048     * @return a stream of random generators.
049     * @since 1.5
050     */
051    default Stream<UniformRandomProvider> jumps() {
052        return Stream.generate(this::jump).sequential();
053    }
054
055    /**
056     * Returns a stream producing the given {@code streamSize} number of new random
057     * generators, each of which implements the {@link UniformRandomProvider}
058     * interface.
059     *
060     * @param streamSize Number of objects to generate.
061     * @return a stream of random generators; the stream is limited to the given
062     * {@code streamSize}.
063     * @throws IllegalArgumentException if {@code streamSize} is negative.
064     * @since 1.5
065     */
066    default Stream<UniformRandomProvider> jumps(long streamSize) {
067        UniformRandomProviderSupport.validateStreamSize(streamSize);
068        return jumps().limit(streamSize);
069    }
070}