sky.monome.util
Class ReversibleHashMap<K1,K2>


java.lang.Object

  extended by sky.monome.util.ReversibleHashMap<K1,K2>

Type Parameters:
K1 - Type of keys in a normal way.
K2 - Type of keys in a reversed way.
All Implemented Interfaces:
java.util.Map<K1,K2>, Synchronized

public final class ReversibleHashMap<K1,K2>
extends java.lang.Object
implements java.util.Map<K1,K2>, Synchronized

Map in which both parameter types are keys inbetween. In a normal way, K1 is the key and K2 is the value. In a reversed way, K2 is the key and K1 is the value. That implies that there will be no doublons in keys AND in values ! A protection mechanism look after that aspect permanently. Note that it goes against standard Map interface specifications.
This map works like a HashMap since it internally contains HashMaps.
This class is strongly thread-safe for all its methods. But you should iterate on it with external synchronization based on the internal lock object accessible with the getLockObject() method specified by the Synchronized interface.
Why do you need a reversed map ? With classic maps, you can find a value associated with a specified key. But you can't find a key associated with a specific value. With reversed maps, you can do that !

Version:
2.1
Author:
PJ Skyman
See Also:
Synchronized

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
ReversibleHashMap()
          Constructs an empty map.
ReversibleHashMap(java.util.Map<? extends K1,? extends K2> map)
          Constructs a map prefilled with all entries of specified map.
 
Method Summary
 void clear()
          Clears all key/value entries from this map.
 boolean containsKey(java.lang.Object key1)
          Indicates whether this map contains the specified key in a normal way.
 boolean containsValue(java.lang.Object key2)
          Indicates whether this map contains the specified value in a normal way.
 java.util.Set<java.util.Map.Entry<K1,K2>> entrySet()
          Returns a set of all key/value entries contained by this map in a normal way.
 K2 get(java.lang.Object key1)
          Returns the value associated with the specified key in a normal way.
 java.lang.Object getLockObject()
          Returns the object over which the internal and external synchronization will operate.
 boolean isEmpty()
          Indicates whether this map is empty.
 java.util.Set<K1> keySet()
          Returns a set of all keys contained by this map in a normal way.
 K2 put(K1 key1, K2 key2)
          Adds or replaces the specified value associated with the specified key in a normal way.
 void putAll(java.util.Map<? extends K1,? extends K2> m)
          Copies all key/value entries of the specified map into this map in a normal way.
 K2 remove(java.lang.Object key1)
          Removes the value associated with the specified key in a normal way.
 boolean reversedContainsKey(java.lang.Object key2)
          Indicates whether this map contains the specified key in a reversed way.
 boolean reversedContainsValue(java.lang.Object key1)
          Indicates whether this map contains the specified value in a reversed way.
 java.util.Set<java.util.Map.Entry<K2,K1>> reversedEntrySet()
          Returns a set of all key/value entries contained by this map in a reversed way.
 K1 reversedGet(java.lang.Object key2)
          Returns the value associated with the specified key in a reversed way.
 java.util.Set<K2> reversedKeySet()
          Returns a set of all keys contained by this map in a reversed way.
 K1 reversedPut(K2 key2, K1 key1)
          Adds or replaces the specified value associated with the specified key in a reversed way.
 void reversedPutAll(java.util.Map<? extends K2,? extends K1> m)
          Copies all key/value entries of the specified map into this map in a reversed way.
 K1 reversedRemove(java.lang.Object key2)
          Removes the value associated with the specified key in a reversed way.
 java.util.Collection<K1> reversedValues()
          Returns a collection of all values contained by this map in a normal way.
 int size()
          Returns the size of this map.
 java.util.Collection<K2> values()
          Returns a collection of all values contained by this map in a normal way.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Constructor Detail

ReversibleHashMap


public ReversibleHashMap()
Constructs an empty map.


ReversibleHashMap


public ReversibleHashMap(java.util.Map<? extends K1,? extends K2> map)
Constructs a map prefilled with all entries of specified map. Only copies of all valid entries will be realized, the specified map will stay unchanged.

Parameters:
map - Map from which the new map will be prefilled.
See Also:
putAll(java.util.Map)
Method Detail

size


public int size()
Returns the size of this map.

Specified by:
size in interface java.util.Map<K1,K2>
Returns:
The size of this map.

isEmpty


public boolean isEmpty()
Indicates whether this map is empty.

Specified by:
isEmpty in interface java.util.Map<K1,K2>
Returns:
true if this map is empty, false otherwise.

containsKey


public boolean containsKey(java.lang.Object key1)
Indicates whether this map contains the specified key in a normal way.

Specified by:
containsKey in interface java.util.Map<K1,K2>
Parameters:
key1 - Key to look for.
Returns:
true if this map contains the specified key, false otherwise.

reversedContainsKey


