Class RefCountingMap<K,T>

java.lang.Object
org.apache.jena.atlas.lib.RefCountingMap<K,T>
Type Parameters:
K -
T -

public class RefCountingMap<K,T> extends 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
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(K key, T value)
    Add a key value pair to the map.
    void
    Clear the map of all keys regardless of reference counts.
    boolean
    contains(K key)
     
    boolean
     
    get(Object key)
    Return the value associated with a key in the map.
    boolean
     
     
     
    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
     

    Methods inherited from class java.lang.Object

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

    • RefCountingMap

      public RefCountingMap()
  • Method Details

    • contains

      public boolean contains(K key)
    • keys

      public 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 Set<K> keySet()
    • containsKey

      public boolean containsKey(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(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.