Class LazySupplier<T,E extends Exception>

java.lang.Object
org.forgerock.util.LazySupplier<T,E>
Type Parameters:
T - the type of value returned by the supplier.
E - the type of exceptions thrown by the supplier.
All Implemented Interfaces:
AutoCloseable, Supplier<T,E>

public final class LazySupplier<T,E extends Exception> extends Object implements Supplier<T,E>, AutoCloseable
A Supplier that lazily computes a value the first time it is accessed and then caches the result to return on subsequent requests. This class is thread safe.
  • Method Details

    • get

      public T get() throws E
      Supplies a value and caches it, ensuring the same value is returned on subsequent calls. If an exception is thrown by the underlying supplier then this is thrown from this method and not cached.
      Specified by:
      get in interface Supplier<T,E extends Exception>
      Returns:
      the lazily-computed value.
      Throws:
      E - if an exception occurs when computing the value.
    • close

      public void close() throws Exception
      If the lazily-cached value has been initialized and is AutoCloseable then it is closed. If the value has not been initialized, or is not closeable, then this method does nothing.
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception - if the AutoCloseable.close() method on the cached value throws an exception.
    • lazy

      public static <T, E extends Exception> LazySupplier<T,E> lazy(Supplier<T,E> supplier)
      Constructs a supplier that will lazily compute and cache a value from the given supplier.
      Type Parameters:
      T - the type of values returned.
      E - the type of exceptions thrown by the supplier.
      Parameters:
      supplier - the supplier to use to compute the value.
      Returns:
      a lazy version of the supplier.