SynchronizedRandomGenerator.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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.random;

  22. /**
  23.  * Any {@link RandomGenerator} implementation can be thread-safe if it
  24.  * is used through an instance of this class.
  25.  * This is achieved by enclosing calls to the methods of the actual
  26.  * generator inside the overridden {@code synchronized} methods of this
  27.  * class.
  28.  */
  29. public class SynchronizedRandomGenerator implements RandomGenerator {

  30.     /** Object to which all calls will be delegated. */
  31.     private final RandomGenerator wrapped;

  32.     /**
  33.      * Creates a synchronized wrapper for the given {@code RandomGenerator}
  34.      * instance.
  35.      *
  36.      * @param rng Generator whose methods will be called through
  37.      * their corresponding overridden synchronized version.
  38.      * To ensure thread-safety, the wrapped generator <em>must</em>
  39.      * not be used directly.
  40.      */
  41.     public SynchronizedRandomGenerator(RandomGenerator rng) {
  42.         wrapped = rng;
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public void setSeed(int seed) {
  47.         synchronized (wrapped) {
  48.             wrapped.setSeed(seed);
  49.         }
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public void setSeed(int[] seed) {
  54.         synchronized (wrapped) {
  55.             wrapped.setSeed(seed);
  56.         }
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public void setSeed(long seed) {
  61.         synchronized (wrapped) {
  62.             wrapped.setSeed(seed);
  63.         }
  64.     }

  65.     /** {@inheritDoc} */
  66.     @Override
  67.     public void nextBytes(byte[] bytes) {
  68.         synchronized (wrapped) {
  69.             wrapped.nextBytes(bytes);
  70.         }
  71.     }

  72.     /** {@inheritDoc} */
  73.     @Override
  74.     public void nextBytes(byte[] bytes, int offset, int len) {
  75.         synchronized (wrapped) {
  76.             wrapped.nextBytes(bytes, offset, len);
  77.         }
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public int nextInt() {
  82.         synchronized (wrapped) {
  83.             return wrapped.nextInt();
  84.         }
  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public int nextInt(int n) {
  89.         synchronized (wrapped) {
  90.             return wrapped.nextInt(n);
  91.         }
  92.     }

  93.     /** {@inheritDoc} */
  94.     @Override
  95.     public long nextLong() {
  96.         synchronized (wrapped) {
  97.             return wrapped.nextLong();
  98.         }
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public long nextLong(long n) {
  103.         synchronized (wrapped) {
  104.             return wrapped.nextLong(n);
  105.         }
  106.     }

  107.     /** {@inheritDoc} */
  108.     @Override
  109.     public boolean nextBoolean() {
  110.         synchronized (wrapped) {
  111.             return wrapped.nextBoolean();
  112.         }
  113.     }

  114.     /** {@inheritDoc} */
  115.     @Override
  116.     public float nextFloat() {
  117.         synchronized (wrapped) {
  118.             return wrapped.nextFloat();
  119.         }
  120.     }

  121.     /** {@inheritDoc} */
  122.     @Override
  123.     public double nextDouble() {
  124.         synchronized (wrapped) {
  125.             return wrapped.nextDouble();
  126.         }
  127.     }

  128.     /** {@inheritDoc} */
  129.     @Override
  130.     public double nextGaussian() {
  131.         synchronized (wrapped) {
  132.             return wrapped.nextGaussian();
  133.         }
  134.     }

  135. }