com.neoworks.util
Class AbstractMultiMap

java.lang.Object
  |
  +--com.neoworks.util.AbstractMultiMap
All Implemented Interfaces:
MultiMap
Direct Known Subclasses:
VectorMultiMap

public abstract class AbstractMultiMap
extends java.lang.Object
implements MultiMap

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

To implement an unmodifiable multimap, the programmer needs only to extend this class and provide an implementation for the entrySet method, which returns a set-view of the multimap's mappings. Typically, the returned set will, in turn, be implemented atop AbstractSet. This set should not support the add or remove methods, and its iterator should not support the remove method.

To implement a modifiable multimap, the programmer must additionally override this class's put method (which otherwise throws an UnsupportedOperationException), and the iterator returned by entrySet().iterator() must additionally implement its remove method.

The programmer should generally provide a void (no argument) and multimap constructor, as per the recommendation in the MultiMap interface specification.

The documentation for each non-abstract methods in this class describes its implementation in detail. Each of these methods may be overridden if the multimap being implemented admits a more efficient implementation.

Version:
0.1
Author:
Stuart Farnan stuart@neoworks.com

Nested Class Summary
 
Nested classes inherited from class com.neoworks.util.MultiMap
MultiMap.Entry
 
Constructor Summary
protected AbstractMultiMap()
          Sole constructor.
 
Method Summary
 void clear()
          Removes all mappings from this multimap (optional operation).
 boolean containsKey(java.lang.Object key)
          Returns true if this multimap contains a mapping for the specified key.
 boolean containsValue(java.lang.Object value)
          Returns true if this multimap maps one or more keys to this value.
abstract  java.util.Set entrySet()
          Returns a set view of the mappings contained in this multimap.
 boolean equals(java.lang.Object o)
          Compares the specified object with this multimap for equality.
 java.util.Set get(java.lang.Object key)
          Returns the set of values to which this multimap maps the specified key.
 int hashCode()
          Returns the hash code value for this multimap.
 boolean isEmpty()
          Returns true if this multimap contains no key-value mappings.
 java.util.Collection keys()
          Returns a collection view of the keys contained in this multimap.
 java.util.Set put(java.lang.Object key, java.lang.Object value)
          Associates the specified value with the specified key in this map (optional operation).
 void putAll(MultiMap map)
          Copies all of the mappings from the specified multimap to this multimap (optional operation).
 java.util.Set remove(java.lang.Object key)
          Removes all the mappings for this key from this multimap if present (optional operation).
 java.lang.Object remove(java.lang.Object key, java.lang.Object value)
          Removes the specified mapping from this multimap if present (optional operation).
 int size()
          Returns the number of key-value mappings in this multimap.
 java.lang.String toString()
          Returns a string representation of this multimap.
 java.util.Collection uniqueKeys()
          Return a Collection of unique keys from the Multimap
 java.util.Collection values()
          Returns a collection view of the values contained in this multimap.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractMultiMap

protected AbstractMultiMap()
Sole constructor. (For invocation by subclass constructors, typically implicit.)

Method Detail

size

public int size()
Returns the number of key-value mappings in this multimap. If the multimap contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

This implementation returns entrySet().size().

Specified by:
size in interface MultiMap
Returns:
the number of key-value mappings in this multimap.

isEmpty

public boolean isEmpty()
Returns true if this multimap contains no key-value mappings.

This implementation returns size() == 0.

Specified by:
isEmpty in interface MultiMap
Returns:
true if this multimap contains no key-value mappings.

containsValue

public boolean containsValue(java.lang.Object value)
Returns true if this multimap maps one or more keys to this value. More formally, returns true if and only if this multimap contains at least one mapping to a value v such that:

(value == null ? v == null : value.equals(v)).

This operation will probably require time linear in the multimap size for most implementations of multimap.

This implementation iterates over entrySet() searching for an entry with the specified value. If such an entry is found, true is returned. If the iteration terminates without finding such an entry, false is returned. Note that this implementation requires linear time in the size of the multimap.

Specified by:
containsValue in interface MultiMap
Parameters:
value - value whose presence in this multimap is to be tested.
Returns:
true if this multimap maps one or more keys to this value.

