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 SummaryFieldsModifier and TypeFieldDescriptionprivate static final @NotNull LazyOptional<Void>private booleanprivate Set<NonNullConsumer<LazyOptional<T>>>private final Objectprivate static final org.apache.logging.log4j.Loggerprivate org.apache.commons.lang3.mutable.Mutable<T>private final NonNullSupplier<T>
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprivateLazyOptional(@Nullable NonNullSupplier<T> instanceSupplier) 
- 
Method SummaryModifier and TypeMethodDescriptionvoidaddListener(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 TgetValue()private TvoidifPresent(NonNullConsumer<? super T> consumer) If non-empty, invoke the specifiedNonNullConsumerwith the object, otherwise do nothing.voidInvalidate thisLazyOptional, making it unavailable for further use, and notifying anylistenersthat this has become invalid and they should update.booleanCheck if thisLazyOptionalis non-empty.<U> LazyOptional<U>lazyMap(NonNullFunction<? super T, ? extends U> mapper) If a thisLazyOptionalis non-empty, return a newLazyOptionalencapsulating the mapping function.<U> Optional<U>map(NonNullFunction<? super T, ? extends U> mapper) If a thisLazyOptionalis non-empty, return a newOptionalencapsulating the mapped value.static <T> LazyOptional<T>of(@Nullable NonNullSupplier<T> instanceSupplier) Construct a newLazyOptionalthat 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.voidremoveListener(NonNullConsumer<LazyOptional<T>> listener) Unregisters alistenerfrom the list to be notified when thisLazyOptionalbecomes invalid (viainvalidate()).
 This allows modder who know they will not need to be notified, to remove the hard reference that this holds to their listener.resolve()
- 
Field Details- 
supplier
- 
lock
- 
resolved
- 
listeners
- 
isValidprivate boolean isValid
- 
EMPTY
- 
LOGGERprivate static final org.apache.logging.log4j.Logger LOGGER
 
- 
- 
Constructor Details- 
LazyOptional
 
- 
- 
Method Details- 
ofConstruct a newLazyOptionalthat wraps the givenNonNullSupplier.- Parameters:
- instanceSupplier- The- NonNullSupplierto wrap. Cannot return null, but can be null itself. If null, this method returns- empty().
 
- 
empty- Returns:
- The singleton empty instance
 
- 
castThis 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
- 
isPresentpublic boolean isPresent()Check if thisLazyOptionalis non-empty.- Returns:
- trueif this- LazyOptionalis non-empty, i.e. holds a non-null supplier
 
- 
ifPresentIf non-empty, invoke the specifiedNonNullConsumerwith the object, otherwise do nothing.- Parameters:
- consumer- The- NonNullConsumerto run if this optional is non-empty.
- Throws:
- NullPointerException- if- consumeris null and this- LazyOptionalis non-empty
 
- 
lazyMapIf a thisLazyOptionalis non-empty, return a newLazyOptionalencapsulating 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 LazyOptionaldescribing the result of applying a mapping function to the value of thisLazyOptional, if a value is present, otherwise an emptyLazyOptional
- Throws:
- NullPointerException- if- mapperis null.
- API Note:
- This method supports post-processing on optional values, without the
          need to explicitly check for a return status., The returned value does not receive invalidation messages from the original LazyOptional. If you need the invalidation, you will need to manage them yourself.
 
- 
mapIf a thisLazyOptionalis non-empty, return a newOptionalencapsulating the mapped value. Otherwise, returnsOptional.empty().- Parameters:
- mapper- A mapping function to apply to the mod object, if present
- Returns:
- An Optionaldescribing the result of applying a mapping function to the value of thisOptional, if a value is present, otherwise an emptyOptional
- Throws:
- NullPointerException- if- mapperis null.
- API Note:
- This method explicitly resolves the value of the LazyOptional. For a non-resolving mapper that will lazily run the mapping, uselazyMap(NonNullFunction).
 
- 
filterResolve 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- A- NonNullPredicateto apply to the result of the contained supplier, if non-empty
- Returns:
- An Optionalcontaining the result of the contained supplier, if and only if the passedNonNullPredicatereturns true, otherwise an emptyOptional
- Throws:
- NullPointerException- If- predicateis null and this- Optionalis non-empty
 
- 
resolve- Returns:
- The resolved optional.
 
- 
orElseResolve the contained supplier if non-empty and return the result, otherwise returnother.- Parameters:
- other- the value to be returned if this- LazyOptionalis empty
- Returns:
- the result of the supplier, if non-empty, otherwise other
 
- 
orElseGetResolve the contained supplier if non-empty and return the result, otherwise return the result ofother.- Parameters:
- other- A- NonNullSupplierwhose result is returned if this- LazyOptionalis empty
- Returns:
- The result of the supplier, if non-empty, otherwise the result of
         other.get()
- Throws:
- NullPointerException- If- otheris null and this- LazyOptionalis non-empty
 
- 
orElseThrowResolve 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- The- NonNullSupplierwhich will return the exception to be thrown
- Returns:
- The result of the supplier
- Throws:
- X- If this- LazyOptionalis empty
- NullPointerException- If- exceptionSupplieris null and this- LazyOptionalis empty
- API Note:
- A method reference to the exception constructor with an empty
          argument list can be used as the supplier. For example,
          IllegalStateException::new
 
- 
addListenerRegister alistenerthat will be called when thisLazyOptionalbecomes invalid (viainvalidate()).If this LazyOptionalis empty, the listener will be called immediately.
- 
removeListenerUnregisters alistenerfrom the list to be notified when thisLazyOptionalbecomes invalid (viainvalidate()).
 This allows modder who know they will not need to be notified, to remove the hard reference that this holds to their listener.
- 
invalidatepublic void invalidate()Invalidate thisLazyOptional, making it unavailable for further use, and notifying anylistenersthat 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. 
 
-