blob: bc27272b0267f97c5fe5f2baa9869ee0ebcbcc81 [file] [log] [blame]
zhenghua9b94fb32017-04-20 14:33:38 -07001/*
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 static com.google.common.base.Preconditions.checkNotNull;
20
21import com.google.common.annotations.GwtCompatible;
22import java.util.Collection;
23import java.util.Set;
cpovirk6c23d7a2021-06-29 12:39:27 -070024import javax.annotation.CheckForNull;
25import org.checkerframework.checker.nullness.qual.Nullable;
zhenghua9b94fb32017-04-20 14:33:38 -070026
27/**
cpovirkfd919e52017-12-04 08:51:35 -080028 * A set which forwards all its method calls to another set. Subclasses should override one or more
29 * methods to modify the behavior of the backing set as desired per the <a
zhenghua9b94fb32017-04-20 14:33:38 -070030 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
31 *
cpovirkfd919e52017-12-04 08:51:35 -080032 * <p><b>Warning:</b> The methods of {@code ForwardingSet} forward <b>indiscriminately</b> to the
33 * methods of the delegate. For example, overriding {@link #add} alone <b>will not</b> change the
34 * behavior of {@link #addAll}, which can lead to unexpected behavior. In this case, you should
35 * override {@code addAll} as well, either providing your own implementation, or delegating to the
36 * provided {@code standardAddAll} method.
zhenghua9b94fb32017-04-20 14:33:38 -070037 *
38 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
39 * default} methods. Instead, it inherits their default implementations. When those implementations
40 * invoke methods, they invoke methods on the {@code ForwardingSet}.
41 *
cpovirkfd919e52017-12-04 08:51:35 -080042 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even when all of the
43 * methods that they depend on are thread-safe.
zhenghua9b94fb32017-04-20 14:33:38 -070044 *
45 * @author Kevin Bourrillion
46 * @author Louis Wasserman
47 * @since 2.0
48 */
49@GwtCompatible
cpovirk6c23d7a2021-06-29 12:39:27 -070050@ElementTypesAreNonnullByDefault
51public abstract class ForwardingSet<E extends @Nullable Object> extends ForwardingCollection<E>
52 implements Set<E> {
zhenghua9b94fb32017-04-20 14:33:38 -070053 // TODO(lowasser): identify places where thread safety is actually lost
54
55 /** Constructor for use by subclasses. */
56 protected ForwardingSet() {}
57
58 @Override
59 protected abstract Set<E> delegate();
60
61 @Override
cpovirk6c23d7a2021-06-29 12:39:27 -070062 public boolean equals(@CheckForNull Object object) {
zhenghua9b94fb32017-04-20 14:33:38 -070063 return object == this || delegate().equals(object);
64 }
65
66 @Override
67 public int hashCode() {
68 return delegate().hashCode();
69 }
70
71 /**
cpovirkfd919e52017-12-04 08:51:35 -080072 * A sensible definition of {@link #removeAll} in terms of {@link #iterator} and {@link #remove}.
73 * If you override {@code iterator} or {@code remove}, you may wish to override {@link #removeAll}
74 * to forward to this implementation.
zhenghua9b94fb32017-04-20 14:33:38 -070075 *
76 * @since 7.0 (this version overrides the {@code ForwardingCollection} version as of 12.0)
77 */
78 @Override
79 protected boolean standardRemoveAll(Collection<?> collection) {
80 return Sets.removeAllImpl(this, checkNotNull(collection)); // for GWT
81 }
82
83 /**
cpovirkfd919e52017-12-04 08:51:35 -080084 * A sensible definition of {@link #equals} in terms of {@link #size} and {@link #containsAll}. If
85 * you override either of those methods, you may wish to override {@link #equals} to forward to
86 * this implementation.
zhenghua9b94fb32017-04-20 14:33:38 -070087 *
88 * @since 7.0
89 */
cpovirk6c23d7a2021-06-29 12:39:27 -070090 protected boolean standardEquals(@CheckForNull Object object) {
zhenghua9b94fb32017-04-20 14:33:38 -070091 return Sets.equalsImpl(this, object);
92 }
93
94 /**
cpovirkfd919e52017-12-04 08:51:35 -080095 * A sensible definition of {@link #hashCode} in terms of {@link #iterator}. If you override
96 * {@link #iterator}, you may wish to override {@link #equals} to forward to this implementation.
zhenghua9b94fb32017-04-20 14:33:38 -070097 *
98 * @since 7.0
99 */
100 protected int standardHashCode() {
101 return Sets.hashCodeImpl(this);
102 }
103}