ptolemy.actor.gt.data
Class TwoWayHashMap<K,V>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.HashMap<K,V>
          extended by ptolemy.actor.gt.data.TwoWayHashMap<K,V>
Type Parameters:
K - The key type.
V - The value type.
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<K,V>
Direct Known Subclasses:
SequentialTwoWayHashMap

public class TwoWayHashMap<K,V>
extends java.util.HashMap<K,V>

A hash map that has one-to-one relationship between keys and values. With the implementation of two hash maps instead of one, the lookup for keys with values is just as efficient as the lookup for values with keys.

Since:
Ptolemy II 6.1
Version:
$Id: TwoWayHashMap.java 57040 2010-01-27 20:52:32Z cxh $
Author:
Thomas Huining Feng
See Also:
Serialized Form
Accepted Rating:
Red (tfeng)
Proposed Rating:
Yellow (tfeng)

Field Summary
private  java.util.HashMap<V,K> _reverseMap
          The reverse hash map that maps values back to keys.
 
Constructor Summary
TwoWayHashMap()
          Construct an empty two-way hash map.
TwoWayHashMap(int initialCapacity)
          Construct an empty two-way hash map with an initial capacity.
TwoWayHashMap(int initialCapacity, float loadFactor)
          Construct an empty two-way hash map with an initial capacity and a load factor.
TwoWayHashMap(java.util.Map<? extends K,? extends V> map)
          Construct a two-way hash map with the given hash map, which must have a one-to-one relationship between elements.
 
Method Summary
 void clear()
          Clear this two-way hash map.
 java.lang.Object clone()
          Clone this two-way hash map and return the clone.
 boolean containsValue(java.lang.Object value)
          Test whether this two-way hash map contains the given value.
 K getKey(java.lang.Object value)
          Given a value, return the corresponding key in this two-way hash map.
 V put(K key, V value)
          Put a value into this two-way hash map and associate it with a key.
 V remove(java.lang.Object key)
          Remove the value associated with the given key.
 java.util.Set<V> values()
          Return a set containing all the values in this two-way hash map.
 
Methods inherited from class java.util.HashMap
containsKey, entrySet, get, isEmpty, keySet, putAll, size
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
equals, hashCode
 

Field Detail

_reverseMap

private java.util.HashMap<V,K> _reverseMap
The reverse hash map that maps values back to keys.

Constructor Detail

TwoWayHashMap

public TwoWayHashMap()
Construct an empty two-way hash map.


TwoWayHashMap

public TwoWayHashMap(java.util.Map<? extends K,? extends V> map)
Construct a two-way hash map with the given hash map, which must have a one-to-one relationship between elements.

Parameters:
map - The hash map.

TwoWayHashMap

public TwoWayHashMap(int initialCapacity)
Construct an empty two-way hash map with an initial capacity.

Parameters:
initialCapacity - The initial capacity.

TwoWayHashMap

public TwoWayHashMap(int initialCapacity,
                     float loadFactor)
Construct an empty two-way hash map with an initial capacity and a load factor.

Parameters:
initialCapacity - The initial capacity.
loadFactor - The load factor.
Method Detail

clear

public void clear()
Clear this two-way hash map.

Specified by:
clear in interface java.util.Map<K,V>
Overrides:
clear in class java.util.HashMap<K,V>

clone

public java.lang.Object clone()
Clone this two-way hash map and return the clone.

Overrides:
clone in class java.util.HashMap<K,V>
Returns:
The clone.

containsValue

public boolean containsValue(java.lang.Object value)
Test whether this two-way hash map contains the given value.

Specified by:
containsValue in interface java.util.Map<K,V>
Overrides:
containsValue in class java.util.HashMap<K,V>
Parameters:
value - The value.
Returns:
true if the value is contained.

getKey

public K getKey(java.lang.Object value)
Given a value, return the corresponding key in this two-way hash map.

Parameters:
value - The value.
Returns:
the key.

put

public V put(K key,
             V value)
Put a value into this two-way hash map and associate it with a key.

Specified by:
put in interface java.util.Map<K,V>
Overrides:
put in class java.util.HashMap<K,V>
Parameters:
key - The key.
value - The value.
Returns:
The value previously associated with the key in this two-way hash map.

remove

public V remove(java.lang.Object key)
Remove the value associated with the given key.

Specified by:
remove in interface java.util.Map<K,V>
Overrides:
remove in class java.util.HashMap<K,V>
Parameters:
key - The key.
Returns:
The removed value, or null if not found.

values

public java.util.Set<V> values()
Return a set containing all the values in this two-way hash map.

Specified by:
values in interface java.util.Map<K,V>
Overrides:
values in class java.util.HashMap<K,V>
Returns:
The set.