public final class Lazy<T> extends Object implements Value<T>, Supplier<T>, Serializable
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);| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object o)
Clarifies that values have a proper equals() method implemented.
|
Option<T> |
filter(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()
A
Lazy's value is computed synchronously. |
boolean |
isEmpty()
Checks, this
Value is empty, i.e. |
boolean |
isEvaluated()
Checks, if this lazy value is evaluated.
|
boolean |
isLazy()
A
Lazy's value is computed lazily. |
boolean |
isSingleValued()
States whether this is a single-valued type.
|
Iterator<T> |
iterator()
Returns a rich
io.vavr.collection.Iterator. |
<U> Lazy<U> |
map(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 widened
Lazy<? extends T> to Lazy<T>
by performing a type-safe cast. |
static <T> Lazy<T> |
of(Supplier<? extends T> supplier)
Creates a
Lazy that requests its value from a given Supplier. |
Lazy<T> |
peek(Consumer<? super T> action)
Performs the given
action on the first element if this is an eager implementation. |
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>>. |
String |
stringPrefix()
Returns the name of this Value type, which is used by toString().
|
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
<U> U |
transform(Function<? super Lazy<T>,? extends U> f)
Transforms this
Lazy. |
static <T> T |
val(Supplier<? extends T> supplier,
Class<T> type)
|
clone, finalize, getClass, notify, notifyAll, wait, wait, waitcollect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, narrow, 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, toVectorpublic static <T> Lazy<T> narrow(Lazy<? extends T> lazy)
Lazy<? extends T> to Lazy<T>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.T - Component type of the Lazy.lazy - A Lazy.lazy instance as narrowed type Lazy<T>.public static <T> Lazy<T> of(Supplier<? extends T> supplier)
Lazy that requests its value from a given Supplier. The supplier is asked only once,
the value is memoized.T - type of the lazy valuesupplier - A supplierpublic static <T> Lazy<Seq<T>> sequence(Iterable<? extends Lazy<? extends T>> values)
Lazy values into a single Lazy by transforming an
Iterable<Lazy<? extends T>> into a Lazy<Seq<T>>.T - Type of the lazy values.values - An iterable of lazy values.NullPointerException - if values is null@GwtIncompatible(value="reflection is not supported") public static <T> T val(Supplier<? extends T> supplier, Class<T> type)
T - type of the lazy valuesupplier - A suppliertype - An interfacepublic T get()
public boolean isAsync()
Lazy's value is computed synchronously.public boolean isEmpty()
ValueValue is empty, i.e. if the underlying value is absent.public boolean isEvaluated()
Note: A value is internally evaluated (once) by calling get().
UnsupportedOperationException - if this value is undefinedpublic boolean isLazy()
Lazy's value is computed lazily.public boolean isSingleValued()
ValueisSingleValued in interface Value<T>true if this is single-valued, false otherwise.public Iterator<T> iterator()
Valueio.vavr.collection.Iterator.public <U> Lazy<U> map(Function<? super T,? extends U> mapper)
Valuepublic Lazy<T> peek(Consumer<? super T> action)
Valueaction 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.public <U> U transform(Function<? super Lazy<T>,? extends U> f)
Lazy.U - Type of transformation resultf - A transformationUNullPointerException - if f is nullpublic String stringPrefix()
ValuestringPrefix in interface Value<T>public boolean equals(Object o)
Valuepublic int hashCode()
ValueSee Object.hashCode().
Copyright © 2019. All Rights Reserved.