LongJumpableUniformRandomProvider.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 very large number of
  21.  * steps of the output sequence in a single operation.
  22.  *
  23.  * @since 1.3
  24.  */
  25. public interface LongJumpableUniformRandomProvider extends JumpableUniformRandomProvider {
  26.     /**
  27.      * Creates a copy of the JumpableUniformRandomProvider 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 long 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 long jump for use in parallel computations.</p>
  37.      *
  38.      * <p>The returned copy may be jumped {@code m / n} times before overlap with the current
  39.      * instance where {@code m} is the long jump length and {@code n}
  40.      * is the jump length of the {@link #jump()} method.
  41.      *
  42.      * @return A copy of the current state.
  43.      */
  44.     JumpableUniformRandomProvider longJump();

  45.     /**
  46.      * Returns an effectively unlimited stream of new random generators, each of which
  47.      * implements the {@link JumpableUniformRandomProvider} interface.
  48.      *
  49.      * @return a stream of random generators.
  50.      * @since 1.5
  51.      */
  52.     default Stream<JumpableUniformRandomProvider> longJumps() {
  53.         return Stream.generate(this::longJump).sequential();
  54.     }

  55.     /**
  56.      * Returns a stream producing the given {@code streamSize} number of new random
  57.      * generators, each of which implements the {@link JumpableUniformRandomProvider}
  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<JumpableUniformRandomProvider> longJumps(long streamSize) {
  67.         UniformRandomProviderSupport.validateStreamSize(streamSize);
  68.         return longJumps().limit(streamSize);
  69.     }
  70. }