AbstractSerializableListDecorator.java

  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.collections4.list;

  18. import java.io.IOException;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutputStream;
  21. import java.util.Collection;
  22. import java.util.List;

  23. /**
  24.  * Serializable subclass of AbstractListDecorator.
  25.  *
  26.  * @param <E> the type of the elements in the list.
  27.  * @since 3.1
  28.  */
  29. public abstract class AbstractSerializableListDecorator<E> extends AbstractListDecorator<E> {

  30.     /** Serialization version */
  31.     private static final long serialVersionUID = 2684959196747496299L;

  32.     /**
  33.      * Constructor that wraps (not copies).
  34.      *
  35.      * @param list  the list to decorate, must not be null
  36.      * @throws NullPointerException if list is null
  37.      */
  38.     protected AbstractSerializableListDecorator(final List<E> list) {
  39.         super(list);
  40.     }

  41.     /**
  42.      * Deserializes the list in using a custom routine.
  43.      *
  44.      * @param in  the input stream
  45.      * @throws IOException if an error occurs while reading from the stream
  46.      * @throws ClassNotFoundException if an object read from the stream cannot be loaded
  47.      */
  48.     @SuppressWarnings("unchecked")
  49.     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
  50.         in.defaultReadObject();
  51.         setCollection((Collection<E>) in.readObject());
  52.     }

  53.     /**
  54.      * Serializes this object to an ObjectOutputStream.
  55.      *
  56.      * @param out the target ObjectOutputStream.
  57.      * @throws IOException thrown when an I/O errors occur writing to the target stream.
  58.      */
  59.     private void writeObject(final ObjectOutputStream out) throws IOException {
  60.         out.defaultWriteObject();
  61.         out.writeObject(decorated());
  62.     }

  63. }