Interface Lazy<T>

All Known Subinterfaces:
LazyResettable<C>
All Known Implementing Classes:
LazyImpl, LazyResettableImpl, LazyResettableWithTearDown

public interface Lazy<T>
This utility provides lazy initialization for a wrapped object. API-wise, it can be considered similar to Optional with the main difference that since it will never be Optional.empty(), the get() function will never fail. Rather, get() will result in initializing the object if initialization has not happened yet. All other functions mimic the behavior of their counterpart in Optional and do NOT trigger initialization if it has not happened yet. Note that Lazy does allow wrapping a null if that is what the lazy initialization returns. For the use case of lazy initializing to something which may or may not exist, the implementer should consider combining this class with Optional, thus resulting in a Lazy<Optional<T>>. It is recommended to use instances of Lazy exclusively in conjunction with the final keyword, such that it is not allowed to swap the reference to a new Lazy instance. Following the recommended usage allows making the assumption that there will be no more than one initialization, and that the transition from uninitialized to initialized is one-way and irreversible. This interface promises to be threadsafe if multiple concurrent calls happen before the first initialization finishes, and furthermore guarantees that the initialization routine will happen at most once.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Lazy<Boolean>
     
    static final Lazy<Boolean>
     
  • Method Summary

    Modifier and Type
    Method
    Description
    filter(Predicate<? super T> predicate)
    If a value is initialized, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.
    <U> Optional<U>
    flatMap(Function<? super T,Optional<U>> mapper)
    If a value is initialized, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional.
    get()
     
    void
    ifPresent(Consumer<? super T> consumer)
     
    boolean
     
    <U> Optional<U>
    map(Function<? super T,? extends U> mapper)
    If a value is initialized, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result.
    static <C> Lazy<C>
    of(Supplier<C> supplier)
     
    orElse(T other)
    Return the value if initialized, otherwise return other.
    orElseGet(Supplier<? extends T> other)
    Return the value if initialized, otherwise invoke other and return the result of that invocation.
    <X extends Throwable>
    T
    orElseThrow(Supplier<? extends X> exceptionSupplier)
    Return the contained value, if initialized, otherwise throw an exception to be created by the provided supplier.
  • Field Details

  • Method Details

    • of

      static <C> Lazy<C> of(Supplier<C> supplier)
      Parameters:
      supplier - to initialize the wrapped value
      Returns:
      an instance of Lazy which will execute the if needed
    • get

      T get()
      Returns:
      the wrapped value, with the side-effect of initializing it has not happened yet
    • ifPresent

      void ifPresent(Consumer<? super T> consumer)
      Parameters:
      consumer - to pass the wrapped value to, only if it has already been initialized
    • isPresent

      boolean isPresent()
      Returns:
      true if there is a value present (i.e. initialized), otherwise false
    • filter

      Optional<T> filter(Predicate<? super T> predicate)
      If a value is initialized, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.
      Parameters:
      predicate - a predicate to apply to the value, if initialized
      Returns:
      an Optional describing the value of this Optional if a value is present and the value matches the given predicate, otherwise an empty Optional
      Throws:
      NullPointerException - if the predicate is null
    • map

      <U> Optional<U> map(Function<? super T,? extends U> mapper)
      If a value is initialized, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result. Otherwise return an empty Optional.
      Type Parameters:
      U - The type of the result of the mapping function
      Parameters:
      mapper - a mapping function to apply to the value, if initialized
      Returns:
      an Optional describing the result of applying a mapping function to the value of this Optional, if a value is initialized, otherwise an empty Optional
      Throws:
      NullPointerException - if the mapping function is null
    • flatMap

      <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)
      If a value is initialized, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional. This method is similar to map(Function), but the provided mapper is one whose result is already an Optional, and if invoked, flatMap does not wrap it with an additional Optional.
      Type Parameters:
      U - The type parameter to the Optional returned by the mapping function
      Parameters:
      mapper - a mapping function to apply to the value, if initialized
      Returns:
      the result of applying an Optional-bearing mapping function to the value of this Optional, if a value is initialized, otherwise an empty Optional
      Throws:
      NullPointerException - if the mapping function is null or returns a null result
    • orElse

      T orElse(T other)
      Return the value if initialized, otherwise return other.
      Parameters:
      other - the value to be returned if there is no value initialized, may be null
      Returns:
      the value, if initialized, otherwise other
    • orElseGet

      T orElseGet(Supplier<? extends T> other)
      Return the value if initialized, otherwise invoke other and return the result of that invocation.
      Parameters:
      other - a Supplier whose result is returned if no value is initialized
      Returns:
      the value if initialized otherwise the result of other.get()
      Throws:
      NullPointerException - if value is not initialized and other is null
    • orElseThrow

      <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
      Return the contained value, if initialized, otherwise throw an exception to be created by the provided supplier.
      Type Parameters:
      X - Type of the exception to be thrown
      Parameters:
      exceptionSupplier - The supplier which will return the exception to be thrown
      Returns:
      the initialized value
      Throws:
      X - if there is no value initialized
      NullPointerException - if no value is initialized and exceptionSupplier is null