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

Class Class, % Method, % Line, %
ForwardingCollection 100% (1/1) 8% (2/25) 6.1% (2/33)


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.GwtCompatible; 20 import com.google.common.base.Objects; 21 import com.google.errorprone.annotations.CanIgnoreReturnValue; 22 import java.util.Collection; 23 import java.util.Iterator; 24 import org.checkerframework.checker.nullness.qual.Nullable; 25  26 /** 27  * A collection which forwards all its method calls to another collection. Subclasses should 28  * override one or more methods to modify the behavior of the backing collection as desired per the 29  * <a href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 30  * 31  * <p><b>Warning:</b> The methods of {@code ForwardingCollection} forward <b>indiscriminately</b> to 32  * the methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change 33  * the behavior of {@link #addAll}, which can lead to unexpected behavior. In this case, you should 34  * override {@code addAll} as well, either providing your own implementation, or delegating to the 35  * provided {@code standardAddAll} method. 36  * 37  * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code 38  * default} methods. Instead, it inherits their default implementations. When those implementations 39  * invoke methods, they invoke methods on the {@code ForwardingCollection}. 40  * 41  * <p>The {@code standard} methods are not guaranteed to be thread-safe, even when all of the 42  * methods that they depend on are thread-safe. 43  * 44  * @author Kevin Bourrillion 45  * @author Louis Wasserman 46  * @since 2.0 47  */ 48 @GwtCompatible 49 public abstract class ForwardingCollection<E> extends ForwardingObject implements Collection<E> { 50  // TODO(lowasser): identify places where thread safety is actually lost 51  52  /** Constructor for use by subclasses. */ 53  protected ForwardingCollection() {} 54  55  @Override 56  protected abstract Collection<E> delegate(); 57  58  @Override 59  public Iterator<E> iterator() { 60  return delegate().iterator(); 61  } 62  63  @Override 64  public int size() { 65  return delegate().size(); 66  } 67  68  @CanIgnoreReturnValue 69  @Override 70  public boolean removeAll(Collection<?> collection) { 71  return delegate().removeAll(collection); 72  } 73  74  @Override 75  public boolean isEmpty() { 76  return delegate().isEmpty(); 77  } 78  79  @Override 80  public boolean contains(Object object) { 81  return delegate().contains(object); 82  } 83  84  @CanIgnoreReturnValue 85  @Override 86  public boolean add(E element) { 87  return delegate().add(element); 88  } 89  90  @CanIgnoreReturnValue 91  @Override 92  public boolean remove(Object object) { 93  return delegate().remove(object); 94  } 95  96  @Override 97  public boolean containsAll(Collection<?> collection) { 98  return delegate().containsAll(collection); 99  } 100  101  @CanIgnoreReturnValue 102  @Override 103  public boolean addAll(Collection<? extends E> collection) { 104  return delegate().addAll(collection); 105  } 106  107  @CanIgnoreReturnValue 108  @Override 109  public boolean retainAll(Collection<?> collection) { 110  return delegate().retainAll(collection); 111  } 112  113  @Override 114  public void clear() { 115  delegate().clear(); 116  } 117  118  @Override 119  public Object[] toArray() { 120  return delegate().toArray(); 121  } 122  123  @CanIgnoreReturnValue 124  @Override 125  public <T> T[] toArray(T[] array) { 126  return delegate().toArray(array); 127  } 128  129  /** 130  * A sensible definition of {@link #contains} in terms of {@link #iterator}. If you override 131  * {@link #iterator}, you may wish to override {@link #contains} to forward to this 132  * implementation. 133  * 134  * @since 7.0 135  */ 136  protected boolean standardContains(@Nullable Object object) { 137  return Iterators.contains(iterator(), object); 138  } 139  140  /** 141  * A sensible definition of {@link #containsAll} in terms of {@link #contains} . If you override 142  * {@link #contains}, you may wish to override {@link #containsAll} to forward to this 143  * implementation. 144  * 145  * @since 7.0 146  */ 147  protected boolean standardContainsAll(Collection<?> collection) { 148  return Collections2.containsAllImpl(this, collection); 149  } 150  151  /** 152  * A sensible definition of {@link #addAll} in terms of {@link #add}. If you override {@link 153  * #add}, you may wish to override {@link #addAll} to forward to this implementation. 154  * 155  * @since 7.0 156  */ 157  protected boolean standardAddAll(Collection<? extends E> collection) { 158  return Iterators.addAll(this, collection.iterator()); 159  } 160  161  /** 162  * A sensible definition of {@link #remove} in terms of {@link #iterator}, using the iterator's 163  * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link 164  * #remove} to forward to this implementation. 165  * 166  * @since 7.0 167  */ 168  protected boolean standardRemove(@Nullable Object object) { 169  Iterator<E> iterator = iterator(); 170  while (iterator.hasNext()) { 171  if (Objects.equal(iterator.next(), object)) { 172  iterator.remove(); 173  return true; 174  } 175  } 176  return false; 177  } 178  179  /** 180  * A sensible definition of {@link #removeAll} in terms of {@link #iterator}, using the iterator's 181  * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link 182  * #removeAll} to forward to this implementation. 183  * 184  * @since 7.0 185  */ 186  protected boolean standardRemoveAll(Collection<?> collection) { 187  return Iterators.removeAll(iterator(), collection); 188  } 189  190  /** 191  * A sensible definition of {@link #retainAll} in terms of {@link #iterator}, using the iterator's 192  * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link 193  * #retainAll} to forward to this implementation. 194  * 195  * @since 7.0 196  */ 197  protected boolean standardRetainAll(Collection<?> collection) { 198  return Iterators.retainAll(iterator(), collection); 199  } 200  201  /** 202  * A sensible definition of {@link #clear} in terms of {@link #iterator}, using the iterator's 203  * {@code remove} method. If you override {@link #iterator}, you may wish to override {@link 204  * #clear} to forward to this implementation. 205  * 206  * @since 7.0 207  */ 208  protected void standardClear() { 209  Iterators.clear(iterator()); 210  } 211  212  /** 213  * A sensible definition of {@link #isEmpty} as {@code !iterator().hasNext}. If you override 214  * {@link #isEmpty}, you may wish to override {@link #isEmpty} to forward to this implementation. 215  * Alternately, it may be more efficient to implement {@code isEmpty} as {@code size() == 0}. 216  * 217  * @since 7.0 218  */ 219  protected boolean standardIsEmpty() { 220  return !iterator().hasNext(); 221  } 222  223  /** 224  * A sensible definition of {@link #toString} in terms of {@link #iterator}. If you override 225  * {@link #iterator}, you may wish to override {@link #toString} to forward to this 226  * implementation. 227  * 228  * @since 7.0 229  */ 230  protected String standardToString() { 231  return Collections2.toStringImpl(this); 232  } 233  234  /** 235  * A sensible definition of {@link #toArray()} in terms of {@link #toArray(Object[])}. If you 236  * override {@link #toArray(Object[])}, you may wish to override {@link #toArray} to forward to 237  * this implementation. 238  * 239  * @since 7.0 240  */ 241  protected Object[] standardToArray() { 242  Object[] newArray = new Object[size()]; 243  return toArray(newArray); 244  } 245  246  /** 247  * A sensible definition of {@link #toArray(Object[])} in terms of {@link #size} and {@link 248  * #iterator}. If you override either of these methods, you may wish to override {@link #toArray} 249  * to forward to this implementation. 250  * 251  * @since 7.0 252  */ 253  protected <T> T[] standardToArray(T[] array) { 254  return ObjectArrays.toArrayImpl(this, array); 255  } 256 }