View Javadoc
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    *      http://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  package org.apache.commons.rng.core.source32;
18  
19  import java.util.Random;
20  import java.io.IOException;
21  import java.io.ObjectOutputStream;
22  import java.io.ObjectInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.ByteArrayInputStream;
25  
26  /**
27   * A provider that uses the {@link Random#nextInt()} method of the JDK's
28   * {@link Random} class as the source of randomness.
29   *
30   * <p>
31   * <b>Caveat:</b> All the other calls will be redirected to the methods
32   * implemented within this library.
33   * </p>
34   *
35   * <p>
36   * The state of this source of randomness is saved and restored through
37   * the serialization of the {@link Random} instance.
38   * </p>
39   *
40   * @since 1.0
41   */
42  public class JDKRandom extends IntProvider {
43      /** Delegate.  Cannot be "final" (to allow serialization). */
44      private Random delegate;
45  
46      /**
47       * Creates an instance with the given seed.
48       *
49       * @param seed Initial seed.
50       */
51      public JDKRandom(Long seed) {
52          delegate = new Random(seed);
53      }
54  
55      /**
56       * {@inheritDoc}
57       *
58       * @see Random#nextInt()
59       */
60      @Override
61      public int next() {
62          return delegate.nextInt();
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      protected byte[] getStateInternal() {
68          try {
69              final ByteArrayOutputStream bos = new ByteArrayOutputStream();
70              final ObjectOutputStream oos = new ObjectOutputStream(bos);
71  
72              // Serialize the "delegate".
73              oos.writeObject(delegate);
74  
75              return bos.toByteArray();
76          } catch (IOException e) {
77              // Workaround checked exception.
78              throw new IllegalStateException(e);
79          }
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      protected void setStateInternal(byte[] s) {
85          try {
86              final ByteArrayInputStream bis = new ByteArrayInputStream(s);
87              final ObjectInputStream ois = new ObjectInputStream(bis);
88  
89              delegate = (Random) ois.readObject();
90          } catch (ClassNotFoundException e) {
91              // Workaround checked exception.
92              throw new IllegalStateException(e);
93          } catch (IOException e) {
94              // Workaround checked exception.
95              throw new IllegalStateException(e);
96          }
97      }
98  }