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
017 package com.google.common.collect;
018
019 import static com.google.common.base.Preconditions.checkNotNull;
020
021 import com.google.common.annotations.Beta;
022 import com.google.common.annotations.GwtCompatible;
023
024 import java.util.Collection;
025 import java.util.Set;
026
027 import javax.annotation.Nullable;
028
029 /**
030 * A set which forwards all its method calls to another set. Subclasses should
031 * override one or more methods to modify the behavior of the backing set as
032 * desired per the <a
033 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
034 *
035 * <p><b>Warning:</b> The methods of {@code ForwardingSet} forward
036 * <b>indiscriminately</b> to the methods of the delegate. For example,
037 * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
038 * #addAll}, which can lead to unexpected behavior. In this case, you should
039 * override {@code addAll} as well, either providing your own implementation, or
040 * delegating to the provided {@code standardAddAll} method.
041 *
042 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
043 * when all of the methods that they depend on are thread-safe.
044 *
045 * @author Kevin Bourrillion
046 * @author Louis Wasserman
047 * @since 2.0 (imported from Google Collections Library)
048 */
049 @GwtCompatible
050 public abstract class ForwardingSet<E> extends ForwardingCollection<E>
051 implements Set<E> {
052 // TODO(user): identify places where thread safety is actually lost
053
054 /** Constructor for use by subclasses. */
055 protected ForwardingSet() {}
056
057
058 @Override
059 protected abstract Set<E> delegate();
060
061
062 @Override
063 public boolean equals(@Nullable Object object) {
064 return object == this || delegate().equals(object);
065 }
066
067
068 @Override
069 public int hashCode() {
070 return delegate().hashCode();
071 }
072
073 /**
074 * A sensible definition of {@link #removeAll} in terms of {@link #iterator}
075 * and {@link #remove}. If you override {@code iterator} or {@code remove},
076 * you may wish to override {@link #removeAll} to forward to this
077 * implementation.
078 *
079 * @since 7.0 (this version overrides the {@code ForwardingCollection} version as of 12.0)
080 */
081
082 @Override
083 protected boolean standardRemoveAll(Collection<?> collection) {
084 return Sets.removeAllImpl(this, checkNotNull(collection)); // for GWT
085 }
086
087 /**
088 * A sensible definition of {@link #equals} in terms of {@link #size} and
089 * {@link #containsAll}. If you override either of those methods, you may wish
090 * to override {@link #equals} to forward to this implementation.
091 *
092 * @since 7.0
093 */
094 @Beta protected boolean standardEquals(@Nullable Object object) {
095 return Sets.equalsImpl(this, object);
096 }
097
098 /**
099 * A sensible definition of {@link #hashCode} in terms of {@link #iterator}.
100 * If you override {@link #iterator}, you may wish to override {@link #equals}
101 * to forward to this implementation.
102 *
103 * @since 7.0
104 */
105 @Beta protected int standardHashCode() {
106 return Sets.hashCodeImpl(this);
107 }
108 }