Class LazyFactory<T>

java.lang.Object
io.github.cowwoc.pouch10.core.LazyFactory<T>
Type Parameters:
T - the type of the value
All Implemented Interfaces:
Factory<T>, Reference<T>, Closeable, AutoCloseable

public abstract class LazyFactory<T> extends Object implements Factory<T>
A factory that initializes a value on demand.

Instances of LazyFactory are not safe for use by multiple threads. If such synchronization is required then it is recommended that ConcurrentLazyFactory be used.

  • Constructor Details

    • LazyFactory

      protected LazyFactory()
      Creates a new instance.
  • Method Details

    • create

      public static <T> LazyFactory<T> create(Supplier<T> supplier, Consumer<T> disposer)
      Creates a new LazyFactory.
      Type Parameters:
      T - the type of value returned by the factory
      Parameters:
      supplier - supplies the factory value
      disposer - implements disposeValue(T)
      Returns:
      a new LazyFactory
    • create

      public static <T extends AutoCloseable> LazyFactory<T> create(Supplier<T> supplier)
      Creates a new LazyFactory that disposes its value by invoking close(). If close() throws a checked exception, it is wrapped in a RuntimeException or an exception that extends it.
      Type Parameters:
      T - the type of value returned by the factory
      Parameters:
      supplier - supplies the factory value
      Returns:
      a new LazyFactory
    • disposeValue

      protected abstract void disposeValue(T value)
      Disposes the value.

      This method is invoked the first time close() is invoked, and only if the value was already initialized. This method may not invoke any other method as the factory is already marked as closed.

      Parameters:
      value - the value to dispose
    • getValue

      public final T getValue()
      Returns the value. Subsequent invocations of this method return the same value.
      Specified by:
      getValue in interface Reference<T>
      Returns:
      an object of type <T>
      Throws:
      IllegalStateException - if the factory is closed
    • close

      public final void close()
      Description copied from interface: Factory
      Disposes the Factory and the value. Subsequent invocations of this method have no effect. Invoking any other method after this one results in IllegalStateException being thrown.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface Factory<T>
    • toString

      public String toString()
    • createValue

      protected abstract T createValue()
      Creates the value. This method is invoked the first time Reference.getValue() is invoked.
      Returns:
      the value
    • isInitialized

      public boolean isInitialized()
      Description copied from interface: Reference
      Indicates if the value was initialized.

      A value may get initialized immediately after this method returns false but once it returns true it will continue to do so indefinitely.

      Specified by:
      isInitialized in interface Reference<T>
      Returns:
      true if the value was initialized