harpoon.Util.Collections
Class GenericMultiMap

java.lang.Object
  |
  +--harpoon.Util.Collections.GenericMultiMap
All Implemented Interfaces:
BinaryRelation, Map, MultiMap

public class GenericMultiMap
extends Object
implements MultiMap

GenericMultiMap is a default implementation of a MultiMap.

FSK: right now the implementation tries to preserve the property that if a key 'k' maps to an empty collection 'c' in some MultiMap 'mm', then users of 'mm' will not be able to see that 'k' is a member of the keySet for 'mm'. However, it does not preserve this property when mm.getValues(k) is used as a means to operate on the state of 'mm', and it is not clear to me whether one can even ensure that the property can be maintained if arbitrary operations on mm.getValues(k) are passed on to 'mm'.

Version:
$Id: GenericMultiMap.java,v 1.3 2002/02/26 22:47:36 cananian Exp $
Author:
Felix S. Klock II <pnkfelix@mit.edu>

Nested Class Summary
 
Nested classes inherited from class harpoon.Util.Collections.MultiMap
MultiMap.Factory
 
Nested classes inherited from class java.util.Map
Map.Entry
 
Constructor Summary
GenericMultiMap()
          Creates a MultiMap using a HashMap for the map and HashSets for the value collections.
GenericMultiMap(CollectionFactory cf)
          Creates a MultiMap using a HashMap for the map and the specified CollectionFactory to create the value collections.
GenericMultiMap(Map m)
          Makes a new MultiMap initialized with all of the Map.Entrys in m.
GenericMultiMap(MapFactory mf, CollectionFactory cf)
          Creates a MultiMap using the specified MapFactory to create the map and the specified CollectionFactory to create the value collections.
 
Method Summary
 boolean add(Object key, Object value)
          Ensures that this contains an association from key to value.
 boolean addAll(MultiMap mm)
          Add all mappings in the given multimap to this multimap.
 boolean addAll(Object key, Collection values)
          Adds to the current mappings: associations for key to each value in values.
 void clear()
           
 boolean contains(Object a, Object b)
          Returns true if a has a mapping to b in this.
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
 Set entrySet()
          Returns a set view of the mappings contained in this map.
 boolean equals(Object o)
           
 Object get(Object key)
          Returns some arbitrary value from the set of values to which this map maps the specified key.
 Collection getValues(Object key)
          Returns the collection of Values associated with key.
 int hashCode()
           
 boolean isEmpty()
           
 Set keySet()
          Returns a set view of the keys in this map.
 Object put(Object key, Object value)
          Associates the specified value with the specified key in this map.
 void putAll(Map t)
          Copies the mappings from the specified map to this map.
 Object remove(Object key)
          Removes all mappings for this key from this map if present.
 boolean remove(Object key, Object value)
          Removes a mapping from key to value from this map if present.
 boolean removeAll(Object key, Collection values)
          Removes from the current mappings: associations for key to any value in values.
 boolean retainAll(Object key, Collection values)
          Removes from the current mappings: associations for key to any value not in values.
 int size()
          Returns the number of key-value mappings in this map (keys which map to multiple values count multiple times).
 String toString()
           
 Collection values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GenericMultiMap

public GenericMultiMap()
Creates a MultiMap using a HashMap for the map and HashSets for the value collections. To gain more control over the specific sets/map used in internal representation of this, use the more specific constructor that takes CollectionFactorys.


GenericMultiMap

public GenericMultiMap(CollectionFactory cf)
Creates a MultiMap using a HashMap for the map and the specified CollectionFactory to create the value collections.


GenericMultiMap

public GenericMultiMap(MapFactory mf,
                       CollectionFactory cf)
Creates a MultiMap using the specified MapFactory to create the map and the specified CollectionFactory to create the value collections.


GenericMultiMap

public GenericMultiMap(Map m)
Makes a new MultiMap initialized with all of the Map.Entrys in m.

Method Detail

size

public int size()
Description copied from interface: MultiMap
Returns the number of key-value mappings in this map (keys which map to multiple values count multiple times).

Specified by:
size in interface MultiMap

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map

get

public Object get(Object key)
Returns some arbitrary value from the set of values to which this map maps the specified key. Returns null if the map contains no mapping for the key; it's also possible that the map explicitly maps the key to null. The containsKey operation may be used to distinquish these two cases. Note that if only the put method is used to modify this, then get will operate just as it would in any other Map.

Specified by:
get in interface MultiMap

put

public Object put(Object key,
                  Object value)
Associates the specified value with the specified key in this map. If the map previously contained any mappings for this key, all of the old values are replaced. Returns some value that was previous associated with the specified key, or null if no values were associated previously.

Specified by:
put in interface MultiMap

remove

public Object remove(Object key)
Removes all mappings for this key from this map if present. Returns some previous value associated with specified key, or null if there was no mapping for key.

Specified by:
remove in interface MultiMap
Returns:
one of the previous values associated with the key, or null if Map associated no values with the key. Note that a zero-sized collection is not returned in the latter case, and that a null return value may be ambiguous if the map associated null with the given key (in addition to possibly other values).

remove

public boolean remove(Object key,
                      Object value)
Removes a mapping from key to value from this map if present. Note that if multiple mappings from key to value are permitted by this map, then only one is guaranteed to be removed. Returns true if this was modified as a result of this operation, else returns false.

Specified by:
remove in interface MultiMap

putAll

public void putAll(Map t)
Copies the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map. Note that putAll(mm) where mm is a MultiMap will NOT add all of the mappings in mm; it will only add all of the Keys in mm, mapping each Key to one of the Values it mapped to in mm. To add all of the mappings from another MultiMap, use addAll(MultiMap).

Specified by:
putAll in interface MultiMap

clear

public void clear()
Specified by:
clear in interface Map

equals

public boolean equals(Object o)
Specified by:
equals in interface Map
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Map
Overrides:
hashCode in class Object

add

public boolean add(Object key,
                   Object value)
Ensures that this contains an association from key to value. (MultiMap specific operation).

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

addAll

public boolean addAll(Object key,
                      Collection values)
Adds to the current mappings: associations for key to each value in values. (MultiMap specific operation).

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

addAll

public boolean addAll(MultiMap mm)
Add all mappings in the given multimap to this multimap.

Specified by:
addAll in interface MultiMap

retainAll

public boolean retainAll(Object key,
                         Collection values)
Removes from the current mappings: associations for key to any value not in values. (MultiMap specific operation).

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

removeAll

public boolean removeAll(Object key,
                         Collection values)
Removes from the current mappings: associations for key to any value in values. (MultiMap specific operation).

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

getValues

public Collection getValues(Object key)
Returns the collection of Values associated with key. Modifications to the returned Collection affect this as well. If there are no Values currently associated with key, constructs a new, mutable, empty Collection and returns it. (MultiMap specific operation).

Specified by:
getValues in interface MultiMap

contains

public boolean contains(Object a,
                        Object b)
Returns true if a has a mapping to b in this. (MultiMap specific operation).

Specified by:
contains in interface MultiMap

toString

public String toString()
Overrides:
toString in class Object

keySet

public Set keySet()
Returns a set view of the keys in this map.

Specified by:
keySet in interface Map

values

public Collection values()
Returns a collection view of the values contained in this map.

Specified by:
values in interface Map

entrySet

public Set entrySet()
Returns a set view of the mappings contained in this map. This view is fully modifiable; the elements are Map.Entrys. The returned set is actually a MultiMapSet, from which you can get back the original MultiMap.

Specified by:
entrySet in interface Map