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; 021 022import java.util.Collection; 023import java.util.Iterator; 024import java.util.List; 025import java.util.ListIterator; 026 027import 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 053 */ 054@GwtCompatible 055public abstract class ForwardingList<E> extends ForwardingCollection<E> implements List<E> { 056 // TODO(lowasser): identify places where thread safety is actually lost 057 058 /** Constructor for use by subclasses. */ 059 protected ForwardingList() {} 060 061 @Override 062 protected abstract List<E> delegate(); 063 064 @Override 065 public void add(int index, E element) { 066 delegate().add(index, element); 067 } 068 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 @Override 100 public E remove(int index) { 101 return delegate().remove(index); 102 } 103 104 @Override 105 public E set(int index, E element) { 106 return delegate().set(index, element); 107 } 108 109 @Override 110 public List<E> subList(int fromIndex, int toIndex) { 111 return delegate().subList(fromIndex, toIndex); 112 } 113 114 @Override 115 public boolean equals(@Nullable Object object) { 116 return object == this || delegate().equals(object); 117 } 118 119 @Override 120 public int hashCode() { 121 return delegate().hashCode(); 122 } 123 124 /** 125 * A sensible default implementation of {@link #add(Object)}, in terms of 126 * {@link #add(int, Object)}. If you override {@link #add(int, Object)}, you 127 * may wish to override {@link #add(Object)} to forward to this 128 * implementation. 129 * 130 * @since 7.0 131 */ 132 protected boolean standardAdd(E element) { 133 add(size(), element); 134 return true; 135 } 136 137 /** 138 * A sensible default implementation of {@link #addAll(int, Collection)}, in 139 * terms of the {@code add} method of {@link #listIterator(int)}. If you 140 * override {@link #listIterator(int)}, you may wish to override {@link 141 * #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 151 * #listIterator()}. If you override {@link #listIterator()}, you may wish to 152 * override {@link #indexOf} to forward to 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 162 * {@link #listIterator(int)}. If you override {@link #listIterator(int)}, you 163 * may wish to override {@link #lastIndexOf} to forward to this 164 * implementation. 165 * 166 * @since 7.0 167 */ 168 protected int standardLastIndexOf(@Nullable Object element) { 169 return Lists.lastIndexOfImpl(this, element); 170 } 171 172 /** 173 * A sensible default implementation of {@link #iterator}, in terms of 174 * {@link #listIterator()}. If you override {@link #listIterator()}, you may 175 * wish to override {@link #iterator} to forward to this implementation. 176 * 177 * @since 7.0 178 */ 179 protected Iterator<E> standardIterator() { 180 return listIterator(); 181 } 182 183 /** 184 * A sensible default implementation of {@link #listIterator()}, in terms of 185 * {@link #listIterator(int)}. If you override {@link #listIterator(int)}, you 186 * may wish to override {@link #listIterator()} to forward to this 187 * implementation. 188 * 189 * @since 7.0 190 */ 191 protected ListIterator<E> standardListIterator() { 192 return listIterator(0); 193 } 194 195 /** 196 * A sensible default implementation of {@link #listIterator(int)}, in terms 197 * of {@link #size}, {@link #get(int)}, {@link #set(int, Object)}, {@link 198 * #add(int, Object)}, and {@link #remove(int)}. If you override any of these 199 * methods, you may wish to override {@link #listIterator(int)} to forward to 200 * this implementation. 201 * 202 * @since 7.0 203 */ 204 @Beta 205 protected ListIterator<E> standardListIterator(int start) { 206 return Lists.listIteratorImpl(this, start); 207 } 208 209 /** 210 * A sensible default implementation of {@link #subList(int, int)}. If you 211 * override any other methods, you may wish to override {@link #subList(int, 212 * int)} to forward to this implementation. 213 * 214 * @since 7.0 215 */ 216 @Beta 217 protected List<E> standardSubList(int fromIndex, int toIndex) { 218 return Lists.subListImpl(this, fromIndex, toIndex); 219 } 220 221 /** 222 * A sensible definition of {@link #equals(Object)} in terms of {@link #size} 223 * and {@link #iterator}. If you override either of those methods, you may 224 * wish to override {@link #equals(Object)} to forward to this implementation. 225 * 226 * @since 7.0 227 */ 228 @Beta 229 protected boolean standardEquals(@Nullable Object object) { 230 return Lists.equalsImpl(this, object); 231 } 232 233 /** 234 * A sensible definition of {@link #hashCode} in terms of {@link #iterator}. 235 * If you override {@link #iterator}, you may wish to override {@link 236 * #hashCode} to forward to this implementation. 237 * 238 * @since 7.0 239 */ 240 @Beta 241 protected int standardHashCode() { 242 return Lists.hashCodeImpl(this); 243 } 244}