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 23 import java.util.Collection; 24 import java.util.Set; 25 26 import javax.annotation.Nullable; 27 28 /** 29 * A set which forwards all its method calls to another set. Subclasses should 30 * override one or more methods to modify the behavior of the backing set as 31 * desired per the <a 32 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. 33 * 34 * <p><b>Warning:</b> The methods of {@code ForwardingSet} forward 35 * <b>indiscriminately</b> to the methods of the delegate. For example, 36 * overriding {@link #add} alone <b>will not</b> change the behavior of {@link 37 * #addAll}, which can lead to unexpected behavior. In this case, you should 38 * override {@code addAll} as well, either providing your own implementation, or 39 * delegating to the provided {@code standardAddAll} method. 40 * 41 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even 42 * when all of the methods that they depend on are thread-safe. 43 * 44 * @author Kevin Bourrillion 45 * @author Louis Wasserman 46 * @since 2.0 (imported from Google Collections Library) 47 */ 48 @GwtCompatible 49 public abstract class ForwardingSet<E> extends ForwardingCollection<E> 50 implements Set<E> { 51 // TODO(user): identify places where thread safety is actually lost 52 53 /** Constructor for use by subclasses. */ 54 protected ForwardingSet() {} 55 56 @Override protected abstract Set<E> delegate(); 57 58 @Override public boolean equals(@Nullable Object object) { 59 return object == this || delegate().equals(object); 60 } 61 62 @Override public int hashCode() { 63 return delegate().hashCode(); 64 } 65 66 /** 67 * A sensible definition of {@link #removeAll} in terms of {@link #iterator} 68 * and {@link #remove}. If you override {@code iterator} or {@code remove}, 69 * you may wish to override {@link #removeAll} to forward to this 70 * implementation. 71 * 72 * @since 7.0 (this version overrides the {@code ForwardingCollection} version as of 12.0) 73 */ 74 @Override 75 protected boolean standardRemoveAll(Collection<?> collection) { 76 return Sets.removeAllImpl(this, checkNotNull(collection)); // for GWT 77 } 78 79 /** 80 * A sensible definition of {@link #equals} in terms of {@link #size} and 81 * {@link #containsAll}. If you override either of those methods, you may wish 82 * to override {@link #equals} to forward to this implementation. 83 * 84 * @since 7.0 85 */ 86 protected boolean standardEquals(@Nullable Object object) { 87 return Sets.equalsImpl(this, object); 88 } 89 90 /** 91 * A sensible definition of {@link #hashCode} in terms of {@link #iterator}. 92 * If you override {@link #iterator}, you may wish to override {@link #equals} 93 * to forward to this implementation. 94 * 95 * @since 7.0 96 */ 97 protected int standardHashCode() { 98 return Sets.hashCodeImpl(this); 99 } 100 } 101