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