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