gumbo.util
Class AbstractBimap

java.lang.Object
  |
  +--gumbo.util.AbstractBimap
All Implemented Interfaces:
Bimap, java.util.Map
Direct Known Subclasses:
AbstractGroupBimap

public class AbstractBimap
extends java.lang.Object
implements Bimap

An abstract implementation of the Bimap interface. Intended as a lightweight proxy for a bimap's stores, with lazy build of the stores and this object's immutable views. Uses the decorator pattern to wrap an external target store or, if none, uses lazy build for an internal one.

Version:
$Revision: 1.2 $
Author:
Jon Barrilleaux (jonb@jmbaai.com) of JMB and Associates Inc.

Nested Class Summary
 
Nested classes inherited from class java.util.Map
java.util.Map.Entry
 
Constructor Summary
AbstractBimap()
          Constructs an instance, with empty lazy build stores (see newValueStore(), newKeyStore()).
AbstractBimap(java.util.Map init)
          Constructs an instance, with initialized stores (see newValueStore(), newKeyStore()).
AbstractBimap(java.util.Map init, java.util.Map valueMap)
          Constructs an instance, with wrapped and initialized stores.
 
Method Summary
 void clear()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
 java.util.Set entrySet()
          Gets an immutable view of the key-value entries (Map.Entry) in this bimap.
 boolean equals(java.lang.Object obj)
           
 java.lang.Object get(java.lang.Object key)
           
 Bimap getBimap()
          Gets a singleton immutable view of this bimap.
protected  java.util.Map getKeyMapStore()
          Gets this object's value-to-keys (Object to Group-Set of Object) map store.
 java.util.Set getKeys(java.lang.Object value)
          Gets an immutable snapshot of the keys associated with a value.
protected  java.util.Map getValueMapStore()
          Gets this object's key-to-value map store (Object to Object).
 int hashCode()
           
 boolean isEmpty()
           
 java.util.Set keySet()
          Gets an immutable view of the keys (Object) in this bimap.
protected  Group newKeyGroup()
          Called by this object when it needs to build a new key group (Object) in the value-to-keys map.
protected  java.util.Set newKeyGroupStore()
          Called by this object when it needs to lazily build the store for a key group in the value-to-keys map.
protected  java.util.Map newKeyMapStore()
          Called by this object when it needs to lazily build the value-to-keys map store.
protected  java.util.Map newValueMapStore()
          Called by this object when it needs to lazily build the key-to-value map store.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
           
 void putAll(java.util.Map map)
           
 java.lang.Object remove(java.lang.Object key)
           
 void removeAllKeys(java.util.Collection keys)
          Removes all the keys and their associated values.
 void removeAllValues(java.util.Collection values)
          Removes all the values and their associated keys.
 void removeKey(java.lang.Object key)
          Removes the key and its associated value.
 void removeValue(java.lang.Object value)
          Removes the value and its associated keys.
 int size()
           
 java.lang.String toString()
           
 java.util.Collection values()
          Gets an immutable view of the values (Object) in this bimap.
 java.util.Set valueSet()
          Gets an immutable view of the values (Object) in this bimap as a set.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractBimap

public AbstractBimap()
Constructs an instance, with empty lazy build stores (see newValueStore(), newKeyStore()).


AbstractBimap

public AbstractBimap(java.util.Map init)
Constructs an instance, with initialized stores (see newValueStore(), newKeyStore()).

Parameters:
init - The initial key-to-value map (Object to Object). None if null.

AbstractBimap

public AbstractBimap(java.util.Map init,
                     java.util.Map valueMap)
Constructs an instance, with wrapped and initialized stores. Master constructor.

Parameters:
init - The initial key-to-value map (Object to Object). None if null.
valueMap - The target key-to-value map store being wrapped. If null, defaults to the lazy build target (see newValueStore()). If init is non-null, any contents will be lost.
Method Detail

newValueMapStore

protected java.util.Map newValueMapStore()
Called by this object when it needs to lazily build the key-to-value map store. Defaults to a HashMap. Override as needed with a different map type.

Returns:
A new empty store. Never null.

newKeyMapStore

