edu.stanford.nlp.util
Class CollectionValuedMap<K,V>

java.lang.Object
  extended by edu.stanford.nlp.util.CollectionValuedMap<K,V>
Type Parameters:
K - Key type of map
V - Type of the Collection that is the Map's value
All Implemented Interfaces:
java.io.Serializable, java.util.Map<K,java.util.Collection<V>>
Direct Known Subclasses:
DeltaCollectionValuedMap

public class CollectionValuedMap<K,V>
extends java.lang.Object
implements java.util.Map<K,java.util.Collection<V>>, java.io.Serializable

Map from keys to Collections. Important methods are the add(K, V) and remove(java.lang.Object) methods for adding and removing a value to/from the Collection associated with the key, and the get(java.lang.Object) method for getting the Collection associated with a key. The class is quite general, because on construction, it is possible to pass a MapFactory which will be used to create the underlying map and a CollectionFactory which will be used to create the Collections. Thus this class can be configured to act like a "HashSetValuedMap" or a "ListValuedMap", or even a "HashSetValuedIdentityHashMap". The possibilities are endless!

Author:
Teg Grenager (grenager@cs.stanford.edu), Sarah Spikes (sdspikes@cs.stanford.edu) - cleanup and filling in types
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Field Summary
protected  CollectionFactory<V> cf
           
protected  MapFactory<K,java.util.Collection<V>> mf
           
 
Constructor Summary
CollectionValuedMap()
          Creates a new empty CollectionValuedMap which uses a HashMap as the underlying Map, and HashSets as the Collections in each mapping.
CollectionValuedMap(CollectionFactory<V> cf)
          Creates a new empty CollectionValuedMap which uses a HashMap as the underlying Map.
CollectionValuedMap(CollectionValuedMap<K,V> cvm)
          Creates a new CollectionValuedMap with all of the mappings from cvm.
CollectionValuedMap(MapFactory<K,java.util.Collection<V>> mf, CollectionFactory<V> cf, boolean treatCollectionsAsImmutable)
          Creates a new empty CollectionValuedMap.
 
Method Summary
 void add(K key, V value)
          Adds the value to the Collection mapped to by the key.
 void addAll(java.util.Map<K,V> m)
          Adds all of the mappings in m to this CollectionValuedMap.
 java.util.Collection<V> allValues()
           
 void clear()
          Clears this Map.
 CollectionValuedMap<K,V> clone()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
          Unsupported.
 CollectionValuedMap<K,V> deltaClone()
          Creates a "delta clone" of this Map, where only the differences are represented.
 java.util.Set<java.util.Map.Entry<K,java.util.Collection<V>>> entrySet()
          Each element of the Set is a Map.Entry object, where getKey() returns the key of the mapping, and getValue() returns the Collection mapped to by the key.
 boolean equals(java.lang.Object o)
           
 java.util.Collection<V> get(java.lang.Object key)
           
 int hashCode()
           
 boolean isEmpty()
           
 java.util.Set<K> keySet()
           
static void main(java.lang.String[] args)
          For testing only.
 java.util.Collection<V> put(K key, java.util.Collection<V> collection)
          Replaces current Collection mapped to key with the specified Collection.
 void putAll(java.util.Map<? extends K,? extends java.util.Collection<V>> m)
          Unsupported.
 java.util.Collection<V> remove(java.lang.Object key)
          Removes the mapping associated with this key from this Map.
 void removeMapping(K key, V value)
          Removes the value from the Collection mapped to by this key, leaving the rest of the collection intact.
 int size()
          The number of keys in this map.
 java.lang.String toString()
           
 java.util.Collection<java.util.Collection<V>> values()
           
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

cf

protected CollectionFactory<V> cf

mf

protected MapFactory<K,java.util.Collection<V>> mf
Constructor Detail

CollectionValuedMap

public CollectionValuedMap(MapFactory<K,java.util.Collection<V>> mf,
                           CollectionFactory<V> cf,
                           boolean treatCollectionsAsImmutable)
Creates a new empty CollectionValuedMap.

Parameters:
mf - a MapFactory which will be used to generate the underlying Map
cf - a CollectionFactory which will be used to generate the Collections in each mapping
treatCollectionsAsImmutable - if true, forces this Map to create new a Collection everytime a new value is added to or deleted from the Collection a mapping.

CollectionValuedMap

