Class Lazy<T>
java.lang.Object
io.vavr.Lazy<T>
- All Implemented Interfaces:
Value<T>, Serializable, Iterable<T>, Supplier<T>
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate TbooleanClarifies that values have a proper equals() method implemented.get()Evaluates this lazy value and caches it, when called the first time.inthashCode()Clarifies that values have a proper hashCode() method implemented.booleanisAsync()ALazy's value is computed synchronously.booleanisEmpty()Checks, thisValueis empty, i.e.booleanChecks, if this lazy value is evaluated.booleanisLazy()ALazy's value is computed lazily.booleanStates whether this is a single-valued type.iterator()Returns a richio.vavr.collection.Iterator.<U> Lazy<U> Maps the underlying value to a different component type.static <T> Lazy<T> Narrows a widenedLazy<? extends T>toLazy<T>by performing a type-safe cast.static <T> Lazy<T> Creates aLazythat requests its value from a givenSupplier.Performs the givenactionon the first element if this is an eager implementation.Reduces manyLazyvalues into a singleLazyby transforming anIterable<Lazy<? extends T>>into aLazy<Seq<T>>.Returns the name of this Value type, which is used by toString().toString()Clarifies that values have a proper toString() method implemented.<U> UTransforms thisLazy.static <T> Tprivate voidEnsures that the value is evaluated before serialization.Methods inherited from interface 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
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
lock
-
supplier
-
value
-
-
Constructor Details
-
Lazy
-
-
Method Details
-
narrow
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
lazyinstance as narrowed typeLazy<T>.
-
of
-
sequence
Reduces manyLazyvalues into a singleLazyby 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:
NullPointerException- if values is null
-
val
@GwtIncompatible("reflection is not supported") public static <T> T val(Supplier<? extends T> supplier, Class<T> type) - Type Parameters:
T- type of the lazy value- Parameters:
supplier- A suppliertype- An interface- Returns:
- A new instance of T
-
filter
-
get
-
computeValue
-
isAsync
-
isEmpty
-
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
-
isSingleValued
public boolean isSingleValued()Description copied from interface:ValueStates whether this is a single-valued type.- Specified by:
isSingleValuedin interfaceValue<T>- Returns:
trueif this is single-valued,falseotherwise.
-
iterator
-
map
-
peek
Description copied from interface:ValuePerforms the givenactionon the first element if this is an eager implementation. Performs the givenactionon all elements (the first immediately, successive deferred), if this is a lazy implementation. -
transform
Transforms thisLazy.- Type Parameters:
U- Type of transformation result- Parameters:
f- A transformation- Returns:
- An instance of type
U - Throws:
NullPointerException- iffis null
-
stringPrefix
Description copied from interface:ValueReturns the name of this Value type, which is used by toString().- Specified by:
stringPrefixin interfaceValue<T>- Returns:
- This type name.
-
equals
Description copied from interface:ValueClarifies that values have a proper equals() method implemented. -
hashCode
-
toString
-
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.
-