Class RefCountingMap<K,​T>

  • Type Parameters:
    K -
    T -

    public class RefCountingMap<K,​T>
    extends java.lang.Object
    A key -> value 'map' which reference counts entries.

    The same (key,value) pair can be added to the map several times and then removed several times. A reference count is incremented for each addition and, provided the count is greater than 0, decremented on removal.

    The pair is removed from the map when a remove decrements the reference count to 0.

    This class is thread safe.

    • Constructor Summary

      Constructors 
      Constructor Description
      RefCountingMap()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(K key, T value)
      Add a key value pair to the map.
      void clear()
      Clear the map of all keys regardless of reference counts.
      boolean contains​(K key)  
      boolean containsKey​(java.lang.Object key)  
      T get​(java.lang.Object key)
      Return the value associated with a key in the map.
      boolean isEmpty()  
      java.util.Collection<K> keys()  
      java.util.Set<K> keySet()  
      int refCount​(K key)
      Return the reference count for the entry corresponding to a key in the map.
      void remove​(K key)
      Decrement the reference count for a key, and remove the corresponding entry from the map if the result is 0.
      void removeAll​(K key)
      Remove the entry corresponding to the key from the map completely.
      int size()  
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RefCountingMap

        public RefCountingMap()
    • Method Detail

      • contains

        public boolean contains​(K key)
      • keys

        public java.util.Collection<K> keys()
      • size

        public int size()
      • isEmpty

        public boolean isEmpty()
      • clear

        public void clear()
        Clear the map of all keys regardless of reference counts.
      • keySet

        public java.util.Set<K> keySet()
      • containsKey

        public boolean containsKey​(java.lang.Object key)
      • add

        public void add​(K key,
                        T value)
        Add a key value pair to the map.

        if there is no entry in the map for the key, then a key value pair is added to the map with a reference count of 1.

        If there is already an entry in the map for the same key and value, the reference count for that entry is incremented.

        if there is an entry in the map for the key, but with a different value, then that entry is replaced with a new entry for the key and value with a reference count of 1.

        Parameters:
        key -
        value -
      • remove

        public void remove​(K key)
        Decrement the reference count for a key, and remove the corresponding entry from the map if the result is 0.

        Do nothing if there is no entry in the map corresponding to the key.

        Parameters:
        key -
      • removeAll

        public void removeAll​(K key)
        Remove the entry corresponding to the key from the map completely.

        This method ignores the reference count.

        Parameters:
        key -
      • refCount

        public int refCount​(K key)
        Return the reference count for the entry corresponding to a key in the map.

        Returns 0 if there is no entry in the map corresponding to the key.

        Parameters:
        key -
        Returns:
        the reference count for the entry corresponding to key in the map, or 0 if there is no corresponding entry.
      • get

        public T get​(java.lang.Object key)
        Return the value associated with a key in the map.
        Parameters:
        key -
        Returns:
        the value associated with the key, or null if there is no such value.