Class AbstractDualBidiMap<K,V>
- java.lang.Object
-
- org.apache.commons.collections4.bidimap.AbstractDualBidiMap<K,V>
-
- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map
- All Implemented Interfaces:
java.util.Map<K,V>,BidiMap<K,V>,Get<K,V>,IterableGet<K,V>,IterableMap<K,V>,Put<K,V>
- Direct Known Subclasses:
DualHashBidiMap,DualLinkedHashBidiMap,DualTreeBidiMap
public abstract class AbstractDualBidiMap<K,V> extends java.lang.Object implements BidiMap<K,V>
AbstractBidiMapimplemented using two maps.An implementation can be written simply by implementing the
createBidiMap(Map, Map, BidiMap)method.- Since:
- 3.0
- See Also:
DualHashBidiMap,DualTreeBidiMap
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractDualBidiMap.BidiMapIterator<K,V>Inner class MapIterator.protected static classAbstractDualBidiMap.EntrySet<K,V>Inner class EntrySet.protected static classAbstractDualBidiMap.EntrySetIterator<K,V>Inner class EntrySetIterator.protected static classAbstractDualBidiMap.KeySet<K>Inner class KeySet.protected static classAbstractDualBidiMap.KeySetIterator<K>Inner class KeySetIterator.protected static classAbstractDualBidiMap.MapEntry<K,V>Inner class MapEntry.protected static classAbstractDualBidiMap.Values<V>Inner class Values.protected static classAbstractDualBidiMap.ValuesIterator<V>Inner class ValuesIterator.protected static classAbstractDualBidiMap.View<K,V,E>Inner class View.
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractDualBidiMap()Creates an empty map, initialized bycreateMap.protectedAbstractDualBidiMap(java.util.Map<K,V> normalMap, java.util.Map<V,K> reverseMap)Creates an empty map using the two maps specified as storage.protectedAbstractDualBidiMap(java.util.Map<K,V> normalMap, java.util.Map<V,K> reverseMap, BidiMap<V,K> inverseBidiMap)Constructs a map that decorates the specified maps, used by the subclasscreateBidiMapimplementation.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidclear()Removes all of the mappings from this map.booleancontainsKey(java.lang.Object key)Tests for presence of a given key.booleancontainsValue(java.lang.Object value)Tests for presence of a given value.protected abstract BidiMap<V,K>createBidiMap(java.util.Map<V,K> normalMap, java.util.Map<K,V> reverseMap, BidiMap<K,V> inverseMap)Creates a new instance of the subclass.protected java.util.Iterator<java.util.Map.Entry<K,V>>createEntrySetIterator(java.util.Iterator<java.util.Map.Entry<K,V>> iterator)Creates an entry set iterator.protected java.util.Iterator<K>createKeySetIterator(java.util.Iterator<K> iterator)Creates a key set iterator.protected java.util.Iterator<V>createValuesIterator(java.util.Iterator<V> iterator)Creates a values iterator.java.util.Set<java.util.Map.Entry<K,V>>entrySet()Gets an entrySet view of the map.booleanequals(java.lang.Object obj)Vget(java.lang.Object key)Gets a value at a given key.KgetKey(java.lang.Object value)Gets the key that is currently mapped to the specified value.inthashCode()BidiMap<V,K>inverseBidiMap()Gets a view of this map where the keys and values are reversed.booleanisEmpty()Tests whether this instance contains any key-value mappings.java.util.Set<K>keySet()Gets a keySet view of the map.MapIterator<K,V>mapIterator()Obtains aMapIteratorover the map.Vput(K key, V value)Puts the key-value pair into the map, replacing any previous pair.voidputAll(java.util.Map<? extends K,? extends V> map)Copies all of the mappings from the specified map to this map.Vremove(java.lang.Object key)Remove a key-value mappings.KremoveValue(java.lang.Object value)Removes the key-value pair that is currently mapped to the specified value (optional operation).intsize()Gets the number of key-value mappings in this map.java.lang.StringtoString()java.util.Set<V>values()Gets a values view of the map.
-
-
-
Constructor Detail
-
AbstractDualBidiMap
protected AbstractDualBidiMap()
Creates an empty map, initialized bycreateMap.This constructor remains in place for deserialization. All other usage is deprecated in favor of
AbstractDualBidiMap(Map, Map).
-
AbstractDualBidiMap
protected AbstractDualBidiMap(java.util.Map<K,V> normalMap, java.util.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 mapreverseMap- the reverse direction map- Since:
- 3.1
-
AbstractDualBidiMap
protected AbstractDualBidiMap(java.util.Map<K,V> normalMap, java.util.Map<V,K> reverseMap, BidiMap<V,K> 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
-
clear
public void clear()
Description copied from interface:PutRemoves all of the mappings from this map.
-
containsKey
public boolean containsKey(java.lang.Object key)
Description copied from interface:GetTests for presence of a given key.
-
containsValue
public boolean containsValue(java.lang.Object value)
Description copied from interface:GetTests for presence of a given value.
-
createBidiMap
protected abstract BidiMap<V,K> createBidiMap(java.util.Map<V,K> normalMap, java.util.Map<K,V> reverseMap, BidiMap<K,V> 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 bidi map
-
createEntrySetIterator
protected java.util.Iterator<java.util.Map.Entry<K,V>> createEntrySetIterator(java.util.Iterator<java.util.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
-
createKeySetIterator
protected java.util.Iterator<K> createKeySetIterator(java.util.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
-
createValuesIterator
protected java.util.Iterator<V> createValuesIterator(java.util.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 java.util.Set<java.util.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).
-
equals
public boolean equals(java.lang.Object obj)
-
get
public V get(java.lang.Object key)
Description copied from interface:GetGets a value at a given key.
-
getKey
public K 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).
-
hashCode
public int hashCode()
-
inverseBidiMap
public BidiMap<V,K> 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<K,V>- Returns:
- an inverted bidirectional map
-
isEmpty
public boolean isEmpty()
Description copied from interface:GetTests whether this instance contains any key-value mappings.
-
keySet
public java.util.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.
-
mapIterator
public MapIterator<K,V> mapIterator()
Obtains aMapIteratorover the map. The iterator implementsAbstractDualBidiMap.BidiMapIterator. This implementation relies on the entrySet iterator.- Specified by:
mapIteratorin interfaceIterableGet<K,V>- Returns:
- a map iterator
-
put
public V put(K key, V 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<? extends K,? extends V> map)
Description copied from interface:PutCopies all of the mappings from the specified map to this map.
-
remove
public V remove(java.lang.Object key)
Description copied from interface:GetRemove a key-value mappings.
-
removeValue
public K 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<K,V>- Parameters:
value- the value to find the key-value pair for- Returns:
- the key that was removed,
nullif nothing removed
-
size
public int size()
Description copied from interface:GetGets the number of key-value mappings in this map.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-