Package io.vavr

Class Lazy<T>

java.lang.Object
io.vavr.Lazy<T>
All Implemented Interfaces:
Value<T>, Serializable, Iterable<T>, Supplier<T>

public final class Lazy<T> extends Object implements Value<T>, Supplier<T>, Serializable
Represents a lazy evaluated value. Compared to a Supplier, Lazy is memoizing, i.e. it evaluates only once and therefore is referential transparent.
 
 final Lazy<Double> l = Lazy.of(Math::random);
 l.isEvaluated(); // = false
 l.get();         // = 0.123 (random generated)
 l.isEvaluated(); // = true
 l.get();         // = 0.123 (memoized)
 
 
Example of creating a real lazy value (works only with interfaces):
final CharSequence chars = Lazy.val(() -> "Yay!", CharSequence.class);
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • lock

      private final ReentrantLock lock
    • supplier

      private transient volatile Supplier<? extends T> supplier
    • value

      private T value
  • Constructor Details

    • Lazy

      private Lazy(Supplier<? extends T> supplier)
  • Method Details

    • narrow

      public static <T> Lazy<T> narrow(Lazy<? extends T> lazy)
      Narrows a widened Lazy<? extends T> to Lazy<T> by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.
      Type Parameters:
      T - Component type of the Lazy.
      Parameters:
      lazy - A Lazy.
      Returns:
      the given lazy instance as narrowed type Lazy<T>.
    • of

      public static <T> Lazy<T> of(Supplier<? extends T> supplier)
      Creates a Lazy that requests its value from a given Supplier. The supplier is asked only once, the value is memoized.
      Type Parameters:
      T - type of the lazy value
      Parameters:
      supplier - A supplier
      Returns:
      A new instance of Lazy
    • sequence

      public static <T> Lazy<Seq<T>> sequence(Iterable<? extends Lazy<? extends T>> values)
      Reduces many Lazy values into a single Lazy by transforming an Iterable<Lazy<? extends T>> into a Lazy<Seq<T>>.
      Type Parameters:
      T - Type of the lazy values.
      Parameters:
      values - An iterable of lazy values.
      Returns:
      A lazy sequence of values.
      Throws:
      NullPointerException - if values is null
    • val

      @GwtIncompatible("reflection is not supported") public static <T> T val(Supplier<? extends T> supplier, Class<T> type)
      Creates a real _lazy value_ of type T, backed by a Proxy which delegates to a Lazy instance.
      Type Parameters:
      T - type of the lazy value
      Parameters:
      supplier - A supplier
      type - An interface
      Returns:
      A new instance of T
    • filter

      public Option<T> filter(Predicate<? super T> predicate)
    • get

      public T get()
      Evaluates this lazy value and caches it, when called the first time. On subsequent calls, returns the cached value.
      Specified by:
      get in interface Supplier<T>
      Specified by:
      get in interface Value<T>
      Returns:
      the lazy evaluated value
    • computeValue

      private T computeValue()
    • isAsync

      public boolean isAsync()
      A Lazy's value is computed synchronously.
      Specified by:
      isAsync in interface Value<T>
      Returns:
      false
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: Value
      Checks, this Value is empty, i.e. if the underlying value is absent.
      Specified by:
      isEmpty in interface Value<T>
      Returns:
      false, if no underlying value is present, true otherwise.
    • isEvaluated

      public boolean isEvaluated()
      Checks, if this lazy value is evaluated.

      Note: A value is internally evaluated (once) by calling get().

      Returns:
      true, if the value is evaluated, false otherwise.
      Throws:
      UnsupportedOperationException - if this value is undefined
    • isLazy

      public boolean isLazy()
      A Lazy's value is computed lazily.
      Specified by:
      isLazy in interface Value<T>
      Returns:
      true
    • isSingleValued

      public boolean isSingleValued()
      Description copied from interface: Value
      States whether this is a single-valued type.
      Specified by:
      isSingleValued in interface Value<T>
      Returns:
      true if this is single-valued, false otherwise.
    • iterator

      public Iterator<T> iterator()
      Description copied from interface: Value
      Returns a rich io.vavr.collection.Iterator.
      Specified by:
      iterator in interface Iterable<T>
      Specified by:
      iterator in interface Value<T>
      Returns:
      A new Iterator
    • map

      public <U> Lazy<U> map(Function<? super T,? extends U> mapper)
      Description copied from interface: Value
      Maps the underlying value to a different component type.
      Specified by:
      map in interface Value<T>
      Type Parameters:
      U - The new component type
      Parameters:
      mapper - A mapper
      Returns:
      A new value
    • peek

      public Lazy<T> peek(Consumer<? super T> action)
      Description copied from interface: Value
      Performs the given action on the first element if this is an eager implementation. Performs the given action on all elements (the first immediately, successive deferred), if this is a lazy implementation.
      Specified by:
      peek in interface Value<T>
      Parameters:
      action - The action that will be performed on the element(s).
      Returns:
      this instance
    • transform

      public <U> U transform(Function<? super Lazy<T>,? extends U> f)
      Transforms this Lazy.
      Type Parameters:
      U - Type of transformation result
      Parameters:
      f - A transformation
      Returns:
      An instance of type U
      Throws:
      NullPointerException - if f is null
    • stringPrefix

      public String stringPrefix()
      Description copied from interface: Value
      Returns the name of this Value type, which is used by toString().
      Specified by:
      stringPrefix in interface Value<T>
      Returns:
      This type name.
    • equals

      public boolean equals(Object o)
      Description copied from interface: Value
      Clarifies that values have a proper equals() method implemented.

      See Object.equals(Object).

      Specified by:
      equals in interface Value<T>
      Overrides:
      equals in class Object
      Parameters:
      o - An object
      Returns:
      true, if this equals o, false otherwise
    • hashCode

      public int hashCode()
      Description copied from interface: Value
      Clarifies that values have a proper hashCode() method implemented.

      See Object.hashCode().

      Specified by:
      hashCode in interface Value<T>
      Overrides:
      hashCode in class Object
      Returns:
      The hashcode of this object
    • toString

      public String toString()
      Description copied from interface: Value
      Clarifies that values have a proper toString() method implemented.

      See Object.toString().

      Specified by:
      toString in interface Value<T>
      Overrides:
      toString in class Object
      Returns:
      A String representation of this object
    • writeObject

      @GwtIncompatible("The Java serialization protocol is explicitly not supported") private void writeObject(ObjectOutputStream s) throws IOException
      Ensures that the value is evaluated before serialization.
      Parameters:
      s - An object serialization stream.
      Throws:
      IOException - If an error occurs writing to the stream.