1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.rng; 18 19 import java.util.stream.Stream; 20 21 /** 22 * Applies to generators that can be advanced a very large number of 23 * steps of the output sequence in a single operation. 24 * 25 * @since 1.3 26 */ 27 public interface LongJumpableUniformRandomProvider extends JumpableUniformRandomProvider { 28 /** 29 * Creates a copy of the JumpableUniformRandomProvider and then advances the 30 * state of the current instance. The copy is returned. 31 * 32 * <p>The current state will be advanced in a single operation by the equivalent of a 33 * number of sequential calls to a method that updates the state of the provider. The 34 * size of the long jump is implementation dependent.</p> 35 * 36 * <p>Repeat invocations of this method will create a series of generators 37 * that are uniformly spaced at intervals of the output sequence. Each generator provides 38 * non-overlapping output for the length of the long jump for use in parallel computations.</p> 39 * 40 * <p>The returned copy may be jumped {@code m / n} times before overlap with the current 41 * instance where {@code m} is the long jump length and {@code n} 42 * is the jump length of the {@link #jump()} method. 43 * 44 * @return A copy of the current state. 45 */ 46 JumpableUniformRandomProvider longJump(); 47 48 /** 49 * Returns an effectively unlimited stream of new random generators, each of which 50 * implements the {@link JumpableUniformRandomProvider} interface. 51 * 52 * @return a stream of random generators. 53 * @since 1.5 54 */ 55 default Stream<JumpableUniformRandomProvider> longJumps() { 56 return Stream.generate(this::longJump).sequential(); 57 } 58 59 /** 60 * Returns a stream producing the given {@code streamSize} number of new random 61 * generators, each of which implements the {@link JumpableUniformRandomProvider} 62 * interface. 63 * 64 * @param streamSize Number of objects to generate. 65 * @return a stream of random generators; the stream is limited to the given 66 * {@code streamSize}. 67 * @throws IllegalArgumentException if {@code streamSize} is negative. 68 * @since 1.5 69 */ 70 default Stream<JumpableUniformRandomProvider> longJumps(long streamSize) { 71 UniformRandomProviderSupport.validateStreamSize(streamSize); 72 return longJumps().limit(streamSize); 73 } 74 }