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.rng.core.source32;
018
019import java.util.Random;
020import java.io.IOException;
021import java.io.ObjectOutputStream;
022import java.io.ObjectInputStream;
023import java.io.ByteArrayOutputStream;
024import java.io.ByteArrayInputStream;
025
026/**
027 * A provider that uses the {@link Random#nextInt()} method of the JDK's
028 * {@link Random} class as the source of randomness.
029 *
030 * <p>
031 * <b>Caveat:</b> All the other calls will be redirected to the methods
032 * implemented within this library.
033 * </p>
034 *
035 * <p>
036 * The state of this source of randomness is saved and restored through
037 * the serialization of the {@link Random} instance.
038 * </p>
039 *
040 * @since 1.0
041 */
042public class JDKRandom extends IntProvider {
043    /** Delegate.  Cannot be "final" (to allow serialization). */
044    private Random delegate;
045
046    /**
047     * Creates an instance with the given seed.
048     *
049     * @param seed Initial seed.
050     */
051    public JDKRandom(Long seed) {
052        delegate = new Random(seed);
053    }
054
055    /**
056     * {@inheritDoc}
057     *
058     * @see Random#nextInt()
059     */
060    @Override
061    public int next() {
062        return delegate.nextInt();
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    protected byte[] getStateInternal() {
068        try {
069            final ByteArrayOutputStream bos = new ByteArrayOutputStream();
070            final ObjectOutputStream oos = new ObjectOutputStream(bos);
071
072            // Serialize the "delegate".
073            oos.writeObject(delegate);
074
075            return bos.toByteArray();
076        } catch (IOException e) {
077            // Workaround checked exception.
078            throw new RuntimeException(e);
079        }
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    protected void setStateInternal(byte[] s) {
085        try {
086            final ByteArrayInputStream bis = new ByteArrayInputStream(s);
087            final ObjectInputStream ois = new ObjectInputStream(bis);
088
089            delegate = (Random) ois.readObject();
090        } catch (ClassNotFoundException e) {
091            // Workaround checked exception.
092            throw new RuntimeException(e);
093        } catch (IOException e) {
094            // Workaround checked exception.
095            throw new RuntimeException(e);
096        }
097    }
098}