com.google.common.collect
Class ForwardingMap<K,V>

java.lang.Object
  extended by com.google.common.collect.ForwardingObject
      extended by com.google.common.collect.ForwardingMap<K,V>
All Implemented Interfaces:
Map<K,V>
Direct Known Subclasses:
EnumBiMap, EnumHashBiMap, ForwardingConcurrentMap, ForwardingSortedMap, HashBiMap, ImmutableClassToInstanceMap, MutableClassToInstanceMap

@GwtCompatible
public abstract class ForwardingMap<K,V>
extends ForwardingObject
implements Map<K,V>

A map which forwards all its method calls to another map. Subclasses should override one or more methods to modify the behavior of the backing map as desired per the decorator pattern.

Warning: The methods of ForwardingMap forward indiscriminately to the methods of the delegate. For example, overriding put(K, V) alone will not change the behavior of putAll(java.util.Map), which can lead to unexpected behavior. In this case, you should override putAll as well, either providing your own implementation, or delegating to the provided standardPutAll method.

Each of the standard methods, where appropriate, use Objects.equal(java.lang.Object, java.lang.Object) to test equality for both keys and values. This may not be the desired behavior for map implementations that use non-standard notions of key equality, such as a SortedMap whose comparator is not consistent with equals.

The standard methods and the collection views they return are not guaranteed to be thread-safe, even when all of the methods that they depend on are thread-safe.

Since:
2.0 (imported from Google Collections Library)
Author:
Kevin Bourrillion, Jared Levy, Louis Wasserman

Nested Class Summary
protected  class ForwardingMap.StandardEntrySet
          A sensible implementation of Map.entrySet() in terms of the following methods: clear(), containsKey(java.lang.Object), get(java.lang.Object), isEmpty(), remove(java.lang.Object), and size().
protected  class ForwardingMap.StandardKeySet
          A sensible implementation of Map.keySet() in terms of the following methods: clear(), containsKey(java.lang.Object), isEmpty(), remove(java.lang.Object), size(), and the Set.iterator() method of entrySet().
protected  class ForwardingMap.StandardValues
          A sensible implementation of Map.values() in terms of the following methods: clear(), containsValue(java.lang.Object), isEmpty(), size(), and the Set.iterator() method of entrySet().
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
protected ForwardingMap()
          Constructor for use by subclasses.
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
protected abstract  Map<K,V> delegate()
          Returns the backing delegate instance that methods are forwarded to.
 Set<Map.Entry<K,V>> entrySet()
           
 boolean equals(Object object)
           
 V get(Object key)
           
 int hashCode()
           
 boolean isEmpty()
           
 Set<K> keySet()
           
 V put(K key, V value)
           
 void putAll(Map<? extends K,? extends V> map)
           
 V remove(Object object)
           
 int size()
           
protected  void standardClear()
          A sensible definition of clear() in terms of the iterator method of entrySet().
protected  boolean standardContainsKey(Object key)
          A sensible, albeit inefficient, definition of containsKey(java.lang.Object) in terms of the iterator method of entrySet().
protected  boolean standardContainsValue(Object value)
          A sensible definition of containsValue(java.lang.Object) in terms of the iterator method of entrySet().
protected  Set<Map.Entry<K,V>> standardEntrySet(Supplier<Iterator<Map.Entry<K,V>>> entryIteratorSupplier)
          Deprecated. Use StandardEntrySet instead. This method will be removed from Guava in Guava release 11.0.
protected  boolean standardEquals(Object object)
          A sensible definition of equals(java.lang.Object) in terms of the equals method of entrySet().
protected  int standardHashCode()
          A sensible definition of hashCode() in terms of the iterator method of entrySet().
protected  boolean standardIsEmpty()
          A sensible definition of isEmpty() in terms of the iterator method of entrySet().
protected  Set<K> standardKeySet()
          Deprecated. Use the StandardKeySet constructor instead. This method will be removed from Guava in Guava release 11.0.
protected  void standardPutAll(Map<? extends K,? extends V> map)
          A sensible definition of putAll(Map) in terms of put(Object, Object).
protected  V standardRemove(Object key)
          A sensible, albeit inefficient, definition of remove(java.lang.Object) in terms of the iterator method of entrySet().
protected  String standardToString()
          A sensible definition of ForwardingObject.toString() in terms of the iterator method of entrySet().
protected  Collection<V> standardValues()
          Deprecated. Use the StandardValues constructor instead. This method will be removed from Guava in Guava release 11.0.
 Collection<V> values()
           
 
Methods inherited from class com.google.common.collect.ForwardingObject
toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ForwardingMap

protected ForwardingMap()
Constructor for use by subclasses.

Method Detail

delegate

