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 very large number of
023 * steps of the output sequence in a single operation.
024 *
025 * @since 1.3
026 */
027public interface LongJumpableUniformRandomProvider extends JumpableUniformRandomProvider {
028    /**
029     * Creates a copy of the JumpableUniformRandomProvider 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 long 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 long jump for use in parallel computations.</p>
039     *
040     * <p>The returned copy may be jumped {@code m / n} times before overlap with the current
041     * instance where {@code m} is the long jump length and {@code n}
042     * is the jump length of the {@link #jump()} method.
043     *
044     * @return A copy of the current state.
045     */
046    JumpableUniformRandomProvider longJump();
047
048    /**
049     * Returns an effectively unlimited stream of new random generators, each of which
050     * implements the {@link JumpableUniformRandomProvider} interface.
051     *
052     * @return a stream of random generators.
053     * @since 1.5
054     */
055    default Stream<JumpableUniformRandomProvider> longJumps() {
056        return Stream.generate(this::longJump).sequential();
057    }
058
059    /**
060     * Returns a stream producing the given {@code streamSize} number of new random
061     * generators, each of which implements the {@link JumpableUniformRandomProvider}
062     * interface.
063     *
064     * @param streamSize Number of objects to generate.
065     * @return a stream of random generators; the stream is limited to the given
066     * {@code streamSize}.
067     * @throws IllegalArgumentException if {@code streamSize} is negative.
068     * @since 1.5
069     */
070    default Stream<JumpableUniformRandomProvider> longJumps(long streamSize) {
071        UniformRandomProviderSupport.validateStreamSize(streamSize);
072        return longJumps().limit(streamSize);
073    }
074}