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.
Example of creating a real lazy value (works only with interfaces):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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private TcomputeValue()booleanequals(java.lang.Object o)Clarifies that values have a proper equals() method implemented.Option<T>filter(java.util.function.Predicate<? super T> predicate)Tget()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.booleanisEvaluated()Checks, if this lazy value is evaluated.booleanisLazy()ALazy's value is computed lazily.booleanisSingleValued()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 aLazythat requests its value from a givenSupplier.Lazy<T>peek(java.util.function.Consumer<? super T> action)Performs the givenactionon the first element if this is an eager implementation.static <T> Lazy<Seq<T>>sequence(java.lang.Iterable<? extends Lazy<? extends T>> values)Reduces manyLazyvalues into a singleLazyby transforming anIterable<Lazy<? extends T>>into aLazy<Seq<T>>.java.lang.StringstringPrefix()Returns the name of this Value type, which is used by toString().java.lang.StringtoString()Clarifies that values have a proper toString() method implemented.<U> Utransform(java.util.function.Function<? super Lazy<T>,? extends U> f)Transforms thisLazy.static <T> Tval(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 aLazyinstance.private voidwriteObject(java.io.ObjectOutputStream s)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 Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
lock
private final java.util.concurrent.locks.ReentrantLock lock
-
supplier
private transient volatile java.util.function.Supplier<? extends T> supplier
-
value
private T value
-
-
Constructor Detail
-
Lazy
private Lazy(java.util.function.Supplier<? extends T> supplier)
-
-
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
lazyinstance as narrowed typeLazy<T>.
-
of
public static <T> Lazy<T> of(java.util.function.Supplier<? extends T> supplier)
Creates aLazythat 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 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:
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 aLazyinstance.- 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.
-
computeValue
private T computeValue()
-
isAsync
public boolean isAsync()
ALazy's value is computed synchronously.
-
isEmpty
public boolean isEmpty()
Description copied from interface:ValueChecks, thisValueis 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:ValueStates whether this is a single-valued type.- Specified by:
isSingleValuedin interfaceValue<T>- Returns:
trueif this is single-valued,falseotherwise.
-
iterator
public Iterator<T> iterator()
Description copied from interface:ValueReturns a richio.vavr.collection.Iterator.
-
map
public <U> Lazy<U> map(java.util.function.Function<? super T,? extends U> mapper)
Description copied from interface:ValueMaps the underlying value to a different component type.
-
peek
public Lazy<T> peek(java.util.function.Consumer<? super T> action)
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
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- iffis null
-
stringPrefix
public java.lang.String 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
public boolean equals(java.lang.Object o)
Description copied from interface:ValueClarifies that values have a proper equals() method implemented.
-
hashCode
public int hashCode()
Description copied from interface:ValueClarifies that values have a proper hashCode() method implemented.See Object.hashCode().
-
toString
public java.lang.String toString()
Description copied from interface:ValueClarifies that values have a proper toString() method implemented.See Object.toString().
-
writeObject
@GwtIncompatible("The Java serialization protocol is explicitly not supported") private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException
Ensures that the value is evaluated before serialization.- Parameters:
s- An object serialization stream.- Throws:
java.io.IOException- If an error occurs writing to the stream.
-
-