org.apache.commons.collections.bidimap
Class AbstractDualBidiMap<K,V>

java.lang.Object
  extended by org.apache.commons.collections.bidimap.AbstractDualBidiMap<K,V>
All Implemented Interfaces:
Map<K,V>, BidiMap<K,V>, IterableMap<K,V>
Direct Known Subclasses:
DualHashBidiMap, DualTreeBidiMap

public abstract class AbstractDualBidiMap<K,V>
extends Object
implements BidiMap<K,V>

Abstract BidiMap implemented using two maps.

An implementation can be written simply by implementing the createMap method.

Since:
Commons Collections 3.0
Version:
$Id: AbstractDualBidiMap.java,v 1.1.1.1 2005/05/23 04:33:38 pents90 Exp $
Author:
Matthew Hawthorne, Matt Hall, John Watkinson, Stephen Colebourne
See Also:
DualHashBidiMap, DualTreeBidiMap

Nested Class Summary
protected static class AbstractDualBidiMap.BidiMapIterator<K,V>
          Inner class MapIterator.
protected static class AbstractDualBidiMap.EntrySet<K,V>
          Inner class EntrySet.
protected static class AbstractDualBidiMap.EntrySetIterator<K,V>
          Inner class EntrySetIterator.
protected static class AbstractDualBidiMap.KeySet<K,V>
          Inner class KeySet.
protected static class AbstractDualBidiMap.KeySetIterator<K,V>
          Inner class KeySetIterator.
protected static class AbstractDualBidiMap.MapEntry<K,V>
          Inner class MapEntry.
protected static class AbstractDualBidiMap.Values<K,V>
          Inner class Values.
protected static class AbstractDualBidiMap.ValuesIterator<K,V>
          Inner class ValuesIterator.
protected static class AbstractDualBidiMap.View<K,V,E>
          Inner class View.
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
protected  Set<Map.Entry<K,V>> entrySet
          View of the entries.
protected  Map<K,V> forwardMap
          Delegate maps.
protected  BidiMap<V,K> inverseBidiMap
          Inverse view of this map.
protected  Map<V,K> inverseMap
           
protected  Set<K> keySet
          View of the keys.
protected  Collection<V> values
          View of the values.
 
Constructor Summary
protected AbstractDualBidiMap()
          Deprecated. should not be used.
protected AbstractDualBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap)
          Creates an empty map using the two maps specified as storage.
protected AbstractDualBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap, BidiMap<V,K> inverseBidiMap)
          Constructs a map that decorates the specified maps, used by the subclass createBidiMap implementation.
 
Method Summary
 void clear()
           
 boolean containsKey(Object key)
           
 boolean containsValue(Object value)
           
protected abstract
<K,V> BidiMap<K,V>
createBidiMap(Map<K,V> normalMap, Map<V,K> reverseMap, BidiMap<V,K> inverseMap)
          Creates a new instance of the subclass.
protected  Iterator<Map.Entry<K,V>> createEntrySetIterator(Iterator<Map.Entry<K,V>> iterator)
          Creates an entry set iterator.
protected  Iterator<K> createKeySetIterator(Iterator<K> iterator)
          Creates a key set iterator.
protected  Map createMap()
          Deprecated. For constructors, use the new two map constructor. For deserialization, populate the maps array directly in readObject.
protected  Iterator<V> createValuesIterator(Iterator<V> iterator)
          Creates a values iterator.
 Set<Map.Entry<K,V>> entrySet()
          Gets an entrySet view of the map.
 boolean equals(Object obj)
           
 V get(Object key)
           
 K getKey(V value)
          Gets the key that is currently mapped to the specified value.
 int hashCode()
           
 BidiMap<V,K> inverseBidiMap()
          Gets a view of this map where the keys and values are reversed.
 boolean isEmpty()
           
 Set<K> keySet()
          Gets a keySet view of the map.
 MapIterator<K,V> mapIterator()
          Obtains a MapIterator over the map.
 V put(K key, V value)
          Puts the key-value pair into the map, replacing any previous pair.
 void putAll(Map<? extends K,? extends V> map)
           
 V remove(Object key)
           
 K removeValue(V value)
          Removes the key-value pair that is currently mapped to the specified value (optional operation).
 int size()
           
 String toString()
           
 Collection<V> values()
          Gets a values view of the map.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

