Interface ExtensionContext.Store
- All Known Implementing Classes:
NamespaceAwareStore
- Enclosing interface:
ExtensionContext
Store
provides methods for extensions to save and retrieve data.-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic interface
Classes implementing this interface indicate that they want toExtensionContext.Store.CloseableResource.close()
some underlying resource or resources when the enclosingStore
is closed. -
Method Summary
Modifier and TypeMethodDescriptionGet the value that is stored under the suppliedkey
.<V> V
Get the value of the specified required type that is stored under the suppliedkey
.default <V> V
getOrComputeIfAbsent
(Class<V> type) Get the object of typetype
that is present in thisStore
(keyed bytype
); and otherwise invoke the default constructor fortype
to generate the object, store it, and return it.<K,
V> Object getOrComputeIfAbsent
(K key, Function<K, V> defaultCreator) Get the value that is stored under the suppliedkey
.<K,
V> V getOrComputeIfAbsent
(K key, Function<K, V> defaultCreator, Class<V> requiredType) Get the value of the specified required type that is stored under the suppliedkey
.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 supplieddefaultValue
if no value is found for the suppliedkey
in this store or in an ancestor.void
Store avalue
for later retrieval under the suppliedkey
.Remove the value that was previously stored under the suppliedkey
.<V> V
Remove 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
ExtensionContext
for the suppliedkey
, ancestors of the context will be queried for a value with the samekey
in theNamespace
used 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
ExtensionContext
for the suppliedkey
, ancestors of the context will be queried for a value with the samekey
in theNamespace
used to create this store.- Type Parameters:
V
- the value type- Parameters:
key
- the key; nevernull
requiredType
- 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 supplieddefaultValue
if no value is found for the suppliedkey
in this store or in an ancestor.If no value is stored in the current
ExtensionContext
for the suppliedkey
, ancestors of the context will be queried for a value with the samekey
in theNamespace
used to create this store.- Type Parameters:
V
- the value type- Parameters:
key
- the key; nevernull
requiredType
- the required type of the value; nevernull
defaultValue
- the default value- Returns:
- the value; potentially
null
- Since:
- 5.5
- See Also:
-
getOrComputeIfAbsent
Get the object of typetype
that is present in thisStore
(keyed bytype
); and otherwise invoke the default constructor fortype
to generate the object, store it, and return it.This method is a shortcut for the following, where
X
is the type of object we wish to retrieve from the store.X x = store.getOrComputeIfAbsent(X.class, key -> new X(), X.class); // Equivalent to: // X x = store.getOrComputeIfAbsent(X.class);
See
getOrComputeIfAbsent(Object, Function, Class)
for further details.If
type
implementsExtensionContext.Store.CloseableResource
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:
-
getOrComputeIfAbsent
Get the value that is stored under the suppliedkey
.If no value is stored in the current
ExtensionContext
for the suppliedkey
, ancestors of the context will be queried for a value with the samekey
in theNamespace
used to create this store. If no value is found for the suppliedkey
, a new value will be computed by thedefaultCreator
(given thekey
as input), stored, and returned.For greater type safety, consider using
getOrComputeIfAbsent(Object, Function, Class)
instead.If the created value is an instance of
ExtensionContext.Store.CloseableResource
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; nevernull
defaultCreator
- the function called with the suppliedkey
to create a new value; nevernull
but may returnnull
- Returns:
- the value; potentially
null
- See Also:
-
getOrComputeIfAbsent
Get the value of the specified required type that is stored under the suppliedkey
.If no value is stored in the current
ExtensionContext
for the suppliedkey
, ancestors of the context will be queried for a value with the samekey
in theNamespace
used to create this store. If no value is found for the suppliedkey
, a new value will be computed by thedefaultCreator
(given thekey
as input), stored, and returned.If
requiredType
implementsExtensionContext.Store.CloseableResource
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; 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 value; potentially
null
- See Also:
-
put
Store avalue
for later retrieval under the suppliedkey
.A stored
value
is visible in childExtensionContexts
for the store'sNamespace
unless they overwrite it.If the
value
is an instance ofExtensionContext.Store.CloseableResource
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; nevernull
value
- 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.CloseableResource
API 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
null
if 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.CloseableResource
API will not be honored for values that are manually removed via this method.- Type Parameters:
V
- the value type- Parameters:
key
- the key; nevernull
requiredType
- the required type of the value; nevernull
- Returns:
- the previous value or
null
if no value was present for the specified key - See Also:
-