Package io.vavr
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 T
boolean
Clarifies that values have a proper equals() method implemented.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
Checks, if this lazy value is evaluated.boolean
isLazy()
ALazy
's value is computed lazily.boolean
States 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 aLazy
that requests its value from a givenSupplier
.Performs the givenaction
on the first element if this is an eager implementation.Reduces manyLazy
values into a singleLazy
by 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> U
Transforms thisLazy
.static <T> T
private void
Ensures that the value is evaluated before serialization.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
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
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
lazy
instance as narrowed typeLazy<T>
.
-
of
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
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:
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
Evaluates this lazy value and caches it, when called the first time. On subsequent calls, returns the cached value. -
computeValue
-
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:
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
Description copied from interface:Value
Returns a richio.vavr.collection.Iterator
. -
map
Description copied from interface:Value
Maps the underlying value to a different component type. -
peek
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
Transforms thisLazy
.- Type Parameters:
U
- Type of transformation result- Parameters:
f
- A transformation- Returns:
- An instance of type
U
- Throws:
NullPointerException
- iff
is null
-
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
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
Description copied from interface:Value
Clarifies that values have a proper toString() method implemented.See Object.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.
-