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 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
21 */
22 package org.hipparchus.random;
23
24 /**
25 * Any {@link RandomGenerator} implementation can be thread-safe if it
26 * is used through an instance of this class.
27 * This is achieved by enclosing calls to the methods of the actual
28 * generator inside the overridden {@code synchronized} methods of this
29 * class.
30 */
31 public class SynchronizedRandomGenerator implements RandomGenerator {
32
33 /** Object to which all calls will be delegated. */
34 private final RandomGenerator wrapped;
35
36 /**
37 * Creates a synchronized wrapper for the given {@code RandomGenerator}
38 * instance.
39 *
40 * @param rng Generator whose methods will be called through
41 * their corresponding overridden synchronized version.
42 * To ensure thread-safety, the wrapped generator <em>must</em>
43 * not be used directly.
44 */
45 public SynchronizedRandomGenerator(RandomGenerator rng) {
46 wrapped = rng;
47 }
48
49 /** {@inheritDoc} */
50 @Override
51 public void setSeed(int seed) {
52 synchronized (wrapped) {
53 wrapped.setSeed(seed);
54 }
55 }
56
57 /** {@inheritDoc} */
58 @Override
59 public void setSeed(int[] seed) {
60 synchronized (wrapped) {
61 wrapped.setSeed(seed);
62 }
63 }
64
65 /** {@inheritDoc} */
66 @Override
67 public void setSeed(long seed) {
68 synchronized (wrapped) {
69 wrapped.setSeed(seed);
70 }
71 }
72
73 /** {@inheritDoc} */
74 @Override
75 public void nextBytes(byte[] bytes) {
76 synchronized (wrapped) {
77 wrapped.nextBytes(bytes);
78 }
79 }
80
81 /** {@inheritDoc} */
82 @Override
83 public void nextBytes(byte[] bytes, int offset, int len) {
84 synchronized (wrapped) {
85 wrapped.nextBytes(bytes, offset, len);
86 }
87 }
88
89 /** {@inheritDoc} */
90 @Override
91 public int nextInt() {
92 synchronized (wrapped) {
93 return wrapped.nextInt();
94 }
95 }
96
97 /** {@inheritDoc} */
98 @Override
99 public int nextInt(int n) {
100 synchronized (wrapped) {
101 return wrapped.nextInt(n);
102 }
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public long nextLong() {
108 synchronized (wrapped) {
109 return wrapped.nextLong();
110 }
111 }
112
113 /** {@inheritDoc} */
114 @Override
115 public long nextLong(long n) {
116 synchronized (wrapped) {
117 return wrapped.nextLong(n);
118 }
119 }
120
121 /** {@inheritDoc} */
122 @Override
123 public boolean nextBoolean() {
124 synchronized (wrapped) {
125 return wrapped.nextBoolean();
126 }
127 }
128
129 /** {@inheritDoc} */
130 @Override
131 public float nextFloat() {
132 synchronized (wrapped) {
133 return wrapped.nextFloat();
134 }
135 }
136
137 /** {@inheritDoc} */
138 @Override
139 public double nextDouble() {
140 synchronized (wrapped) {
141 return wrapped.nextDouble();
142 }
143 }
144
145 /** {@inheritDoc} */
146 @Override
147 public double nextGaussian() {
148 synchronized (wrapped) {
149 return wrapped.nextGaussian();
150 }
151 }
152
153 }