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