001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.random;
018
019/**
020 * Any {@link RandomGenerator} implementation can be thread-safe if it
021 * is used through an instance of this class.
022 * This is achieved by enclosing calls to the methods of the actual
023 * generator inside the overridden {@code synchronized} methods of this
024 * class.
025 *
026 * @since 3.1
027 */
028public class SynchronizedRandomGenerator implements RandomGenerator {
029    /** Object to which all calls will be delegated. */
030    private final RandomGenerator wrapped;
031
032    /**
033     * Creates a synchronized wrapper for the given {@code RandomGenerator}
034     * instance.
035     *
036     * @param rng Generator whose methods will be called through
037     * their corresponding overridden synchronized version.
038     * To ensure thread-safety, the wrapped generator <em>must</em>
039     * not be used directly.
040     */
041    public SynchronizedRandomGenerator(RandomGenerator rng) {
042        wrapped = rng;
043    }
044
045    /**
046     * {@inheritDoc}
047     */
048    public synchronized void setSeed(int seed) {
049        wrapped.setSeed(seed);
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    public synchronized void setSeed(int[] seed) {
056        wrapped.setSeed(seed);
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    public synchronized void setSeed(long seed) {
063        wrapped.setSeed(seed);
064    }
065
066    /**
067     * {@inheritDoc}
068     */
069    public synchronized void nextBytes(byte[] bytes) {
070        wrapped.nextBytes(bytes);
071    }
072
073    /**
074     * {@inheritDoc}
075     */
076    public synchronized int nextInt() {
077        return wrapped.nextInt();
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    public synchronized int nextInt(int n) {
084        return wrapped.nextInt(n);
085    }
086
087    /**
088     * {@inheritDoc}
089     */
090    public synchronized long nextLong() {
091        return wrapped.nextLong();
092    }
093
094    /**
095     * {@inheritDoc}
096     */
097    public synchronized boolean nextBoolean() {
098        return wrapped.nextBoolean();
099    }
100
101    /**
102     * {@inheritDoc}
103     */
104    public synchronized float nextFloat() {
105        return wrapped.nextFloat();
106    }
107
108    /**
109     * {@inheritDoc}
110     */
111    public synchronized double nextDouble() {
112        return wrapped.nextDouble();
113    }
114
115    /**
116     * {@inheritDoc}
117     */
118    public synchronized double nextGaussian() {
119        return wrapped.nextGaussian();
120    }
121}