Interface ExtensionContext.Store
- All Known Implementing Classes:
NamespaceAwareStore
- Enclosing interface:
ExtensionContext
Store provides methods for extensions to save and retrieve data.- Since:
- 5.0
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceDeprecated. -
Method Summary
Modifier and TypeMethodDescriptiondefault <V> VcomputeIfAbsent(Class<V> type) Return the object of typetypeif it is present and notnullin thisStore(keyed bytype); otherwise, invoke the default constructor fortypeto generate the object, store it, and return it.<K,V> Object computeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator) Return the value of the specified required type that is stored under the suppliedkey.<K,V> V computeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType) Get the value of the specified required type that is stored under the suppliedkey.@Nullable ObjectGet the value that is stored under the suppliedkey.<V> @Nullable VGet the value of the specified required type that is stored under the suppliedkey.default <V> VgetOrComputeIfAbsent(Class<V> type) Deprecated.Please usecomputeIfAbsent(Class)instead.getOrComputeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator) Deprecated.Please usecomputeIfAbsent(Object, Function)instead.<K, V extends @Nullable Object>
@Nullable VgetOrComputeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType) Deprecated.default <V> VgetOrDefault(Object key, Class<V> requiredType, V defaultValue) Get the value of the specified required type that is stored under the suppliedkey, or the supplieddefaultValueif no value is found for the suppliedkeyin this store or in an ancestor.voidStore avaluefor later retrieval under the suppliedkey.@Nullable ObjectRemove the value that was previously stored under the suppliedkey.<V> @Nullable VRemove the value of the specified required type that was previously stored under the suppliedkey.
-
Method Details
-
get
Get the value that is stored under the suppliedkey.If no value is stored in the current
ExtensionContextfor the suppliedkey, ancestors of the context will be queried for a value with the samekeyin theNamespaceused to create this store.For greater type safety, consider using
get(Object, Class)instead.- Parameters:
key- the key; nevernull- Returns:
- the value; potentially
null - See Also:
-
get
Get the value of the specified required type that is stored under the suppliedkey.If no value is stored in the current
ExtensionContextfor the suppliedkey, ancestors of the context will be queried for a value with the samekeyin theNamespaceused to create this store.- Type Parameters:
V- the value type- Parameters:
key- the key; nevernullrequiredType- the required type of the value; nevernull- Returns:
- the value; potentially
null - See Also:
-
getOrDefault
@API(status=STABLE, since="5.5") default <V> V getOrDefault(Object key, Class<V> requiredType, V defaultValue) Get the value of the specified required type that is stored under the suppliedkey, or the supplieddefaultValueif no value is found for the suppliedkeyin this store or in an ancestor.If no value is stored in the current
ExtensionContextfor the suppliedkey, ancestors of the context will be queried for a value with the samekeyin theNamespaceused to create this store.- Type Parameters:
V- the value type- Parameters:
key- the key; nevernullrequiredType- the required type of the value; nevernulldefaultValue- the default value; nevernull- Returns:
- the value; never
null - Since:
- 5.5
- See Also:
-
getOrComputeIfAbsent
Deprecated.Please usecomputeIfAbsent(Class)instead.Get the object of typetypethat is present in thisStore(keyed bytype); and otherwise invoke the default constructor fortypeto generate the object, store it, and return it.This method is a shortcut for the following, where
Xis the type of object we wish to retrieve from the store.X x = store.computeIfAbsent(X.class, key -> new X(), X.class); // Equivalent to: // X x = store.computeIfAbsent(X.class);
See
computeIfAbsent(Object, Function, Class)for further details.If
typeimplementsExtensionContext.Store.CloseableResourceorAutoCloseable(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then theclose()method will be invoked on the stored object when the store is closed.- Type Parameters:
V- the key and value type- Parameters:
type- the type of object to retrieve; nevernull- Returns:
- the object; never
null - Since:
- 5.1
- See Also:
-
computeIfAbsent
Return the object of typetypeif it is present and notnullin thisStore(keyed bytype); otherwise, invoke the default constructor fortypeto generate the object, store it, and return it.This method is a shortcut for the following, where
Xis the type of object we wish to retrieve from the store.X x = store.computeIfAbsent(X.class, key -> new X(), X.class); // Equivalent to: // X x = store.computeIfAbsent(X.class);
See
computeIfAbsent(Object, Function, Class)for further details.If
typeimplementsExtensionContext.Store.CloseableResourceorAutoCloseable(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then theclose()method will be invoked on the stored object when the store is closed.- Type Parameters:
V- the key and value type- Parameters:
type- the type of object to retrieve; nevernull- Returns:
- the object; never
null - Since:
- 6.0
- See Also:
-
getOrComputeIfAbsent
@Deprecated @API(status=DEPRECATED, since="6.0") <K, V extends @Nullable Object> @Nullable Object getOrComputeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator) Deprecated.Please usecomputeIfAbsent(Object, Function)instead.Get the value that is stored under the suppliedkey.If no value is stored in the current
ExtensionContextfor the suppliedkey, ancestors of the context will be queried for a value with the samekeyin theNamespaceused to create this store. If no value is found for the suppliedkey, a new value will be computed by thedefaultCreator(given thekeyas input), stored, and returned.For greater type safety, consider using
computeIfAbsent(Object, Function, Class)instead.If the created value is an instance of
ExtensionContext.Store.CloseableResourceorAutoCloseable(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then theclose()method will be invoked on the stored object when the store is closed.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the key; nevernulldefaultCreator- the function called with the suppliedkeyto create a new value; nevernullbut may returnnull- Returns:
- the value; potentially
null - See Also:
-
computeIfAbsent
Return the value of the specified required type that is stored under the suppliedkey.If no value is stored in the current
ExtensionContextfor the suppliedkey, ancestors of the context will be queried for a value with the samekeyin theNamespaceused to create this store. If no value is found for the suppliedkeyor the value isnull, a new value will be computed by thedefaultCreator(given thekeyas input), stored, and returned.For greater type safety, consider using
computeIfAbsent(Object, Function, Class)instead.If the created value is an instance of
ExtensionContext.Store.CloseableResourceorAutoCloseable(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then theclose()method will be invoked on the stored object when the store is closed.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- the key; nevernulldefaultCreator- the function called with the suppliedkeyto create a new value; nevernulland must not returnnull- Returns:
- the value; never
null - Since:
- 6.0
- See Also:
-
getOrComputeIfAbsent
@Deprecated @API(status=DEPRECATED, since="6.0") <K, V extends @Nullable Object> @Nullable V getOrComputeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType) Deprecated.Get the value of the specified required type that is stored under the suppliedkey.If no value is stored in the current
ExtensionContextfor the suppliedkey, ancestors of the context will be queried for a value with the samekeyin theNamespaceused to create this store. If no value is found for the suppliedkey, a new value will be computed by thedefaultCreator(given thekeyas input), stored, and returned.If the created value implements
ExtensionContext.Store.CloseableResourceorAutoCloseable(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then theclose()method will be invoked on the stored object when the store is closed.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- 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 value; potentially
null - See Also:
-
computeIfAbsent
<K,V> V computeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType) Get the value of the specified required type that is stored under the suppliedkey.If no value is stored in the current
ExtensionContextfor the suppliedkey, ancestors of the context will be queried for a value with the samekeyin theNamespaceused to create this store. If no value is found for the suppliedkeyor the value isnull, a new value will be computed by thedefaultCreator(given thekeyas input), stored, and returned.If the created value is an instance of
ExtensionContext.Store.CloseableResourceorAutoCloseable(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then theclose()method will be invoked on the stored object when the store is closed.- Type Parameters:
K- the key typeV- the value type- Parameters:
key- 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 value; never
null - See Also:
-
put
Store avaluefor later retrieval under the suppliedkey.A stored
valueis visible in childExtensionContextsfor the store'sNamespaceunless they overwrite it.If the
valueis an instance ofExtensionContext.Store.CloseableResourceorAutoCloseable(unless thejunit.jupiter.extensions.store.close.autocloseable.enabledconfiguration parameter is set tofalse), then theclose()method will be invoked on the stored object when the store is closed.- Parameters:
key- the key under which the value should be stored; nevernullvalue- the value to store; may benull- See Also:
-
remove
Remove the value that was previously stored under the suppliedkey.The value will only be removed in the current
ExtensionContext, not in ancestors. In addition, theExtensionContext.Store.CloseableResourceandAutoCloseableAPI will not be honored for values that are manually removed via this method.For greater type safety, consider using
remove(Object, Class)instead.- Parameters:
key- the key; nevernull- Returns:
- the previous value or
nullif no value was present for the specified key - See Also:
-
remove
Remove the value of the specified required type that was previously stored under the suppliedkey.The value will only be removed in the current
ExtensionContext, not in ancestors. In addition, theExtensionContext.Store.CloseableResourceandAutoCloseableAPI will not be honored for values that are manually removed via this method.- Type Parameters:
V- the value type- Parameters:
key- the key; nevernullrequiredType- the required type of the value; nevernull- Returns:
- the previous value or
nullif no value was present for the specified key - See Also:
-
AutoCloseabledirectly.