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