org.apache.commons.collections.bidimap
Class AbstractDualBidiMap
java.lang.Object
org.apache.commons.collections.bidimap.AbstractDualBidiMap
- BidiMap, IterableMap, Map
public abstract class AbstractDualBidiMap
extends java.lang.Object
Abstract
BidiMap
implemented using two maps.
An implementation can be written simply by implementing the
createMap
method.
$Id: AbstractDualBidiMap.java,v 1.14 2004/06/22 21:54:35 scolebourne Exp $- Matthew Hawthorne
- Stephen Colebourne
AbstractDualBidiMap() - Creates an empty map, initialised by
createMap .
|
AbstractDualBidiMap(Map normalMap, Map reverseMap) - Creates an empty map using the two maps specified as storage.
|
AbstractDualBidiMap(Map normalMap, Map reverseMap, BidiMap inverseBidiMap) - Constructs a map that decorates the specified maps,
used by the subclass
createBidiMap implementation.
|
void | clear()
|
boolean | containsKey(Object key)
|
boolean | containsValue(Object value)
|
protected abstract BidiMap | createBidiMap(Map normalMap, Map reverseMap, BidiMap inverseMap) - Creates a new instance of the subclass.
|
protected Iterator | createEntrySetIterator(Iterator iterator) - Creates an entry set iterator.
|
protected Iterator | createKeySetIterator(Iterator iterator) - Creates a key set iterator.
|
protected Map | createMap() - For constructors, use the new two map constructor.
|
protected Iterator | createValuesIterator(Iterator iterator) - Creates a values iterator.
|
Set | entrySet() - Gets an entrySet view of the map.
|
boolean | equals(Object obj)
|
Object | get(Object key)
|
Object | getKey(Object value) - Gets the key that is currently mapped to the specified value.
|
int | hashCode()
|
BidiMap | inverseBidiMap() - Gets a view of this map where the keys and values are reversed.
|
boolean | isEmpty()
|
Set | keySet() - Gets a keySet view of the map.
|
MapIterator | mapIterator() - Obtains a
MapIterator over the map.
|
Object | put(Object key, Object value) - Puts the key-value pair into the map, replacing any previous pair.
|
void | putAll(Map map)
|
Object | remove(Object key)
|
Object | removeValue(Object value) - Removes the key-value pair that is currently mapped to the specified
value (optional operation).
|
int | size()
|
String | toString()
|
Collection | values() - Gets a values view of the map.
|
entrySet
protected Set entrySet
View of the entries.
inverseBidiMap
protected BidiMap inverseBidiMap
Inverse view of this map.
keySet
protected Set keySet
View of the keys.
maps
protected final Map[] maps
Delegate map array. The first map contains standard entries, and the
second contains inverses.
values
protected Collection values
View of the values.
AbstractDualBidiMap
protected AbstractDualBidiMap()
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 normalMap,
Map 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.
normalMap
- the normal direction mapreverseMap
- the reverse direction map
AbstractDualBidiMap
protected AbstractDualBidiMap(Map normalMap,
Map reverseMap,
BidiMap inverseBidiMap)
Constructs a map that decorates the specified maps,
used by the subclass createBidiMap
implementation.
normalMap
- the normal direction mapreverseMap
- the reverse direction mapinverseBidiMap
- the inverse BidiMap
containsKey
public boolean containsKey(Object key)
containsValue
public boolean containsValue(Object value)
createBidiMap
protected abstract BidiMap createBidiMap(Map normalMap,
Map reverseMap,
BidiMap inverseMap)
Creates a new instance of the subclass.
normalMap
- the normal direction mapreverseMap
- the reverse direction mapinverseMap
- this map, which is the inverse in the new map
createEntrySetIterator
protected Iterator createEntrySetIterator(Iterator iterator)
Creates an entry set iterator.
Subclasses can override this to return iterators with different properties.
iterator
- the iterator to decorate
createKeySetIterator
protected Iterator createKeySetIterator(Iterator iterator)
Creates a key set iterator.
Subclasses can override this to return iterators with different properties.
iterator
- the iterator to decorate
createMap
protected Map createMap()
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.
- the map to be used for internal storage
createValuesIterator
protected Iterator createValuesIterator(Iterator iterator)
Creates a values iterator.
Subclasses can override this to return iterators with different properties.
iterator
- the iterator to decorate
entrySet
public Set 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).
equals
public boolean equals(Object obj)
get
public Object get(Object key)
getKey
public Object getKey(Object value)
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)
.
- getKey in interface BidiMap
value
- the value to find the key for
- the mapped key, or
null
if not found
hashCode
public int hashCode()
inverseBidiMap
public BidiMap inverseBidiMap()
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.
- inverseBidiMap in interface BidiMap
- an inverted bidirectional map
isEmpty
public boolean isEmpty()
keySet
public Set 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.
mapIterator
public MapIterator 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).
- mapIterator in interface BidiMap
- mapIterator in interface IterableMap
put
public Object put(Object key,
Object value)
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
- put in interface BidiMap
key
- the key to storevalue
- the value to store
- the previous value mapped to this key
putAll
public void putAll(Map map)
remove
public Object remove(Object key)
removeValue
public Object removeValue(Object value)
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)
.
- removeValue in interface BidiMap
value
- the value to find the key-value pair for
- the key that was removed,
null
if nothing removed
toString
public String toString()
values
public Collection 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.
Copyright © 2001-2006 Apache Software Foundation. All Rights Reserved.