T - The type of the optional value.@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault public class LazyOptional<T> extends java.lang.Object
Optional.
It also provides the ability to listen for invalidation, via
#addListener(Consumer). This method is invoked when the provider of
this object calls invalidate().
To create an instance of this class, use of(NonNullSupplier). Note
that this accepts a NonNullSupplier, so the result of the supplier
must never be null.
The empty instance can be retrieved with empty().
| Modifier and Type | Field and Description |
|---|---|
private static LazyOptional<java.lang.Void> |
EMPTY |
private boolean |
isValid |
private java.util.Set<NonNullConsumer<LazyOptional<T>>> |
listeners |
private static org.apache.logging.log4j.Logger |
LOGGER |
private java.util.concurrent.atomic.AtomicReference<T> |
resolved |
private NonNullSupplier<T> |
supplier |
| Modifier | Constructor and Description |
|---|---|
private |
LazyOptional(NonNullSupplier<T> instanceSupplier) |
| Modifier and Type | Method and Description |
|---|---|
void |
addListener(NonNullConsumer<LazyOptional<T>> listener)
|
<X> LazyOptional<X> |
cast()
This method hides an unchecked cast to the inferred type.
|
static <T> LazyOptional<T> |
empty() |
LazyOptional<T> |
filter(NonNullPredicate<? super T> predicate)
Resolve the contained supplier if non-empty, and filter it by the given
NonNullPredicate, returning empty if false. |
private T |
getValue() |
private T |
getValueUnsafe() |
void |
ifPresent(NonNullConsumer<? super T> consumer)
If non-empty, invoke the specified
NonNullConsumer with the object,
otherwise do nothing. |
void |
invalidate()
Invalidate this
LazyOptional, making it unavailable for further use,
and notifying any listeners that this
has become invalid and they should update. |
boolean |
isPresent()
Check if this
LazyOptional is non-empty. |
<U> LazyOptional<U> |
map(NonNullFunction<? super T,? extends U> mapper)
If a this
LazyOptional is non-empty, return a new
LazyOptional encapsulating the mapping function. |
static <T> LazyOptional<T> |
of(NonNullSupplier<T> instanceSupplier)
Construct a new
LazyOptional that wraps the given
NonNullSupplier. |
T |
orElse(T other)
Resolve the contained supplier if non-empty and return the result, otherwise return
other. |
T |
orElseGet(NonNullSupplier<? extends T> other)
Resolve the contained supplier if non-empty and return the result, otherwise return the
result of
other. |
<X extends java.lang.Throwable> |
orElseThrow(NonNullSupplier<? extends X> exceptionSupplier)
Resolve the contained supplier if non-empty and return the result, otherwise throw the
exception created by the provided
NonNullSupplier. |
private final NonNullSupplier<T> supplier
private java.util.concurrent.atomic.AtomicReference<T> resolved
private java.util.Set<NonNullConsumer<LazyOptional<T>>> listeners
private boolean isValid
@Nonnull private static final LazyOptional<java.lang.Void> EMPTY
private static final org.apache.logging.log4j.Logger LOGGER
private LazyOptional(@Nullable
NonNullSupplier<T> instanceSupplier)
public static <T> LazyOptional<T> of(@Nullable NonNullSupplier<T> instanceSupplier)
LazyOptional that wraps the given
NonNullSupplier.instanceSupplier - The NonNullSupplier to wrap. Cannot return
null, but can be null itself. If null, this method
returns empty().public static <T> LazyOptional<T> empty()
public <X> LazyOptional<X> cast()
Capability.orEmpty(Capability, LazyOptional) should be used.LazyOptional, cast to the inferred generic type@Nullable private T getValue()
private T getValueUnsafe()
public boolean isPresent()
LazyOptional is non-empty.true if this LazyOptional is non-empty, i.e. holds a
non-null supplierpublic void ifPresent(NonNullConsumer<? super T> consumer)
NonNullConsumer with the object,
otherwise do nothing.The - NonNullConsumer to run if this optional is non-empty.java.lang.NullPointerException - if consumer is null and this LazyOptional is non-emptypublic <U> LazyOptional<U> map(NonNullFunction<? super T,? extends U> mapper)
LazyOptional is non-empty, return a new
LazyOptional encapsulating the mapping function. Otherwise, returns
empty().
The supplier inside this object is NOT resolved.
U - The type of the result of the mapping functionmapper - A mapping function to apply to the mod object, if presentLazyOptional describing the result of applying a mapping
function to the value of this LazyOptional, if a value is
present, otherwise an empty LazyOptionaljava.lang.NullPointerException - if mapper is null and this LazyOptional is non-emptypublic LazyOptional<T> filter(NonNullPredicate<? super T> predicate)
NonNullPredicate, returning empty if false.
It is important to note that this method is not lazy, as it must resolve the value of the supplier to validate it with the predicate.
predicate - A NonNullPredicate to apply to the result of the
contained supplier, if non-emptyLazyOptional containing the result of the contained
supplier, if and only if the passed NonNullPredicate returns
true, otherwise an empty LazyOptionaljava.lang.NullPointerException - If predicate is null and this
LazyOptional is non-emptypublic T orElse(T other)
other.other - the value to be returned if this LazyOptional is emptyotherpublic T orElseGet(NonNullSupplier<? extends T> other)
other.other - A NonNullSupplier whose result is returned if this
LazyOptional is emptyother.get()java.lang.NullPointerException - If other is null and this
LazyOptional is non-emptypublic <X extends java.lang.Throwable> T orElseThrow(NonNullSupplier<? extends X> exceptionSupplier) throws X extends java.lang.Throwable
NonNullSupplier.X - Type of the exception to be thrownexceptionSupplier - The NonNullSupplier which will return the
exception to be thrownX - If this LazyOptional is emptyjava.lang.NullPointerException - If exceptionSupplier is null and this
LazyOptional is emptyX extends java.lang.Throwablepublic void addListener(NonNullConsumer<LazyOptional<T>> listener)
listener that will be called when this LazyOptional becomes invalid (via invalidate()).
If this LazyOptional is empty, the listener will be called immediately.
public void invalidate()
LazyOptional, making it unavailable for further use,
and notifying any listeners that this
has become invalid and they should update.
This would typically be used with capability objects. For example, a TE would call this, if they are covered with a microblock panel, thus cutting off pipe connectivity to this side.
Also should be called for all when a TE is invalidated, or a world/chunk unloads, or a entity dies, etc... This allows modders to keep a cache of capability objects instead of re-checking them every tick.