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