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

java.lang.Object
  extended by edu.stanford.nlp.util.CollectionValuedMap<K,V>
All Implemented Interfaces:
Serializable, Map<K,Collection<V>>
Direct Known Subclasses:
DeltaCollectionValuedMap

public class CollectionValuedMap<K,V>
extends Object
implements Map<K,Collection<V>>, Serializable

A class which can store mappings from Object keys to Collections of Object values. Important methods are the add(Object key, Object value) and removeMapping(Object key, Object value) methods for adding and removing a value to/from the Collection associated with the key, and the get(Object key) 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)
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  CollectionFactory cf
           
protected  MapFactory 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 cf)
          Creates a new empty CollectionValuedMap which uses a HashMap as the underlying Map.
CollectionValuedMap(CollectionValuedMap cvm)
          Creates a new CollectionValuedMap with all of the mappings from cvm.
CollectionValuedMap(MapFactory mf, CollectionFactory 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(Map<K,V> m)
          Adds all of the mappings in m to this CollectionValuedMap.
 Collection<V> allValues()
           
 void clear()
          Clears this Map.
 Object clone()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
          Unsupported.
 Object deltaClone()
          Creates a "delta clone" of this Map, where only the differences are represented.
 Set 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(Object o)
           
 Collection<V> get(Object key)
           
 int hashCode()
           
 boolean isEmpty()
           
 Set<K> keySet()
           
static void main(String[] args)
          For testing only.
 Collection<V> put(K key, Collection<V> collection)
          Replaces current Collection mapped to key with the specified Collection.
 void putAll(Map<? extends K,? extends Collection<V>> m)
          Unsupported.
 Collection remove(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.
 String toString()
           
 Collection values()
          Unsupported.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

cf

protected CollectionFactory cf

mf

protected MapFactory mf
Constructor Detail

CollectionValuedMap

public CollectionValuedMap(MapFactory mf,
                           CollectionFactory 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 cvm)
Creates a new CollectionValuedMap with all of the mappings from cvm. Same as clone().

Parameters:
cvm -

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 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 Collection<V> put(K key,
                         Collection<V> collection)
Replaces current Collection mapped to key with the specified Collection. Use carefully!

Specified by:
put in interface Map<K,Collection<V>>
Parameters:
key -
collection -
Returns:

putAll

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

Specified by:
putAll in interface Map<K,Collection<V>>
Parameters:
m -

get

public Collection<V> get(Object key)
Specified by:
get in interface Map<K,Collection<V>>
Parameters:
key -
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.

Parameters:
key -
value -

addAll

public void addAll(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.

Parameters:
m -

remove

public Collection remove(Object key)
Removes the mapping associated with this key from this Map.

Specified by:
remove in interface Map<K,Collection<V>>
Parameters:
key -
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 Map<K,Collection<V>>

containsKey

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

containsValue

public boolean containsValue(Object value)
Unsupported.

Specified by:
containsValue in interface Map<K,Collection<V>>
Parameters:
value -
Returns:

isEmpty

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

entrySet

public Set 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 Map<K,Collection<V>>
Returns:
a Set view of the mappings contained in this map.

keySet

public Set<K> keySet()
Specified by:
keySet in interface Map<K,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 Map<K,Collection<V>>
Returns:

values

public Collection values()
Unsupported.

Specified by:
values in interface Map<K,Collection<V>>
Returns:

allValues

public Collection<V> allValues()

equals

public boolean equals(Object o)
Specified by:
equals in interface Map<K,Collection<V>>
Overrides:
equals in class Object
Parameters:
o -
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 Map<K,Collection<V>>
Overrides:
hashCode in class Object
Returns:
the hashcode of the underlying Map

deltaClone

public Object deltaClone()
Creates a "delta clone" of this Map, where only the differences are represented.

Returns:

clone

public Object clone()
Overrides:
clone in class Object
Returns:
a clone of this Map

toString

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

main

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

Parameters:
args - from command line


Stanford NLP Group