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.collections4.list;
018
019import java.io.IOException;
020import java.io.ObjectInputStream;
021import java.io.ObjectOutputStream;
022import java.util.Collection;
023import java.util.List;
024
025/**
026 * Serializable subclass of AbstractListDecorator.
027 *
028 * @since 3.1
029 */
030public abstract class AbstractSerializableListDecorator<E>
031        extends AbstractListDecorator<E> {
032
033    /** Serialization version */
034    private static final long serialVersionUID = 2684959196747496299L;
035
036    /**
037     * Constructor that wraps (not copies).
038     *
039     * @param list  the list to decorate, must not be null
040     * @throws NullPointerException if list is null
041     */
042    protected AbstractSerializableListDecorator(final List<E> list) {
043        super(list);
044    }
045
046    /**
047     * Read the list in using a custom routine.
048     *
049     * @param in  the input stream
050     * @throws IOException if an error occurs while reading from the stream
051     * @throws ClassNotFoundException if an object read from the stream can not be loaded
052     */
053    @SuppressWarnings("unchecked")
054    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
055        in.defaultReadObject();
056        setCollection((Collection<E>) in.readObject());
057    }
058
059    /**
060     * Write the list out using a custom routine.
061     *
062     * @param out  the output stream
063     * @throws IOException if an error occurs while writing to the stream
064     */
065    private void writeObject(final ObjectOutputStream out) throws IOException {
066        out.defaultWriteObject();
067        out.writeObject(decorated());
068    }
069
070}