001/*
002 * Java Genetic Algorithm Library (jenetics-8.3.0).
003 * Copyright (c) 2007-2025 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * 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 * Author:
018 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020package io.jenetics.util;
021
022import static java.util.Objects.requireNonNull;
023
024import java.io.Serial;
025import java.util.Random;
026import java.util.random.RandomGenerator;
027import java.util.stream.DoubleStream;
028import java.util.stream.IntStream;
029import java.util.stream.LongStream;
030
031/**
032 * Some places in the Java API still require a {@link Random} object instead of
033 * the new {@link RandomGenerator}. This class can be used by using this adapter
034 * class.
035 * {@snippet lang="java":
036 * final var random = RandomGenerator.getDefault();
037 * final var bi = new BigInteger(100, RandomAdapter.of(random));
038 * }
039 *
040 * @deprecated Will be removed in the next major version. Use the
041 *            {@link Random#from(RandomGenerator)} method instead.
042 *
043 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
044 * @since 7.0
045 * @version 7.0
046 */
047@Deprecated(since = "8.3", forRemoval = true)
048public final class RandomAdapter extends Random {
049
050        @Serial
051        private static final long serialVersionUID = 1;
052
053        private final RandomGenerator _random;
054
055        private RandomAdapter(final RandomGenerator random) {
056                _random = requireNonNull(random);
057        }
058
059        @Override
060        public boolean isDeprecated() {
061                return _random.isDeprecated();
062        }
063
064        @Override
065        public DoubleStream doubles() {
066                return _random.doubles();
067        }
068
069        @Override
070        public DoubleStream doubles(double randomNumberOrigin, double randomNumberBound) {
071                return _random.doubles(randomNumberOrigin, randomNumberBound);
072        }
073
074        @Override
075        public DoubleStream doubles(long streamSize) {
076                return _random.doubles(streamSize);
077        }
078
079        @Override
080        public DoubleStream doubles(long streamSize, double randomNumberOrigin, double randomNumberBound) {
081                return _random.doubles(streamSize, randomNumberOrigin, randomNumberBound);
082        }
083
084        @Override
085        public IntStream ints() {
086                return _random.ints();
087        }
088
089        @Override
090        public IntStream ints(int randomNumberOrigin, int randomNumberBound) {
091                return _random.ints(randomNumberOrigin, randomNumberBound);
092        }
093
094        @Override
095        public IntStream ints(long streamSize) {
096                return _random.ints(streamSize);
097        }
098
099        @Override
100        public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound) {
101                return _random.ints(streamSize, randomNumberOrigin, randomNumberBound);
102        }
103
104        @Override
105        public LongStream longs() {
106                return _random.longs();
107        }
108
109        @Override
110        public LongStream longs(long randomNumberOrigin, long randomNumberBound) {
111                return _random.longs(randomNumberOrigin, randomNumberBound);
112        }
113
114        @Override
115        public LongStream longs(long streamSize) {
116                return _random.longs(streamSize);
117        }
118
119        @Override
120        public LongStream longs(long streamSize, long randomNumberOrigin, long randomNumberBound) {
121                return _random.longs(streamSize, randomNumberOrigin, randomNumberBound);
122        }
123
124        @Override
125        public boolean nextBoolean() {
126                return _random.nextBoolean();
127        }
128
129        @Override
130        public void nextBytes(byte[] bytes) {
131                _random.nextBytes(bytes);
132        }
133
134        @Override
135        public float nextFloat() {
136                return _random.nextFloat();
137        }
138
139        @Override
140        public float nextFloat(float bound) {
141                return _random.nextFloat(bound);
142        }
143
144        @Override
145        public float nextFloat(float origin, float bound) {
146                return _random.nextFloat(origin, bound);
147        }
148
149        @Override
150        public double nextDouble() {
151                return _random.nextDouble();
152        }
153
154        @Override
155        public double nextDouble(double bound) {
156                return _random.nextDouble(bound);
157        }
158
159        @Override
160        public double nextDouble(double origin, double bound) {
161                return _random.nextDouble(origin, bound);
162        }
163
164        @Override
165        public int nextInt() {
166                return _random.nextInt();
167        }
168
169        @Override
170        public int nextInt(int bound) {
171                return _random.nextInt(bound);
172        }
173
174        @Override
175        public int nextInt(int origin, int bound) {
176                return _random.nextInt(origin, bound);
177        }
178
179        @Override
180        public long nextLong() {
181                return _random.nextLong();
182        }
183
184        @Override
185        public long nextLong(long bound) {
186                return _random.nextLong(bound);
187        }
188
189        @Override
190        public long nextLong(long origin, long bound) {
191                return _random.nextLong(origin, bound);
192        }
193
194        @Override
195        public double nextGaussian() {
196                return _random.nextGaussian();
197        }
198
199        @Override
200        public double nextGaussian(double mean, double stddev) {
201                return _random.nextGaussian(mean, stddev);
202        }
203
204        @Override
205        public double nextExponential() {
206                return _random.nextExponential();
207        }
208
209        /**
210         * Create a new {@link Random} object from the given {@code random} generator.
211         *
212         * @param random the random generator to adapt
213         * @return the adapted random generator
214         */
215        public static Random of(final RandomGenerator random) {
216                return new RandomAdapter(random);
217        }
218}