Class MapCreateCountdown<K,V>

Object
MapCreateCountdown<K,V>
Type Parameters:
K - identifier (key)-type
V - value-type

public class MapCreateCountdown<K,V> extends Object
Like a MapCreate but with an associated counter for each element.

The counter is initially incremented, and decremented each time an element is accessed.

When the counter reaches 0 for a particular element, a function is called on the element, and the element is removed from the map.

The actually element is created lazily, only when first needed.

An element may never be null.

This structure is thread-safe.

Author:
Owen Feehan
  • Constructor Details

    • MapCreateCountdown

      public MapCreateCountdown(Supplier<V> createNewElement, CheckedBiConsumer<K,V,OperationFailedException> cleanUpElement)
      Creates without a comparator, using a HashMap internally.
      Parameters:
      createNewElement - called as necessary to create a new element in the tree.
      cleanUpElement - called on an element when its reference count reaches 0, after it is removed from the map.
  • Method Details

    • increment

      public void increment(K key)
      Increments the entry associated with key, creating a counter for it if none already exists.
      Parameters:
      key - the key.
    • keySet

      public Set<K> keySet()
      The keys in the map.
      Returns:
      a set view of the keys contained in this map.
    • processElement

      public void processElement(K key, CheckedConsumer<V,OperationFailedException> operation) throws OperationFailedException
      Runs an operation on the element, creating if necessary, without decrementing the count.
      Parameters:
      key - the key in the map.
      operation - the operation to run on key.
      Throws:
      OperationFailedException - if thrown by operation, of if key does not exist in the map.
    • processElementDecrement

      public void processElementDecrement(K key, CheckedConsumer<V,OperationFailedException> operation) throws OperationFailedException
      Runs an operation on the element, creating if necessary, and decrementing the count.
      Parameters:
      key - the key in the map.
      operation - the operation to run on key.
      Throws:
      OperationFailedException - if thrown by operation, of if key does not exist in the map.
    • cleanUpRemaining

      public void cleanUpRemaining() throws OperationFailedException
      Applies the clean-up function on any keys remaining in the map.
      Throws:
      OperationFailedException - if thrown by the clean-up routine.