public CollectionValuedMap(CollectionValuedMap<K,V> cvm)
Creates a new CollectionValuedMap with all of the mappings from cvm. Same as clone().


CollectionValuedMap

public CollectionValuedMap()
Creates a new empty CollectionValuedMap which uses a HashMap as the underlying Map, and HashSets as the Collections in each mapping. Does not treat Collections as immutable.


CollectionValuedMap

public CollectionValuedMap(CollectionFactory<V> cf)
Creates a new empty CollectionValuedMap which uses a HashMap as the underlying Map. Does not treat Collections as immutable.

Parameters:
cf - a CollectionFactory which will be used to generate the Collections in each mapping
Method Detail

put

public java.util.Collection<V> put(K key,
                                   java.util.Collection<V> collection)
Replaces current Collection mapped to key with the specified Collection. Use carefully!

Specified by:
put in interface java.util.Map<K,java.util.Collection<V>>

putAll

public void putAll(java.util.Map<? extends K,? extends java.util.Collection<V>> m)
Unsupported. Use addAll(Map) instead.

Specified by:
putAll in interface java.util.Map<K,java.util.Collection<V>>

get

public java.util.Collection<V> get(java.lang.Object key)
Specified by:
get in interface java.util.Map<K,java.util.Collection<V>>
Returns:
the Collection mapped to by key, never null, but may be empty.

add

public void add(K key,
                V value)
Adds the value to the Collection mapped to by the key.


addAll

public void addAll(java.util.Map<K,V> m)
Adds all of the mappings in m to this CollectionValuedMap. If m is a CollectionValuedMap, it will behave strangely. Use the constructor instead.


remove

public java.util.Collection<V> remove(java.lang.Object key)
Removes the mapping associated with this key from this Map.

Specified by:
remove in interface java.util.Map<K,java.util.Collection<V>>
Returns:
the Collection mapped to by this key.

removeMapping

public void removeMapping(K key,
                          V value)
Removes the value from the Collection mapped to by this key, leaving the rest of the collection intact.

Parameters:
key - the key to the Collection to remove the value from
value - the value to remove

clear

public void clear()
Clears this Map.

Specified by:
clear in interface java.util.Map<K,java.util.Collection<V>>

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map<K,java.util.Collection<V>>
Returns:
true iff this key is in this map

containsValue

public boolean containsValue(java.lang.Object value)
Unsupported.

Specified by:
containsValue in interface java.util.Map<K,java.util.Collection<V>>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map<K,java.util.Collection<V>>
Returns:
true iff this Map has no mappings in it.

entrySet

public java.util.Set<java.util.Map.Entry<K,java.util.Collection<V>>> entrySet()
Each element of the Set is a Map.Entry object, where getKey() returns the key of the mapping, and getValue() returns the Collection mapped to by the key.

Specified by:
entrySet in interface java.util.Map<K,java.util.Collection<V>>
Returns:
a Set view of the mappings contained in this map.

keySet

public java.util.Set<K> keySet()
Specified by:
keySet in interface java.util.Map<K,java.util.Collection<V>>
Returns:
a Set view of the keys in this Map.

size

public int size()
The number of keys in this map.

Specified by:
size in interface java.util.Map<K,java.util.Collection<V>>

values

public java.util.Collection<java.util.Collection<V>> values()
Specified by:
values in interface java.util.Map<K,java.util.Collection<V>>
Returns:
a collection of the values (really, a collection of values) in this Map

allValues

public java.util.Collection<V> allValues()

equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface java.util.Map<K,java.util.Collection<V>>
Overrides:
equals in class java.lang.Object
Returns:
true iff o is a CollectionValuedMap, and each key maps to the a Collection of the same objects in o as it does in this CollectionValuedMap.

hashCode

public int hashCode()
Specified by:
hashCode in interface java.util.Map<K,java.util.Collection<V>>
Overrides:
hashCode in class java.lang.Object
Returns:
the hashcode of the underlying Map

deltaClone

public CollectionValuedMap<K,V> deltaClone()
Creates a "delta clone" of this Map, where only the differences are represented.


clone

public CollectionValuedMap<K,V> clone()
Overrides:
clone in class java.lang.Object
Returns:
a clone of this Map

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
A String representation of this CollectionValuedMap, with special machinery to avoid recursion problems

main

public static void main(java.lang.String[] args)
For testing only.

Parameters:
args - from command line


Stanford NLP Group