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

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

public class InvertibleTreeMap<X,Y>
extends java.util.TreeMap<X,Y>

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

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

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
InvertibleTreeMap()
           
InvertibleTreeMap(java.util.Comparator<? super X> c)
          add the comparator on the keys to provide an ordering.
 
Method Summary
 void clear()
           
 IMultiMap<Y,X> getInverse()
           
 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.TreeMap
ceilingEntry, ceilingKey, clone, comparator, containsKey, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, get, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, navigableKeySet, pollFirstEntry, pollLastEntry, putAll, size, subMap, subMap, tailMap, tailMap, values
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, isEmpty, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode, isEmpty
 

Constructor Detail

InvertibleTreeMap

public InvertibleTreeMap(java.util.Comparator<? super X> c)
add the comparator on the keys to provide an ordering.

Parameters:
c -

InvertibleTreeMap

public InvertibleTreeMap()
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.TreeMap<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.TreeMap<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.TreeMap<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()