Package io.grpc.xds.internal.security
Class ReferenceCountingMap<K,V extends Closeable>
- java.lang.Object
-
- io.grpc.xds.internal.security.ReferenceCountingMap<K,V>
-
- Type Parameters:
K
- Key type for the mapV
- Value type for the map - it should be aCloseable
@ThreadSafe public final class ReferenceCountingMap<K,V extends Closeable> extends java.lang.Object
A map for managing reference-counted shared resources - typically providers.A key (of generic type K) identifies a provider (of generic type V). The map also depends on a factory
ReferenceCountingMap.ValueFactory
to create a new instance of V as needed. Values are ref-counted and closed by callingCloseable.close()
when ref-count reaches zero.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
ReferenceCountingMap.Instance<V extends Closeable>
static interface
ReferenceCountingMap.ValueFactory<K,V extends Closeable>
A factory to create a value from the given key.
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<K,ReferenceCountingMap.Instance<V>>
instances
private ReferenceCountingMap.ValueFactory<K,V>
valueFactory
-
Constructor Summary
Constructors Constructor Description ReferenceCountingMap(ReferenceCountingMap.ValueFactory<K,V> valueFactory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description V
get(K key)
Gets an existing instance of a provider.private V
getInternal(K key)
V
release(K key, V value)
Releases an instance of the given value.private V
releaseInternal(K key, V value)
-
-
-
Field Detail
-
instances
private final java.util.Map<K,ReferenceCountingMap.Instance<V extends Closeable>> instances
-
valueFactory
private final ReferenceCountingMap.ValueFactory<K,V extends Closeable> valueFactory
-
-
Constructor Detail
-
ReferenceCountingMap
public ReferenceCountingMap(ReferenceCountingMap.ValueFactory<K,V> valueFactory)
-
-
Method Detail
-
get
@CheckReturnValue public V get(K key)
Gets an existing instance of a provider. If it doesn't exist, creates a new one using the provided<K, V>
-
release
public V release(K key, V value)
Releases an instance of the given value.The instance must have been obtained from
get(Object)
. Otherwise will throw IllegalArgumentException.Caller must not release a reference more than once. It's advised that you clear the reference to the instance with the null returned by this method.
- Parameters:
key
- for the instance to be releasedvalue
- the instance to be released- Returns:
- a null which the caller can use to clear the reference to that instance.
-
-