Class LegacyOptionalExtensions
java.lang.Object
ru.progrm_jarvis.javacommons.object.extension.LegacyOptionalExtensions
Extensions to provide new
Optional methods on older Java versions.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> voidifPresentOrElse(@NotNull Optional<T> optional, @NotNull Consumer<? super T> action, @NotNull Runnable emptyAction) Runs the corresponding action depending on whether the optional ispresent.static <T> booleanChecks if the given optional isempty.static <T> @NotNull Optional<T>or(@NotNull Optional<T> optional, @NonNull Supplier<@NonNull ? extends Optional<? extends T>> supplier) Returns the given optional if it ispresentor an optional provided by the given supplier otherwise.static <T> TorElseThrow(@NotNull Optional<T> optional) Returns the value of the optional if it ispresentor throwsNoSuchElementExceptionotherwise.static <T> Stream<T>
-
Method Details
-
isEmpty
Checks if the given optional isempty.- Type Parameters:
T- type of the value- Parameters:
optional- optional on which the operation happens- Returns:
trueif the optional isOptional.empty()andfalseotherwise- API note
- this method is available on
Optionalitself since Java 11
-
ifPresentOrElse
public static <T> void ifPresentOrElse(@NotNull @NotNull Optional<T> optional, @NotNull @NotNull Consumer<? super T> action, @NotNull @NotNull Runnable emptyAction) Runs the corresponding action depending on whether the optional ispresent.- Type Parameters:
T- type of the value- Parameters:
optional- optional on which the operation happensaction- action to be run on non-emptyoptional's valueemptyAction- action to be run if the optional is empty- Throws:
NullPointerException- if the optional ispresentbutactionisnullor if the optional isemptybutemptyActionisnull- API note
- this method is available on
Optionalitself since Java 9
-
or
@Contract("_, null -> fail") @NotNull public static <T> @NotNull Optional<T> or(@NotNull @NotNull Optional<T> optional, @NonNull @NonNull Supplier<@NonNull ? extends Optional<? extends T>> supplier) Returns the given optional if it ispresentor an optional provided by the given supplier otherwise.- Type Parameters:
T- type of the value- Parameters:
optional- optional on which the operation happenssupplier- supplier of the value in case of optional beingOptional.empty()- Returns:
- given optional if it is
presentor an optional provided by the given supplier otherwise - Throws:
NullPointerException- ifsupplierisnullor the value provided by it isnull- API note
- this method is available on
Optionalitself since Java 9
-
stream
- Type Parameters:
T- type of the value- Parameters:
optional- optional on which the operation happens- Returns:
- given optional if it is
presentor an optional provided by the given supplier otherwise
-
orElseThrow
Returns the value of the optional if it ispresentor throwsNoSuchElementExceptionotherwise.- Type Parameters:
T- type of the value- Parameters:
optional- optional on which the operation happens- Returns:
- the value of the optional if it is
present
-