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