protected abstract Map<K,V> delegate()
Description copied from class: ForwardingObject
Returns the backing delegate instance that methods are forwarded to. Abstract subclasses generally override this method with an abstract method that has a more specific return type, such as ForwardingSet.delegate(). Concrete subclasses override this method to supply the instance being decorated.

Specified by:
delegate in class ForwardingObject

size

public int size()
Specified by:
size in interface Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

remove

public V remove(Object object)
Specified by:
remove in interface Map<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>

get

public V get(Object key)
Specified by:
get in interface Map<K,V>

put

public V put(K key,
             V value)
Specified by:
put in interface Map<K,V>

putAll

public void putAll(Map<? extends K,? extends V> map)
Specified by:
putAll in interface Map<K,V>

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,V>

values

public Collection<V> values()
Specified by:
values in interface Map<K,V>

entrySet

public Set<Map.Entry<K,V>> entrySet()
Specified by:
entrySet in interface Map<K,V>

equals

public boolean equals(@Nullable
                      Object object)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object

standardPutAll

@Beta
protected void standardPutAll(Map<? extends K,? extends V> map)
A sensible definition of putAll(Map) in terms of put(Object, Object). If you override put(Object, Object), you may wish to override putAll(Map) to forward to this implementation.

Since:
7.0

standardRemove

@Beta
protected V standardRemove(@Nullable
                                Object key)
A sensible, albeit inefficient, definition of remove(java.lang.Object) in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override remove(java.lang.Object) to forward to this implementation.

Alternately, you may wish to override remove(java.lang.Object) with keySet().remove, assuming that approach would not lead to an infinite loop.

Since:
7.0

standardClear

@Beta
protected void standardClear()
A sensible definition of clear() in terms of the iterator method of entrySet(). In many cases, you may wish to override clear() to forward to this implementation.

Since:
7.0

standardKeySet

@Beta
@Deprecated
protected Set<K> standardKeySet()
Deprecated. Use the StandardKeySet constructor instead. This method will be removed from Guava in Guava release 11.0.

A sensible definition of keySet() in terms of the following methods: clear(), containsKey(java.lang.Object), isEmpty(), remove(java.lang.Object), size(), and the iterator method of entrySet(). In many cases, you may wish to override keySet() to forward to this implementation.

Since:
7.0

standardContainsKey

@Beta
protected boolean standardContainsKey(@Nullable
                                           Object key)
A sensible, albeit inefficient, definition of containsKey(java.lang.Object) in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override containsKey(java.lang.Object) to forward to this implementation.

Since:
7.0

standardValues

@Beta
@Deprecated
protected Collection<V> standardValues()
Deprecated. Use the StandardValues constructor instead. This method will be removed from Guava in Guava release 11.0.

A sensible definition of values() in terms of the following methods: clear(), containsValue(java.lang.Object), isEmpty(), size(), and the iterator method of entrySet(). In many cases, you may wish to override values() to forward to this implementation.

Since:
7.0

standardContainsValue

@Beta
protected boolean standardContainsValue(@Nullable
                                             Object value)
A sensible definition of containsValue(java.lang.Object) in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override containsValue(java.lang.Object) to forward to this implementation.

Since:
7.0

standardEntrySet

@Deprecated
@Beta
protected Set<Map.Entry<K,V>> standardEntrySet(Supplier<Iterator<Map.Entry<K,V>>> entryIteratorSupplier)
Deprecated. Use StandardEntrySet instead. This method will be removed from Guava in Guava release 11.0.

A sensible definition of entrySet() in terms of the specified Supplier, which is used to generate iterators over the entry set, and in terms of the following methods: clear(), containsKey(java.lang.Object), get(java.lang.Object), isEmpty(), remove(java.lang.Object), and size(). In many cases, you may wish to override entrySet() to forward to this implementation.

Parameters:
entryIteratorSupplier - A creator for iterators over the entry set. Each call to get must return an iterator that will traverse the entire entry set.
Since:
7.0

standardIsEmpty

@Beta
protected boolean standardIsEmpty()
A sensible definition of isEmpty() in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override isEmpty() to forward to this implementation.

Since:
7.0

standardEquals

@Beta
protected boolean standardEquals(@Nullable
                                      Object object)
A sensible definition of equals(java.lang.Object) in terms of the equals method of entrySet(). If you override entrySet(), you may wish to override equals(java.lang.Object) to forward to this implementation.

Since:
7.0

standardHashCode

@Beta
protected int standardHashCode()
A sensible definition of hashCode() in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override hashCode() to forward to this implementation.

Since:
7.0

standardToString

@Beta
protected String standardToString()
A sensible definition of ForwardingObject.toString() in terms of the iterator method of entrySet(). If you override entrySet(), you may wish to override ForwardingObject.toString() to forward to this implementation.

Since:
7.0


Copyright © 2010-2011. All Rights Reserved.