JUtil

net.cscott.jutil
Class GenericMultiMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by net.cscott.jutil.AbstractMultiMap<K,V>
          extended by net.cscott.jutil.GenericMultiMap<K,V>
All Implemented Interfaces:
Map<K,V>, MultiMap<K,V>

public class GenericMultiMap<K,V>
extends AbstractMultiMap<K,V>

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.6 2006-10-30 19:58:05 cananian Exp $
Author:
Felix S. Klock II

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,V>, AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Constructor Summary
GenericMultiMap()
          Creates a MultiMap using a HashMap for the map and HashSets for the value collections.
GenericMultiMap(CollectionFactory<V> cf)
          Creates a MultiMap using a HashMap for the map and the specified CollectionFactory to create the value collections.
GenericMultiMap(MapFactory<K,Collection<V>> mf, CollectionFactory<V> cf)
          Creates a MultiMap using the specified MapFactory to create the map and the specified CollectionFactory to create the value collections.
 
Method Summary
 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)
           
 MultiMapSet<K,V> entrySet()
          Returns a set view of the mappings contained in this map.
 V get(Object key)
          Returns some arbitrary value from the set of values to which this map maps the specified key.
 Collection<V> getValues(K key)
          Returns the collection of Values associated with key.
 boolean isEmpty()
           
 Set<K> keySet()
          Returns a set view of the keys in this map.
 V 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.
 int size()
          Returns the number of key-value mappings in this map (keys which map to multiple values count multiple times).
 Collection<V> values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class net.cscott.jutil.AbstractMultiMap
add, addAll, addAll, equals, hashCode, put, putAll, removeAll, retainAll, toString
 
Methods inherited from class java.util.AbstractMap
clone
 
Methods inherited from class java.lang.Object
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<V> cf)
Creates a MultiMap using a HashMap for the map and the specified CollectionFactory to create the value collections.


GenericMultiMap

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

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 Map<K,V>
Specified by:
size in interface MultiMap<K,V>
Overrides:
size in class AbstractMultiMap<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>
Overrides:
isEmpty in class AbstractMultiMap<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMultiMap<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>
Overrides:
containsValue in class AbstractMultiMap<K,V>

get

public V 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 Map<K,V>
Specified by:
get in interface MultiMap<K,V>
Overrides:
get in class AbstractMultiMap<K,V>

remove

public V 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 Map<K,V>
Specified by:
remove in interface MultiMap<K,V>
Specified by:
remove in class AbstractMultiMap<K,V>
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<K,V>
Specified by:
remove in class AbstractMultiMap<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>
Specified by:
clear in class AbstractMultiMap<K,V>

getValues

public Collection<V> getValues(K 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<K,V>
Specified by:
getValues in class AbstractMultiMap<K,V>

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<K,V>
Specified by:
contains in class AbstractMultiMap<K,V>

keySet

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

Specified by:
keySet in interface Map<K,V>
Overrides:
keySet in class AbstractMap<K,V>

values

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

Specified by:
values in interface Map<K,V>
Overrides:
values in class AbstractMap<K,V>

entrySet

public MultiMapSet<K,V> 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<K,V>
Specified by:
entrySet in interface MultiMap<K,V>
Specified by:
entrySet in class AbstractMultiMap<K,V>

JUtil

Copyright (c) 2006 C. Scott Ananian