containsKey

public boolean containsKey(java.lang.Object key)
Returns true if this multimap contains a mapping for the specified key.

This implementation iterates over entrySet() searching for an entry with the specified key. If such an entry is found, true is returned. If the iteration terminates without finding such an entry, false is returned. Note that this implementation requires linear time in the size of the multimap; many implementations will override this method.

Specified by:
containsKey in interface MultiMap
Parameters:
key - Key whose presence in this multimap is to be tested.
Returns:
true if this multimap contains a mapping for the specified key.

get

public java.util.Set get(java.lang.Object key)
Returns the set of values to which this multimap maps the specified key. Returns null if the multimap contains no mapping for this key. A return value of null does indicate that the multimap contains no mapping for the key; it's possible that the map explicitly maps the key to null, in this case the returned set will contain null possibly along with other objects. The containsKey operation may also be used to distinguish these two cases.

This implementation iterates over entrySet() searching for an entry with the specified key. If such an entry is found, the entry's values are placed in a Set and returned. If the iteration terminates without finding such an entry, null is returned. This implementation allows null keys. Note that this implementation requires linear time in the size of the multimap; many implementations will override this method.

Specified by:
get in interface MultiMap
Parameters:
key - Key whose associated value is to be returned.
Returns:
A Set of values to which this multimap maps the specified key.
See Also:
containsKey(Object)

put

public java.util.Set put(java.lang.Object key,
                         java.lang.Object value)
Associates the specified value with the specified key in this map (optional operation). If the multimap previously contained a mapping for this key, the old value is not lost.

This implementation always throws an UnsupportedOperationException.

Specified by:
put in interface MultiMap
Parameters:
key - Key with which the specified value is to be associated.
value - Value to be associated with the specified key.
Returns:
Set of values associated with the specified key.

remove

public java.util.Set remove(java.lang.Object key)
Removes all the mappings for this key from this multimap if present (optional operation).

This implementation iterates over entrySet() searching for entries with the specified key. If such an entry is found, its value is obtained with its getValue operation, the entry is removed from the collection (and the backing map) with the iterator's remove operation, and the saved value is added to a Set object which is returned at the end of this method. If the iteration terminates without finding such an entry, null is returned. Note that this implementation requires linear time in the size of the map; many implementations will override this method.

Specified by:
remove in interface MultiMap
Parameters:
key - Key whose mappings is to be removed from the multimap.
Returns:
Set of values associated with the specified key, or null if there were no mappings for key.

remove

public java.lang.Object remove(java.lang.Object key,
                               java.lang.Object value)
Removes the specified mapping from this multimap if present (optional operation).

This implementation iterates over entrySet() searching for entries with the specified key and value. If such an entry is found, its value is obtained with its getValue operation, the entry is removed from the collection (and the backing map) with the iterator's remove operation, it is returned at the end of this method. If the iteration terminates without finding such an entry, null is returned. Note that this implementation requires linear time in the size of the map; many implementations will override this method.

Specified by:
remove in interface MultiMap
Parameters:
key - Key whose mapping is to be removed from the multimap.
value - Value that is being mapped to by the given key.
Returns:
Value associated with the specified key, or null if there were no mappings for key.

putAll

public void putAll(MultiMap map)
Copies all of the mappings from the specified multimap to this multimap (optional operation). These mappings will add to any mappings that this multimap had for any of the keys currently in the specified multimap.

This implementation iterates over the specified multi's entrySet() collection, and calls this multimap's put operation once for each entry returned by the iteration.

Specified by:
putAll in interface MultiMap
Parameters:
map - Mappings to be stored in this multimap.

clear

public void clear()
Removes all mappings from this multimap (optional operation).

This implementation calls entrySet().clear().

Specified by:
clear in interface MultiMap

keys

public java.util.Collection keys()
Returns a collection view of the keys contained in this multimap. The collection is backed by the multimap, so changes to the multimap are reflected in the collection, and vice-versa. (If the multimap is modified while an iteration over the set is in progress, the results of the iteration are undefined.) The collection supports element removal, which removes the corresponding entry from the multimap, via the Iterator.remove, Collection.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

This implementation returns a Collection that subclasses AbstractCollection. The subclass's iterator method returns a "wrapper object" over this multimap's entrySet() iterator. The size method delegates to this multimap's size method and the contains method delegates to this multimap's containsKey method.

The collection is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same collection.

Specified by:
keys in interface MultiMap
Returns:
A collection view of the keys contained in this multimap.

uniqueKeys

public java.util.Collection uniqueKeys()
Return a Collection of unique keys from the Multimap

Specified by:
uniqueKeys in interface MultiMap
Returns:
A Collection of unique keys

values

public java.util.Collection values()
Returns a collection view of the values contained in this multimap. The collection is backed by the multimap, so changes to the multimap are reflected in the collection, and vice-versa. (If the multimap is modified while an iteration over the collection is in progress, the results of the iteration are undefined.) The collection supports element removal, which removes the corresponding entry from the multimap, via the Iterator.remove, Collection.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

This implementation returns a Collection that subclasses AbstractCollection. The subclass's iterator method returns a "wrapper object" over this multimap's entrySet() iterator. The size method delegates to this multimap's size method and the contains method delegates to this multimap's containsValue method.

The collection is created the first time this method is called, and returned in response to all subsequent calls. No synchronization is performed, so there is a slight chance that multiple calls to this method will not all return the same collection.

Specified by:
values in interface MultiMap
Returns:
A collection view of the values contained in this multimap.

entrySet

public abstract java.util.Set entrySet()
Returns a set view of the mappings contained in this multimap. Each element in this set is a Map.Entry. The set is backed by the map, so changes to the multimap are reflected in the set, and vice-versa. (If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.) The set supports element removal, which removes the corresponding entry from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Specified by:
entrySet in interface MultiMap
Returns:
A set view of the mappings contained in this multimap.

equals

public boolean equals(java.lang.Object o)
Compares the specified object with this multimap for equality. Returns true if the given object is also a multimap and the two multimaps represent the same mappings. More formally, two maps t1 and t2 represent the same mappings iff:

t1.keySet().equals(t2.keySet())

and for every key k in t1.keySet(),

(t1.get(k)==null ? t2.get(k)==null : t1.get(k).equals(t2.get(k))). This ensures that the equals method works properly across different implementations of the multimap interface.

This implementation first checks if the specified object is this map; if so it returns true. Then, it checks if the specified object is a multimap whose size is identical to the size of this set; if not, it it returns false. If so, it iterates over this multimap's entrySet collection, and checks that the specified map contains each mapping that this multimap contains. If the specified map fails to contain such a mapping, false is returned. If the iteration completes, true is returned.

Specified by:
equals in interface MultiMap
Overrides:
equals in class java.lang.Object
Parameters:
o - Object to be compared for equality with this multimap.
Returns:
true if the specified object is equal to this multimap.

hashCode

public int hashCode()
Returns the hash code value for this multimap. The hash code of a multimap is defined to be the sum of the hash codes of each entry in the multimap's entrySet() view. This ensures that t1.equals(t2) implies that t1.hashCode()==t2.hashCode() for any two maps t1 and t2, as required by the general contract of Object.hashCode.

This implementation iterates over entrySet(), calling hashCode on each element (entry) in the Collection, and adding up the results.

Specified by:
hashCode in interface MultiMap
Overrides:
hashCode in class java.lang.Object
Returns:
The hash code value for this multimap.
See Also:
MultiMap.Entry.hashCode()

toString

public java.lang.String toString()
Returns a string representation of this multimap. The string representation consists of a list of key-value mappings in the order returned by the multimap's entrySet view's iterator, enclosed in braces ("{}"). Adjacent mappings are separated by the characters ", " (comma and space). Each key-value mapping is rendered as the key followed by an equals sign ("=") followed by the associated value. Keys and values are converted to strings as by String.valueOf(Object).

This implementation creates an empty string buffer, appends a left brace, and iterates over the multimap's entrySet view, appending the string representation of each MultiMap.Entry in turn. After appending each entry except the last, the string ", " is appended. Finally a right brace is appended. A String is obtained from the StringBuffer, and returned.

Overrides:
toString in class java.lang.Object
Returns:
A String representation of this multimap.