org.knowceans.map
Class InvertibleHashMap<X,Y>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<X,Y>
          extended by org.knowceans.map.InvertibleHashMap<X,Y>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<X,Y>

public class InvertibleHashMap<X,Y>
extends java.util.HashMap<X,Y>

HashMap that keeps an inverse. With getInverse(), the (forward) key can easily be found from the (forward) value.

In relational terms, this class implements an n:1 relation.

By convention, this class does not permit null values.

Author:
heinrich
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Constructor Summary
InvertibleHashMap()
           
InvertibleHashMap(IMultiMap<Y,X> inverse)
          allows to set an inverse type, for instance to sort by value using a TreeMultiMap.
 
Method Summary
 void checkConsistency()
          performs a simple check of consistency of the inverse with the forward map by a check if every value-key pair corresponds to a key-value pair with identical references and vice versa.
 void clear()
           
 IMultiMap<Y,X> getInverse()
          never use for write operations.
 java.util.Set<X> getInverse(java.lang.Object val)
          gets keys for a value as a Set.
 java.util.Set<Y> getInverseKeys()
          returns the keys of the inverse map.
static void main(java.lang.String[] args)
           
 Y put(X key, Y val)
          put a new key-value pair.
 Y remove(java.lang.Object key)
          removes the (forward) key and its value from the map.
 
Methods inherited from class java.util.HashMap
clone, containsKey, containsValue, entrySet, get, isEmpty, keySet, putAll, size, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

InvertibleHashMap

public InvertibleHashMap()

InvertibleHashMap

public InvertibleHashMap(IMultiMap<Y,X> inverse)
allows to set an inverse type, for instance to sort by value using a TreeMultiMap.

Method Detail

main

public static void main(java.lang.String[] args)

clear

public void clear()
Specified by:
clear in interface java.util.Map<X,Y>
Overrides:
clear in class java.util.HashMap<X,Y>

put

public Y put(X key,
             Y val)
put a new key-value pair. In the inverse map, the (forward) key needs to be removed from the Set that is pointed to by its old value, and the key put to the Set for the new value.

Specified by:
put in interface java.util.Map<X,Y>
Overrides:
put in class java.util.HashMap<X,Y>

remove

public Y remove(java.lang.Object key)
removes the (forward) key and its value from the map. In the inverse map, the (forward) key is removed from the set of (forward) keys that match the corresponding (forward) value (inverse key).

Specified by:
remove in interface java.util.Map<X,Y>
Overrides:
remove in class java.util.HashMap<X,Y>

getInverse

public java.util.Set<X> getInverse(java.lang.Object val)
gets keys for a value as a Set.

Parameters:
val -
Returns:

getInverseKeys

public java.util.Set<Y> getInverseKeys()
returns the keys of the inverse map. Use this preferably over values().

Returns:

getInverse

public IMultiMap<Y,X> getInverse()
never use for write operations.

TODO: return UnmodifiableMap but that's not subclass of IMultiMap.

Returns:

checkConsistency

public void checkConsistency()
performs a simple check of consistency of the inverse with the forward map by a check if every value-key pair corresponds to a key-value pair with identical references and vice versa.