javadoc.src-html.com.google.common.collect.ForwardingSet.html Maven / Gradle / Ivy
The newest version!
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
017 package com.google.common.collect;
018
019 import com.google.common.annotations.Beta;
020 import com.google.common.annotations.GwtCompatible;
021
022 import java.util.Set;
023
024 import javax.annotation.Nullable;
025
026 /**
027 * A set which forwards all its method calls to another set. Subclasses should
028 * override one or more methods to modify the behavior of the backing set as
029 * desired per the <a
030 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
031 *
032 * <p><b>Warning:</b> The methods of {@code ForwardingSet} forward
033 * <b>indiscriminately</b> to the methods of the delegate. For example,
034 * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
035 * #addAll}, which can lead to unexpected behavior. In this case, you should
036 * override {@code addAll} as well, either providing your own implementation, or
037 * delegating to the provided {@code standardAddAll} method.
038 *
039 * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
040 * when all of the methods that they depend on are thread-safe.
041 *
042 * @author Kevin Bourrillion
043 * @author Louis Wasserman
044 * @since 2.0 (imported from Google Collections Library)
045 */
046 @GwtCompatible
047 public abstract class ForwardingSet<E> extends ForwardingCollection<E>
048 implements Set<E> {
049 // TODO(user): identify places where thread safety is actually lost
050
051 /** Constructor for use by subclasses. */
052 protected ForwardingSet() {}
053
054 @Override protected abstract Set<E> delegate();
055
056 @Override public boolean equals(@Nullable Object object) {
057 return object == this || delegate().equals(object);
058 }
059
060 @Override public int hashCode() {
061 return delegate().hashCode();
062 }
063
064 /**
065 * A sensible definition of {@link #equals} in terms of {@link #size} and
066 * {@link #containsAll}. If you override either of those methods, you may wish
067 * to override {@link #equals} to forward to this implementation.
068 *
069 * @since 7.0
070 */
071 @Beta protected boolean standardEquals(@Nullable Object object) {
072 return Sets.equalsImpl(this, object);
073 }
074
075 /**
076 * A sensible definition of {@link #hashCode} in terms of {@link #iterator}.
077 * If you override {@link #iterator}, you may wish to override {@link #equals}
078 * to forward to this implementation.
079 *
080 * @since 7.0
081 */
082 @Beta protected int standardHashCode() {
083 return Sets.hashCodeImpl(this);
084 }
085 }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy