common
Class HashMultiMapDiff

java.lang.Object
  extended by java.util.AbstractMap
      extended by common.HashMultiMapDiff
All Implemented Interfaces:
MultiMap, MultiMapDiff, java.lang.Cloneable, java.util.Map
Direct Known Subclasses:
IndexedMultiMapDiff

public class HashMultiMapDiff
extends java.util.AbstractMap
implements MultiMapDiff, java.lang.Cloneable

Implementation of MultiMapDiff that uses a hash map from objects HashSetDiffs.


Nested Class Summary
protected  class HashMultiMapDiff.ValueSet
           
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface common.MultiMap
MultiMap.EmptyMultiMap
 
Field Summary
 
Fields inherited from interface common.MultiMap
EMPTY_MULTI_MAP
 
Constructor Summary
HashMultiMapDiff(MultiMap underlying)
          Creates a new HashMultiMapDiff with the given underlying MultiMap.
HashMultiMapDiff(MultiMap underlying, boolean maintainOrder)
          Creates a new HashMultiMapDiff with the given underlying MultiMap.
 
Method Summary
 boolean add(java.lang.Object key, java.lang.Object value)
          Adds the given value to the set associated with the given key.
 boolean addAll(java.lang.Object key, java.util.Set values)
          Adds all elements of the given set to the set associated with the given key.
 void changeUnderlying()
          Changes the underlying multi-map to equal this multi-map.
 void clear()
           
 void clearChanges()
          Resets this multi-map to be equal to the underlying multi-map.
 java.lang.Object clone()
          Returns a shallow copy of this multi-map diff, expressed relative to the same underlying multi-map.
 boolean containsKey(java.lang.Object key)
           
protected  java.util.Set emptySet()
           
 java.util.Set entrySet()
           
 java.lang.Object get(java.lang.Object key)
          Returns the set associated with the given key.
 java.util.Set getAddedValues(java.lang.Object key)
          Returns the set of values that are associated with the given key in this multi-map and not in the underlying multi-map.
 java.util.Set getChangedKeys()
          Returns the set of keys whose associated value sets are different in this multi-map and the underlying multi-map.
protected  java.util.Set getCurrent(java.lang.Object key)
           
 java.util.Set getRemovedValues(java.lang.Object key)
          Returns the set of values that are associated with the given key in the underlying multi-map but not in this multi-map.
protected  SetDiff newDiff(java.util.Set underlying)
           
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Associates the given key with the given value, which must be a Set.
 java.lang.Object remove(java.lang.Object key)
           
 boolean remove(java.lang.Object key, java.lang.Object value)
          Removes the given value from the set associated with the given key.
 boolean removeAll(java.lang.Object key, java.util.Set values)
          Removes all elements of the given set from the set associated with the given key.
 int size()
           
 
Methods inherited from class java.util.AbstractMap
containsValue, equals, hashCode, isEmpty, keySet, putAll, toString, values
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
containsValue, equals, hashCode, isEmpty, keySet, putAll, values
 

Constructor Detail

HashMultiMapDiff

public HashMultiMapDiff(MultiMap underlying)
Creates a new HashMultiMapDiff with the given underlying MultiMap.


HashMultiMapDiff

public HashMultiMapDiff(MultiMap underlying,
                        boolean maintainOrder)
Creates a new HashMultiMapDiff with the given underlying MultiMap. If maintainOrder is true, uses linked data structures so that the order of iteration is predictable.

Method Detail

size

public int size()
Specified by:
size in interface java.util.Map
Overrides:
size in class java.util.AbstractMap

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map
Overrides:
containsKey in class java.util.AbstractMap

get

public java.lang.Object get(java.lang.Object key)
Description copied from interface: MultiMap
Returns the set associated with the given key. If the key is not in the map, returns an empty set. The set returned is modifiable and backed by this multi-map: if values are added for the given key, they will show up in the returned set. However, the returned set may lose its connection to this multi-map if the multi-map's put method is called or if all the values for the given key are removed.

