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