Class Lazy<T>
java.lang.Object
io.vavr.Lazy<T>
- Type Parameters:
T- the type of the lazily evaluated value
- All Implemented Interfaces:
Value<T>, Serializable, Iterable<T>, Supplier<T>
Represents a lazily evaluated value. Unlike a standard
Supplier,
Lazy is memoizing: the computation is performed at most once, ensuring referential transparency.
This type behaves more like a Functor than a Monad: it represents a value rather than capturing
a specific state. Therefore, it does not provide operations like flatMap or orElse.
Example usage:
final Lazy<Double> l = Lazy.of(Math::random);
l.isEvaluated(); // false
double value = l.get(); // evaluates and returns a random number, e.g., 0.123
l.isEvaluated(); // true
double memoizedValue = l.get(); // returns the same value as before, e.g., 0.123
Creating a truly lazy value for an interface type:
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.Filters this lazy value by applying the given predicate to the evaluated value.get()Evaluates this lazy value on the first call and caches the result.inthashCode()Clarifies that values have a proper hashCode() method implemented.booleanisAsync()Indicates that thisLazyvalue is computed synchronously.booleanisEmpty()Checks, thisValueis empty, i.e.booleanChecks whether this lazy value has been evaluated.booleanisLazy()Indicates that thisLazyvalue 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.<U> Lazy<U> mapTo(U value) Maps the underlying value to another fixed value.Maps the underlying value to Voidstatic <T> Lazy<T> Narrows aLazy<? extends T>toLazy<T>via a type-safe cast.static <T> Lazy<T> Creates aLazyinstance that obtains its value from the givenSupplier.Performs the givenactionon the first element if this is an eager implementation.Combines multipleLazyinstances into a singleLazycontaining a sequence of their evaluated values.Returns the name of this Value type, which is used by toString().toString()Clarifies that values have a proper toString() method implemented.<U> UApplies a transformation function to the value contained in thisLazy, producing a newLazyinstance of the transformed value.static <T> Tprivate voidForces the lazy value to be evaluated before it is serialized.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 aLazy<? extends T>toLazy<T>via a type-safe cast. Safe here because the lazy value is immutable and no elements can be added that would violate the type (covariance)- Type Parameters:
T- the target element type- Parameters:
lazy- the lazy value to narrow- Returns:
- the same lazy value viewed as
Lazy<T>
-
of
Creates aLazyinstance that obtains its value from the givenSupplier. The supplier is invoked at most once, and the result is cached for subsequent calls.- Type Parameters:
T- the type of the lazy value- Parameters:
supplier- the supplier providing the value- Returns:
- a new
Lazyinstance
-
sequence
Combines multipleLazyinstances into a singleLazycontaining a sequence of their evaluated values.Transforms an
Iterable<Lazy<? extends T>>into aLazy<Seq<T>>, evaluating each value lazily when the resultingLazyis accessed.- Type Parameters:
T- the type of the lazy values- Parameters:
values- anIterableof lazy values- Returns:
- a
Lazycontaining a sequence of the evaluated values - Throws:
NullPointerException- ifvaluesis null
-
val
@GwtIncompatible("reflection is not supported") public static <T> T val(@NonNull Supplier<? extends T> supplier, @NonNull Class<T> type) - Type Parameters:
T- the type of the lazy value- Parameters:
supplier- the supplier providing the value when neededtype- the interface class that the proxy should implement- Returns:
- a new proxy instance of type
Tthat evaluates lazily
-
filter
Filters this lazy value by applying the given predicate to the evaluated value.If the predicate matches the evaluated value, it returns
Somecontaining the value. Otherwise, it returnsNone.- Parameters:
predicate- the predicate to test the value- Returns:
Some(value)if the predicate is satisfied,Noneotherwise- Throws:
NullPointerException- ifpredicateisnull
-
get
-
computeValue
-
isAsync
-
isEmpty
-
isEvaluated
public boolean isEvaluated()Checks whether this lazy value has been evaluated.Note: The value is evaluated internally (at most once) when
get()is called.- Returns:
trueif the value has been evaluated,falseotherwise- Throws:
UnsupportedOperationException- if this lazy 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
Description copied from interface:ValueMaps the underlying value to a different component type. -
mapTo
-
mapToVoid
-
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
Applies a transformation function to the value contained in thisLazy, producing a newLazyinstance of the transformed value.- Type Parameters:
U- the type of the result of the transformation- Parameters:
f- the function to transform the value- Returns:
- a new
Lazyinstance containing the transformed value - 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 Forces the lazy value to be evaluated before it is serialized.- Parameters:
s- the object output stream to write to- Throws:
IOException- if an I/O error occurs during serialization
-