Specified by:
get in interface MultiMap
Specified by:
get in interface java.util.Map
Overrides:
get in class java.util.AbstractMap

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Description copied from interface: MultiMap
Associates the given key with the given value, which must be a Set. If the given set is empty, the key is removed from the map.

Specified by:
put in interface MultiMap
Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.AbstractMap
Returns:
the set previously associated with this key, or an empty set if the key was not in the map

add

public boolean add(java.lang.Object key,
                   java.lang.Object value)
Description copied from interface: MultiMap
Adds the given value to the set associated with the given key. If the key is not yet in the map, it is added.

Specified by:
add in interface MultiMap
Returns:
true if the MultiMap changed as a result of this call

addAll

public boolean addAll(java.lang.Object key,
                      java.util.Set values)
Description copied from interface: MultiMap
Adds all elements of the given set to the set associated with the given key. If the key is not yet in the map, it is added.

Specified by:
addAll in interface MultiMap
Returns:
true if the MultiMap changed as a result of this call

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map
Overrides:
remove in class java.util.AbstractMap

remove

public boolean remove(java.lang.Object key,
                      java.lang.Object value)
Description copied from interface: MultiMap
Removes the given value from the set associated with the given key. Does nothing if the value is not in that set. If the set ends up being empty, then the key is removed from the map.

Specified by:
remove in interface MultiMap
Returns:
true if the MultiMap changed as a result of this call

removeAll

public boolean removeAll(java.lang.Object key,
                         java.util.Set values)
Description copied from interface: MultiMap
Removes all elements of the given set from the set associated with the given key. If the associated set ends up being empty, then the key is removed from the map.

Specified by:
removeAll in interface MultiMap
Returns:
true if the MultiMap changed as a result of this call

clear

public void clear()
Specified by:
clear in interface java.util.Map
Overrides:
clear in class java.util.AbstractMap

entrySet

public java.util.Set entrySet()
Specified by:
entrySet in interface java.util.Map
Specified by:
entrySet in class java.util.AbstractMap

getChangedKeys

public java.util.Set getChangedKeys()
Description copied from interface: MultiMapDiff
Returns the set of keys whose associated value sets are different in this multi-map and the underlying multi-map. This includes keys that were added or removed.

The returned set may or may not be kept up to date as changes are made to this MultiMapDiff.

Specified by:
getChangedKeys in interface MultiMapDiff
Returns:
unmodifiable Set of Objects

getAddedValues

public java.util.Set getAddedValues(java.lang.Object key)
Description copied from interface: MultiMapDiff
Returns the set of values that are associated with the given key in this multi-map and not in the underlying multi-map. Returns an empty set if the key is not in this multi-map.

The returned set may or may not be kept up to date as changes are made to this MultiMapDiff.

Specified by:
getAddedValues in interface MultiMapDiff
Returns:
unmodifiable Set of Objects

getRemovedValues

public java.util.Set getRemovedValues(java.lang.Object key)
Description copied from interface: MultiMapDiff
Returns the set of values that are associated with the given key in the underlying multi-map but not in this multi-map. Returns an empty set if the key is not in the underlying multi-map.

The returned set may or may not be kept up to date as changes are made to this MultiMapDiff.

Specified by:
getRemovedValues in interface MultiMapDiff
Returns:
unmodifiable Set of Objects

changeUnderlying

public void changeUnderlying()
Changes the underlying multi-map to equal this multi-map.

Specified by:
changeUnderlying in interface MultiMapDiff

clearChanges

public void clearChanges()
Resets this multi-map to be equal to the underlying multi-map.

Specified by:
clearChanges in interface MultiMapDiff

clone

public java.lang.Object clone()
Returns a shallow copy of this multi-map diff, expressed relative to the same underlying multi-map. The copy is "shallow" in that it does not create new copies of the keys or elements; however, it does make copies of the Set objects used to store elements in the map.

Overrides:
clone in class java.util.AbstractMap

emptySet

protected java.util.Set emptySet()

newDiff

protected SetDiff newDiff(java.util.Set underlying)

getCurrent

protected java.util.Set getCurrent(java.lang.Object key)