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, Greg Turnquist
  • 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 other)
    Returns a new Lazy that will return the given value in case the current one does not yield in a result.
    orElse(T other)
    Returns the value of the lazy computation or the given default value in case the computation yields null.
    orElseGet(Supplier<? extends T> supplier)
    Returns the value of the lazy computation or the value produced by the given Supplier in case the original value is 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:
      a Lazy wrapping the given Supplier.
    • 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:
      a Lazy wrapping value.
    • empty

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

      public T get()
      Returns the value created by the configured Supplier. Will return the same instance for subsequent lookups.
      Specified by:
      get in interface Supplier<T>
      Returns:
      the value created by the configured Supplier. Will return the same instance for subsequent lookups.
      Throws:
      IllegalStateException - if the resolved value is null.
    • getNullable

      @Nullable public T getNullable()
      Returns the value of the lazy evaluation.
      Returns:
      the value of the lazy evaluation, can be null.
      Since:
      2.2
    • 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:
      an Optional value created by the configured Supplier or an empty Optional if the resolved value is null.
    • or

      public Lazy<T> or(T other)
      Returns a new Lazy that will return the given value in case the current one does not yield in a result.
      Parameters:
      other - must not be null.
      Returns:
      a new Lazy that will yield its value if present, otherwise other.
    • 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 - the supplying function that produces a value to be returned, must not be null.
      Returns:
      a new Lazy that will yield its value if present, otherwise the result produced by the supplying function.
    • orElse

      @Nullable public T orElse(@Nullable T other)
      Returns the value of the lazy computation or the given default value in case the computation yields null.
      Parameters:
      other - the value to be returned, if no value is present. May be null.
      Returns:
      the value, if present, otherwise other.
    • orElseGet

      @Nullable public T orElseGet(Supplier<? extends T> supplier)
      Returns the value of the lazy computation or the value produced by the given Supplier in case the original value is null.
      Parameters:
      supplier - the supplying function that produces a value to be returned, must not be null.
      Returns:
      the value, if present, otherwise the result produced by the supplying function.
    • 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:
      an Lazy describing the result of applying a mapping function to the value of this Lazy or throwing IllegalStateException if the Lazy is empty.
    • 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:
      the result of applying an Lazy-bearing mapping function to the value of this Lazy or a Lazy throwing IllegalStateException if the Lazy is empty.
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • 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