N - Namespace type@API(status=EXPERIMENTAL,
since="5.10")
public final class NamespacedHierarchicalStore<N>
extends java.lang.Object
implements java.lang.AutoCloseable
NamespacedHierarchicalStore is a hierarchical, namespaced key-value store.
Its closing behavior can be customized by passing a
NamespacedHierarchicalStore.CloseAction to the
NamespacedHierarchicalStore(NamespacedHierarchicalStore, CloseAction)
constructor.
This class is thread-safe.
| Modifier and Type | Class and Description |
|---|---|
static interface |
NamespacedHierarchicalStore.CloseAction<N>
Called for each successfully stored non-null value in the store when a
NamespacedHierarchicalStore is
closed. |
private static class |
NamespacedHierarchicalStore.CompositeKey<N> |
private static class |
NamespacedHierarchicalStore.EvaluatedValue<N> |
private static class |
NamespacedHierarchicalStore.MemoizingSupplier
Thread-safe
Supplier that memoizes the result of calling its
delegate and ensures it is called at most once. |
private static class |
NamespacedHierarchicalStore.StoredValue |
| Modifier and Type | Field and Description |
|---|---|
private NamespacedHierarchicalStore.CloseAction<N> |
closeAction |
private java.util.concurrent.atomic.AtomicInteger |
insertOrderSequence |
private NamespacedHierarchicalStore<N> |
parentStore |
private java.util.concurrent.ConcurrentMap<NamespacedHierarchicalStore.CompositeKey<N>,NamespacedHierarchicalStore.StoredValue> |
storedValues |
| Constructor and Description |
|---|
NamespacedHierarchicalStore(NamespacedHierarchicalStore<N> parentStore)
Create a new store with the supplied parent.
|
NamespacedHierarchicalStore(NamespacedHierarchicalStore<N> parentStore,
NamespacedHierarchicalStore.CloseAction<N> closeAction)
Create a new store with the supplied parent and close action.
|
| Modifier and Type | Method and Description |
|---|---|
private <T> T |
castToRequiredType(java.lang.Object key,
java.lang.Object value,
java.lang.Class<T> requiredType) |
void |
close()
If a close action is configured, it will be called with all successfully
stored values in reverse insertion order.
|
java.lang.Object |
get(N namespace,
java.lang.Object key)
Get the value stored for the supplied namespace and key in this store or
the parent store, if present.
|
<T> T |
get(N namespace,
java.lang.Object key,
java.lang.Class<T> requiredType)
Get the value stored for the supplied namespace and key in this store or
the parent store, if present, and cast it to the supplied required type.
|
<K,V> java.lang.Object |
getOrComputeIfAbsent(N namespace,
K key,
java.util.function.Function<K,V> defaultCreator)
Get the value stored for the supplied namespace and key in this store or
the parent store, if present, or call the supplied function to compute it.
|
<K,V> V |
getOrComputeIfAbsent(N namespace,
K key,
java.util.function.Function<K,V> defaultCreator,
java.lang.Class<V> requiredType)
Get the value stored for the supplied namespace and key in this store or
the parent store, if present, or call the supplied function to compute it
and, finally, cast it to the supplied required type.
|
private NamespacedHierarchicalStore.StoredValue |
getStoredValue(NamespacedHierarchicalStore.CompositeKey<N> compositeKey) |
NamespacedHierarchicalStore<N> |
newChild()
Create a child store with this store as its parent using the same close
action.
|
java.lang.Object |
put(N namespace,
java.lang.Object key,
java.lang.Object value)
Put the supplied value for the supplied namespace and key into this
store and return the previously associated value in this store.
|
java.lang.Object |
remove(N namespace,
java.lang.Object key)
Remove the value stored for the supplied namespace and key from this
store.
|
<T> T |
remove(N namespace,
java.lang.Object key,
java.lang.Class<T> requiredType)
Remove the value stored for the supplied namespace and key from this
store and cast it to the supplied required type.
|
private NamespacedHierarchicalStore.StoredValue |
storedValue(java.util.function.Supplier<java.lang.Object> value) |
private final java.util.concurrent.atomic.AtomicInteger insertOrderSequence
private final java.util.concurrent.ConcurrentMap<NamespacedHierarchicalStore.CompositeKey<N>,NamespacedHierarchicalStore.StoredValue> storedValues
private final NamespacedHierarchicalStore<N> parentStore
private final NamespacedHierarchicalStore.CloseAction<N> closeAction
public NamespacedHierarchicalStore(NamespacedHierarchicalStore<N> parentStore)
parentStore - the parent store to use for lookups; may be nullpublic NamespacedHierarchicalStore(NamespacedHierarchicalStore<N> parentStore, NamespacedHierarchicalStore.CloseAction<N> closeAction)
parentStore - the parent store to use for lookups; may be nullcloseAction - the action to be called for each stored value when this
store is closed; may be nullpublic NamespacedHierarchicalStore<N> newChild()
public void close()
Closing a store does not close its parent or any of its children.
close in interface java.lang.AutoCloseablepublic java.lang.Object get(N namespace, java.lang.Object key)
namespace - the namespace; never nullkey - the key; never nullnullpublic <T> T get(N namespace, java.lang.Object key, java.lang.Class<T> requiredType) throws NamespacedHierarchicalStoreException
namespace - the namespace; never nullkey - the key; never nullrequiredType - the required type of the value; never nullnullNamespacedHierarchicalStoreException - if the stored value cannot
be cast to the required typepublic <K,V> java.lang.Object getOrComputeIfAbsent(N namespace, K key, java.util.function.Function<K,V> defaultCreator)
namespace - the namespace; never nullkey - the key; never nulldefaultCreator - the function called with the supplied key
to create a new value; never null but may return nullnullpublic <K,V> V getOrComputeIfAbsent(N namespace, K key, java.util.function.Function<K,V> defaultCreator, java.lang.Class<V> requiredType) throws NamespacedHierarchicalStoreException
namespace - the namespace; never nullkey - the key; never nulldefaultCreator - the function called with the supplied key
to create a new value; never null but may return nullrequiredType - the required type of the value; never nullnullNamespacedHierarchicalStoreException - if the stored value cannot
be cast to the required typepublic java.lang.Object put(N namespace, java.lang.Object key, java.lang.Object value) throws NamespacedHierarchicalStoreException
The NamespacedHierarchicalStore.CloseAction will not be called for the previously
stored value, if any.
namespace - the namespace; never nullkey - the key; never nullvalue - the value to store; may be nullnullNamespacedHierarchicalStoreException - if the stored value cannot
be cast to the required typepublic java.lang.Object remove(N namespace, java.lang.Object key)
The NamespacedHierarchicalStore.CloseAction will not be called for the removed
value.
namespace - the namespace; never nullkey - the key; never nullnullpublic <T> T remove(N namespace, java.lang.Object key, java.lang.Class<T> requiredType) throws NamespacedHierarchicalStoreException
The NamespacedHierarchicalStore.CloseAction will not be called for the removed
value.
namespace - the namespace; never nullkey - the key; never nullrequiredType - the required type of the value; never nullnullNamespacedHierarchicalStoreException - if the stored value cannot
be cast to the required typeprivate NamespacedHierarchicalStore.StoredValue storedValue(java.util.function.Supplier<java.lang.Object> value)
private NamespacedHierarchicalStore.StoredValue getStoredValue(NamespacedHierarchicalStore.CompositeKey<N> compositeKey)
private <T> T castToRequiredType(java.lang.Object key,
java.lang.Object value,
java.lang.Class<T> requiredType)