Top Description Fields Constructors Methods
io.vavr

public final Class Lazy<T>

extends Object
implements Value<T>, Supplier<T>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.function.Supplier, io.vavr.Value, java.lang.Iterable
Imports
io.vavr.collection.Iterator, .Seq, .Vector, io.vavr.control.Option, java.io.IOException, .ObjectOutputStream, .Serializable, java.lang.reflect.InvocationHandler, .Proxy, java.util.Objects, java.util.concurrent.locks.ReentrantLock, java.util.function.Consumer, .Function, .Predicate, .Supplier

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);
Author
Daniel Dietrich

Field Summary

Modifier and TypeField and Description
private final ReentrantLock
private static final long
private transient volatile Supplier<? extends T>
private T

Constructor Summary

AccessConstructor and Description
private
Lazy(Supplier<? extends T> supplier)

Method Summary

Modifier and TypeMethod and Description
private T
public boolean
equals(Object
An object
o
)

Overrides java.lang.Object.equals.

Implements io.vavr.Value.equals.

Clarifies that values have a proper equals() method implemented.

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

public T

Returns:

the lazy evaluated value
get
()

Implements io.vavr.Value.get, java.util.function.Supplier.get.

Evaluates this lazy value and caches it, when called the first time.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Implements io.vavr.Value.hashCode.

Clarifies that values have a proper hashCode() method implemented.

public boolean

Returns:

false
isAsync
()

Implements io.vavr.Value.isAsync.

A Lazy's value is computed synchronously.

public boolean
isEmpty()

Implements io.vavr.Value.isEmpty.

Checks, this Value is empty, i.e. if the underlying value is absent.

public boolean

Returns:

true, if the value is evaluated, false otherwise.
isEvaluated
()

Checks, if this lazy value is evaluated.

public boolean

Returns:

true
isLazy
()

Implements io.vavr.Value.isLazy.

A Lazy's value is computed lazily.

public boolean
isSingleValued()

Implements io.vavr.Value.isSingleValued.

States whether this is a single-valued type.

public Iterator<T>
iterator()

Implements io.vavr.Value.iterator.

Returns a rich io.vavr.collection.Iterator.

public <U> Lazy<U>
map(Function<? super T, ? extends U>
A mapper
mapper
)

Implements io.vavr.Value.map.

Maps the underlying value to a different component type.

public static <
Component type of the Lazy.
T
>
Lazy<T>

Returns:

the given lazy instance as narrowed type Lazy<T>.
narrow
(Lazy<? extends T>
A Lazy.
lazy
)

Narrows a widened Lazy<?

public static <
type of the lazy value
T
>
Lazy<T>

Returns:

A new instance of Lazy
of
(Supplier<? extends T>
A supplier
supplier
)

Creates a Lazy that requests its value from a given Supplier.

public Lazy<T>
peek(Consumer<? super T>
The action that will be performed on the element(s).
action
)

Implements io.vavr.Value.peek.

Performs the given action on the first element if this is an eager implementation.

public static <
Type of the lazy values.
T
>
Lazy<Seq<T>>

Returns:

A lazy sequence of values.
sequence
(Iterable<? extends Lazy<? extends T>>
An iterable of lazy values.
values
)

Reduces many Lazy values into a single Lazy by transforming an Iterable<Lazy<?

public String
stringPrefix()

Implements io.vavr.Value.stringPrefix.

Returns the name of this Value type, which is used by toString().

public String
toString()

Overrides java.lang.Object.toString.

Implements io.vavr.Value.toString.

Clarifies that values have a proper toString() method implemented.

public <
Type of transformation result
U
>
U

Returns:

An instance of type U
transform
(Function<? super Lazy<T>, ? extends U>
A transformation
f
)

Transforms this Lazy.

public static <
type of the lazy value
T
>
T

Returns:

A new instance of T
val
(Supplier<? extends T>
A supplier
supplier
,
Class<T>
An interface
type
)

Creates a real _lazy value_ of type T, backed by a java.lang.reflect.Proxy which delegates to a Lazy instance.

private void
writeObject(ObjectOutputStream
An object serialization stream.
s
)

Ensures that the value is evaluated before serialization.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

lockback to summary
private final ReentrantLock lock
serialVersionUIDback to summary
private static final long serialVersionUID
supplierback to summary
private transient volatile Supplier<? extends T> supplier
valueback to summary
private T value
Annotations
@SuppressWarnings:serial

Constructor Detail

Lazyback to summary
private Lazy(Supplier<? extends T> supplier)

Method Detail

computeValueback to summary
private T computeValue()
equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Implements io.vavr.Value.equals.

Doc from io.vavr.Value.equals.

Clarifies that values have a proper equals() method implemented.

See Object.equals(Object).

Parameters
o:Object

An object

Returns:boolean

true, if this equals o, false otherwise

Annotations
@Override
filterback to summary
public Option<T> filter(Predicate<? super T> predicate)
getback to summary
public T get()

