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 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
054public abstract class ForwardingList<E> extends ForwardingCollection<E> implements List<E> {
055  // TODO(lowasser): identify places where thread safety is actually lost
056
057  /** Constructor for use by subclasses. */
058  protected ForwardingList() {}
059
060  @Override
061  protected abstract List<E> delegate();
062
063  @Override
064  public void add(int index, E element) {
065    delegate().add(index, element);
066  }
067
068  @CanIgnoreReturnValue
069  @Override
070  public boolean addAll(int index, Collection<? extends E> elements) {
071    return delegate().addAll(index, elements);
072  }
073
074  @Override
075  public E get(int index) {
076    return delegate().get(index);
077  }
078
079  @Override
080  public int indexOf(Object element) {
081    return delegate().indexOf(element);
082  }
083
084  @Override
085  public int lastIndexOf(Object element) {
086    return delegate().lastIndexOf(element);
087  }
088
089  @Override
090  public ListIterator<E> listIterator() {
091    return delegate().listIterator();
092  }
093
094  @Override
095  public ListIterator<E> listIterator(int index) {
096    return delegate().listIterator(index);
097  }
098
099  @CanIgnoreReturnValue
100  @Override
101  public E remove(int index) {
102    return delegate().remove(index);
103  }
104
105  @CanIgnoreReturnValue
106  @Override
107  public E set(int index, E element) {
108    return delegate().set(index, element);
109  }
110
111  @Override
112  public List<E> subList(int fromIndex, int toIndex) {
113    return delegate().subList(fromIndex, toIndex);
114  }
115
116  @Override
117  public boolean equals(@Nullable Object object) {
118    return object == this || delegate().equals(object);
119  }
120
121  @Override
122  public int hashCode() {
123    return delegate().hashCode();
124  }
125
126  /**
127   * A sensible default implementation of {@link #add(Object)}, in terms of {@link #add(int,
128   * Object)}. If you override {@link #add(int, Object)}, you may wish to override {@link
129   * #add(Object)} to forward to this implementation.
130   *
131   * @since 7.0
132   */
133  protected boolean standardAdd(E element) {
134    add(size(), element);
135    return true;
136  }
137
138  /**
139   * A sensible default implementation of {@link #addAll(int, Collection)}, in terms of the {@code
140   * add} method of {@link #listIterator(int)}. If you override {@link #listIterator(int)}, you may
141   * wish to override {@link #addAll(int, Collection)} to forward to this implementation.
142   *
143   * @since 7.0
144   */
145  protected boolean standardAddAll(int index, Iterable<? extends E> elements) {
146    return Lists.addAllImpl(this, index, elements);
147  }
148
149  /**
150   * A sensible default implementation of {@link #indexOf}, in terms of {@link #listIterator()}. If
151   * you override {@link #listIterator()}, you may wish to override {@link #indexOf} to forward to
152   * this implementation.
153   *
154   * @since 7.0
155   */
156  protected int standardIndexOf(@Nullable Object element) {
157    return Lists.indexOfImpl(this, element);
158  }
159
160  /**
161   * A sensible default implementation of {@link #lastIndexOf}, in terms of {@link
162   * #listIterator(int)}. If you override {@link #listIterator(int)}, you may wish to override
163   * {@link #lastIndexOf} to forward to this implementation.
164   *
165   * @since 7.0
166   */
167  protected int standardLastIndexOf(@Nullable Object element) {
168    return Lists.lastIndexOfImpl(this, element);
169  }
170
171  /**
172   * A sensible default implementation of {@link #iterator}, in terms of {@link #listIterator()}. If
173   * you override {@link #listIterator()}, you may wish to override {@link #iterator} to forward to
174   * this implementation.
175   *
176   * @since 7.0
177   */
178  protected Iterator<E> standardIterator() {
179    return listIterator();
180  }
181
182  /**
183   * A sensible default implementation of {@link #listIterator()}, in terms of {@link
184   * #listIterator(int)}. If you override {@link #listIterator(int)}, you may wish to override
185   * {@link #listIterator()} to forward to this implementation.
186   *
187   * @since 7.0
188   */
189  protected ListIterator<E> standardListIterator() {
190    return listIterator(0);
191  }
192
193  /**
194   * A sensible default implementation of {@link #listIterator(int)}, in terms of {@link #size},
195   * {@link #get(int)}, {@link #set(int, Object)}, {@link #add(int, Object)}, and {@link
196   * #remove(int)}. If you override any of these methods, you may wish to override {@link
197   * #listIterator(int)} to forward to this implementation.
198   *
199   * @since 7.0
200   */
201  @Beta
202  protected ListIterator<E> standardListIterator(int start) {
203    return Lists.listIteratorImpl(this, start);
204  }
205
206  /**
207   * A sensible default implementation of {@link #subList(int, int)}. If you override any other
208   * methods, you may wish to override {@link #subList(int, int)} to forward to this implementation.
209   *
210   * @since 7.0
211   */
212  @Beta
213  protected List<E> standardSubList(int fromIndex, int toIndex) {
214    return Lists.subListImpl(this, fromIndex, toIndex);
215  }
216
217  /**
218   * A sensible definition of {@link #equals(Object)} in terms of {@link #size} and {@link
219   * #iterator}. If you override either of those methods, you may wish to override {@link
220   * #equals(Object)} to forward to this implementation.
221   *
222   * @since 7.0
223   */
224  @Beta
225  protected boolean standardEquals(@Nullable Object object) {
226    return Lists.equalsImpl(this, object);
227  }
228
229  /**
230   * A sensible definition of {@link #hashCode} in terms of {@link #iterator}. If you override
231   * {@link #iterator}, you may wish to override {@link #hashCode} to forward to this
232   * implementation.
233   *
234   * @since 7.0
235   */
236  @Beta
237  protected int standardHashCode() {
238    return Lists.hashCodeImpl(this);
239  }
240}