common
Interface MultiMap

All Superinterfaces:
java.util.Map
All Known Subinterfaces:
IndexedMultiMap, MultiMapDiff
All Known Implementing Classes:
HashMultiMap, HashMultiMapDiff, IndexedHashMultiMap, IndexedMultiMap.EmptyIndexedMultiMap, IndexedMultiMapDiff, MultiMap.EmptyMultiMap

public interface MultiMap
extends java.util.Map

A map where keys are mapped to non-empty Set objects. Has the same behavior as the standard Map interface, except that the value passed to put must be a Set; get returns an empty set instead of null when the given key is not in the map; and there are new add and remove methods.


Nested Class Summary
static class MultiMap.EmptyMultiMap
           
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
static MultiMap EMPTY_MULTI_MAP
          An unmodifiable multi-map that maps all keys to the empty set.
 
Method Summary
 boolean add(java.lang.Object key, java.lang.Object value)
          Adds the given value to the set associated with the given key.
 boolean addAll(java.lang.Object key, java.util.Set values)
          Adds all elements of the given set to the set associated with the given key.
 java.lang.Object get(java.lang.Object key)
          Returns the set associated with the given key.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates the given key with the given value, which must be a Set.
 boolean remove(java.lang.Object key, java.lang.Object value)
          Removes the given value from the set associated with the given key.
 boolean removeAll(java.lang.Object key, java.util.Set values)
          Removes all elements of the given set from the set associated with the given key.
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, hashCode, isEmpty, keySet, putAll, remove, size, values
 

Field Detail

EMPTY_MULTI_MAP

static final MultiMap EMPTY_MULTI_MAP
An unmodifiable multi-map that maps all keys to the empty set.

Method Detail

get

java.lang.Object get(java.lang.Object key)
Returns the set associated with the given key. If the key is not in the map, returns an empty set. The set returned is modifiable and backed by this multi-map: if values are added for the given key, they will show up in the returned set. However, the returned set may lose its connection to this multi-map if the multi-map's put method is called or if all the values for the given key are removed.

Specified by:
get in interface java.util.Map

put

java.lang.Object put(java.lang.Object key,
                     java.lang.Object value)
Associates the given key with the given value, which must be a Set. If the given set is empty, the key is removed from the map.

Specified by:
put in interface java.util.Map
Returns:
the set previously associated with this key, or an empty set if the key was not in the map
Throws:
java.lang.IllegalArgumentException - if value is not a Set

add

boolean add(java.lang.Object key,
            java.lang.Object value)
Adds the given value to the set associated with the given key. If the key is not yet in the map, it is added.

Returns:
true if the MultiMap changed as a result of this call

addAll

boolean addAll(java.lang.Object key,
               java.util.Set values)
Adds all elements of the given set to the set associated with the given key. If the key is not yet in the map, it is added.

Returns:
true if the MultiMap changed as a result of this call

remove

boolean remove(java.lang.Object key,
               java.lang.Object value)
Removes the given value from the set associated with the given key. Does nothing if the value is not in that set. If the set ends up being empty, then the key is removed from the map.

Returns:
true if the MultiMap changed as a result of this call

removeAll

boolean removeAll(java.lang.Object key,
                  java.util.Set values)
Removes all elements of the given set from the set associated with the given key. If the associated set ends up being empty, then the key is removed from the map.

Returns:
true if the MultiMap changed as a result of this call