forwardMap

protected transient Map<K,V> forwardMap
Delegate maps. The first map contains standard entries, and the second contains inverses.


inverseMap

protected transient Map<V,K> inverseMap

inverseBidiMap

protected transient BidiMap<V,K> inverseBidiMap
Inverse view of this map.


keySet

protected transient Set<K> keySet
View of the keys.


values

protected transient Collection<V> values
View of the values.


entrySet

protected transient Set<Map.Entry<K,V>> entrySet
View of the entries.

Constructor Detail

AbstractDualBidiMap

protected AbstractDualBidiMap()
Deprecated. should not be used.

Creates an empty map, initialised by createMap.

This constructor remains in place for deserialization. All other usage is deprecated in favour of AbstractDualBidiMap(Map, Map).


AbstractDualBidiMap

protected AbstractDualBidiMap(Map<K,V> normalMap,
                              Map<V,K> reverseMap)
Creates an empty map using the two maps specified as storage.

The two maps must be a matching pair, normal and reverse. They will typically both be empty.

Neither map is validated, so nulls may be passed in. If you choose to do this then the subclass constructor must populate the maps[] instance variable itself.

Parameters:
normalMap - the normal direction map
reverseMap - the reverse direction map
Since:
Commons Collections 3.1

AbstractDualBidiMap

protected AbstractDualBidiMap(Map<K,V> normalMap,
                              Map<V,K> reverseMap,
                              BidiMap<V,K> inverseBidiMap)
Constructs a map that decorates the specified maps, used by the subclass createBidiMap implementation.

Parameters:
normalMap - the normal direction map
reverseMap - the reverse direction map
inverseBidiMap - the inverse BidiMap
Method Detail

createMap

protected Map createMap()
Deprecated. For constructors, use the new two map constructor. For deserialization, populate the maps array directly in readObject.

Creates a new instance of the map used by the subclass to store data.

This design is deeply flawed and has been deprecated. It relied on subclass data being used during a superclass constructor.

Returns:
the map to be used for internal storage

createBidiMap

protected abstract <K,V> BidiMap<K,V> createBidiMap(Map<K,V> normalMap,
                                                    Map<V,K> reverseMap,
                                                    BidiMap<V,K> inverseMap)
Creates a new instance of the subclass.

Parameters:
normalMap - the normal direction map
reverseMap - the reverse direction map
inverseMap - this map, which is the inverse in the new map
Returns:
the inverse map

get

public V get(Object key)
Specified by:
get in interface Map<K,V>

size

public int size()
Specified by:
size in interface Map<K,V>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Map<K,V>

containsKey

public boolean containsKey(Object key)
Specified by:
containsKey in interface Map<K,V>

equals

public boolean equals(Object obj)
Specified by:
equals in interface Map<K,V>
Overrides:
equals in class Object

hashCode

public int hashCode()
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in class Object

toString

public String toString()
Overrides:
toString in class Object

put

public V put(K key,
             V value)
Description copied from interface: BidiMap
Puts the key-value pair into the map, replacing any previous pair.

When adding a key-value pair, the value may already exist in the map against a different key. That mapping is removed, to ensure that the value only occurs once in the inverse map.

  BidiMap map1 = new DualHashBidiMap();
  map.put("A","B");  // contains A mapped to B, as per Map
  map.put("A","C");  // contains A mapped to C, as per Map
 

BidiMap map2 = new DualHashBidiMap(); map.put("A","B"); // contains A mapped to B, as per Map map.put("C","B"); // contains C mapped to B, key A is removed