protected java.util.Map newKeyMapStore()
Called by this object when it needs to lazily build the value-to-keys map store. Defaults to a HashMap. Override as needed with a different map type.

Returns:
A new empty store. Never null.

newKeyGroupStore

protected java.util.Set newKeyGroupStore()
Called by this object when it needs to lazily build the store for a key group in the value-to-keys map. Defaults to a HashSet. Override as needed with a different set type.

Returns:
A new empty store. Never null.

getValueMapStore

protected final java.util.Map getValueMapStore()
Gets this object's key-to-value map store (Object to Object). Used by the system to access and lazily build the mutable store if there is none. Master accessor.

Returns:
The group store. Never null.
Throws:
java.lang.IllegalStateException - New value map store is null.

getKeyMapStore

protected final java.util.Map getKeyMapStore()
Gets this object's value-to-keys (Object to Group-Set of Object) map store. Used by the system to access and lazily build the mutable store if there is none. Master accessor.

Returns:
The group store. Never null.
Throws:
java.lang.IllegalStateException - New key map store is null.

newKeyGroup

protected final Group newKeyGroup()
Called by this object when it needs to build a new key group (Object) in the value-to-keys map. Calls newKeyGroupStore() to lazy build the group backing store. Master mutator.

Returns:
A new empty group. Never null.
Throws:
java.lang.IllegalStateException - New key group store is null.

removeValue

public void removeValue(java.lang.Object value)
Description copied from interface: Bimap
Removes the value and its associated keys.

Specified by:
removeValue in interface Bimap

removeAllValues

public void removeAllValues(java.util.Collection values)
Description copied from interface: Bimap
Removes all the values and their associated keys.

Specified by:
removeAllValues in interface Bimap

removeKey

public void removeKey(java.lang.Object key)
Description copied from interface: Bimap
Removes the key and its associated value. Similar in function to remove(Object).

Specified by:
removeKey in interface Bimap

removeAllKeys

public void removeAllKeys(java.util.Collection keys)
Description copied from interface: Bimap
Removes all the keys and their associated values.

Specified by:
removeAllKeys in interface Bimap

getKeys

public java.util.Set getKeys(java.lang.Object value)
Description copied from interface: Bimap
Gets an immutable snapshot of the keys associated with a value.

Specified by:
getKeys in interface Bimap

valueSet

public java.util.Set valueSet()
Description copied from interface: Bimap
Gets an immutable view of the values (Object) in this bimap as a set.

Specified by:
valueSet in interface Bimap

getBimap

public Bimap getBimap()
Description copied from interface: Bimap
Gets a singleton immutable view of this bimap. To avoid unnecessary lazy builds, if possible, use other accessors instead of this view.

Specified by:
getBimap in interface Bimap
Returns:
The view. Never null.

clear

public void clear()
Specified by:
clear in interface java.util.Map

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface java.util.Map

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface java.util.Map

get

public java.lang.Object get(java.lang.Object key)
Specified by:
get in interface java.util.Map

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface java.util.Map

put

public java.lang.Object put(java.lang.Object key,
                            java.lang.Object value)
Specified by:
put in interface java.util.Map

putAll

public void putAll(java.util.Map map)
Specified by:
putAll in interface java.util.Map

remove

public java.lang.Object remove(java.lang.Object key)
Specified by:
remove in interface java.util.Map

size

public int size()
Specified by:
size in interface java.util.Map

keySet

public java.util.Set keySet()
Description copied from interface: Bimap
Gets an immutable view of the keys (Object) in this bimap.

Specified by:
keySet in interface Bimap

entrySet

public java.util.Set entrySet()
Description copied from interface: Bimap
Gets an immutable view of the key-value entries (Map.Entry) in this bimap.

Specified by:
entrySet in interface Bimap

values

public java.util.Collection values()
Description copied from interface: Bimap
Gets an immutable view of the values (Object) in this bimap.

Specified by:
values in interface Bimap

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

equals

public boolean equals(java.lang.Object obj)
Specified by:
equals in interface java.util.Map
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Specified by:
hashCode in interface java.util.Map
Overrides:
hashCode in class java.lang.Object