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