001/*
002 * Licensed to the author 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 de.cuioss.test.generator.internal.net.java.quickcheck.generator.support;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.io.IOException;
022import java.io.ObjectInputStream;
023import java.io.ObjectOutputStream;
024
025import de.cuioss.test.generator.internal.net.java.quickcheck.Generator;
026
027/**
028 * A cloning generator which uses object serialization to create clones of the
029 * prototype object. For each call of {@link CloningGenerator#next()} a new copy
030 * of the prototype will be generated.
031 *
032 * @param <T>
033 *            Type of the prototype object
034 *
035 */
036public class CloningGenerator<T> implements Generator<T> {
037
038    private final T prototype;
039
040    public CloningGenerator(T prototype) {
041        this.prototype = prototype;
042    }
043
044    /**
045     * Generate a new instance of the prototype object.
046     */
047    @Override
048    public T next() {
049        try {
050            return cloneObject();
051        } catch (IOException e) {
052            throw new IllegalArgumentException("prototype " + prototype
053                    + " not serializable.", e);
054        } catch (ClassNotFoundException e) {
055            throw new RuntimeException("this should not happen "
056                    + e.getMessage(), e);
057        }
058    }
059
060    private T cloneObject() throws IOException, ClassNotFoundException {
061        ByteArrayOutputStream bytesStream = new ByteArrayOutputStream();
062        ObjectOutputStream objectOutputStream = new ObjectOutputStream(
063                bytesStream);
064        objectOutputStream.writeObject(prototype);
065        objectOutputStream.flush();
066        objectOutputStream.close();
067        ObjectInputStream objectInputStream = new ObjectInputStream(
068                new ByteArrayInputStream(bytesStream.toByteArray()));
069        return castObjectToT(objectInputStream);
070    }
071
072    @SuppressWarnings("unchecked")
073    private T castObjectToT(ObjectInputStream objectInputStream)
074        throws IOException, ClassNotFoundException {
075        return (T) objectInputStream.readObject();
076    }
077
078}