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