Class CloseableMemoizingSupplier<T extends java.lang.AutoCloseable>

  • All Implemented Interfaces:
    CloseableMemoize<T>, Memoize<T>, java.lang.AutoCloseable, java.util.function.Supplier<T>

    class CloseableMemoizingSupplier<T extends java.lang.AutoCloseable>
    extends java.lang.Object
    implements CloseableMemoize<T>
    The object can exist in one of three states:
    • initial which means neither get or close has been called. From this state, the object can transition directly to either open or closed.
    • open which means memoized is non-null with the value from the wrapped supplier. From this state, the object can transition to closed.
    • closed which means memoized is null and any value it may have held was closed. This is a terminal state.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean initial  
      private java.util.concurrent.locks.StampedLock lock  
      private T memoized  
    • Constructor Summary

      Constructors 
      Constructor Description
      CloseableMemoizingSupplier​(java.util.function.Supplier<? extends T> supplier)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      CloseableMemoize<T> accept​(java.util.function.Consumer<? super T> consumer)
      Call the consumer with the value of this memoizing supplier.
      void close()  
      T get()
      Get the memoized AutoCloseable value.
      CloseableMemoize<T> ifPresent​(java.util.function.Consumer<? super T> consumer)
      If a value is memoized, call the consumer with the value of this memoizing supplier.
      private T initial()  
      boolean isClosed()
      Returns whether this memoizing supplier is closed.
      boolean isPresent()
      If a value is memoized, return true.
      T peek()
      Peek the memoized value, if any.
      java.lang.String toString()  
      private static <T extends java.lang.AutoCloseable>
      T
      value​(T value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • lock

        private final java.util.concurrent.locks.StampedLock lock
      • initial

        private volatile boolean initial
      • memoized

        private T extends java.lang.AutoCloseable memoized
    • Constructor Detail

      • CloseableMemoizingSupplier

        CloseableMemoizingSupplier​(java.util.function.Supplier<? extends T> supplier)
    • Method Detail

      • get

        public T get()
        Description copied from interface: CloseableMemoize
        Get the memoized AutoCloseable value.
        Specified by:
        get in interface CloseableMemoize<T extends java.lang.AutoCloseable>
        Specified by:
        get in interface Memoize<T extends java.lang.AutoCloseable>
        Specified by:
        get in interface java.util.function.Supplier<T extends java.lang.AutoCloseable>
        Returns:
        The memoized AutoCloseable value.
      • initial

        private T initial()
      • value

        private static <T extends java.lang.AutoCloseable> T value​(T value)
      • peek

        public T peek()
        Description copied from interface: Memoize
        Peek the memoized value, if any.

        This method will not result in a call to the source supplier.

        Specified by:
        peek in interface Memoize<T extends java.lang.AutoCloseable>
        Returns:
        The memoized value if a value is memoized; otherwise null.
      • isPresent

        public boolean isPresent()
        Description copied from interface: Memoize
        If a value is memoized, return true. Otherwise return false.

        This method will not result in a call to the source supplier.

        Specified by:
        isPresent in interface Memoize<T extends java.lang.AutoCloseable>
        Returns:
        true if a value is memoized; otherwise false.
      • isClosed

        public boolean isClosed()
        Description copied from interface: CloseableMemoize
        Returns whether this memoizing supplier is closed.
        Specified by:
        isClosed in interface CloseableMemoize<T extends java.lang.AutoCloseable>
        Returns:
        true If this memoizing supplier is closed; otherwise false.
      • close

        public void close()
                   throws java.lang.Exception
        Specified by:
        close in interface java.lang.AutoCloseable
        Throws:
        java.lang.Exception
      • accept

        public CloseableMemoize<T> accept​(java.util.function.Consumer<? super T> consumer)
        Description copied from interface: CloseableMemoize
        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 CloseableMemoize<T extends java.lang.AutoCloseable>
        Specified by:
        accept in interface Memoize<T extends java.lang.AutoCloseable>
        Parameters:
        consumer - The consumer to accept the value of this memoizing supplier. Must not be null.
        Returns:
        This memoizing supplier.
      • ifPresent

        public CloseableMemoize<T> ifPresent​(java.util.function.Consumer<? super T> consumer)
        Description copied from interface: CloseableMemoize
        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 CloseableMemoize<T extends java.lang.AutoCloseable>
        Specified by:
        ifPresent in interface Memoize<T 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.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object