Specified by:
put in interface Map<K,V>
Specified by:
put in interface BidiMap<K,V>
Parameters:
key - the key to store
value - the value to store
Returns:
the previous value mapped to this key

putAll

public void putAll(Map<? extends K,? extends V> map)
Specified by:
putAll in interface Map<K,V>

remove

public V remove(Object key)
Specified by:
remove in interface Map<K,V>

clear

public void clear()
Specified by:
clear in interface Map<K,V>

containsValue

public boolean containsValue(Object value)
Specified by:
containsValue in interface Map<K,V>

mapIterator

public MapIterator<K,V> mapIterator()
Obtains a MapIterator over the map. The iterator implements ResetableMapIterator. This implementation relies on the entrySet iterator.

The setValue() methods only allow a new value to be set. If the value being set is already in the map, an IllegalArgumentException is thrown (as setValue cannot change the size of the map).

Specified by:
mapIterator in interface BidiMap<K,V>
Specified by:
mapIterator in interface IterableMap<K,V>
Returns:
a map iterator

getKey

public K getKey(V value)
Description copied from interface: BidiMap
Gets the key that is currently mapped to the specified value.

If the value is not contained in the map, null is returned.

Implementations should seek to make this method perform equally as well as get(Object).

Specified by:
getKey in interface BidiMap<K,V>
Parameters:
value - the value to find the key for
Returns:
the mapped key, or null if not found

removeValue

public K removeValue(V value)
Description copied from interface: BidiMap
Removes the key-value pair that is currently mapped to the specified value (optional operation).

If the value is not contained in the map, null is returned.

Implementations should seek to make this method perform equally as well as remove(Object).

Specified by:
removeValue in interface BidiMap<K,V>
Parameters:
value - the value to find the key-value pair for
Returns:
the key that was removed, null if nothing removed

inverseBidiMap

public BidiMap<V,K> inverseBidiMap()
Description copied from interface: BidiMap
Gets a view of this map where the keys and values are reversed.

Changes to one map will be visible in the other and vice versa. This enables both directions of the map to be accessed as a Map.

Implementations should seek to avoid creating a new object every time this method is called. See AbstractMap.values() etc. Calling this method on the inverse map should return the original.

Specified by:
inverseBidiMap in interface BidiMap<K,V>
Returns:
an inverted bidirectional map

keySet

public Set<K> keySet()
Gets a keySet view of the map. Changes made on the view are reflected in the map. The set supports remove and clear but not add.

Specified by:
keySet in interface Map<K,V>
Returns:
the keySet view

createKeySetIterator

protected Iterator<K> createKeySetIterator(Iterator<K> iterator)
Creates a key set iterator. Subclasses can override this to return iterators with different properties.

Parameters:
iterator - the iterator to decorate
Returns:
the keySet iterator

values

public Collection<V> values()
Gets a values view of the map. Changes made on the view are reflected in the map. The set supports remove and clear but not add.

Specified by:
values in interface Map<K,V>
Returns:
the values view

createValuesIterator

protected Iterator<V> createValuesIterator(Iterator<V> iterator)
Creates a values iterator. Subclasses can override this to return iterators with different properties.

Parameters:
iterator - the iterator to decorate
Returns:
the values iterator

entrySet

public Set<Map.Entry<K,V>> entrySet()
Gets an entrySet view of the map. Changes made on the set are reflected in the map. The set supports remove and clear but not add.

The Map Entry setValue() method only allow a new value to be set. If the value being set is already in the map, an IllegalArgumentException is thrown (as setValue cannot change the size of the map).

Specified by:
entrySet in interface Map<K,V>
Returns:
the entrySet view

createEntrySetIterator

protected Iterator<Map.Entry<K,V>> createEntrySetIterator(Iterator<Map.Entry<K,V>> iterator)
Creates an entry set iterator. Subclasses can override this to return iterators with different properties.

Parameters:
iterator - the iterator to decorate
Returns:
the entrySet iterator


Copyright © 2005-2005 Apache Software Foundation, Matt Hall, John Watkinson. All Rights Reserved.