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    import com.google.common.base.Objects;
022    import com.google.common.base.Supplier;
023    
024    import java.util.Collection;
025    import java.util.Iterator;
026    import java.util.Map;
027    import java.util.Set;
028    
029    import javax.annotation.Nullable;
030    
031    /**
032     * A map which forwards all its method calls to another map. Subclasses should
033     * override one or more methods to modify the behavior of the backing map as
034     * desired per the <a
035     * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
036     *
037     * <p><i>Warning:</i> The methods of {@code ForwardingMap} forward
038     * <i>indiscriminately</i> to the methods of the delegate. For example,
039     * overriding {@link #put} alone <i>will not</i> change the behavior of {@link
040     * #putAll}, which can lead to unexpected behavior. In this case, you should
041     * override {@code putAll} as well, either providing your own implementation, or
042     * delegating to the provided {@code standardPutAll} method.
043     *
044     * <p>Each of the {@code standard} methods, where appropriate, use {@link
045     * Objects#equal} to test equality for both keys and values. This may not be
046     * the desired behavior for map implementations that use non-standard notions of
047     * key equality, such as a {@code SortedMap} whose comparator is not consistent
048     * with {@code equals}.
049     *
050     * <p>The {@code standard} methods and the collection views they return are not
051     * guaranteed to be thread-safe, even when all of the methods that they depend
052     * on are thread-safe.
053     *
054     * @author Kevin Bourrillion
055     * @author Jared Levy
056     * @author Louis Wasserman
057     * @since 2.0 (imported from Google Collections Library)
058     */
059    @GwtCompatible
060    public abstract class ForwardingMap<K, V> extends ForwardingObject
061        implements Map<K, V> {
062      // TODO(user): identify places where thread safety is actually lost
063    
064      /** Constructor for use by subclasses. */
065      protected ForwardingMap() {}
066    
067      @Override protected abstract Map<K, V> delegate();
068    
069      @Override
070      public int size() {
071        return delegate().size();
072      }
073    
074      @Override
075      public boolean isEmpty() {
076        return delegate().isEmpty();
077      }
078    
079      @Override
080      public V remove(Object object) {
081        return delegate().remove(object);
082      }
083    
084      @Override
085      public void clear() {
086        delegate().clear();
087      }
088    
089      @Override
090      public boolean containsKey(Object key) {
091        return delegate().containsKey(key);
092      }
093    
094      @Override
095      public boolean containsValue(Object value) {
096        return delegate().containsValue(value);
097      }
098    
099      @Override
100      public V get(Object key) {
101        return delegate().get(key);
102      }
103    
104      @Override
105      public V put(K key, 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 public boolean equals(@Nullable Object object) {
130        return object == this || delegate().equals(object);
131      }
132    
133      @Override public int hashCode() {
134        return delegate().hashCode();
135      }
136    
137      /**
138       * A sensible definition of {@link #putAll(Map)} in terms of {@link
139       * #put(Object, Object)}. If you override {@link #put(Object, Object)}, you
140       * may wish to override {@link #putAll(Map)} to forward to this
141       * implementation.
142       *
143       * @since 7.0
144       */
145      @Beta protected void standardPutAll(Map<? extends K, ? extends V> map) {
146        Maps.putAllImpl(this, map);
147      }
148    
149      /**
150       * A sensible, albeit inefficient, definition of {@link #remove} in terms of
151       * the {@code iterator} method of {@link #entrySet}. If you override {@link
152       * #entrySet}, you may wish to override {@link #remove} to forward to this
153       * implementation.
154       *
155       * <p>Alternately, you may wish to override {@link #remove} with {@code
156       * keySet().remove}, assuming that approach would not lead to an infinite
157       * loop.
158       *
159       * @since 7.0
160       */
161      @Beta protected V standardRemove(@Nullable Object key) {
162        Iterator<Entry<K, V>> entryIterator = entrySet().iterator();
163        while (entryIterator.hasNext()) {
164          Entry<K, V> entry = entryIterator.next();
165          if (Objects.equal(entry.getKey(), key)) {
166            V value = entry.getValue();
167            entryIterator.remove();
168            return value;
169          }
170        }
171        return null;
172      }
173    
174      /**
175       * A sensible definition of {@link #clear} in terms of the {@code iterator}
176       * method of {@link #entrySet}. In many cases, you may wish to override
177       * {@link #clear} to forward to this implementation.
178       *
179       * @since 7.0
180       */
181      @Beta protected void standardClear() {
182        Iterator<Entry<K, V>> entryIterator = entrySet().iterator();
183        while (entryIterator.hasNext()) {
184          entryIterator.next();
185          entryIterator.remove();
186        }
187      }
188    
189      /**
190       * A sensible definition of {@link #keySet} in terms of the following methods:
191       * {@link #clear}, {@link #containsKey}, {@link #isEmpty}, {@link #remove},
192       * {@link #size}, and the {@code iterator} method of {@link #entrySet}. In
193       * many cases, you may wish to override {@link #keySet} to forward to this
194       * implementation.
195       *
196       * @since 7.0
197       * @deprecated Use the {@code StandardKeySet} constructor instead.  This
198       *             method will be removed from Guava in Guava release 11.0.
199       */
200      @Beta @Deprecated protected Set<K> standardKeySet() {
201        return new StandardKeySet();
202      }
203    
204      /**
205       * A sensible implementation of {@link Map#keySet} in terms of the following
206       * methods: {@link ForwardingMap#clear}, {@link ForwardingMap#containsKey},
207       * {@link ForwardingMap#isEmpty}, {@link ForwardingMap#remove}, {@link
208       * ForwardingMap#size}, and the {@link Set#iterator} method of {@link
209       * ForwardingMap#entrySet}. In many cases, you may wish to override {@link
210       * ForwardingMap#keySet} to forward to this implementation or a subclass
211       * thereof.
212       *
213       * @since 10.0
214       */
215      @Beta
216      protected class StandardKeySet extends Maps.KeySet<K, V> {
217        @Override
218        Map<K, V> map() {
219          return ForwardingMap.this;
220        }
221      }
222    
223      /**
224       * A sensible, albeit inefficient, definition of {@link #containsKey} in terms
225       * of the {@code iterator} method of {@link #entrySet}. If you override {@link
226       * #entrySet}, you may wish to override {@link #containsKey} to forward to
227       * this implementation.
228       *
229       * @since 7.0
230       */
231      @Beta protected boolean standardContainsKey(@Nullable Object key) {
232        return Maps.containsKeyImpl(this, key);
233      }
234    
235      /**
236       * A sensible definition of {@link #values} in terms of the following methods:
237       * {@link #clear}, {@link #containsValue}, {@link #isEmpty}, {@link #size},
238       * and the {@code iterator} method of {@link #entrySet}. In many cases, you
239       * may wish to override {@link #values} to forward to this implementation.
240       *
241       * @since 7.0
242       * @deprecated Use the {@code StandardValues} constructor instead.  This
243       *             method will be removed from Guava in Guava release 11.0.
244       */
245      @Beta @Deprecated protected Collection<V> standardValues() {
246        return new StandardValues();
247      }
248    
249      /**
250       * A sensible implementation of {@link Map#values} in terms of the following
251       * methods: {@link ForwardingMap#clear}, {@link ForwardingMap#containsValue},
252       * {@link ForwardingMap#isEmpty}, {@link ForwardingMap#size}, and the {@link
253       * Set#iterator} method of {@link ForwardingMap#entrySet}. In many cases, you
254       * may wish to override {@link ForwardingMap#values} to forward to this
255       * implementation or a subclass thereof.
256       *
257       * @since 10.0
258       */
259      @Beta
260      protected class StandardValues extends Maps.Values<K, V> {
261        @Override
262        Map<K, V> map() {
263          return ForwardingMap.this;
264        }
265      }
266    
267      /**
268       * A sensible definition of {@link #containsValue} in terms of the {@code
269       * iterator} method of {@link #entrySet}. If you override {@link #entrySet},
270       * you may wish to override {@link #containsValue} to forward to this
271       * implementation.
272       *
273       * @since 7.0
274       */
275      @Beta protected boolean standardContainsValue(@Nullable Object value) {
276        return Maps.containsValueImpl(this, value);
277      }
278    
279      /**
280       * A sensible definition of {@link #entrySet} in terms of the specified {@code
281       * Supplier}, which is used to generate iterators over the entry set, and in
282       * terms of the following methods: {@link #clear}, {@link #containsKey},
283       * {@link #get}, {@link #isEmpty}, {@link #remove}, and {@link #size}. In many
284       * cases, you may wish to override {@link #entrySet} to forward to this
285       * implementation.
286       *
287       * @param entryIteratorSupplier A creator for iterators over the entry set.
288       *        Each call to {@code get} must return an iterator that will
289       *        traverse the entire entry set.
290       *
291       * @since 7.0
292       * @deprecated Use {@code StandardEntrySet} instead.  This method will be
293       *             removed from Guava in Guava release 11.0.
294       */
295      @Deprecated @Beta
296       protected Set<Entry<K, V>> standardEntrySet(
297          final Supplier<Iterator<Entry<K, V>>> entryIteratorSupplier) {
298        return new StandardEntrySet() {
299          @Override public Iterator<Map.Entry<K, V>> iterator() {
300            return entryIteratorSupplier.get();
301          }
302        };
303      }
304    
305      /**
306       * A sensible implementation of {@link Map#entrySet} in terms of the following
307       * methods: {@link ForwardingMap#clear}, {@link ForwardingMap#containsKey},
308       * {@link ForwardingMap#get}, {@link ForwardingMap#isEmpty}, {@link
309       * ForwardingMap#remove}, and {@link ForwardingMap#size}. In many cases, you
310       * may wish to override {@link #entrySet} to forward to this implementation
311       * or a subclass thereof.
312       *
313       * @since 10.0
314       */
315      @Beta
316      protected abstract class StandardEntrySet extends Maps.EntrySet<K, V> {
317        @Override
318        Map<K, V> map() {
319          return ForwardingMap.this;
320        }
321      }
322    
323      /**
324       * A sensible definition of {@link #isEmpty} in terms of the {@code iterator}
325       * method of {@link #entrySet}. If you override {@link #entrySet}, you may
326       * wish to override {@link #isEmpty} to forward to this implementation.
327       *
328       * @since 7.0
329       */
330      @Beta protected boolean standardIsEmpty() {
331        return !entrySet().iterator().hasNext();
332      }
333    
334      /**
335       * A sensible definition of {@link #equals} in terms of the {@code equals}
336       * method of {@link #entrySet}. If you override {@link #entrySet}, you may
337       * wish to override {@link #equals} to forward to this implementation.
338       *
339       * @since 7.0
340       */
341      @Beta protected boolean standardEquals(@Nullable Object object) {
342        return Maps.equalsImpl(this, object);
343      }
344    
345      /**
346       * A sensible definition of {@link #hashCode} in terms of the {@code iterator}
347       * method of {@link #entrySet}. If you override {@link #entrySet}, you may
348       * wish to override {@link #hashCode} to forward to this implementation.
349       *
350       * @since 7.0
351       */
352      @Beta protected int standardHashCode() {
353        return Sets.hashCodeImpl(entrySet());
354      }
355    
356      /**
357       * A sensible definition of {@link #toString} in terms of the {@code iterator}
358       * method of {@link #entrySet}. If you override {@link #entrySet}, you may
359       * wish to override {@link #toString} to forward to this implementation.
360       *
361       * @since 7.0
362       */
363      @Beta protected String standardToString() {
364        return Maps.toStringImpl(this);
365      }
366    }