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