org.apache.muse.util
Class MultiMap

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap
          extended by org.apache.muse.util.MultiMap
All Implemented Interfaces:
Serializable, Cloneable, Map

public final class MultiMap
extends HashMap

MultiMap is a Java version of the C++ STL class std::multimap. It allows users to associate more than one value with a unique key. Each key points to a collection of values that can be traversed independently of the map. The integrity of the map is such that it only protects the key values, not the values they point to.

All entries into the multimap are (Object, java.util.Collection), where each key object must be unique.

Author:
Dan Jemiolo (danj)
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
MultiMap()
          The default constructor creates a new map using a HashSet as the value collection type.
MultiMap(Class collectionClass)
           
 
Method Summary
 void clear()
           
 boolean containsValue(Object value)
           
 int keySetSize()
           
 Object put(Object key, Object value)
          Associates the value with the given key.
 void putAll(Map map)
          Adds all of the entries in a generic map to the multimap.
 Object remove(Object key)
          Removes all values associated with the given key.
 int size()
          NOTE: This method takes O(n) time, where n is the number of keys in the map.
 
Methods inherited from class java.util.HashMap
clone, containsKey, entrySet, get, isEmpty, keySet, values
 
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
equals, hashCode
 

Constructor Detail

MultiMap

public MultiMap()
The default constructor creates a new map using a HashSet as the value collection type.

See Also:
MultiMap(Class)

MultiMap

public MultiMap(Class collectionClass)
Parameters:
collectionClass - The Collection type to use for the map's values. The Class must be an implementation of java.util.Collection.
Method Detail

clear

public final void clear()
Specified by:
clear in interface Map
Overrides:
clear in class HashMap

containsValue

public final boolean containsValue(Object value)
Specified by:
containsValue in interface Map
Overrides:
containsValue in class HashMap
Parameters:
value -
Returns:
True if one or more of the keys in this map points to the given value.

keySetSize

public final int keySetSize()
Returns:
The number of keys in the map, not the number of entries.
See Also:
size()

put

public final Object put(Object key,
                        Object value)
Associates the value with the given key. If the key is not already in the map, it is added; otherwise, the value is simply added to the key's collection.

Specified by:
put in interface Map
Overrides:
put in class HashMap
Returns:
The value reference on success or null if the value was already associated with the key.

putAll

public final void putAll(Map map)
Adds all of the entries in a generic map to the multimap.

NOTE: Because we cannot guarantee that the implementation of the base class putAll(Map) simply calls the put method in a loop, we must override it here to ensure this happens. Otherwise, HashMap might associate the values directly with the keys, breaking the unofficial key -> Collection system.

Specified by:
putAll in interface Map
Overrides:
putAll in class HashMap
Parameters:
map - The Map to copy - this does not have to be another MultiMap.

remove

public final Object remove(Object key)
Removes all values associated with the given key.

Specified by:
remove in interface Map
Overrides:
remove in class HashMap
Parameters:
key -
Returns:
The Collection of values previously associated with the key.

size

public final int size()
NOTE: This method takes O(n) time, where n is the number of keys in the map. It would be more efficient to keep a counter for the size, but this would require overriding more methods and dealing with the complicated issue of map integrity and entrySet(). This implementation is the most robust when you consider that all Maps allow users to modify their contents without using the interface directly.

Specified by:
size in interface Map
Overrides:
size in class HashMap
Returns:
The sum of the sizes of all the map entries (value collections).


Copyright © 2005-2011 Apache Web Services - Muse. All Rights Reserved.