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