JumpableUniformRandomProvider.java

  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. import java.util.stream.Stream;

  19. /**
  20.  * Applies to generators that can be advanced a large number of
  21.  * steps of the output sequence in a single operation.
  22.  *
  23.  * @since 1.3
  24.  */
  25. public interface JumpableUniformRandomProvider extends UniformRandomProvider {
  26.     /**
  27.      * Creates a copy of the UniformRandomProvider and then advances the
  28.      * state of the current instance. The copy is returned.
  29.      *
  30.      * <p>The current state will be advanced in a single operation by the equivalent of a
  31.      * number of sequential calls to a method that updates the state of the provider. The
  32.      * size of the jump is implementation dependent.</p>
  33.      *
  34.      * <p>Repeat invocations of this method will create a series of generators
  35.      * that are uniformly spaced at intervals of the output sequence. Each generator provides
  36.      * non-overlapping output for the length of the jump for use in parallel computations.</p>
  37.      *
  38.      * @return A copy of the current state.
  39.      */
  40.     UniformRandomProvider jump();

  41.     /**
  42.      * Returns an effectively unlimited stream of new random generators, each of which
  43.      * implements the {@link UniformRandomProvider} interface.
  44.      *
  45.      * @return a stream of random generators.
  46.      * @since 1.5
  47.      */
  48.     default Stream<UniformRandomProvider> jumps() {
  49.         return Stream.generate(this::jump).sequential();
  50.     }

  51.     /**
  52.      * Returns a stream producing the given {@code streamSize} number of new random
  53.      * generators, each of which implements the {@link UniformRandomProvider}
  54.      * interface.
  55.      *
  56.      * @param streamSize Number of objects to generate.
  57.      * @return a stream of random generators; the stream is limited to the given
  58.      * {@code streamSize}.
  59.      * @throws IllegalArgumentException if {@code streamSize} is negative.
  60.      * @since 1.5
  61.      */
  62.     default Stream<UniformRandomProvider> jumps(long streamSize) {
  63.         UniformRandomProviderSupport.validateStreamSize(streamSize);
  64.         return jumps().limit(streamSize);
  65.     }
  66. }