common
Class HashMultiMap

java.lang.Object
  extended by java.util.AbstractMap
      extended by common.HashMultiMap
All Implemented Interfaces:
MultiMap, java.lang.Cloneable, java.util.Map
Direct Known Subclasses:
IndexedHashMultiMap

public class HashMultiMap
extends java.util.AbstractMap
implements MultiMap, java.lang.Cloneable

Implementation of the MultiMap interface that uses a HashMap for the map and HashSets for automatically created sets.

Technically, a MultiMap implementation should put a wrapper around the iterators for the underlying data structure so the values associated with keys are unmodifiable sets. Also, its Map.Entry objects should have a setValue method that removes the key when the value is set to an empty set. For now, we don't bother with these issues.


Nested Class Summary
protected  class HashMultiMap.ValueSet
           
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface common.MultiMap
MultiMap.EmptyMultiMap
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
protected  boolean maintainOrder
           
protected  java.util.HashMap map
           
 
Fields inherited from interface common.MultiMap
EMPTY_MULTI_MAP
 
Constructor Summary
HashMultiMap()
          Creates a new, empty HashMultiMap.
HashMultiMap(boolean maintainOrder)
          Creates a new, empty HashMultiMap.
HashMultiMap(MultiMap orig)
          Creates a new HashMultiMap that is equal to the given MultiMap.
 
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.
 void clear()
           
 java.lang.Object clone()
          Returns a shallow copy of this multi-map.
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
protected  java.util.Set copySet(java.util.Set orig)
           
protected  java.util.Set emptySet()
           
 java.util.Set entrySet()
           
 boolean equals(java.lang.Object o)
           
 java.lang.Object get(java.lang.Object key)
          Returns the set associated with the given key.
 int hashCode()
           
 java.util.Set keySet()
           
protected  java.util.Set newSet()
           
 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.
 java.lang.Object remove(java.lang.Object key)
           
 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.
 int size()
           
 
Methods inherited from class java.util.AbstractMap
isEmpty, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
isEmpty, putAll, values
 

Field Detail

map

protected java.util.HashMap map

maintainOrder

protected boolean maintainOrder
Constructor Detail

HashMultiMap

public HashMultiMap()
Creates a new, empty HashMultiMap.


HashMultiMap

public HashMultiMap(boolean maintainOrder)
Creates a new, empty HashMultiMap. If maintainOrder is true, then this instance uses a LinkedHashMap and LinkedHashSets so that the iteration orders for keys, and for elements of value sets, are predictable.


HashMultiMap

public HashMultiMap(MultiMap orig)
Creates a new HashMultiMap that is equal to the given MultiMap.

Method Detail

size

public int size()
Specified by:
size in interface java.util.Map
Overrides:
size in class java.util.AbstractMap

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class java.util.AbstractMap

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map
Overrides:
containsValue in class java.util.AbstractMap

get

public java.lang.Object get(java.lang.Object key)
Description copied from interface: MultiMap
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 MultiMap
Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Description copied from interface: MultiMap
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 MultiMap
Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.AbstractMap
Returns:
the set previously associated with this key, or an empty set if the key was not in the map

add

public boolean add(java.lang.Object key,
                   java.lang.Object value)
Description copied from interface: MultiMap
Adds the given value to the set associated with the given key. If the key is not yet in the map, it is added.

Specified by:
add in interface MultiMap
Returns:
true if the MultiMap changed as a result of this call

addAll

public boolean addAll(java.lang.Object key,
                      java.util.Set values)
Description copied from interface: MultiMap
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.

Specified by:
addAll in interface MultiMap
Returns:
true if the MultiMap changed as a result of this call

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.AbstractMap

remove

public boolean remove(java.lang.Object key,
                      java.lang.Object value)
Description copied from interface: MultiMap
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.

Specified by:
remove in interface MultiMap
Returns:
true if the MultiMap changed as a result of this call

removeAll

public boolean removeAll(java.lang.Object key,
                         java.util.Set values)
Description copied from interface: MultiMap
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.

Specified by:
removeAll in interface MultiMap
Returns:
true if the MultiMap changed as a result of this call

clear

public void clear()
Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.AbstractMap

entrySet

public java.util.Set entrySet()
Specified by:
entrySet in interface java.util.Map
Specified by:
entrySet in class java.util.AbstractMap

keySet

public java.util.Set keySet()
Specified by:
keySet in interface java.util.Map
Overrides:
keySet in class java.util.AbstractMap

equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface java.util.Map
Overrides:
equals in class java.util.AbstractMap

hashCode

public int hashCode()
Specified by:
hashCode in interface java.util.Map
Overrides:
hashCode in class java.util.AbstractMap

clone

public java.lang.Object clone()
Returns a shallow copy of this multi-map. he copy is "shallow" in that it does not create new copies of the keys or elements; however, it does make copies of the Set objects used to store elements in the map.

Overrides:
clone in class java.util.AbstractMap

emptySet

protected java.util.Set emptySet()

newSet

protected java.util.Set newSet()

copySet

protected java.util.Set copySet(java.util.Set orig)