net.sf.molae.pipe.trafo
Class InvertedMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by net.sf.molae.pipe.basic.AbstractMapWithSet<K,V>
          extended by net.sf.molae.pipe.trafo.EntryTransformedMap<Map.Entry<V,K>,K,V>
              extended by net.sf.molae.pipe.trafo.InvertedMap<K,V>
All Implemented Interfaces:
Map<K,V>

public final class InvertedMap<K,V>
extends EntryTransformedMap<Map.Entry<V,K>,K,V>

A map that exchanges keys and values for each entry. Before using this class, it must be ensured, that values are unique in the base map. Note that this class has a bad performance in general. Therefore it is recommended to store the result in another map.

See Also:
MapAsTransformer

Nested Class Summary
 
Nested classes/interfaces inherited from class net.sf.molae.pipe.basic.AbstractMapWithSet
AbstractMapWithSet.EntrySet
 
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
InvertedMap(Map<V,K> m)
          Constructs an InvertedMap object.
 
Method Summary
 boolean containsKey(Object key)
          Returns true if this map contains a mapping for the specified key.
 boolean containsValue(Object value)
          Returns true if this map maps a key to the specified value.
protected  TwoWayTransformer<Map.Entry<V,K>,Map.Entry<K,V>> getTransformer()
          Returns the underlying transformer.
static boolean isInvertible(Map<?,?> m)
          Checks that no value occurs twice in the specified map.
 Set<K> keySet()
          Returns a set view of the keys contained in this map.
 Collection<V> values()
          Returns a collection view of the values contained in this map.
 
Methods inherited from class net.sf.molae.pipe.trafo.EntryTransformedMap
clear, getEntrySetIterator, size
 
Methods inherited from class net.sf.molae.pipe.basic.AbstractMapWithSet
entrySet
 
Methods inherited from class java.util.AbstractMap
clone, equals, get, hashCode, isEmpty, put, putAll, remove, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InvertedMap

public InvertedMap(Map<V,K> m)
Constructs an InvertedMap object.

Parameters:
m - underlying map.
Throws:
NullPointerException - if any of the specified objects is null.
Method Detail

getTransformer

protected TwoWayTransformer<Map.Entry<V,K>,Map.Entry<K,V>> getTransformer()
Description copied from class: EntryTransformedMap
Returns the underlying transformer.

Overrides:
getTransformer in class EntryTransformedMap<Map.Entry<V,K>,K,V>
Returns:
the underlying transformer

containsKey

public boolean containsKey(Object key)
Returns true if this map contains a mapping for the specified key. This is the case if the base map contains the specified key as a value.

Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in class AbstractMap<K,V>
Parameters:
key - key whose presence in this map is to be tested.
Returns:
true if this map contains a mapping for the specified key.

containsValue

public boolean containsValue(Object value)
Returns true if this map maps a key to the specified value. This is the case if the base map contains the specified value as a key.

Specified by:
containsValue in interface Map<K,V>
Overrides:
containsValue in class AbstractMap<K,V>
Parameters:
value - value whose presence in this map is to be tested.
Returns:
true if this map maps a key to the specified value

keySet

public Set<K> keySet()
Returns a set view of the keys contained in this map. This is a set representation of the values() of the base map.

Specified by:
keySet in interface Map<K,V>
Overrides:
keySet in class AbstractMapWithSet<K,V>
Returns:
a set view of the keys contained in this map

values

public Collection<V> values()
Returns a collection view of the values contained in this map. This is the keySet() of the base map.

Specified by:
values in interface Map<K,V>
Overrides:
values in class AbstractMap<K,V>
Returns:
a collection view of the values contained in this map

isInvertible

public static boolean isInvertible(Map<?,?> m)
Checks that no value occurs twice in the specified map.

Parameters:
m - the map to be examined.
Returns:
true, iff each value in m corresponds to a unique key.