Interface ExtensionContext.Store
-
- Enclosing interface:
- ExtensionContext
public static interface ExtensionContext.StoreStoreprovides methods for extensions to save and retrieve data.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceExtensionContext.Store.CloseableResourceClasses implementing this interface indicate that they want toExtensionContext.Store.CloseableResource.close()some underlying resource or resources when the enclosingStoreis closed.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description java.lang.Objectget(java.lang.Object key)Get the value that is stored under the suppliedkey.<V> Vget(java.lang.Object key, java.lang.Class<V> requiredType)Get the value of the specified required type that is stored under the suppliedkey.default <V> VgetOrComputeIfAbsent(java.lang.Class<V> type)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.<K,V>
java.lang.ObjectgetOrComputeIfAbsent(K key, java.util.function.Function<K,V> defaultCreator)Get the value that is stored under the suppliedkey.<K,V>
VgetOrComputeIfAbsent(K key, java.util.function.Function<K,V> defaultCreator, java.lang.Class<V> requiredType)Get the value of the specified required type that is stored under the suppliedkey.default <V> VgetOrDefault(java.lang.Object key, java.lang.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.voidput(java.lang.Object key, java.lang.Object value)Store avaluefor later retrieval under the suppliedkey.java.lang.Objectremove(java.lang.Object key)Remove the value that was previously stored under the suppliedkey.<V> Vremove(java.lang.Object key, java.lang.Class<V> requiredType)Remove the value of the specified required type that was previously stored under the suppliedkey.
-
-
-
Method Detail
-
get
java.lang.Object get(java.lang.Object key)
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(Object, Class),getOrDefault(Object, Class, Object)
-
get
<V> V get(java.lang.Object key, java.lang.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.- 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:
get(Object),getOrDefault(Object, Class, Object)
-
getOrDefault
@API(status=STABLE, since="5.5") default <V> V getOrDefault(java.lang.Object key, java.lang.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- Returns:
- the value; potentially
null - Since:
- 5.5
- See Also:
get(Object, Class)
-
getOrComputeIfAbsent
@API(status=STABLE, since="5.1") default <V> V getOrComputeIfAbsent(java.lang.Class<V> type)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.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
typeimplementsExtensionContext.Store.CloseableResourcetheclose()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(Object, Function),getOrComputeIfAbsent(Object, Function, Class),ExtensionContext.Store.CloseableResource
-
getOrComputeIfAbsent
<K,V> java.lang.Object getOrComputeIfAbsent(K key, java.util.function.Function<K,V> defaultCreator)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
getOrComputeIfAbsent(Object, Function, Class)instead.If the created value is an instance of
ExtensionContext.Store.CloseableResourcetheclose()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; nevernull- Returns:
- the value; potentially
null - See Also:
getOrComputeIfAbsent(Class),getOrComputeIfAbsent(Object, Function, Class),ExtensionContext.Store.CloseableResource
-
getOrComputeIfAbsent
<K,V> V getOrComputeIfAbsent(K key, java.util.function.Function<K,V> defaultCreator, java.lang.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 suppliedkey, a new value will be computed by thedefaultCreator(given thekeyas input), stored, and returned.If
requiredTypeimplementsExtensionContext.Store.CloseableResourcetheclose()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; nevernullrequiredType- the required type of the value; nevernull- Returns:
- the value; potentially
null - See Also:
getOrComputeIfAbsent(Class),getOrComputeIfAbsent(Object, Function),ExtensionContext.Store.CloseableResource
-
put
void put(java.lang.Object key, java.lang.Object value)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.CloseableResourcetheclose()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:
ExtensionContext.Store.CloseableResource
-
remove
java.lang.Object remove(java.lang.Object key)
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.CloseableResourceAPI 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(Object, Class)
-
remove
<V> V remove(java.lang.Object key, java.lang.Class<V> requiredType)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.CloseableResourceAPI 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:
remove(Object)
-
-