Package io.vavr

Class Lazy<T>

  • All Implemented Interfaces:
    Value<T>, java.io.Serializable, java.lang.Iterable<T>, java.util.function.Supplier<T>

    public final class Lazy<T>
    extends java.lang.Object
    implements Value<T>, java.util.function.Supplier<T>, java.io.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:
    Serialized Form
    • Method Detail

      • 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​(java.util.function.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​(java.lang.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:
        java.lang.NullPointerException - if values is null
      • val

        @GwtIncompatible("reflection is not supported")
        public static <T> T val​(java.util.function.Supplier<? extends T> supplier,
                                java.lang.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​(java.util.function.Predicate<? super T> predicate)
      • filterNot

        public Option<T> filterNot​(java.util.function.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 java.util.function.Supplier<T>
        Specified by:
        get in interface Value<T>
        Returns:
        the lazy evaluated value
      • 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:
        java.lang.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 java.lang.Iterable<T>
        Specified by:
        iterator in interface Value<T>
        Returns:
        A new Iterator
      • map

        public <U> Lazy<U> map​(java.util.function.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​(java.util.function.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​(java.util.function.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:
        java.lang.NullPointerException - if f is null
      • stringPrefix

        public java.lang.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​(java.lang.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 java.lang.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 java.lang.Object
        Returns:
        The hashcode of this object
      • toString

        public java.lang.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 java.lang.Object
        Returns:
        A String representation of this object