Package io.vavr
Class Lazy<T>
- java.lang.Object
-
- io.vavr.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)
final CharSequence chars = Lazy.val(() -> "Yay!", CharSequence.class);
- See Also:
- Serialized Form
-
-
Method Summary
Modifier and Type Method Description boolean
equals(java.lang.Object o)
Clarifies that values have a proper equals() method implemented.Option<T>
filter(java.util.function.Predicate<? super T> predicate)
Option<T>
filterNot(java.util.function.Predicate<? super T> predicate)
T
get()
Evaluates this lazy value and caches it, when called the first time.int
hashCode()
Clarifies that values have a proper hashCode() method implemented.boolean
isAsync()
ALazy
's value is computed synchronously.boolean
isEmpty()
Checks, thisValue
is empty, i.e.boolean
isEvaluated()
Checks, if this lazy value is evaluated.boolean
isLazy()
ALazy
's value is computed lazily.boolean
isSingleValued()
States whether this is a single-valued type.Iterator<T>
iterator()
Returns a richio.vavr.collection.Iterator
.<U> Lazy<U>
map(java.util.function.Function<? super T,? extends U> mapper)
Maps the underlying value to a different component type.static <T> Lazy<T>
narrow(Lazy<? extends T> lazy)
Narrows a widenedLazy<? extends T>
toLazy<T>
by performing a type-safe cast.static <T> Lazy<T>
of(java.util.function.Supplier<? extends T> supplier)
Creates aLazy
that requests its value from a givenSupplier
.Lazy<T>
peek(java.util.function.Consumer<? super T> action)
Performs the givenaction
on the first element if this is an eager implementation.static <T> Lazy<Seq<T>>
sequence(java.lang.Iterable<? extends Lazy<? extends T>> values)
Reduces manyLazy
values into a singleLazy
by transforming anIterable<Lazy<? extends T>>
into aLazy<Seq<T>>
.java.lang.String
stringPrefix()
Returns the name of this Value type, which is used by toString().java.lang.String
toString()
Clarifies that values have a proper toString() method implemented.<U> U
transform(java.util.function.Function<? super Lazy<T>,? extends U> f)
Transforms thisLazy
.static <T> T
val(java.util.function.Supplier<? extends T> supplier, java.lang.Class<T> type)
Creates a real _lazy value_ of typeT
, backed by a Proxy which delegates to aLazy
instance.-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, out, out, spliterator, stderr, stdout, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
-
-
-
Method Detail
-
narrow
public static <T> Lazy<T> narrow(Lazy<? extends T> lazy)
Narrows a widenedLazy<? extends T>
toLazy<T>
by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
T
- Component type of theLazy
.- Parameters:
lazy
- ALazy
.- Returns:
- the given
lazy
instance as narrowed typeLazy<T>
.
-
of
public static <T> Lazy<T> of(java.util.function.Supplier<? extends T> supplier)
Creates aLazy
that requests its value from a givenSupplier
. 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 manyLazy
values into a singleLazy
by transforming anIterable<Lazy<? extends T>>
into aLazy<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 typeT
, backed by a Proxy which delegates to aLazy
instance.- Type Parameters:
T
- type of the lazy value- Parameters:
supplier
- A suppliertype
- An interface- Returns:
- A new instance of T
-
get
public T get()
Evaluates this lazy value and caches it, when called the first time. On subsequent calls, returns the cached value.
-
isAsync
public boolean isAsync()
ALazy
's value is computed synchronously.
-
isEmpty
public boolean isEmpty()
Description copied from interface:Value
Checks, thisValue
is empty, i.e. if the underlying value is absent.
-
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()
ALazy
's value is computed lazily.
-
isSingleValued
public boolean isSingleValued()
Description copied from interface:Value
States whether this is a single-valued type.- Specified by:
isSingleValued
in interfaceValue<T>
- Returns:
true
if this is single-valued,false
otherwise.
-
iterator
public Iterator<T> iterator()
Description copied from interface:Value
Returns a richio.vavr.collection.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.
-
peek
public Lazy<T> peek(java.util.function.Consumer<? super T> action)
Description copied from interface:Value
Performs the givenaction
on the first element if this is an eager implementation. Performs the givenaction
on all elements (the first immediately, successive deferred), if this is a lazy implementation.
-
transform
public <U> U transform(java.util.function.Function<? super Lazy<T>,? extends U> f)
Transforms thisLazy
.- Type Parameters:
U
- Type of transformation result- Parameters:
f
- A transformation- Returns:
- An instance of type
U
- Throws:
java.lang.NullPointerException
- iff
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 interfaceValue<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.
-
hashCode
public int hashCode()
Description copied from interface:Value
Clarifies that values have a proper hashCode() method implemented.See Object.hashCode().
-
toString
public java.lang.String toString()
Description copied from interface:Value
Clarifies that values have a proper toString() method implemented.See Object.toString().
-
-