K
- identifier (key)-typeV
- value-typepublic class MapCreateCountdown<K,V> extends Object
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.
Constructor and Description |
---|
MapCreateCountdown(java.util.function.Supplier<V> createNewElement,
CheckedBiConsumer<K,V,OperationFailedException> cleanUpElement)
Creates without a comparator, using a
HashMap internally. |
Modifier and Type | Method and Description |
---|---|
void |
cleanUpRemaining()
Applies the clean-up function on any keys remaining in the map.
|
void |
increment(K key)
Increments the entry associated with
key , creating a counter for it if none already
exists. |
Set<K> |
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.
|
public MapCreateCountdown(java.util.function.Supplier<V> createNewElement, CheckedBiConsumer<K,V,OperationFailedException> cleanUpElement)
HashMap
internally.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.public void increment(K key)
key
, creating a counter for it if none already
exists.key
- the key.public Set<K> keySet()
public void processElement(K key, CheckedConsumer<V,OperationFailedException> operation) throws OperationFailedException
key
- the key in the map.operation
- the operation to run on key
.OperationFailedException
- if thrown by operation
, of if key
does not
exist in the map.public void processElementDecrement(K key, CheckedConsumer<V,OperationFailedException> operation) throws OperationFailedException
key
- the key in the map.operation
- the operation to run on key
.OperationFailedException
- if thrown by operation
, of if key
does not
exist in the map.public void cleanUpRemaining() throws OperationFailedException
OperationFailedException
- if thrown by the clean-up routine.Copyright © 2010–2023 Owen Feehan, ETH Zurich, University of Zurich, Hoffmann-La Roche. All rights reserved.