Class Lazy<T>

java.lang.Object
org.springframework.data.util.Lazy<T>
All Implemented Interfaces:
Supplier<T>

public class Lazy<T> extends Object implements Supplier<T>
Simple value type to delay the creation of an object using a Supplier returning the produced object for subsequent lookups. Note, that no concurrency control is applied during the lookup of get(), which means in concurrent access scenarios, the provided Supplier can be called multiple times.
Since:
2.0
Author:
Oliver Gierke, Mark Paluch, Henning Rohlfs, Johannes Englmeier
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Lazy<T>
    Creates a pre-resolved empty Lazy.
    boolean
     
    <S> Lazy<S>
    flatMap(Function<? super T,Lazy<? extends S>> function)
    Creates a new Lazy with the given Function lazily applied to the current one.
    get()
    Returns the value created by the configured Supplier.
    Returns the value of the lazy evaluation.
    Returns the Optional value created by the configured Supplier, allowing the absence of values in contrast to get().
    int
     
    <S> Lazy<S>
    map(Function<? super T,? extends S> function)
    Creates a new Lazy with the given Function lazily applied to the current one.
    static <T> Lazy<T>
    of(Supplier<? extends T> supplier)
    Creates a new Lazy to produce an object lazily.
    static <T> Lazy<T>
    of(T value)
    Creates a new Lazy to return the given value.
    or(Supplier<? extends T> supplier)
    Returns a new Lazy that will consume the given supplier in case the current one does not yield in a result.
    or(T value)
    Returns a new Lazy that will return the given value in case the current one does not yield in a result.
    orElse(T value)
    Returns the value of the lazy computation or the given default value in case the computation yields null.
     
    Returns the String representation of the already resolved value or the one provided through the given Supplier if the value has not been resolved yet.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • of

      public static <T> Lazy<T> of(Supplier<? extends T> supplier)
      Creates a new Lazy to produce an object lazily.
      Type Parameters:
      T - the type of which to produce an object of eventually.
      Parameters:
      supplier - the Supplier to create the object lazily.
      Returns:
    • of

      public static <T> Lazy<T> of(T value)
      Creates a new Lazy to return the given value.
      Type Parameters:
      T - the type of the value to return eventually.
      Parameters:
      value - the value to return.
      Returns:
    • empty

      public static <T> Lazy<T> empty()
      Creates a pre-resolved empty Lazy.
      Returns:
      Since:
      2.1
    • get

      public T get()
      Returns the value created by the configured Supplier. Will return the calculated instance for subsequent lookups.
      Specified by:
      get in interface Supplier<T>
      Returns:
    • getOptional

      public Optional<T> getOptional()
      Returns the Optional value created by the configured Supplier, allowing the absence of values in contrast to get(). Will return the calculated instance for subsequent lookups.
      Returns:
    • or

      public Lazy<T> or(Supplier<? extends T> supplier)
      Returns a new Lazy that will consume the given supplier in case the current one does not yield in a result.
      Parameters:
      supplier - must not be null.
      Returns:
    • or

      public Lazy<T> or(T value)
      Returns a new Lazy that will return the given value in case the current one does not yield in a result.
      Parameters:
      value - must not be null.
      Returns:
    • orElse

      @Nullable public T orElse(@Nullable T value)
      Returns the value of the lazy computation or the given default value in case the computation yields null.
      Parameters:
      value -
      Returns:
    • map

      public <S> Lazy<S> map(Function<? super T,? extends S> function)
      Creates a new Lazy with the given Function lazily applied to the current one.
      Parameters:
      function - must not be null.
      Returns:
    • flatMap

      public <S> Lazy<S> flatMap(Function<? super T,Lazy<? extends S>> function)
      Creates a new Lazy with the given Function lazily applied to the current one.
      Parameters:
      function - must not be null.
      Returns:
    • toString

      public String toString(Supplier<String> fallback)
      Returns the String representation of the already resolved value or the one provided through the given Supplier if the value has not been resolved yet.
      Parameters:
      fallback - must not be null.
      Returns:
      will never be null.
      Since:
      3.0.1
    • getNullable

      @Nullable public T getNullable()
      Returns the value of the lazy evaluation.
      Returns:
      Since:
      2.2
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(@Nullable Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object