Coverage Summary for Class: ForwardingSet (com.google.common.collect)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| ForwardingSet | 100% (1/1) | 16.7% (1/6) | 16.7% (1/6) |
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 static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.google.common.annotations.GwtCompatible; 22 import java.util.Collection; 23 import java.util.Set; 24 import org.checkerframework.checker.nullness.qual.Nullable; 25 26 /** 27 * A set which forwards all its method calls to another set. Subclasses should override one or more 28 * methods to modify the behavior of the backing set as desired per the <a 29 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 30 * 31 * <p><b>Warning:</b> The methods of {@code ForwardingSet} forward <b>indiscriminately</b> to the 32 * methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change the 33 * 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 ForwardingSet}. 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 ForwardingSet<E> extends ForwardingCollection<E> implements Set<E> { 50 // TODO(lowasser): identify places where thread safety is actually lost 51 52 /** Constructor for use by subclasses. */ 53 protected ForwardingSet() {} 54 55 @Override 56 protected abstract Set<E> delegate(); 57 58 @Override 59 public boolean equals(@Nullable Object object) { 60 return object == this || delegate().equals(object); 61 } 62 63 @Override 64 public int hashCode() { 65 return delegate().hashCode(); 66 } 67 68 /** 69 * A sensible definition of {@link #removeAll} in terms of {@link #iterator} and {@link #remove}. 70 * If you override {@code iterator} or {@code remove}, you may wish to override {@link #removeAll} 71 * to forward to this implementation. 72 * 73 * @since 7.0 (this version overrides the {@code ForwardingCollection} version as of 12.0) 74 */ 75 @Override 76 protected boolean standardRemoveAll(Collection<?> collection) { 77 return Sets.removeAllImpl(this, checkNotNull(collection)); // for GWT 78 } 79 80 /** 81 * A sensible definition of {@link #equals} in terms of {@link #size} and {@link #containsAll}. If 82 * you override either of those methods, you may wish to override {@link #equals} to forward to 83 * this implementation. 84 * 85 * @since 7.0 86 */ 87 protected boolean standardEquals(@Nullable Object object) { 88 return Sets.equalsImpl(this, object); 89 } 90 91 /** 92 * A sensible definition of {@link #hashCode} in terms of {@link #iterator}. If you override 93 * {@link #iterator}, you may wish to override {@link #equals} to forward to this implementation. 94 * 95 * @since 7.0 96 */ 97 protected int standardHashCode() { 98 return Sets.hashCodeImpl(this); 99 } 100 }