Class NamespacedHierarchicalStore<N>

    • Constructor Detail

      • NamespacedHierarchicalStore

        public NamespacedHierarchicalStore​(NamespacedHierarchicalStore<N> parentStore)
        Create a new store with the supplied parent.
        Parameters:
        parentStore - the parent store to use for lookups; may be null
      • NamespacedHierarchicalStore

        public NamespacedHierarchicalStore​(NamespacedHierarchicalStore<N> parentStore,
                                           NamespacedHierarchicalStore.CloseAction<N> closeAction)
        Create a new store with the supplied parent and close action.
        Parameters:
        parentStore - the parent store to use for lookups; may be null
        closeAction - the action to be called for each stored value when this store is closed; may be null
    • Method Detail

      • newChild

        public NamespacedHierarchicalStore<N> newChild()
        Create a child store with this store as its parent using the same close action.
      • close

        public void close()
        If a close action is configured, it will be called with all successfully stored values in reverse insertion order.

        Closing a store does not close its parent or any of its children.

        Specified by:
        close in interface java.lang.AutoCloseable
      • get

        public java.lang.Object get​(N namespace,
                                    java.lang.Object key)
        Get the value stored for the supplied namespace and key in this store or the parent store, if present.
        Parameters:
        namespace - the namespace; never null
        key - the key; never null
        Returns:
        the stored value; may be null
      • get

        public <T> T get​(N namespace,
                         java.lang.Object key,
                         java.lang.Class<T> requiredType)
                  throws NamespacedHierarchicalStoreException
        Get the value stored for the supplied namespace and key in this store or the parent store, if present, and cast it to the supplied required type.
        Parameters:
        namespace - the namespace; never null
        key - the key; never null
        requiredType - the required type of the value; never null
        Returns:
        the stored value; may be null
        Throws:
        NamespacedHierarchicalStoreException - if the stored value cannot be cast to the required type
      • getOrComputeIfAbsent

        public <K,​V> java.lang.Object getOrComputeIfAbsent​(N namespace,
                                                                 K key,
                                                                 java.util.function.Function<K,​V> defaultCreator)
        Get the value stored for the supplied namespace and key in this store or the parent store, if present, or call the supplied function to compute it.
        Parameters:
        namespace - the namespace; never null
        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 stored value; may be null
      • getOrComputeIfAbsent

        public <K,​V> V getOrComputeIfAbsent​(N namespace,
                                                  K key,
                                                  java.util.function.Function<K,​V> defaultCreator,
                                                  java.lang.Class<V> requiredType)
                                           throws NamespacedHierarchicalStoreException
        Get the value stored for the supplied namespace and key in this store or the parent store, if present, or call the supplied function to compute it and, finally, cast it to the supplied required type.
        Parameters:
        namespace - the namespace; never null
        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 stored value; may be null
        Throws:
        NamespacedHierarchicalStoreException - if the stored value cannot be cast to the required type
      • put

        public java.lang.Object put​(N namespace,
                                    java.lang.Object key,
                                    java.lang.Object value)
                             throws NamespacedHierarchicalStoreException
        Put the supplied value for the supplied namespace and key into this store and return the previously associated value in this store.

        The NamespacedHierarchicalStore.CloseAction will not be called for the previously stored value, if any.

        Parameters:
        namespace - the namespace; never null
        key - the key; never null
        value - the value to store; may be null
        Returns:
        the previously stored value; may be null
        Throws:
        NamespacedHierarchicalStoreException - if the stored value cannot be cast to the required type
      • remove

        public java.lang.Object remove​(N namespace,
                                       java.lang.Object key)
        Remove the value stored for the supplied namespace and key from this store.

        The NamespacedHierarchicalStore.CloseAction will not be called for the removed value.

        Parameters:
        namespace - the namespace; never null
        key - the key; never null
        Returns:
        the previously stored value; may be null
      • remove

        public <T> T remove​(N namespace,
                            java.lang.Object key,
                            java.lang.Class<T> requiredType)
                     throws NamespacedHierarchicalStoreException
        Remove the value stored for the supplied namespace and key from this store and cast it to the supplied required type.

        The NamespacedHierarchicalStore.CloseAction will not be called for the removed value.

        Parameters:
        namespace - the namespace; never null
        key - the key; never null
        requiredType - the required type of the value; never null
        Returns:
        the previously stored value; may be null
        Throws:
        NamespacedHierarchicalStoreException - if the stored value cannot be cast to the required type
      • castToRequiredType

        private <T> T castToRequiredType​(java.lang.Object key,
                                         java.lang.Object value,
                                         java.lang.Class<T> requiredType)