Class NamespacedHierarchicalStore<N>
- Type Parameters:
N- Namespace type
- All Implemented Interfaces:
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. Please note, however, that thread safety is
not guaranteed while the close() method is being invoked.
- Since:
- 1.10
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCalled for each successfully stored non-null value in the store when aNamespacedHierarchicalStoreis closed. -
Constructor Summary
ConstructorsConstructorDescriptionNamespacedHierarchicalStore(@Nullable NamespacedHierarchicalStore<N> parentStore) Create a new store with the supplied parent.NamespacedHierarchicalStore(@Nullable NamespacedHierarchicalStore<N> parentStore, @Nullable NamespacedHierarchicalStore.CloseAction<N> closeAction) Create a new store with the supplied parent and close action. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()If aNamespacedHierarchicalStore.CloseActionis configured, it will be called with all successfully stored values in reverse insertion order.<K,V> Object computeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator) Return the value stored for the supplied namespace and key in this store or the parent store, if present and notnull, or call the supplied function to compute it.<K,V> V computeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType) Return the value stored for the supplied namespace and key in this store or the parent store, if present and notnull, or call the supplied function to compute it and, finally, cast it to the supplied required type.Get the value stored for the supplied namespace and key in this store or the parent store, if present.<T> @Nullable TGet 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.getOrComputeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator) Deprecated.getOrComputeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType) Deprecated.Please usecomputeIfAbsent(Object, Object, Function, Class)instead.Returns the parent store of thisNamespacedHierarchicalStore.booleanisClosed()Determine if this store has been closed.newChild()Create a child store with this store as its parent and this store's close action.Put the supplied value for the supplied namespace and key into this store and return the previously associated value in this store.Remove the value stored for the supplied namespace and key from this store.<T> @Nullable TRemove the value stored for the supplied namespace and key from this store and cast it to the supplied required type.
-
Constructor Details
-
NamespacedHierarchicalStore
Create a new store with the supplied parent.- Parameters:
parentStore- the parent store to use for lookups; may benull
-
NamespacedHierarchicalStore
public NamespacedHierarchicalStore(@Nullable NamespacedHierarchicalStore<N> parentStore, @Nullable NamespacedHierarchicalStore.CloseAction<N> closeAction) Create a new store with the supplied parent and close action.- Parameters:
parentStore- the parent store to use for lookups; may benullcloseAction- the action to be called for each stored value when this store is closed; may benull
-
-
Method Details
-
newChild
Create a child store with this store as its parent and this store's close action. -
getParent
Returns the parent store of thisNamespacedHierarchicalStore.If this store does not have a parent, an empty
Optionalis returned.- Returns:
- an
Optionalcontaining the parent store, or an emptyOptionalif there is no parent - Since:
- 1.13
-
isClosed
Determine if this store has been closed.- Returns:
trueif this store has been closed- Since:
- 1.11
- See Also:
-
close
public void close()If aNamespacedHierarchicalStore.CloseActionis configured, it will be called with all successfully stored values in reverse insertion order.Closing a store does not close its parent or any of its children.
Invocations of this method after the store has already been closed will be ignored.
- Specified by:
closein interfaceAutoCloseable- See Also:
-
get
Get the value stored for the supplied namespace and key in this store or the parent store, if present.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernull- Returns:
- the stored value; may be
null - Throws:
NamespacedHierarchicalStoreException- if this store has already been closed
-
get
public <T> @Nullable T get(N namespace, Object key, Class<T> requiredType) throws NamespacedHierarchicalStoreException 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.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernullrequiredType- the required type of the value; nevernull- Returns:
- the stored value; may be
null - Throws:
NamespacedHierarchicalStoreException- if the stored value cannot be cast to the required type, or if this store has already been closed
-
getOrComputeIfAbsent
@Deprecated(since="6.0") @API(status=DEPRECATED, since="6.0") public <K, V extends @Nullable Object> @Nullable Object getOrComputeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator) Deprecated.Please usecomputeIfAbsent(Object, Object, Function)instead.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.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernulldefaultCreator- the function called with the suppliedkeyto create a new value; nevernullbut may returnnull- Returns:
- the stored value; may be
null - Throws:
NamespacedHierarchicalStoreException- if this store has already been closed
-
computeIfAbsent
@API(status=MAINTAINED, since="6.0") public <K,V> Object computeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator) Return the value stored for the supplied namespace and key in this store or the parent store, if present and notnull, or call the supplied function to compute it.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernulldefaultCreator- the function called with the suppliedkeyto create a new value; nevernulland must not returnnull- Returns:
- the stored value; never
null - Throws:
NamespacedHierarchicalStoreException- if this store has already been closed- Since:
- 6.0
-
getOrComputeIfAbsent
@Deprecated(since="6.0") @API(status=DEPRECATED, since="6.0") public <K, V extends @Nullable Object> @Nullable V getOrComputeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType) throws NamespacedHierarchicalStoreException Deprecated.Please usecomputeIfAbsent(Object, Object, Function, Class)instead.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.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernulldefaultCreator- the function called with the suppliedkeyto create a new value; nevernullbut may returnnullrequiredType- the required type of the value; nevernull- Returns:
- the stored value; may be
null - Throws:
NamespacedHierarchicalStoreException- if the stored value cannot be cast to the required type, or if this store has already been closed
-
computeIfAbsent
@API(status=MAINTAINED, since="6.0") public <K,V> V computeIfAbsent(N namespace, K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType) throws NamespacedHierarchicalStoreException Return the value stored for the supplied namespace and key in this store or the parent store, if present and notnull, or call the supplied function to compute it and, finally, cast it to the supplied required type.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernulldefaultCreator- the function called with the suppliedkeyto create a new value; nevernulland must not returnnullrequiredType- the required type of the value; nevernull- Returns:
- the stored value; never
null - Throws:
NamespacedHierarchicalStoreException- if the stored value cannot be cast to the required type, or if this store has already been closed- Since:
- 6.0
-
put
public @Nullable Object put(N namespace, Object key, @Nullable Object value) throws NamespacedHierarchicalStoreException Put the supplied value for the supplied namespace and key into this store and return the previously associated value in this store.The
NamespacedHierarchicalStore.CloseActionwill not be called for the previously stored value, if any.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernullvalue- the value to store; may benull- Returns:
- the previously stored value; may be
null - Throws:
NamespacedHierarchicalStoreException- if an error occurs while storing the value, or if this store has already been closed
-
remove
Remove the value stored for the supplied namespace and key from this store.The
NamespacedHierarchicalStore.CloseActionwill not be called for the removed value.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernull- Returns:
- the previously stored value; may be
null - Throws:
NamespacedHierarchicalStoreException- if this store has already been closed
-
remove
public <T> @Nullable T remove(N namespace, Object key, Class<T> requiredType) throws NamespacedHierarchicalStoreException Remove the value stored for the supplied namespace and key from this store and cast it to the supplied required type.The
NamespacedHierarchicalStore.CloseActionwill not be called for the removed value.- Parameters:
namespace- the namespace; nevernullkey- the key; nevernullrequiredType- the required type of the value; nevernull- Returns:
- the previously stored value; may be
null - Throws:
NamespacedHierarchicalStoreException- if the stored value cannot be cast to the required type, or if this store has already been closed
-
computeIfAbsent(Object, Object, Function)instead.