ptolemy.util
Class MultiHashMap

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by ptolemy.util.MultiHashMap
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map, MultiMap

public class MultiHashMap
extends java.util.HashMap
implements MultiMap

MultiHashMap is an implementation of the MultiMap interface. It associates a collection of objects to each key. Putting a new object under a key adds to the associated collection. Likewise, removing a object removes from the collection. It is possible that the given object to remove is not contained by the collection. In which case, no changes is made and null is returned. The items in each collection are ordered by insertion, and duplicates are stored in the collections. For example, given a key K and object O1, and O2: MultiHashMap map = new MultiHashMap(); map.put(K, O1); map.put(K, O1); map.put(K, O2); then, map.size(K) would return 3. Iterating through the map returns O1, O1, and O2 in order.

Since:
Ptolemy II 8.0
Version:
$Id: MultiHashMap.java 57053 2010-01-28 02:15:43Z cxh $
Author:
Man-Kit Leung
See Also:
Serialized Form
Accepted Rating:
Red (mankit)
Proposed Rating:
Red (mankit)

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
MultiHashMap()
           
 
Method Summary
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Add the value to the collection associated with the specified key.
 java.lang.Object remove(java.lang.Object key, java.lang.Object value)
          Remove a specified value from the map.
 int size(java.lang.Object key)
          Return the size of the collection mapped to the specified key.
 java.util.Collection values()
          Return a view of the collection containing all values in the map.
 
Methods inherited from class java.util.HashMap
clear, clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, putAll, remove, size
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, putAll, remove, size
 

Constructor Detail

MultiHashMap

public MultiHashMap()
Method Detail

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Add the value to the collection associated with the specified key.

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.HashMap
Parameters:
key - The specified key.
value - The specified value to add to the collection.
Returns:
The value added.

remove

public java.lang.Object remove(java.lang.Object key,
                               java.lang.Object value)
Remove a specified value from the map. The value is removed from the collection mapped to the specified key. If this is the last value removed from the given key, the specified key is also removed from the map. Subsequent call to get(key) will return null.

Specified by:
remove in interface MultiMap
Parameters:
key - The specified key to remove the value from.
value - The specified value to remove.
Returns:
The value removed, or null if nothing is removed.

size

public int size(java.lang.Object key)
Return the size of the collection mapped to the specified key.

Parameters:
key - The specified key.
Returns:
The size of the collection, or zero if key is not in the map.

values

public java.util.Collection values()
Return a view of the collection containing all values in the map. This is a collection containing the union of each collection mapped to the keys.

Specified by:
values in interface java.util.Map
Overrides:
values in class java.util.HashMap
Returns:
A view of all values contained in this map.