Class LazyOptional<T>
- Type Parameters:
T
- The type of the optional value.
Optional
.
It also provides the ability to listen for invalidation, via
addListener(NonNullConsumer)
. 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()
.
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final @NotNull LazyOptional<Void>
private boolean
private Set<NonNullConsumer<LazyOptional<T>>>
private final Object
private static final org.apache.logging.log4j.Logger
private org.apache.commons.lang3.mutable.Mutable<T>
private final NonNullSupplier<T>
-
Constructor Summary
ModifierConstructorDescriptionprivate
LazyOptional
(@Nullable NonNullSupplier<T> instanceSupplier) -
Method Summary
Modifier and TypeMethodDescriptionvoid
addListener
(NonNullConsumer<LazyOptional<T>> listener) <X> LazyOptional<X>
cast()
This method hides an unchecked cast to the inferred type.static <T> LazyOptional<T>
empty()
filter
(NonNullPredicate<? super T> predicate) Resolve the contained supplier if non-empty, and filter it by the givenNonNullPredicate
, returning empty if false.private T
getValue()
private T
void
ifPresent
(NonNullConsumer<? super T> consumer) If non-empty, invoke the specifiedNonNullConsumer
with the object, otherwise do nothing.void
Invalidate thisLazyOptional
, making it unavailable for further use, and notifying anylisteners
that this has become invalid and they should update.boolean
Check if thisLazyOptional
is non-empty.<U> LazyOptional<U>
lazyMap
(NonNullFunction<? super T, ? extends U> mapper) If a thisLazyOptional
is non-empty, return a newLazyOptional
encapsulating the mapping function.<U> Optional<U>
map
(NonNullFunction<? super T, ? extends U> mapper) If a thisLazyOptional
is non-empty, return a newOptional
encapsulating the mapped value.static <T> LazyOptional<T>
of
(@Nullable NonNullSupplier<T> instanceSupplier) Construct a newLazyOptional
that wraps the givenNonNullSupplier
.Resolve the contained supplier if non-empty and return the result, otherwise returnother
.orElseGet
(NonNullSupplier<? extends T> other) Resolve the contained supplier if non-empty and return the result, otherwise return the result ofother
.orElseThrow
(NonNullSupplier<? extends X> exceptionSupplier) Resolve the contained supplier if non-empty and return the result, otherwise throw the exception created by the providedNonNullSupplier
.resolve()
-
Field Details
-
supplier
-
lock
-
resolved
-
listeners
-
isValid
private boolean isValid -
EMPTY
-
LOGGER
private static final org.apache.logging.log4j.Logger LOGGER
-
-
Constructor Details
-
LazyOptional
-
-
Method Details
-
of
Construct a newLazyOptional
that wraps the givenNonNullSupplier
.- Parameters:
instanceSupplier
- TheNonNullSupplier
to wrap. Cannot return null, but can be null itself. If null, this method returnsempty()
.
-
empty
- Returns:
- The singleton empty instance
-
cast
This method hides an unchecked cast to the inferred type. Only use this if you are sure the type should match. For capabilities, generallyCapability.orEmpty(Capability, LazyOptional)
should be used.- Returns:
- This
LazyOptional
, cast to the inferred generic type
-
getValue
-
getValueUnsafe
-
isPresent
public boolean isPresent()Check if thisLazyOptional
is non-empty.- Returns:
true
if thisLazyOptional
is non-empty, i.e. holds a non-null supplier
-
ifPresent
If non-empty, invoke the specifiedNonNullConsumer
with the object, otherwise do nothing.- Parameters:
consumer
- TheNonNullConsumer
to run if this optional is non-empty.- Throws:
NullPointerException
- ifconsumer
is null and thisLazyOptional
is non-empty
-
lazyMap
If a thisLazyOptional
is non-empty, return a newLazyOptional
encapsulating the mapping function. Otherwise, returnsempty()
.The supplier inside this object is NOT resolved.
- Parameters:
mapper
- A mapping function to apply to the mod object, if present- Returns:
- A
LazyOptional
describing the result of applying a mapping function to the value of thisLazyOptional
, if a value is present, otherwise an emptyLazyOptional
- Throws:
NullPointerException
- ifmapper
is null.
-
map
If a thisLazyOptional
is non-empty, return a newOptional
encapsulating the mapped value. Otherwise, returnsOptional.empty()
.- Parameters:
mapper
- A mapping function to apply to the mod object, if present- Returns:
- An
Optional
describing the result of applying a mapping function to the value of thisOptional
, if a value is present, otherwise an emptyOptional
- Throws:
NullPointerException
- ifmapper
is null.
-
filter
Resolve the contained supplier if non-empty, and filter it by the givenNonNullPredicate
, 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.
- Parameters:
predicate
- ANonNullPredicate
to apply to the result of the contained supplier, if non-empty- Returns:
- An
Optional
containing the result of the contained supplier, if and only if the passedNonNullPredicate
returns true, otherwise an emptyOptional
- Throws:
NullPointerException
- Ifpredicate
is null and thisOptional
is non-empty
-
resolve
- Returns:
- The resolved optional.
-
orElse
Resolve the contained supplier if non-empty and return the result, otherwise returnother
.- Parameters:
other
- the value to be returned if thisLazyOptional
is empty- Returns:
- the result of the supplier, if non-empty, otherwise
other
-
orElseGet
Resolve the contained supplier if non-empty and return the result, otherwise return the result ofother
.- Parameters:
other
- ANonNullSupplier
whose result is returned if thisLazyOptional
is empty- Returns:
- The result of the supplier, if non-empty, otherwise the result of
other.get()
- Throws:
NullPointerException
- Ifother
is null and thisLazyOptional
is non-empty
-
orElseThrow
Resolve the contained supplier if non-empty and return the result, otherwise throw the exception created by the providedNonNullSupplier
.- Type Parameters:
X
- Type of the exception to be thrown- Parameters:
exceptionSupplier
- TheNonNullSupplier
which will return the exception to be thrown- Returns:
- The result of the supplier
- Throws:
X
- If thisLazyOptional
is emptyNullPointerException
- IfexceptionSupplier
is null and thisLazyOptional
is empty
-
addListener
Register alistener
that will be called when thisLazyOptional
becomes invalid (viainvalidate()
).If this
LazyOptional
is empty, the listener will be called immediately. -
invalidate
public void invalidate()Invalidate thisLazyOptional
, making it unavailable for further use, and notifying anylisteners
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 (for example, when the TE is removed or unloaded), 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.
-