Interface ExtensionContext.Store

All Known Implementing Classes:
NamespaceAwareStore
Enclosing interface:
ExtensionContext

public static interface ExtensionContext.Store
Store provides methods for extensions to save and retrieve data.
Since:
5.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Deprecated.
    Please extend AutoCloseable directly.
  • Method Summary

    Modifier and Type
    Method
    Description
    default <V> V
    Return the object of type type if it is present and not null in this Store (keyed by type); otherwise, invoke the default constructor for type to 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 supplied key.
    <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 supplied key.
    @Nullable Object
    get(Object key)
    Get the value that is stored under the supplied key.
    <V> @Nullable V
    get(Object key, Class<V> requiredType)
    Get the value of the specified required type that is stored under the supplied key.
    default <V> V
    Deprecated.
    Please use computeIfAbsent(Class) instead.
    <K, V extends @Nullable Object>
    @Nullable Object
    getOrComputeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator)
    Deprecated.
    <K, V extends @Nullable Object>
    @Nullable V
    getOrComputeIfAbsent(K key, Function<? super K, ? extends V> defaultCreator, Class<V> requiredType)
    Deprecated.
    default <V> V
    getOrDefault(Object key, Class<V> requiredType, V defaultValue)
    Get the value of the specified required type that is stored under the supplied key, or the supplied defaultValue if no value is found for the supplied key in this store or in an ancestor.
    void
    put(Object key, @Nullable Object value)
    Store a value for later retrieval under the supplied key.
    @Nullable Object
    Remove the value that was previously stored under the supplied key.
    <V> @Nullable V
    remove(Object key, Class<V> requiredType)
    Remove the value of the specified required type that was previously stored under the supplied key.
  • Method Details

    • get

      @Nullable Object get(Object key)
      Get the value that is stored under the supplied key.

      If no value is stored in the current ExtensionContext for the supplied key, ancestors of the context will be queried for a value with the same key in the Namespace used to create this store.

      For greater type safety, consider using get(Object, Class) instead.

      Parameters:
      key - the key; never null
      Returns:
      the value; potentially null
      See Also:
    • get

      <V> @Nullable V get(Object key, Class<V> requiredType)
      Get the value of the specified required type that is stored under the supplied key.

      If no value is stored in the current ExtensionContext for the supplied key, ancestors of the context will be queried for a value with the same key in the Namespace used to create this store.

      Type Parameters:
      V - the value type
      Parameters:
      key - the key; never null
      requiredType - the required type of the value; never null
      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 supplied key, or the supplied defaultValue if no value is found for the supplied key in this store or in an ancestor.

      If no value is stored in the current ExtensionContext for the supplied key, ancestors of the context will be queried for a value with the same key in the Namespace used to create this store.

      Type Parameters:
      V - the value type
      Parameters:
      key - the key; never null
      requiredType - the required type of the value; never null
      defaultValue - the default value; never null
      Returns:
      the value; never null
      Since:
      5.5
      See Also:
    • getOrComputeIfAbsent

      @Deprecated @API(status=DEPRECATED, since="6.0") default <V> V getOrComputeIfAbsent(Class<V> type)
      Deprecated.
      Please use computeIfAbsent(Class) instead.
      Get the object of type type that is present in this Store (keyed by type); and otherwise invoke the default constructor for type 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.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 type implements ExtensionContext.Store.CloseableResource or AutoCloseable (unless the junit.jupiter.extensions.store.close.autocloseable.enabled configuration parameter is set to false), then the close() 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; never null
      Returns:
      the object; never null
      Since:
      5.1
      See Also:
    • computeIfAbsent

      @API(status=MAINTAINED, since="6.0") default <V> V computeIfAbsent(Class<V> type)
      Return the object of type type if it is present and not null in this Store (keyed by type); otherwise, invoke the default constructor for type 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.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 type implements ExtensionContext.Store.CloseableResource or AutoCloseable (unless the junit.jupiter.extensions.store.close.autocloseable.enabled configuration parameter is set to false), then the close() 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; never null
      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.
      Get the value that is stored under the supplied key.

      If no value is stored in the current ExtensionContext for the supplied key, ancestors of the context will be queried for a value with the same key in the Namespace used to create this store. If no value is found for the supplied key, a new value will be computed by the defaultCreator (given the key as 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.CloseableResource or AutoCloseable (unless the junit.jupiter.extensions.store.close.autocloseable.enabled configuration parameter is set to false), then the close() method will be invoked on the stored object when the store is closed.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      key - the key; never null
      defaultCreator - the function called with the supplied key to create a new value; never null but may return null
      Returns:
      the value; potentially null
      See Also:
    • computeIfAbsent

      <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 supplied key.

      If no value is stored in the current ExtensionContext for the supplied key, ancestors of the context will be queried for a value with the same key in the Namespace used to create this store. If no value is found for the supplied key or the value is null, a new value will be computed by the defaultCreator (given the key as 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.CloseableResource or AutoCloseable (unless the junit.jupiter.extensions.store.close.autocloseable.enabled configuration parameter is set to false), then the close() method will be invoked on the stored object when the store is closed.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      key - the key; never null
      defaultCreator - the function called with the supplied key to create a new value; never null and must not return null
      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 supplied key.

      If no value is stored in the current ExtensionContext for the supplied key, ancestors of the context will be queried for a value with the same key in the Namespace used to create this store. If no value is found for the supplied key, a new value will be computed by the defaultCreator (given the key as input), stored, and returned.

      If the created value implements ExtensionContext.Store.CloseableResource or AutoCloseable (unless the junit.jupiter.extensions.store.close.autocloseable.enabled configuration parameter is set to false), then the close() method will be invoked on the stored object when the store is closed.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      key - the key; never null
      defaultCreator - the function called with the supplied key to create a new value; never null but may return null
      requiredType - the required type of the value; never null
      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 supplied key.

      If no value is stored in the current ExtensionContext for the supplied key, ancestors of the context will be queried for a value with the same key in the Namespace used to create this store. If no value is found for the supplied key or the value is null, a new value will be computed by the defaultCreator (given the key as input), stored, and returned.

      If the created value is an instance of ExtensionContext.Store.CloseableResource or AutoCloseable (unless the junit.jupiter.extensions.store.close.autocloseable.enabled configuration parameter is set to false), then the close() method will be invoked on the stored object when the store is closed.

      Type Parameters:
      K - the key type
      V - the value type
      Parameters:
      key - the key; never null
      defaultCreator - the function called with the supplied key to create a new value; never null and must not return null
      requiredType - the required type of the value; never null
      Returns:
      the value; never null
      See Also:
    • put

      void put(Object key, @Nullable Object value)
      Store a value for later retrieval under the supplied key.

      A stored value is visible in child ExtensionContexts for the store's Namespace unless they overwrite it.

      If the value is an instance of ExtensionContext.Store.CloseableResource or AutoCloseable (unless the junit.jupiter.extensions.store.close.autocloseable.enabled configuration parameter is set to false), then the close() method will be invoked on the stored object when the store is closed.

      Parameters:
      key - the key under which the value should be stored; never null
      value - the value to store; may be null
      See Also:
    • remove

      @Nullable Object remove(Object key)
      Remove the value that was previously stored under the supplied key.

      The value will only be removed in the current ExtensionContext, not in ancestors. In addition, the ExtensionContext.Store.CloseableResource and AutoCloseable 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; never null
      Returns:
      the previous value or null if no value was present for the specified key
      See Also:
    • remove

      <V> @Nullable V remove(Object key, Class<V> requiredType)
      Remove the value of the specified required type that was previously stored under the supplied key.

      The value will only be removed in the current ExtensionContext, not in ancestors. In addition, the ExtensionContext.Store.CloseableResource and AutoCloseable API will not be honored for values that are manually removed via this method.

      Type Parameters:
      V - the value type
      Parameters:
      key - the key; never null
      requiredType - the required type of the value; never null
      Returns:
      the previous value or null if no value was present for the specified key
      See Also: