Interface CloseableMemoize<S extends java.lang.AutoCloseable>

  • Type Parameters:
    S - Type of the value returned.
    All Superinterfaces:
    java.lang.AutoCloseable, Memoize<S>, java.util.function.Supplier<S>

    public interface CloseableMemoize<S extends java.lang.AutoCloseable>
    extends Memoize<S>, java.lang.AutoCloseable
    Closeable memoizing supplier.

    This type extends Memoize and AutoCloseable.

    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      CloseableMemoize<S> accept​(java.util.function.Consumer<? super S> consumer)
      Call the consumer with the value of this memoizing supplier.
      static <T extends java.lang.AutoCloseable>
      CloseableMemoize<T>
      closeableSupplier​(java.util.function.Supplier<? extends T> supplier)
      Creates an AutoCloseable supplier which memoizes the AutoCloseable value returned by the specified supplier.
      S get()
      Get the memoized AutoCloseable value.
      CloseableMemoize<S> ifPresent​(java.util.function.Consumer<? super S> consumer)
      If a value is memoized, call the consumer with the value of this memoizing supplier.
      boolean isClosed()
      Returns whether this memoizing supplier is closed.
      • Methods inherited from interface java.lang.AutoCloseable

        close
    • Method Detail

      • closeableSupplier

        static <T extends java.lang.AutoCloseable> CloseableMemoize<T> closeableSupplier​(java.util.function.Supplier<? extends T> supplier)
        Creates an AutoCloseable supplier which memoizes the AutoCloseable value returned by the specified supplier.

        When the returned supplier is called to get a value, it will call the specified supplier at most once to obtain a value.

        When close() is called on the returned supplier, it will call close() on the memoized value, if present, and dereference the value. After close() is called on the returned supplier, the get() and accept(Consumer) methods of the returned supplier will throw an IllegalStateException.

        Type Parameters:
        T - Type of the value returned by the supplier.
        Parameters:
        supplier - The source supplier. Must not be null. The supplier should not return a null value. If the supplier does return a null value, the returned supplier will be marked closed and its get() and accept(Consumer) methods will throw an IllegalStateException.
        Returns:
        A memoized supplier wrapping the specified supplier.
      • isClosed

        boolean isClosed()
        Returns whether this memoizing supplier is closed.
        Returns:
        true If this memoizing supplier is closed; otherwise false.
      • get

        S get()
        Get the memoized AutoCloseable value.
        Specified by:
        get in interface Memoize<S extends java.lang.AutoCloseable>
        Specified by:
        get in interface java.util.function.Supplier<S extends java.lang.AutoCloseable>
        Returns:
        The memoized AutoCloseable value.
        Throws:
        java.lang.IllegalStateException - If this memoizing supplier is closed.
      • accept

        CloseableMemoize<S> accept​(java.util.function.Consumer<? super S> consumer)
        Call the consumer with the value of this memoizing supplier.

        This method will block closing this memoizing supplier while the consumer is executing.

        Specified by:
        accept in interface Memoize<S extends java.lang.AutoCloseable>
        Parameters:
        consumer - The consumer to accept the value of this memoizing supplier. Must not be null.
        Returns:
        This memoizing supplier.
        Throws:
        java.lang.IllegalStateException - If this memoizing supplier is closed.
      • ifPresent

        CloseableMemoize<S> ifPresent​(java.util.function.Consumer<? super S> consumer)
        If a value is memoized, call the consumer with the value of this memoizing supplier. Otherwise do nothing.

        This method will block closing this memoizing supplier while the consumer is executing.

        Specified by:
        ifPresent in interface Memoize<S extends java.lang.AutoCloseable>
        Parameters:
        consumer - The consumer to accept the value of this memoizing supplier if a value is memoized. Must not be null if a value is memoized.
        Returns:
        This memoizing supplier.