zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 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 | |
| 17 | package com.google.common.collect; |
| 18 | |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import com.google.common.annotations.GwtCompatible; |
| 22 | import java.util.Collection; |
| 23 | import java.util.Set; |
cpovirk | 6c23d7a | 2021-06-29 12:39:27 -0700 | [diff] [blame^] | 24 | import javax.annotation.CheckForNull; |
| 25 | import org.checkerframework.checker.nullness.qual.Nullable; |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 26 | |
| 27 | /** |
cpovirk | fd919e5 | 2017-12-04 08:51:35 -0800 | [diff] [blame] | 28 | * 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 |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 30 | * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>. |
| 31 | * |
cpovirk | fd919e5 | 2017-12-04 08:51:35 -0800 | [diff] [blame] | 32 | * <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. |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 37 | * |
| 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 | * |
cpovirk | fd919e5 | 2017-12-04 08:51:35 -0800 | [diff] [blame] | 42 | * <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. |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 44 | * |
| 45 | * @author Kevin Bourrillion |
| 46 | * @author Louis Wasserman |
| 47 | * @since 2.0 |
| 48 | */ |
| 49 | @GwtCompatible |
cpovirk | 6c23d7a | 2021-06-29 12:39:27 -0700 | [diff] [blame^] | 50 | @ElementTypesAreNonnullByDefault |
| 51 | public abstract class ForwardingSet<E extends @Nullable Object> extends ForwardingCollection<E> |
| 52 | implements Set<E> { |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 53 | // 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 |
cpovirk | 6c23d7a | 2021-06-29 12:39:27 -0700 | [diff] [blame^] | 62 | public boolean equals(@CheckForNull Object object) { |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 63 | return object == this || delegate().equals(object); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public int hashCode() { |
| 68 | return delegate().hashCode(); |
| 69 | } |
| 70 | |
| 71 | /** |
cpovirk | fd919e5 | 2017-12-04 08:51:35 -0800 | [diff] [blame] | 72 | * 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. |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 75 | * |
| 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 | /** |
cpovirk | fd919e5 | 2017-12-04 08:51:35 -0800 | [diff] [blame] | 84 | * 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. |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 87 | * |
| 88 | * @since 7.0 |
| 89 | */ |
cpovirk | 6c23d7a | 2021-06-29 12:39:27 -0700 | [diff] [blame^] | 90 | protected boolean standardEquals(@CheckForNull Object object) { |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 91 | return Sets.equalsImpl(this, object); |
| 92 | } |
| 93 | |
| 94 | /** |
cpovirk | fd919e5 | 2017-12-04 08:51:35 -0800 | [diff] [blame] | 95 | * 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. |
zhenghua | 9b94fb3 | 2017-04-20 14:33:38 -0700 | [diff] [blame] | 97 | * |
| 98 | * @since 7.0 |
| 99 | */ |
| 100 | protected int standardHashCode() { |
| 101 | return Sets.hashCodeImpl(this); |
| 102 | } |
| 103 | } |