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)
Since 2.0.0 you may also create 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 |
isEmpty()
Checks, this
Value is empty, i.e. |
boolean |
isEvaluated()
Checks, if this lazy value is evaluated.
|
boolean |
isSingleValued()
States whether this is a single-valued type.
|
Iterator<T> |
iterator()
Returns a rich
javaslang.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> |
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, wait
contains, corresponds, eq, exists, forAll, forEach, getOption, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, narrow, out, out, stderr, stdout, toArray, toCharSeq, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaOptional, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toList, toMap, toOption, toQueue, toRight, toRight, toSet, toStack, toStream, toTree, toTry, toTry, toVector
spliterator
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 nullpublic 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 isEmpty()
Value
Value
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 isSingleValued()
Value
isSingleValued
in interface Value<T>
true
if this is single-valued, false
otherwise.public Iterator<T> iterator()
Value
javaslang.collection.Iterator
.public <U> Lazy<U> map(Function<? super T,? extends U> mapper)
Value
public Lazy<T> peek(Consumer<? super T> action)
Value
action
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 transformationU
NullPointerException
- if f
is nullpublic String stringPrefix()
Value
stringPrefix
in interface Value<T>
public boolean equals(Object o)
Value
public int hashCode()
Value
See Object.hashCode().
Copyright © 2016. All Rights Reserved.