AbstractListDecorator.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.util.Collection;
  19. import java.util.List;
  20. import java.util.ListIterator;

  21. import org.apache.commons.collections4.collection.AbstractCollectionDecorator;

  22. /**
  23.  * Decorates another {@link List} to provide additional behavior.
  24.  * <p>
  25.  * Methods are forwarded directly to the decorated list.
  26.  * </p>
  27.  *
  28.  * @param <E> the type of the elements in the list.
  29.  * @since 3.0
  30.  */
  31. public abstract class AbstractListDecorator<E> extends AbstractCollectionDecorator<E> implements List<E> {

  32.     /** Serialization version--necessary in an abstract class? */
  33.     private static final long serialVersionUID = 4500739654952315623L;

  34.     /**
  35.      * Constructor only used in deserialization, do not use otherwise.
  36.      * @since 3.1
  37.      */
  38.     protected AbstractListDecorator() {
  39.     }

  40.     /**
  41.      * Constructor that wraps (not copies).
  42.      *
  43.      * @param list  the list to decorate, must not be null
  44.      * @throws NullPointerException if list is null
  45.      */
  46.     protected AbstractListDecorator(final List<E> list) {
  47.         super(list);
  48.     }

  49.     @Override
  50.     public void add(final int index, final E object) {
  51.         decorated().add(index, object);
  52.     }

  53.     @Override
  54.     public boolean addAll(final int index, final Collection<? extends E> coll) {
  55.         return decorated().addAll(index, coll);
  56.     }

  57.     /**
  58.      * Gets the list being decorated.
  59.      *
  60.      * @return the decorated list
  61.      */
  62.     @Override
  63.     protected List<E> decorated() {
  64.         return (List<E>) super.decorated();
  65.     }

  66.     @Override
  67.     public boolean equals(final Object object) {
  68.         return object == this || decorated().equals(object);
  69.     }

  70.     @Override
  71.     public E get(final int index) {
  72.         return decorated().get(index);
  73.     }

  74.     @Override
  75.     public int hashCode() {
  76.         return decorated().hashCode();
  77.     }

  78.     @Override
  79.     public int indexOf(final Object object) {
  80.         return decorated().indexOf(object);
  81.     }

  82.     @Override
  83.     public int lastIndexOf(final Object object) {
  84.         return decorated().lastIndexOf(object);
  85.     }

  86.     @Override
  87.     public ListIterator<E> listIterator() {
  88.         return decorated().listIterator();
  89.     }

  90.     @Override
  91.     public ListIterator<E> listIterator(final int index) {
  92.         return decorated().listIterator(index);
  93.     }

  94.     @Override
  95.     public E remove(final int index) {
  96.         return decorated().remove(index);
  97.     }

  98.     @Override
  99.     public E set(final int index, final E object) {
  100.         return decorated().set(index, object);
  101.     }

  102.     @Override
  103.     public List<E> subList(final int fromIndex, final int toIndex) {
  104.         return decorated().subList(fromIndex, toIndex);
  105.     }

  106. }