JUtil

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

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

public abstract class AbstractMultiMap<K,V>
extends AbstractMap<K,V>
implements MultiMap<K,V>

This class provides a skeletal implementation of the MultiMap interface, to minimize the effort requires to implement this interface.

Version:
$Id: AbstractMultiMap.java,v 1.5 2006-10-30 21:58:57 cananian Exp $
Author:
C. Scott Ananian

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
AbstractMultiMap()
           
 
Method Summary
 boolean add(K key, V value)
          Ensures that this contains an association from key to value.
 boolean addAll(K key, Collection<? extends V> values)
          Adds to the current mappings: associations for key to each value in values.
 boolean addAll(MultiMap<? extends K,? extends V> mm)
          Add all mappings in the given multimap to this multimap.
abstract  void clear()
           
abstract  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)
           
abstract  MultiMapSet<K,V> entrySet()
          Returns a Set view that allows you to recapture the MultiMap view.
 boolean equals(Object o)
           
 V get(Object key)
          Returns some arbitrary value from the collection of values to which this map maps the specified key.
abstract  Collection<V> getValues(K key)
          Returns the collection of Values associated with key.
 int hashCode()
           
 boolean isEmpty()
           
 V put(K key, V value)
          Associates the specified value with the specified key in this map.
 void putAll(Map<? extends K,? extends V> t)
          Copies the mappings from the specified map to this map.
abstract  V remove(Object key)
          Removes mappings from key to all associated values from this map.
abstract  boolean remove(Object key, Object value)
          Removes a mapping from key to value from this map if present.
 boolean removeAll(K key, Collection<?> values)
          Removes from the current mappings: associations for key to any value in values.
 boolean retainAll(K 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()
           
 
Methods inherited from class java.util.AbstractMap
clone, keySet, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
keySet, values
 

Constructor Detail

AbstractMultiMap

public AbstractMultiMap()
Method Detail

toString

public String toString()
Overrides:
toString in class AbstractMap<K,V>

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class AbstractMap<K,V>

add

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

Specified by:
add in interface MultiMap<K,V>
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).

Specified by:
addAll in interface MultiMap<K,V>
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.

Specified by:
addAll in interface MultiMap<K,V>

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

Specified by:
retainAll in interface MultiMap<K,V>
Returns:
true if this mapping changed as a result of the call

removeAll

public boolean removeAll(K 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<K,V>
Returns:
true if this mapping changed as a result of the call

equals

public boolean equals(Object o)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class AbstractMap<K,V>

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

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

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.

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

containsKey

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

containsValue

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

isEmpty

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

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 AbstractMap<K,V>

get

public V get(Object key)
Description copied from interface: MultiMap
Returns some arbitrary value from the collection 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 AbstractMap<K,V>

getValues

public abstract Collection<V> getValues(K key)
Description copied from interface: MultiMap
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, potentially mutable, empty Collection and returns it. (MultiMap specific operation).

Specified by:
getValues in interface MultiMap<K,V>

contains

public abstract boolean contains(Object a,
                                 Object b)
Description copied from interface: MultiMap
Returns true if a has a mapping to b in this. (MultiMap specific operation).

Specified by:
contains in interface MultiMap<K,V>

remove

public abstract V remove(Object key)
Description copied from interface: MultiMap
Removes mappings from key to all associated values from this map. This is consistent with the Map definition of remove.

Specified by:
remove in interface Map<K,V>
Specified by:
remove in interface MultiMap<K,V>
Overrides:
remove in class AbstractMap<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 abstract boolean remove(Object key,
                               Object value)
Description copied from interface: MultiMap
Removes a mapping from key to value from this map if present. (MultiMap specific operation). 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>

clear

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

entrySet

public abstract MultiMapSet<K,V> entrySet()
Description copied from interface: MultiMap
Returns a Set view that allows you to recapture the MultiMap view.

Specified by:
entrySet in interface Map<K,V>
Specified by:
entrySet in interface MultiMap<K,V>
Specified by:
entrySet in class AbstractMap<K,V>

JUtil

Copyright (c) 2006 C. Scott Ananian