Implements io.vavr.Value.get, java.util.function.Supplier.get.

Evaluates this lazy value and caches it, when called the first time. On subsequent calls, returns the cached value.

Returns:T

the lazy evaluated value

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implements io.vavr.Value.hashCode.

Doc from io.vavr.Value.hashCode.

Clarifies that values have a proper hashCode() method implemented.

See Object.hashCode().

Returns:int

The hashcode of this object

Annotations
@Override
isAsyncback to summary
public boolean isAsync()

Implements io.vavr.Value.isAsync.

A Lazy's value is computed synchronously.

Returns:boolean

false

Annotations
@Override
isEmptyback to summary
public boolean isEmpty()

Implements io.vavr.Value.isEmpty.

Doc from io.vavr.Value.isEmpty.

Checks, this Value is empty, i.e. if the underlying value is absent.

Returns:boolean

false, if no underlying value is present, true otherwise.

Annotations
@Override
isEvaluatedback to summary
public boolean isEvaluated()

Checks, if this lazy value is evaluated.

Note

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

Returns:boolean

true, if the value is evaluated, false otherwise.

Exceptions
UnsupportedOperationException:
if this value is undefined
isLazyback to summary
public boolean isLazy()

Implements io.vavr.Value.isLazy.

A Lazy's value is computed lazily.

Returns:boolean

true

Annotations
@Override
isSingleValuedback to summary
public boolean isSingleValued()

Implements io.vavr.Value.isSingleValued.

Doc from io.vavr.Value.isSingleValued.

States whether this is a single-valued type.

Returns:boolean

true if this is single-valued, false otherwise.

Annotations
@Override
iteratorback to summary
public Iterator<T> iterator()

Implements io.vavr.Value.iterator.

Doc from io.vavr.Value.iterator.

Returns a rich io.vavr.collection.Iterator.

Returns:Iterator<T>

A new Iterator

Annotations
@Override
mapback to summary
public <U> Lazy<U> map(Function<? super T, ? extends U> mapper)

Implements io.vavr.Value.map.

Doc from io.vavr.Value.map.

Maps the underlying value to a different component type.

Parameters
<U>
The new component type
mapper:Function<? super T, ? extends U>

A mapper

Returns:Lazy<U>

A new value

Annotations
@Override
narrowback to summary
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.

Parameters
<T>
Component type of the Lazy.
lazy:Lazy<? extends T>

A Lazy.

Returns:Lazy<T>

the given lazy instance as narrowed type Lazy<T>.

Annotations
@SuppressWarnings:unchecked
ofback to summary
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.

Parameters
<T>
type of the lazy value
supplier:Supplier<? extends T>

A supplier

Returns:Lazy<T>

A new instance of Lazy

Annotations
@SuppressWarnings:unchecked
peekback to summary
public Lazy<T> peek(Consumer<? super T> action)

Implements io.vavr.Value.peek.

Doc from io.vavr.Value.peek.

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.

Parameters
action:Consumer<? super T>

The action that will be performed on the element(s).

Returns:Lazy<T>

this instance

Annotations
@Override
sequenceback to summary
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>>.

Parameters
<T>
Type of the lazy values.
values:Iterable<? extends Lazy<? extends T>>

An iterable of lazy values.

Returns:Lazy<Seq<T>>

A lazy sequence of values.

Annotations
@SuppressWarnings:Convert2MethodRef
Exceptions
NullPointerException:
if values is null
stringPrefixback to summary
public String stringPrefix()

Implements io.vavr.Value.stringPrefix.

Doc from io.vavr.Value.stringPrefix.

Returns the name of this Value type, which is used by toString().

Returns:String

This type name.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Implements io.vavr.Value.toString.

Doc from io.vavr.Value.toString.

Clarifies that values have a proper toString() method implemented.

See Object.toString().

Returns:String

A String representation of this object

Annotations
@Override
transformback to summary
public <U> U transform(Function<? super Lazy<T>, ? extends U> f)

Transforms this Lazy.

Parameters
<U>
Type of transformation result
f:Function<? super Lazy<T>, ? extends U>

A transformation

Returns:U

An instance of type U

Exceptions
NullPointerException:
if f is null
valback to summary
public static <T> T val(Supplier<? extends T> supplier, Class<T> type)

Creates a real _lazy value_ of type T, backed by a java.lang.reflect.Proxy which delegates to a Lazy instance.

Parameters
<T>
type of the lazy value
supplier:Supplier<? extends T>

A supplier

type:Class<T>

An interface

Returns:T

A new instance of T

Annotations
@GwtIncompatible:reflection is not supported
@SuppressWarnings:unchecked
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Ensures that the value is evaluated before serialization.

Parameters
s:ObjectOutputStream

An object serialization stream.

Annotations
@GwtIncompatible:The Java serialization protocol is explicitly not supported
Exceptions
IOException:
If an error occurs writing to the stream.