Class AbstractDualBidiMap<K,​V>

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractDualBidiMap()
      Creates an empty map, initialized by createMap.
      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.
      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 subclass createBidiMap implementation.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Removes all of the mappings from this map.
      boolean containsKey​(java.lang.Object key)
      Tests for presence of a given key.
      boolean containsValue​(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.
      boolean equals​(java.lang.Object obj)  
      V get​(java.lang.Object key)
      Gets a value at a given key.
      K getKey​(java.lang.Object 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()
      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 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​(java.util.Map<? extends K,​? extends V> map)
      Copies all of the mappings from the specified map to this map.
      V remove​(java.lang.Object key)
      Remove a key-value mappings.
      K removeValue​(java.lang.Object value)
      Removes the key-value pair that is currently mapped to the specified value (optional operation).
      int size()
      Gets the number of key-value mappings in this map.
      java.lang.String toString()  
      java.util.Set<V> values()
      Gets a values view of the map.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Constructor Detail

      • 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 map
        reverseMap - 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 subclass createBidiMap implementation.
        Parameters:
        normalMap - the normal direction map
        reverseMap - the reverse direction map
        inverseBidiMap - the inverse BidiMap
    • Method Detail

      • clear

        public void clear()
        Description copied from interface: Put
        Removes all of the mappings from this map.
        Specified by:
        clear in interface java.util.Map<K,​V>
        Specified by:
        clear in interface Put<K,​V>
        See Also:
        Map.clear()
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Description copied from interface: Get
        Tests for presence of a given key.
        Specified by:
        containsKey in interface Get<K,​V>
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key - key whose presence in this map is to be tested
        Returns:
        true if this map contains a mapping for the specified key
        See Also:
        Map.containsKey(Object)
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Description copied from interface: Get
        Tests for presence of a given value.
        Specified by:
        containsValue in interface Get<K,​V>
        Specified by:
        containsValue in interface java.util.Map<K,​V>
        Parameters:
        value - value whose presence in this map is to be tested
        Returns:
        true if this map maps one or more keys to the specified value
        See Also:
        Map.containsValue(Object)
      • createBidiMap

        protected abstract BidiMap<V,​KcreateBidiMap​(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 map
        reverseMap - the reverse direction map
        inverseMap - 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<KcreateKeySetIterator​(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<VcreateValuesIterator​(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).

        Specified by:
        entrySet in interface Get<K,​V>
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        the entrySet view
        See Also:
        Map.entrySet()
      • equals

        public boolean equals​(java.lang.Object obj)
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
      • get

        public V get​(java.lang.Object key)
        Description copied from interface: Get
        Gets a value at a given key.
        Specified by:
        get in interface Get<K,​V>
        Specified by:
        get in interface java.util.Map<K,​V>
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value to which the specified key is mapped, or null if this map contains no mapping for the key
        See Also:
        Map.get(Object)
      • getKey

        public K getKey​(java.lang.Object 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
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
      • inverseBidiMap

        public BidiMap<V,​KinverseBidiMap()
        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
      • isEmpty

        public boolean isEmpty()
        Description copied from interface: Get
        Tests whether this instance contains any key-value mappings.
        Specified by:
        isEmpty in interface Get<K,​V>
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Returns:
        true if this map contains no key-value mappings
        See Also:
        Map.isEmpty()
      • keySet

        public java.util.Set<KkeySet()
        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 Get<K,​V>
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        the keySet view
        See Also:
        Map.keySet()
      • 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 BidiMap<K,​V>
        Specified by:
        put in interface java.util.Map<K,​V>
        Specified by:
        put in interface Put<K,​V>
        Parameters:
        key - the key to store
        value - the value to store
        Returns:
        the previous value mapped to this key
        See Also:
        Map.put(Object, Object)
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> map)
        Description copied from interface: Put
        Copies all of the mappings from the specified map to this map.
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Specified by:
        putAll in interface Put<K,​V>
        Parameters:
        map - mappings to be stored in this map
        See Also:
        Map.putAll(Map)
      • remove

        public V remove​(java.lang.Object key)
        Description copied from interface: Get
        Remove a key-value mappings.
        Specified by:
        remove in interface Get<K,​V>
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - key whose mapping is to be removed from the map
        Returns:
        the previous value associated with key, or null if there was no mapping for key.
        See Also:
        Map.remove(Object)
      • removeValue

        public K removeValue​(java.lang.Object 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
      • size

        public int size()
        Description copied from interface: Get
        Gets the number of key-value mappings in this map.
        Specified by:
        size in interface Get<K,​V>
        Specified by:
        size in interface java.util.Map<K,​V>
        Returns:
        the number of key-value mappings in this map.
        See Also:
        Map.size()
      • toString

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

        public java.util.Set<Vvalues()
        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 BidiMap<K,​V>
        Specified by:
        values in interface Get<K,​V>
        Specified by:
        values in interface java.util.Map<K,​V>
        Returns:
        the values view
        See Also:
        Map.values()