public boolean reversedContainsKey(java.lang.Object key2)
Indicates whether this map contains the specified key in a reversed way.

Parameters:
key2 - Key to look for.
Returns:
true if this map contains the specified key, false otherwise.

containsValue


public boolean containsValue(java.lang.Object key2)
Indicates whether this map contains the specified value in a normal way.

Specified by:
containsValue in interface java.util.Map<K1,K2>
Parameters:
key2 - Value to look for.
Returns:
true if this map contains the specified value, false otherwise.

reversedContainsValue


public boolean reversedContainsValue(java.lang.Object key1)
Indicates whether this map contains the specified value in a reversed way.

Parameters:
key1 - Value to look for.
Returns:
true if this map contains the specified value, false otherwise.

get


public K2 get(java.lang.Object key1)
Returns the value associated with the specified key in a normal way.

Specified by:
get in interface java.util.Map<K1,K2>
Parameters:
key1 - Key to find.
Returns:
The value associated with the specified key in a normal way.

reversedGet


public K1 reversedGet(java.lang.Object key2)
Returns the value associated with the specified key in a reversed way.

Parameters:
key2 - Key to find.
Returns:
The value associated with the specified key in a reversed way.

put


public K2 put(K1 key1,

              K2 key2)
Adds or replaces the specified value associated with the specified key in a normal way.

Specified by:
put in interface java.util.Map<K1,K2>
Parameters:
key1 - Key to find or to add.
key2 - Value to associate to the key.
Returns:
The old value associated with the key, or null if the key was not found previously in this map. Returning null can also indicate that the value null was previously associated to the key.

reversedPut


public K1 reversedPut(K2 key2,

                      K1 key1)
Adds or replaces the specified value associated with the specified key in a reversed way.

Parameters:
key2 - Key to find or to add.
key1 - Value to associate to the key.
Returns:
The old value associated with the key, or null if the key was not found previously in this map. Returning null can also indicate that the value null was previously associated to the key.

remove


public K2 remove(java.lang.Object key1)
Removes the value associated with the specified key in a normal way.

Specified by:
remove in interface java.util.Map<K1,K2>
Parameters:
key1 - Key to find and to remove.
Returns:
The old value associated with the key, or null if the key was not found previously in this map. Returning null can also indicate that the value null was previously associated to the key.

reversedRemove


public K1 reversedRemove(java.lang.Object key2)
Removes the value associated with the specified key in a reversed way.

Parameters:
key2 - Key to find and to remove.
Returns:
The old value associated with the key, or null if the key was not found previously in this map. Returning null can also indicate that the value null was previously associated to the key.

putAll


public void putAll(java.util.Map<? extends K1,? extends K2> m)
Copies all key/value entries of the specified map into this map in a normal way. The specified map will stay unchanged after this call.

Specified by:
putAll in interface java.util.Map<K1,K2>
Parameters:
m - Map from which key/value entries will be copied.

reversedPutAll


public void reversedPutAll(java.util.Map<? extends K2,? extends K1> m)
Copies all key/value entries of the specified map into this map in a reversed way. The specified map will stay unchanged after this call.

Parameters:
m - Map from which key/value entries will be copied.

clear


public void clear()
Clears all key/value entries from this map.

Specified by:
clear in interface java.util.Map<K1,K2>

keySet


public java.util.Set<K1> keySet()
Returns a set of all keys contained by this map in a normal way.

Specified by:
keySet in interface java.util.Map<K1,K2>
Returns:
A set of all keys contained by this map in a normal way.

reversedKeySet


public java.util.Set<K2> reversedKeySet()
Returns a set of all keys contained by this map in a reversed way.

Returns:
A set of all keys contained by this map in a reversed way.

values


public java.util.Collection<K2> values()
Returns a collection of all values contained by this map in a normal way.

Specified by:
values in interface java.util.Map<K1,K2>
Returns:
A collection of all values contained by this map in a normal way.

reversedValues


public java.util.Collection<K1> reversedValues()
Returns a collection of all values contained by this map in a normal way.

Returns:
A collection of all values contained by this map in a normal way.

entrySet


public java.util.Set<java.util.Map.Entry<K1,K2>> entrySet()
Returns a set of all key/value entries contained by this map in a normal way.

Specified by:
entrySet in interface java.util.Map<K1,K2>
Returns:
A set of all key/value entries contained by this map in a normal way.

reversedEntrySet


public java.util.Set<java.util.Map.Entry<K2,K1>> reversedEntrySet()
Returns a set of all key/value entries contained by this map in a reversed way.

Returns:
A set of all key/value entries contained by this map in a reversed way.

getLockObject


public java.lang.Object getLockObject()
Returns the object over which the internal and external synchronization will operate. Only implicit and explicit usages of iterator(s) need external synchronization, because all others methods are already internally synchronized.

Specified by:
getLockObject in interface Synchronized
Returns:
The object over which the internal and external synchronization will operate.