Class MapCreateCountdown<K,V>
Object
MapCreateCountdown<K,V>
- Type Parameters:
K
- identifier (key)-typeV
- value-type
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 Summary
ConstructorsConstructorDescriptionMapCreateCountdown
(Supplier<V> createNewElement, CheckedBiConsumer<K, V, OperationFailedException> cleanUpElement) Creates without a comparator, using aHashMap
internally. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Applies the clean-up function on any keys remaining in the map.void
Increments the entry associated withkey
, creating a counter for it if none already exists.keySet()
The keys in the map.void
processElement
(K key, CheckedConsumer<V, OperationFailedException> operation) Runs an operation on the element, creating if necessary, without decrementing the count.void
processElementDecrement
(K key, CheckedConsumer<V, OperationFailedException> operation) Runs an operation on the element, creating if necessary, and decrementing the count.
-
Constructor Details
-
MapCreateCountdown
public MapCreateCountdown(Supplier<V> createNewElement, CheckedBiConsumer<K, V, OperationFailedException> cleanUpElement) Creates without a comparator, using aHashMap
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
Increments the entry associated withkey
, creating a counter for it if none already exists.- Parameters:
key
- the key.
-
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 OperationFailedExceptionRuns an operation on the element, creating if necessary, without decrementing the count.- Parameters:
key
- the key in the map.operation
- the operation to run onkey
.- Throws:
OperationFailedException
- if thrown byoperation
, of ifkey
does not exist in the map.
-
processElementDecrement
public void processElementDecrement(K key, CheckedConsumer<V, OperationFailedException> operation) throws OperationFailedExceptionRuns an operation on the element, creating if necessary, and decrementing the count.- Parameters:
key
- the key in the map.operation
- the operation to run onkey
.- Throws:
OperationFailedException
- if thrown byoperation
, of ifkey
does not exist in the map.
-
cleanUpRemaining
Applies the clean-up function on any keys remaining in the map.- Throws:
OperationFailedException
- if thrown by the clean-up routine.
-