net.cscott.jutil

Class GenericMultiMap<K,V>

public class GenericMultiMap<K,V> extends Object implements MultiMap<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.3 2004/01/13 21:40:19 cananian Exp $

Author: Felix S. Klock II

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
booleanadd(K key, V value)
Ensures that this contains an association from key to value.
booleanaddAll(K key, Collection<? extends V> values)
Adds to the current mappings: associations for key to each value in values.
booleanaddAll(MultiMap<? extends K,? extends V> mm)
Add all mappings in the given multimap to this multimap.
voidclear()
booleancontains(Object a, Object b)
Returns true if a has a mapping to b in this.
booleancontainsKey(Object key)
booleancontainsValue(Object value)
MultiMapSet<K,V>entrySet()
Returns a set view of the mappings contained in this map.
booleanequals(Object o)
Vget(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.
inthashCode()
booleanisEmpty()
Set<K>keySet()
Returns a set view of the keys in this map.
Vput(K key, V value)
Associates the specified value with the specified key in this map.
voidputAll(Map<? extends K,? extends V> t)
Copies the mappings from the specified map to this map.
Vremove(Object key)
Removes all mappings for this key from this map if present.
booleanremove(Object key, Object value)
Removes a mapping from key to value from this map if present.
booleanremoveAll(K key, Collection<?> values)
Removes from the current mappings: associations for key to any value in values.
booleanretainAll(K key, Collection<?> values)
Removes from the current mappings: associations for key to any value not in values.
intsize()
StringtoString()
Collection<V>values()
Returns a collection view of the values contained in this map.

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

add

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

Returns: true if this mapping changed as a result of the call

addAll

public boolean addAll(K key, Collection<? extends V> values)
Adds to the current mappings: associations for key to each value in values. (MultiMap specific operation).

Returns: true if this mapping changed as a result of the call

addAll

public boolean addAll(MultiMap<? extends K,? extends V> mm)
Add all mappings in the given multimap to this multimap.

clear

public void clear()

contains

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

containsKey

public boolean containsKey(Object key)

containsValue

public boolean containsValue(Object value)

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.

equals

public boolean equals(Object o)

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.

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).

hashCode

public int hashCode()

isEmpty

public boolean isEmpty()

keySet

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

put

public V put(K key, V 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.

putAll

public void putAll(Map<? extends K,? extends V> 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).

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.

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.

removeAll

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

Returns: true if this mapping changed as a result of the call

retainAll

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

Returns: true if this mapping changed as a result of the call

size

public int size()

toString

public String toString()

values

public Collection<V> values()
Returns a collection view of the values contained in this map.
Copyright © 2003 C. Scott Ananian