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 large number of
23 * steps of the output sequence in a single operation.
24 *
25 * @since 1.3
26 */
27 public interface JumpableUniformRandomProvider extends UniformRandomProvider {
28 /**
29 * Creates a copy of the UniformRandomProvider 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 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 jump for use in parallel computations.</p>
39 *
40 * @return A copy of the current state.
41 */
42 UniformRandomProvider jump();
43
44 /**
45 * Returns an effectively unlimited stream of new random generators, each of which
46 * implements the {@link UniformRandomProvider} interface.
47 *
48 * @return a stream of random generators.
49 * @since 1.5
50 */
51 default Stream<UniformRandomProvider> jumps() {
52 return Stream.generate(this::jump).sequential();
53 }
54
55 /**
56 * Returns a stream producing the given {@code streamSize} number of new random
57 * generators, each of which implements the {@link UniformRandomProvider}
58 * interface.
59 *
60 * @param streamSize Number of objects to generate.
61 * @return a stream of random generators; the stream is limited to the given
62 * {@code streamSize}.
63 * @throws IllegalArgumentException if {@code streamSize} is negative.
64 * @since 1.5
65 */
66 default Stream<UniformRandomProvider> jumps(long streamSize) {
67 UniformRandomProviderSupport.validateStreamSize(streamSize);
68 return jumps().limit(streamSize);
69 }
70 }