Module org.junit.platform.engine
Class NamespacedHierarchicalStore<N>
java.lang.Object
org.junit.platform.engine.support.store.NamespacedHierarchicalStore<N>
- Type Parameters:
N
- Namespace type
- All Implemented Interfaces:
AutoCloseable
@API(status=EXPERIMENTAL,
since="5.10")
public final class NamespacedHierarchicalStore<N>
extends Object
implements 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.
- Since:
- 5.10
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Called for each successfully stored non-null value in the store when aNamespacedHierarchicalStore
isclosed
. -
Constructor Summary
ConstructorDescriptionNamespacedHierarchicalStore
(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. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
If a close action is configured, it will be called with all successfully stored values in reverse insertion order.Get the value stored for the supplied namespace and key in this store or the parent store, if present.<T> T
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> Object getOrComputeIfAbsent
(N namespace, K key, 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, Function<K, V> defaultCreator, 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.newChild()
Create a child store with this store as its parent using the same 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> T
Remove 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(NamespacedHierarchicalStore<N> parentStore, 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 benull
closeAction
- 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 using the same close action. -
close
public void close()If a close action is 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.
- Specified by:
close
in interfaceAutoCloseable
-
get
Get the value stored for the supplied namespace and key in this store or the parent store, if present.- Parameters:
namespace
- the namespace; nevernull
key
- the key; nevernull
- Returns:
- the stored value; may be
null
-
get
public <T> 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; nevernull
key
- the key; nevernull
requiredType
- 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
-
getOrComputeIfAbsent
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; nevernull
key
- the key; nevernull
defaultCreator
- the function called with the suppliedkey
to create a new value; nevernull
but may returnnull
- Returns:
- the stored value; may be
null
-
getOrComputeIfAbsent
public <K,V> V getOrComputeIfAbsent(N namespace, K key, Function<K, V> defaultCreator, Class<V> requiredType) throws NamespacedHierarchicalStoreExceptionGet 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; nevernull
key
- the key; nevernull
defaultCreator
- the function called with the suppliedkey
to create a new value; nevernull
but may returnnull
requiredType
- 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
-
put
public Object put(N namespace, Object key, 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.CloseAction
will not be called for the previously stored value, if any.- Parameters:
namespace
- the namespace; nevernull
key
- the key; nevernull
value
- the value to store; may benull
- Returns:
- the previously stored value; may be
null
- Throws:
NamespacedHierarchicalStoreException
- if the stored value cannot be cast to the required type
-
remove
Remove the value stored for the supplied namespace and key from this store.The
NamespacedHierarchicalStore.CloseAction
will not be called for the removed value.- Parameters:
namespace
- the namespace; nevernull
key
- the key; nevernull
- Returns:
- the previously stored value; may be
null
-
remove
public <T> 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.CloseAction
will not be called for the removed value.- Parameters:
namespace
- the namespace; nevernull
key
- the key; nevernull
requiredType
- 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
-