Class AbstractWeakConcurrentMap<K,V,L>
- java.lang.Object
-
- java.lang.ref.ReferenceQueue<K>
-
- io.opentelemetry.context.internal.shaded.AbstractWeakConcurrentMap<K,V,L>
-
- All Implemented Interfaces:
java.lang.Iterable<java.util.Map.Entry<K,V>>
,java.lang.Runnable
- Direct Known Subclasses:
WeakConcurrentMap
abstract class AbstractWeakConcurrentMap<K,V,L> extends java.lang.ref.ReferenceQueue<K> implements java.lang.Runnable, java.lang.Iterable<java.util.Map.Entry<K,V>>
A thread-safe map with weak keys. Entries are based on a key's system hash code and keys are considered equal only by reference equality. This class offers an abstract-base implementation that allows to override methods. This class does not implement theMap
interface because this implementation is incompatible with the map contract. While iterating over a map's entries, any key that has not passed iteration is referenced non-weakly.This class has been copied as is from https://github.com/raphw/weak-lock-free/blob/ad0e5e0c04d4a31f9485bf12b89afbc9d75473b3/src/main/java/com/blogspot/mydailyjava/weaklockfree/WeakConcurrentMap.java
This class is internal and is hence not for public use. Its APIs are unstable and can change at any time.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
AbstractWeakConcurrentMap.EntryIterator
private class
AbstractWeakConcurrentMap.SimpleEntry
static class
AbstractWeakConcurrentMap.WeakKey<K>
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.concurrent.ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>,V>
target
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractWeakConcurrentMap()
protected
AbstractWeakConcurrentMap(java.util.concurrent.ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>,V> target)
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description int
approximateSize()
Returns the approximate size of this map where the returned number is at least as big as the actual number of entries.void
clear()
Clears the entire map.boolean
containsKey(K key)
protected V
defaultValue(K key)
Creates a default value.void
expungeStaleEntries()
Cleans all unused references.V
get(K key)
V
getIfPresent(K key)
protected abstract L
getLookupKey(K key)
Override with care as it can cause lookup failures if done incorrectly.java.util.Iterator<java.util.Map.Entry<K,V>>
iterator()
V
put(K key, V value)
V
putIfAbsent(K key, V value)
V
putIfProbablyAbsent(K key, V value)
V
remove(K key)
protected abstract void
resetLookupKey(L lookupKey)
Resets any reusable state in the lookup key.void
run()
java.lang.String
toString()
-
-
-
Field Detail
-
target
final java.util.concurrent.ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>,V> target
-
-
Constructor Detail
-
AbstractWeakConcurrentMap
protected AbstractWeakConcurrentMap()
-
AbstractWeakConcurrentMap
protected AbstractWeakConcurrentMap(java.util.concurrent.ConcurrentMap<AbstractWeakConcurrentMap.WeakKey<K>,V> target)
- Parameters:
target
- ConcurrentMap implementation that this class wraps.
-
-
Method Detail
-
getLookupKey
protected abstract L getLookupKey(K key)
Override with care as it can cause lookup failures if done incorrectly. The result must have the sameObject.hashCode()
as the input and beequal to
a weak reference of the key. When overriding this, also overrideresetLookupKey(L)
.
-
resetLookupKey
protected abstract void resetLookupKey(L lookupKey)
Resets any reusable state in the lookup key.
-
get
public V get(K key)
- Parameters:
key
- The key of the entry.- Returns:
- The value of the entry or the default value if it did not exist.
-
getIfPresent
public V getIfPresent(K key)
- Parameters:
key
- The key of the entry.- Returns:
- The value of the entry or null if it did not exist.
-
containsKey
public boolean containsKey(K key)
- Parameters:
key
- The key of the entry.- Returns:
true
if the key already defines a value.
-
put
public V put(K key, V value)
- Parameters:
key
- The key of the entry.value
- The value of the entry.- Returns:
- The previous entry or
null
if it does not exist.
-
putIfAbsent
public V putIfAbsent(K key, V value)
- Parameters:
key
- The key of the entry.value
- The value of the entry.- Returns:
- The previous entry or
null
if it does not exist.
-
putIfProbablyAbsent
public V putIfProbablyAbsent(K key, V value)
- Parameters:
key
- The key of the entry.value
- The value of the entry.- Returns:
- The previous entry or
null
if it does not exist.
-
remove
public V remove(K key)
- Parameters:
key
- The key of the entry.- Returns:
- The removed entry or
null
if it does not exist.
-
clear
public void clear()
Clears the entire map.
-
defaultValue
protected V defaultValue(K key)
Creates a default value. There is no guarantee that the requested value will be set as a once it is created in case that another thread requests a value for a key concurrently.- Parameters:
key
- The key for which to create a default value.- Returns:
- The default value for a key without value or
null
for not defining a default value.
-
expungeStaleEntries
public void expungeStaleEntries()
Cleans all unused references.
-
approximateSize
public int approximateSize()
Returns the approximate size of this map where the returned number is at least as big as the actual number of entries.- Returns:
- The minimum size of this map.
-
run
public void run()
- Specified by:
run
in interfacejava.lang.Runnable
-
iterator
public java.util.Iterator<java.util.Map.Entry<K,V>> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<K>
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-