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     */
017    package 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     * @version $Id: SynchronizedRandomGenerator.java 1416643 2012-12-03 19:37:14Z tn $
028     */
029    public class SynchronizedRandomGenerator implements RandomGenerator {
030        /** Object to which all calls will be delegated. */
031        private final RandomGenerator wrapped;
032    
033        /**
034         * Creates a synchronized wrapper for the given {@code RandomGenerator}
035         * instance.
036         *
037         * @param rng Generator whose methods will be called through
038         * their corresponding overridden synchronized version.
039         * To ensure thread-safety, the wrapped generator <em>must</em>
040         * not be used directly.
041         */
042        public SynchronizedRandomGenerator(RandomGenerator rng) {
043            wrapped = rng;
044        }
045    
046        /**
047         * {@inheritDoc}
048         */
049        public synchronized void setSeed(int seed) {
050            wrapped.setSeed(seed);
051        }
052    
053        /**
054         * {@inheritDoc}
055         */
056        public synchronized void setSeed(int[] seed) {
057            wrapped.setSeed(seed);
058        }
059    
060        /**
061         * {@inheritDoc}
062         */
063        public synchronized void setSeed(long seed) {
064            wrapped.setSeed(seed);
065        }
066    
067        /**
068         * {@inheritDoc}
069         */
070        public synchronized void nextBytes(byte[] bytes) {
071            wrapped.nextBytes(bytes);
072        }
073    
074        /**
075         * {@inheritDoc}
076         */
077        public synchronized int nextInt() {
078            return wrapped.nextInt();
079        }
080    
081        /**
082         * {@inheritDoc}
083         */
084        public synchronized int nextInt(int n) {
085            return wrapped.nextInt(n);
086        }
087    
088        /**
089         * {@inheritDoc}
090         */
091        public synchronized long nextLong() {
092            return wrapped.nextLong();
093        }
094    
095        /**
096         * {@inheritDoc}
097         */
098        public synchronized boolean nextBoolean() {
099            return wrapped.nextBoolean();
100        }
101    
102        /**
103         * {@inheritDoc}
104         */
105        public synchronized float nextFloat() {
106            return wrapped.nextFloat();
107        }
108    
109        /**
110         * {@inheritDoc}
111         */
112        public synchronized double nextDouble() {
113            return wrapped.nextDouble();
114        }
115    
116        /**
117         * {@inheritDoc}
118         */
119        public synchronized double nextGaussian() {
120            return wrapped.nextGaussian();
121        }
122    }