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.
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      java.lang.Object get​(java.lang.Object key)
      Get the value that is stored under the supplied key.
      <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 supplied key.
      default <V> V getOrComputeIfAbsent​(java.lang.Class<V> type)
      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.
      <K,V> java.lang.Object getOrComputeIfAbsent​(K key, java.util.function.Function<K,V> defaultCreator)
      Get the value that is stored under the supplied key.
      <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 supplied key.
      void put​(java.lang.Object key, java.lang.Object value)
      Store a value for later retrieval under the supplied key.
      java.lang.Object remove​(java.lang.Object key)
      Remove the value that was previously stored under the supplied key.
      <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 supplied key.
    • Method Detail

      • get

        java.lang.Object get​(java.lang.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(Object, Class)
      • 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 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:
        get(Object)
      • getOrComputeIfAbsent

        <K,V> java.lang.Object getOrComputeIfAbsent​(K key,
                                                    java.util.function.Function<K,V> defaultCreator)
        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 getOrComputeIfAbsent(Object, Function, Class) instead.

        If the created value is an instance of ExtensionContext.Store.CloseableResource 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
        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 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 requiredType implements ExtensionContext.Store.CloseableResource 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
        requiredType - the required type of the value; never null
        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 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 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:
        ExtensionContext.Store.CloseableResource
      • remove

        java.lang.Object remove​(java.lang.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 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(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 supplied key.

        The value will only be removed in the current ExtensionContext, not in ancestors. In addition, the ExtensionContext.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; 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:
        remove(Object)