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