001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.common.annotations.Beta;
020import com.google.common.annotations.GwtCompatible;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import java.util.Collection;
023import java.util.Iterator;
024import java.util.List;
025import java.util.ListIterator;
026import javax.annotation.CheckForNull;
027import org.checkerframework.checker.nullness.qual.Nullable;
028
029/**
030 * A list which forwards all its method calls to another list. Subclasses should override one or
031 * more methods to modify the behavior of the backing list as desired per the <a
032 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
033 *
034 * <p>This class does not implement {@link java.util.RandomAccess}. If the delegate supports random
035 * access, the {@code ForwardingList} subclass should implement the {@code RandomAccess} interface.
036 *
037 * <p><b>Warning:</b> The methods of {@code ForwardingList} forward <b>indiscriminately</b> to the
038 * methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change the
039 * behavior of {@link #addAll}, which can lead to unexpected behavior. In this case, you should
040 * override {@code addAll} as well, either providing your own implementation, or delegating to the
041 * provided {@code standardAddAll} method.
042 *
043 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
044 * default} methods. Instead, it inherits their default implementations. When those implementations
045 * invoke methods, they invoke methods on the {@code ForwardingList}.
046 *
047 * <p>The {@code standard} methods and any collection views they return are not guaranteed to be
048 * thread-safe, even when all of the methods that they depend on are thread-safe.
049 *
050 * @author Mike Bostock
051 * @author Louis Wasserman
052 * @since 2.0
053 */
054@GwtCompatible
055@ElementTypesAreNonnullByDefault
056public abstract class ForwardingList<E extends @Nullable Object> extends ForwardingCollection<E>
057    implements List<E> {
058  // TODO(lowasser): identify places where thread safety is actually lost
059
060  /** Constructor for use by subclasses. */
061  protected ForwardingList() {}
062
063  @Override
064  protected abstract List<E> delegate();
065
066  @Override
067  public void add(int index, @ParametricNullness E element) {
068    delegate().add(index, element);
069  }
070
071  @CanIgnoreReturnValue
072  @Override
073  public boolean addAll(int index, Collection<? extends E> elements) {
074    return delegate().addAll(index, elements);
075  }
076
077  @Override
078  @ParametricNullness
079  public E get(int index) {
080    return delegate().get(index);
081  }
082
083  @Override
084  public int indexOf(@CheckForNull Object element) {
085    return delegate().indexOf(element);
086  }
087
088  @Override
089  public int lastIndexOf(@CheckForNull Object element) {
090    return delegate().lastIndexOf(element);
091  }
092
093  @Override
094  public ListIterator<E> listIterator() {
095    return delegate().listIterator();
096  }
097
098  @Override
099  public ListIterator<E> listIterator(int index) {
100    return delegate().listIterator(index);
101  }
102
103  @CanIgnoreReturnValue
104  @Override
105  @ParametricNullness
106  public E remove(int index) {
107    return delegate().remove(index);
108  }
109
110  @CanIgnoreReturnValue
111  @Override
112  @ParametricNullness
113  public E set(int index, @ParametricNullness E element) {
114    return delegate().set(index, element);
115  }
116
117  @Override
118  public List<E> subList(int fromIndex, int toIndex) {
119    return delegate().subList(fromIndex, toIndex);
120  }
121
122  @Override
123  public boolean equals(@CheckForNull Object object) {
124    return object == this || delegate().equals(object);
125  }
126
127  @Override
128  public int hashCode() {
129    return delegate().hashCode();
130  }
131
132  /**
133   * A sensible default implementation of {@link #add(Object)}, in terms of {@link #add(int,
134   * Object)}. If you override {@link #add(int, Object)}, you may wish to override {@link
135   * #add(Object)} to forward to this implementation.
136   *
137   * @since 7.0
138   */
139  protected boolean standardAdd(@ParametricNullness E element) {
140    add(size(), element);
141    return true;
142  }
143
144  /**
145   * A sensible default implementation of {@link #addAll(int, Collection)}, in terms of the {@code
146   * add} method of {@link #listIterator(int)}. If you override {@link #listIterator(int)}, you may
147   * wish to override {@link #addAll(int, Collection)} to forward to this implementation.
148   *
149   * @since 7.0
150   */
151  protected boolean standardAddAll(int index, Iterable<? extends E> elements) {
152    return Lists.addAllImpl(this, index, elements);
153  }
154
155  /**
156   * A sensible default implementation of {@link #indexOf}, in terms of {@link #listIterator()}. If
157   * you override {@link #listIterator()}, you may wish to override {@link #indexOf} to forward to
158   * this implementation.
159   *
160   * @since 7.0
161   */
162  protected int standardIndexOf(@CheckForNull Object element) {
163    return Lists.indexOfImpl(this, element);
164  }
165
166  /**
167   * A sensible default implementation of {@link #lastIndexOf}, in terms of {@link
168   * #listIterator(int)}. If you override {@link #listIterator(int)}, you may wish to override
169   * {@link #lastIndexOf} to forward to this implementation.
170   *
171   * @since 7.0
172   */
173  protected int standardLastIndexOf(@CheckForNull Object element) {
174    return Lists.lastIndexOfImpl(this, element);
175  }
176
177  /**
178   * A sensible default implementation of {@link #iterator}, in terms of {@link #listIterator()}. If
179   * you override {@link #listIterator()}, you may wish to override {@link #iterator} to forward to
180   * this implementation.
181   *
182   * @since 7.0
183   */
184  protected Iterator<E> standardIterator() {
185    return listIterator();
186  }
187
188  /**
189   * A sensible default implementation of {@link #listIterator()}, in terms of {@link
190   * #listIterator(int)}. If you override {@link #listIterator(int)}, you may wish to override
191   * {@link #listIterator()} to forward to this implementation.
192   *
193   * @since 7.0
194   */
195  protected ListIterator<E> standardListIterator() {
196    return listIterator(0);
197  }
198
199  /**
200   * A sensible default implementation of {@link #listIterator(int)}, in terms of {@link #size},
201   * {@link #get(int)}, {@link #set(int, Object)}, {@link #add(int, Object)}, and {@link
202   * #remove(int)}. If you override any of these methods, you may wish to override {@link
203   * #listIterator(int)} to forward to this implementation.
204   *
205   * @since 7.0
206   */
207  @Beta
208  protected ListIterator<E> standardListIterator(int start) {
209    return Lists.listIteratorImpl(this, start);
210  }
211
212  /**
213   * A sensible default implementation of {@link #subList(int, int)}. If you override any other
214   * methods, you may wish to override {@link #subList(int, int)} to forward to this implementation.
215   *
216   * @since 7.0
217   */
218  @Beta
219  protected List<E> standardSubList(int fromIndex, int toIndex) {
220    return Lists.subListImpl(this, fromIndex, toIndex);
221  }
222
223  /**
224   * A sensible definition of {@link #equals(Object)} in terms of {@link #size} and {@link
225   * #iterator}. If you override either of those methods, you may wish to override {@link
226   * #equals(Object)} to forward to this implementation.
227   *
228   * @since 7.0
229   */
230  @Beta
231  protected boolean standardEquals(@CheckForNull Object object) {
232    return Lists.equalsImpl(this, object);
233  }
234
235  /**
236   * A sensible definition of {@link #hashCode} in terms of {@link #iterator}. If you override
237   * {@link #iterator}, you may wish to override {@link #hashCode} to forward to this
238   * implementation.
239   *
240   * @since 7.0
241   */
242  @Beta
243  protected int standardHashCode() {
244    return Lists.hashCodeImpl(this);
245  }
246}