Coverage Summary for Class: ForwardingList (com.google.common.collect)

Class Class, % Method, % Line, %
ForwardingList 0% (0/1) 0% (0/23) 0% (0/24)


1 /* 2  * Copyright (C) 2007 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect; 18  19 import com.google.common.annotations.Beta; 20 import com.google.common.annotations.GwtCompatible; 21 import com.google.errorprone.annotations.CanIgnoreReturnValue; 22 import java.util.Collection; 23 import java.util.Iterator; 24 import java.util.List; 25 import java.util.ListIterator; 26 import org.checkerframework.checker.nullness.qual.Nullable; 27  28 /** 29  * A list which forwards all its method calls to another list. Subclasses should override one or 30  * more methods to modify the behavior of the backing list as desired per the <a 31  * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 32  * 33  * <p>This class does not implement {@link java.util.RandomAccess}. If the delegate supports random 34  * access, the {@code ForwardingList} subclass should implement the {@code RandomAccess} interface. 35  * 36  * <p><b>Warning:</b> The methods of {@code ForwardingList} forward <b>indiscriminately</b> to the 37  * methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change the 38  * behavior of {@link #addAll}, which can lead to unexpected behavior. In this case, you should 39  * override {@code addAll} as well, either providing your own implementation, or delegating to the 40  * provided {@code standardAddAll} method. 41  * 42  * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 43  * default} methods. Instead, it inherits their default implementations. When those implementations 44  * invoke methods, they invoke methods on the {@code ForwardingList}. 45  * 46  * <p>The {@code standard} methods and any collection views they return are not guaranteed to be 47  * thread-safe, even when all of the methods that they depend on are thread-safe. 48  * 49  * @author Mike Bostock 50  * @author Louis Wasserman 51  * @since 2.0 52  */ 53 @GwtCompatible 54 public abstract class ForwardingList<E> extends ForwardingCollection<E> implements List<E> { 55  // TODO(lowasser): identify places where thread safety is actually lost 56  57  /** Constructor for use by subclasses. */ 58  protected ForwardingList() {} 59  60  @Override 61  protected abstract List<E> delegate(); 62  63  @Override 64  public void add(int index, E element) { 65  delegate().add(index, element); 66  } 67  68  @CanIgnoreReturnValue 69  @Override 70  public boolean addAll(int index, Collection<? extends E> elements) { 71  return delegate().addAll(index, elements); 72  } 73  74  @Override 75  public E get(int index) { 76  return delegate().get(index); 77  } 78  79  @Override 80  public int indexOf(Object element) { 81  return delegate().indexOf(element); 82  } 83  84  @Override 85  public int lastIndexOf(Object element) { 86  return delegate().lastIndexOf(element); 87  } 88  89  @Override 90  public ListIterator<E> listIterator() { 91  return delegate().listIterator(); 92  } 93  94  @Override 95  public ListIterator<E> listIterator(int index) { 96  return delegate().listIterator(index); 97  } 98  99  @CanIgnoreReturnValue 100  @Override 101  public E remove(int index) { 102  return delegate().remove(index); 103  } 104  105  @CanIgnoreReturnValue 106  @Override 107  public E set(int index, E element) { 108  return delegate().set(index, element); 109  } 110  111  @Override 112  public List<E> subList(int fromIndex, int toIndex) { 113  return delegate().subList(fromIndex, toIndex); 114  } 115  116  @Override 117  public boolean equals(@Nullable Object object) { 118  return object == this || delegate().equals(object); 119  } 120  121  @Override 122  public int hashCode() { 123  return delegate().hashCode(); 124  } 125  126  /** 127  * A sensible default implementation of {@link #add(Object)}, in terms of {@link #add(int, 128  * Object)}. If you override {@link #add(int, Object)}, you may wish to override {@link 129  * #add(Object)} to forward to this implementation. 130  * 131  * @since 7.0 132  */ 133  protected boolean standardAdd(E element) { 134  add(size(), element); 135  return true; 136  } 137  138  /** 139  * A sensible default implementation of {@link #addAll(int, Collection)}, in terms of the {@code 140  * add} method of {@link #listIterator(int)}. If you override {@link #listIterator(int)}, you may 141  * wish to override {@link #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 #listIterator()}. If 151  * you override {@link #listIterator()}, you may wish to override {@link #indexOf} to forward to 152  * 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 {@link 162  * #listIterator(int)}. If you override {@link #listIterator(int)}, you may wish to override 163  * {@link #lastIndexOf} to forward to this implementation. 164  * 165  * @since 7.0 166  */ 167  protected int standardLastIndexOf(@Nullable Object element) { 168  return Lists.lastIndexOfImpl(this, element); 169  } 170  171  /** 172  * A sensible default implementation of {@link #iterator}, in terms of {@link #listIterator()}. If 173  * you override {@link #listIterator()}, you may wish to override {@link #iterator} to forward to 174  * this implementation. 175  * 176  * @since 7.0 177  */ 178  protected Iterator<E> standardIterator() { 179  return listIterator(); 180  } 181  182  /** 183  * A sensible default implementation of {@link #listIterator()}, in terms of {@link 184  * #listIterator(int)}. If you override {@link #listIterator(int)}, you may wish to override 185  * {@link #listIterator()} to forward to this implementation. 186  * 187  * @since 7.0 188  */ 189  protected ListIterator<E> standardListIterator() { 190  return listIterator(0); 191  } 192  193  /** 194  * A sensible default implementation of {@link #listIterator(int)}, in terms of {@link #size}, 195  * {@link #get(int)}, {@link #set(int, Object)}, {@link #add(int, Object)}, and {@link 196  * #remove(int)}. If you override any of these methods, you may wish to override {@link 197  * #listIterator(int)} to forward to this implementation. 198  * 199  * @since 7.0 200  */ 201  @Beta 202  protected ListIterator<E> standardListIterator(int start) { 203  return Lists.listIteratorImpl(this, start); 204  } 205  206  /** 207  * A sensible default implementation of {@link #subList(int, int)}. If you override any other 208  * methods, you may wish to override {@link #subList(int, int)} to forward to this implementation. 209  * 210  * @since 7.0 211  */ 212  @Beta 213  protected List<E> standardSubList(int fromIndex, int toIndex) { 214  return Lists.subListImpl(this, fromIndex, toIndex); 215  } 216  217  /** 218  * A sensible definition of {@link #equals(Object)} in terms of {@link #size} and {@link 219  * #iterator}. If you override either of those methods, you may wish to override {@link 220  * #equals(Object)} to forward to this implementation. 221  * 222  * @since 7.0 223  */ 224  @Beta 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  @Beta 237  protected int standardHashCode() { 238  return Lists.hashCodeImpl(this); 239  } 240 }