Class NamespacedHierarchicalStore<N>
- java.lang.Object
-
- org.junit.platform.engine.support.store.NamespacedHierarchicalStore<N>
-
- Type Parameters:
N- Namespace type
- All Implemented Interfaces:
java.lang.AutoCloseable
@API(status=MAINTAINED, since="1.13.3") public final class NamespacedHierarchicalStore<N> extends java.lang.Object implements java.lang.AutoCloseableNamespacedHierarchicalStoreis a hierarchical, namespaced key-value store.Its closing behavior can be customized by passing a
NamespacedHierarchicalStore.CloseActionto theNamespacedHierarchicalStore(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 Classes Modifier and Type Class Description static interfaceNamespacedHierarchicalStore.CloseAction<N>Called for each successfully stored non-null value in the store when aNamespacedHierarchicalStoreis closed.private static classNamespacedHierarchicalStore.CompositeKey<N>private static classNamespacedHierarchicalStore.EvaluatedValue<N>private static classNamespacedHierarchicalStore.MemoizingSupplierThread-safeSupplierthat memoizes the result of calling its delegate and ensures it is called at most once.private static classNamespacedHierarchicalStore.StoredValue
-
Field Summary
Fields Modifier and Type Field Description private NamespacedHierarchicalStore.CloseAction<N>closeActionprivate booleanclosedprivate java.util.concurrent.atomic.AtomicIntegerinsertOrderSequenceprivate NamespacedHierarchicalStore<N>parentStoreprivate java.util.concurrent.ConcurrentMap<NamespacedHierarchicalStore.CompositeKey<N>,NamespacedHierarchicalStore.StoredValue>storedValues
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private <T> TcastToRequiredType(java.lang.Object key, java.lang.Object value, java.lang.Class<T> requiredType)voidclose()If aNamespacedHierarchicalStore.CloseActionis configured, it will be called with all successfully stored values in reverse insertion order.java.lang.Objectget(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> Tget(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.ObjectgetOrComputeIfAbsent(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>
VgetOrComputeIfAbsent(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.java.util.Optional<NamespacedHierarchicalStore<N>>getParent()Returns the parent store of thisNamespacedHierarchicalStore.private NamespacedHierarchicalStore.StoredValuegetStoredValue(NamespacedHierarchicalStore.CompositeKey<N> compositeKey)booleanisClosed()Determine if this store has been closed.NamespacedHierarchicalStore<N>newChild()Create a child store with this store as its parent and this store's close action.java.lang.Objectput(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.private voidrejectIfClosed()java.lang.Objectremove(N namespace, java.lang.Object key)Remove the value stored for the supplied namespace and key from this store.<T> Tremove(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.StoredValuestoredValue(java.util.function.Supplier<java.lang.Object> value)
-
-
-
Field Detail
-
insertOrderSequence
private final java.util.concurrent.atomic.AtomicInteger insertOrderSequence
-
storedValues
private final java.util.concurrent.ConcurrentMap<NamespacedHierarchicalStore.CompositeKey<N>,NamespacedHierarchicalStore.StoredValue> storedValues
-
parentStore
private final NamespacedHierarchicalStore<N> parentStore
-
closeAction
private final NamespacedHierarchicalStore.CloseAction<N> closeAction
-
closed
private volatile boolean closed
-
-
Constructor Detail
-
NamespacedHierarchicalStore
public NamespacedHierarchicalStore(NamespacedHierarchicalStore<N> parentStore)
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 benullcloseAction- the action to be called for each stored value when this store is closed; may benull
-
-
Method Detail
-
newChild
public NamespacedHierarchicalStore<N> newChild()
Create a child store with this store as its parent and this store's close action.
-
getParent
@API(status=MAINTAINED, since="5.13.3") public java.util.Optional<NamespacedHierarchicalStore<N>> 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
@API(status=MAINTAINED, since="1.13.3") public boolean isClosed()Determine if this store has been closed.- Returns:
trueif this store has been closed- Since:
- 1.11
- See Also:
close()
-
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 interfacejava.lang.AutoCloseable- See Also:
isClosed()
-
get
public 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.- 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> T get(N namespace, java.lang.Object key, java.lang.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
public <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.- 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
-
getOrComputeIfAbsent
public <K,V> V getOrComputeIfAbsent(N namespace, K key, java.util.function.Function<K,V> defaultCreator, java.lang.Class<V> requiredType) throws NamespacedHierarchicalStoreException
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
-
put
public java.lang.Object put(N namespace, java.lang.Object key, java.lang.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
public java.lang.Object remove(N namespace, java.lang.Object key)
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> T remove(N namespace, java.lang.Object key, java.lang.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
-
storedValue
private NamespacedHierarchicalStore.StoredValue storedValue(java.util.function.Supplier<java.lang.Object> value)
-
getStoredValue
private NamespacedHierarchicalStore.StoredValue getStoredValue(NamespacedHierarchicalStore.CompositeKey<N> compositeKey)
-
castToRequiredType
private <T> T castToRequiredType(java.lang.Object key, java.lang.Object value, java.lang.Class<T> requiredType)
-
rejectIfClosed
private void rejectIfClosed()
-
-