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 com.google.common.annotations.GwtCompatible;
020import com.google.common.base.Objects;
021import com.google.errorprone.annotations.CanIgnoreReturnValue;
022import java.util.Collection;
023import java.util.Iterator;
024import java.util.Map;
025import java.util.Set;
026import org.jspecify.annotations.Nullable;
027
028/**
029 * A map which forwards all its method calls to another map. Subclasses should override one or more
030 * methods to modify the behavior of the backing map as desired per the <a
031 * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
032 *
033 * <p><b>Warning:</b> The methods of {@code ForwardingMap} forward <i>indiscriminately</i> to the
034 * methods of the delegate. For example, overriding {@link #put} alone <i>will not</i> change the
035 * behavior of {@link #putAll}, which can lead to unexpected behavior. In this case, you should
036 * override {@code putAll} as well, either providing your own implementation, or delegating to the
037 * provided {@code standardPutAll} method.
038 *
039 * <p><b>{@code default} method warning:</b> This class does <i>not</i> forward calls to {@code
040 * default} methods. Instead, it inherits their default implementations. When those implementations
041 * invoke methods, they invoke methods on the {@code ForwardingMap}.
042 *
043 * <p>Each of the {@code standard} methods, where appropriate, use {@link Objects#equal} to test
044 * equality for both keys and values. This may not be the desired behavior for map implementations
045 * that use non-standard notions of key equality, such as a {@code SortedMap} whose comparator is
046 * not consistent with {@code equals}.
047 *
048 * <p>The {@code standard} methods and the collection views they return are not guaranteed to be
049 * thread-safe, even when all of the methods that they depend on are thread-safe.
050 *
051 * @author Kevin Bourrillion
052 * @author Jared Levy
053 * @author Louis Wasserman
054 * @since 2.0
055 */
056@GwtCompatible
057public abstract class ForwardingMap<K extends @Nullable Object, V extends @Nullable Object>
058    extends ForwardingObject implements Map<K, V> {
059  // TODO(lowasser): identify places where thread safety is actually lost
060
061  /** Constructor for use by subclasses. */
062  protected ForwardingMap() {}
063
064  @Override
065  protected abstract Map<K, V> delegate();
066
067  @Override
068  public int size() {
069    return delegate().size();
070  }
071
072  @Override
073  public boolean isEmpty() {
074    return delegate().isEmpty();
075  }
076
077  @CanIgnoreReturnValue
078  @Override
079  public @Nullable V remove(@Nullable Object key) {
080    return delegate().remove(key);
081  }
082
083  @Override
084  public void clear() {
085    delegate().clear();
086  }
087
088  @Override
089  public boolean containsKey(@Nullable Object key) {
090    return delegate().containsKey(key);
091  }
092
093  @Override
094  public boolean containsValue(@Nullable Object value) {
095    return delegate().containsValue(value);
096  }
097
098  @Override
099  public @Nullable V get(@Nullable Object key) {
100    return delegate().get(key);
101  }
102
103  @CanIgnoreReturnValue
104  @Override
105  public @Nullable V put(@ParametricNullness K key, @ParametricNullness V value) {
106    return delegate().put(key, value);
107  }
108
109  @Override
110  public void putAll(Map<? extends K, ? extends V> map) {
111    delegate().putAll(map);
112  }
113
114  @Override
115  public Set<K> keySet() {
116    return delegate().keySet();
117  }
118
119  @Override
120  public Collection<V> values() {
121    return delegate().values();
122  }
123
124  @Override
125  public Set<Entry<K, V>> entrySet() {
126    return delegate().entrySet();
127  }
128
129  @Override
130  public boolean equals(@Nullable Object object) {
131    return object == this || delegate().equals(object);
132  }
133
134  @Override
135  public int hashCode() {
136    return delegate().hashCode();
137  }
138
139  /**
140   * A sensible definition of {@link #putAll(Map)} in terms of {@link #put(Object, Object)}. If you
141   * override {@link #put(Object, Object)}, you may wish to override {@link #putAll(Map)} to forward
142   * to this implementation.
143   *
144   * @since 7.0
145   */
146  protected void standardPutAll(Map<? extends K, ? extends V> map) {
147    Maps.putAllImpl(this, map);
148  }
149
150  /**
151   * A sensible, albeit inefficient, definition of {@link #remove} in terms of the {@code iterator}
152   * method of {@link #entrySet}. If you override {@link #entrySet}, you may wish to override {@link
153   * #remove} to forward to this implementation.
154   *
155   * <p>Alternately, you may wish to override {@link #remove} with {@code keySet().remove}, assuming
156   * that approach would not lead to an infinite loop.
157   *
158   * @since 7.0
159   */
160  protected @Nullable V standardRemove(@Nullable Object key) {
161    Iterator<Entry<K, V>> entryIterator = entrySet().iterator();
162    while (entryIterator.hasNext()) {
163      Entry<K, V> entry = entryIterator.next();
164      if (Objects.equal(entry.getKey(), key)) {
165        V value = entry.getValue();
166        entryIterator.remove();
167        return value;
168      }
169    }
170    return null;
171  }
172
173  /**
174   * A sensible definition of {@link #clear} in terms of the {@code iterator} method of {@link
175   * #entrySet}. In many cases, you may wish to override {@link #clear} to forward to this
176   * implementation.
177   *
178   * @since 7.0
179   */
180  protected void standardClear() {
181    Iterators.clear(entrySet().iterator());
182  }
183
184  /**
185   * A sensible implementation of {@link Map#keySet} in terms of the following methods: {@link
186   * ForwardingMap#clear}, {@link ForwardingMap#containsKey}, {@link ForwardingMap#isEmpty}, {@link
187   * ForwardingMap#remove}, {@link ForwardingMap#size}, and the {@link Set#iterator} method of
188   * {@link ForwardingMap#entrySet}. In many cases, you may wish to override {@link
189   * ForwardingMap#keySet} to forward to this implementation or a subclass thereof.
190   *
191   * @since 10.0
192   */
193  protected class StandardKeySet extends Maps.KeySet<K, V> {
194    /** Constructor for use by subclasses. */
195    public StandardKeySet() {
196      super(ForwardingMap.this);
197    }
198  }
199
200  /**
201   * A sensible, albeit inefficient, definition of {@link #containsKey} in terms of the {@code
202   * iterator} method of {@link #entrySet}. If you override {@link #entrySet}, you may wish to
203   * override {@link #containsKey} to forward to this implementation.
204   *
205   * @since 7.0
206   */
207  protected boolean standardContainsKey(@Nullable Object key) {
208    return Maps.containsKeyImpl(this, key);
209  }
210
211  /**
212   * A sensible implementation of {@link Map#values} in terms of the following methods: {@link
213   * ForwardingMap#clear}, {@link ForwardingMap#containsValue}, {@link ForwardingMap#isEmpty},
214   * {@link ForwardingMap#size}, and the {@link Set#iterator} method of {@link
215   * ForwardingMap#entrySet}. In many cases, you may wish to override {@link ForwardingMap#values}
216   * to forward to this implementation or a subclass thereof.
217   *
218   * @since 10.0
219   */
220  protected class StandardValues extends Maps.Values<K, V> {
221    /** Constructor for use by subclasses. */
222    public StandardValues() {
223      super(ForwardingMap.this);
224    }
225  }
226
227  /**
228   * A sensible definition of {@link #containsValue} in terms of the {@code iterator} method of
229   * {@link #entrySet}. If you override {@link #entrySet}, you may wish to override {@link
230   * #containsValue} to forward to this implementation.
231   *
232   * @since 7.0
233   */
234  protected boolean standardContainsValue(@Nullable Object value) {
235    return Maps.containsValueImpl(this, value);
236  }
237
238  /**
239   * A sensible implementation of {@link Map#entrySet} in terms of the following methods: {@link
240   * ForwardingMap#clear}, {@link ForwardingMap#containsKey}, {@link ForwardingMap#get}, {@link
241   * ForwardingMap#isEmpty}, {@link ForwardingMap#remove}, and {@link ForwardingMap#size}. In many
242   * cases, you may wish to override {@link #entrySet} to forward to this implementation or a
243   * subclass thereof.
244   *
245   * @since 10.0
246   */
247  protected abstract class StandardEntrySet extends Maps.EntrySet<K, V> {
248    /** Constructor for use by subclasses. */
249    protected StandardEntrySet() {}
250
251    @Override
252    Map<K, V> map() {
253      return ForwardingMap.this;
254    }
255  }
256
257  /**
258   * A sensible definition of {@link #isEmpty} in terms of the {@code iterator} method of {@link
259   * #entrySet}. If you override {@link #entrySet}, you may wish to override {@link #isEmpty} to
260   * forward to this implementation.
261   *
262   * @since 7.0
263   */
264  protected boolean standardIsEmpty() {
265    return !entrySet().iterator().hasNext();
266  }
267
268  /**
269   * A sensible definition of {@link #equals} in terms of the {@code equals} method of {@link
270   * #entrySet}. If you override {@link #entrySet}, you may wish to override {@link #equals} to
271   * forward to this implementation.
272   *
273   * @since 7.0
274   */
275  protected boolean standardEquals(@Nullable Object object) {
276    return Maps.equalsImpl(this, object);
277  }
278
279  /**
280   * A sensible definition of {@link #hashCode} in terms of the {@code iterator} method of {@link
281   * #entrySet}. If you override {@link #entrySet}, you may wish to override {@link #hashCode} to
282   * forward to this implementation.
283   *
284   * @since 7.0
285   */
286  protected int standardHashCode() {
287    return Sets.hashCodeImpl(entrySet());
288  }
289
290  /**
291   * A sensible definition of {@link #toString} in terms of the {@code iterator} method of {@link
292   * #entrySet}. If you override {@link #entrySet}, you may wish to override {@link #toString} to
293   * forward to this implementation.
294   *
295   * @since 7.0
296   */
297  protected String standardToString() {
298    return Maps.toStringImpl(this);
299  }
300}