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