Class AbstractDualBidiMap
- java.lang.Object
-
- org.apache.commons.collections.bidimap.AbstractDualBidiMap
-
- All Implemented Interfaces:
java.util.Map,BidiMap,IterableMap
- Direct Known Subclasses:
DualHashBidiMap,DualTreeBidiMap
public abstract class AbstractDualBidiMap extends java.lang.Object implements BidiMap
AbstractBidiMapimplemented using two maps.An implementation can be written simply by implementing the
createMapmethod.- Since:
- Commons Collections 3.0
- Version:
- $Id: AbstractDualBidiMap.java 646777 2008-04-10 12:33:15Z niallp $
- See Also:
DualHashBidiMap,DualTreeBidiMap
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractDualBidiMap.BidiMapIteratorInner class MapIterator.protected static classAbstractDualBidiMap.EntrySetInner class EntrySet.protected static classAbstractDualBidiMap.EntrySetIteratorInner class EntrySetIterator.protected static classAbstractDualBidiMap.KeySetInner class KeySet.protected static classAbstractDualBidiMap.KeySetIteratorInner class KeySetIterator.protected static classAbstractDualBidiMap.MapEntryInner class MapEntry.protected static classAbstractDualBidiMap.ValuesInner class Values.protected static classAbstractDualBidiMap.ValuesIteratorInner class ValuesIterator.protected static classAbstractDualBidiMap.ViewInner class View.
-
Field Summary
Fields Modifier and Type Field Description protected java.util.SetentrySetView of the entries.protected BidiMapinverseBidiMapInverse view of this map.protected java.util.SetkeySetView of the keys.protected java.util.Map[]mapsDelegate map array.protected java.util.CollectionvaluesView of the values.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractDualBidiMap()Creates an empty map, initialised bycreateMap.protectedAbstractDualBidiMap(java.util.Map normalMap, java.util.Map reverseMap)Creates an empty map using the two maps specified as storage.protectedAbstractDualBidiMap(java.util.Map normalMap, java.util.Map reverseMap, BidiMap inverseBidiMap)Constructs a map that decorates the specified maps, used by the subclasscreateBidiMapimplementation.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclear()booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)protected abstract BidiMapcreateBidiMap(java.util.Map normalMap, java.util.Map reverseMap, BidiMap inverseMap)Creates a new instance of the subclass.protected java.util.IteratorcreateEntrySetIterator(java.util.Iterator iterator)Creates an entry set iterator.protected java.util.IteratorcreateKeySetIterator(java.util.Iterator iterator)Creates a key set iterator.protected java.util.MapcreateMap()Deprecated.For constructors, use the new two map constructor.protected java.util.IteratorcreateValuesIterator(java.util.Iterator iterator)Creates a values iterator.java.util.SetentrySet()Gets an entrySet view of the map.booleanequals(java.lang.Object obj)java.lang.Objectget(java.lang.Object key)java.lang.ObjectgetKey(java.lang.Object value)Gets the key that is currently mapped to the specified value.inthashCode()BidiMapinverseBidiMap()Gets a view of this map where the keys and values are reversed.booleanisEmpty()java.util.SetkeySet()Gets a keySet view of the map.MapIteratormapIterator()Obtains aMapIteratorover the map.java.lang.Objectput(java.lang.Object key, java.lang.Object value)Puts the key-value pair into the map, replacing any previous pair.voidputAll(java.util.Map map)java.lang.Objectremove(java.lang.Object key)java.lang.ObjectremoveValue(java.lang.Object value)Removes the key-value pair that is currently mapped to the specified value (optional operation).intsize()java.lang.StringtoString()java.util.Collectionvalues()Gets a values view of the map.
-
-
-
Field Detail
-
maps
protected final transient java.util.Map[] maps
Delegate map array. The first map contains standard entries, and the second contains inverses.
-
inverseBidiMap
protected transient BidiMap inverseBidiMap
Inverse view of this map.
-
keySet
protected transient java.util.Set keySet
View of the keys.
-
values
protected transient java.util.Collection values
View of the values.
-
entrySet
protected transient java.util.Set entrySet
View of the entries.
-
-
Constructor Detail
-
AbstractDualBidiMap
protected AbstractDualBidiMap()
Creates an empty map, initialised bycreateMap.This constructor remains in place for deserialization. All other usage is deprecated in favour of
AbstractDualBidiMap(Map, Map).
-
AbstractDualBidiMap
protected AbstractDualBidiMap(java.util.Map normalMap, java.util.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.- Parameters:
normalMap- the normal direction mapreverseMap- the reverse direction map- Since:
- Commons Collections 3.1
-
AbstractDualBidiMap
protected AbstractDualBidiMap(java.util.Map normalMap, java.util.Map reverseMap, BidiMap inverseBidiMap)Constructs a map that decorates the specified maps, used by the subclasscreateBidiMapimplementation.- Parameters:
normalMap- the normal direction mapreverseMap- the reverse direction mapinverseBidiMap- the inverse BidiMap
-
-
Method Detail
-
createMap
protected java.util.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 BidiMap createBidiMap(java.util.Map normalMap, java.util.Map reverseMap, BidiMap inverseMap)
Creates a new instance of the subclass.- Parameters:
normalMap- the normal direction mapreverseMap- the reverse direction mapinverseMap- this map, which is the inverse in the new map- Returns:
- the inverse map
-
get
public java.lang.Object get(java.lang.Object key)
- Specified by:
getin interfacejava.util.Map
-
size
public int size()
- Specified by:
sizein interfacejava.util.Map
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Map
-
containsKey
public boolean containsKey(java.lang.Object key)
- Specified by:
containsKeyin interfacejava.util.Map
-
equals
public boolean equals(java.lang.Object obj)
- Specified by:
equalsin interfacejava.util.Map- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCodein interfacejava.util.Map- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
put
public java.lang.Object put(java.lang.Object key, java.lang.Object value)Description copied from interface:BidiMapPuts 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
-
putAll
public void putAll(java.util.Map map)
- Specified by:
putAllin interfacejava.util.Map
-
remove
public java.lang.Object remove(java.lang.Object key)
- Specified by:
removein interfacejava.util.Map
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValuein interfacejava.util.Map
-
mapIterator
public MapIterator mapIterator()
Obtains aMapIteratorover the map. The iterator implementsResetableMapIterator. 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:
mapIteratorin interfaceBidiMap- Specified by:
mapIteratorin interfaceIterableMap- Returns:
- a map iterator
-
getKey
public java.lang.Object getKey(java.lang.Object value)
Description copied from interface:BidiMapGets the key that is currently mapped to the specified value.If the value is not contained in the map,
nullis returned.Implementations should seek to make this method perform equally as well as
get(Object).
-
removeValue
public java.lang.Object removeValue(java.lang.Object value)
Description copied from interface:BidiMapRemoves the key-value pair that is currently mapped to the specified value (optional operation).If the value is not contained in the map,
nullis returned.Implementations should seek to make this method perform equally as well as
remove(Object).- Specified by:
removeValuein interfaceBidiMap- Parameters:
value- the value to find the key-value pair for- Returns:
- the key that was removed,
nullif nothing removed
-
inverseBidiMap
public BidiMap inverseBidiMap()
Description copied from interface:BidiMapGets 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:
inverseBidiMapin interfaceBidiMap- Returns:
- an inverted bidirectional map
-
keySet
public java.util.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.- Specified by:
keySetin interfacejava.util.Map- Returns:
- the keySet view
-
createKeySetIterator
protected java.util.Iterator createKeySetIterator(java.util.Iterator 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 java.util.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.- Specified by:
valuesin interfacejava.util.Map- Returns:
- the values view
-
createValuesIterator
protected java.util.Iterator createValuesIterator(java.util.Iterator 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 java.util.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).
- Specified by:
entrySetin interfacejava.util.Map- Returns:
- the entrySet view
-
createEntrySetIterator
protected java.util.Iterator createEntrySetIterator(java.util.Iterator 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
-
-