Package com.globalmentor.util
Class Optionals
- java.lang.Object
-
- com.globalmentor.util.Optionals
-
public class Optionals extends java.lang.Object
Utilities to use withOptional
.- Author:
- Garret Wilson
-
-
Constructor Summary
Constructors Constructor Description Optionals()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> void
ifPresentOrElse(java.util.Optional<T> optional, java.util.function.Consumer<? super T> action, java.lang.Runnable emptyAction)
Performs an action with the value if a value is present, otherwise performs another action.static boolean
isPresentAndEquals(java.util.Optional<?> optional, java.lang.Object object)
Determines whether an optional value is present and is equal to some other nullable object.static <T> java.util.Optional<T>
or(java.util.Optional<T> optional, java.util.function.Supplier<java.util.Optional<T>> supplier)
Returns another another optional from a supplier if the given optional is not present.static <T> java.util.stream.Stream<T>
stream(java.util.Optional<T> optional)
Converts an optional to a stream.static java.util.OptionalDouble
toOptionalDouble(java.util.Optional<java.lang.Double> optional)
Converts an optional wrapperDouble
instance to a primitive containingOptionalDouble
instance.static java.util.OptionalInt
toOptionalInt(java.util.Optional<java.lang.Integer> optional)
Converts an optional wrapperInteger
instance to a primitive containingOptionalInt
instance.static java.util.OptionalLong
toOptionalLong(java.util.Optional<java.lang.Long> optional)
Converts an optional wrapperLong
instance to a primitive containingOptionalLong
instance.
-
-
-
Method Detail
-
or
public static <T> java.util.Optional<T> or(@Nonnull java.util.Optional<T> optional, @Nonnull java.util.function.Supplier<java.util.Optional<T>> supplier)
Returns another another optional from a supplier if the given optional is not present.- API Note:
- This method duplicates functionality in Java 9.
- Type Parameters:
T
- The type of value contained in the optional.- Parameters:
optional
- The optional to checksupplier
- The supplier of an alternative optional if the value of the given optional is not present.- Returns:
- The given optional or, if the value is not present, an optional one returned by the given supplier.
- Throws:
java.lang.NullPointerException
- if the supplier isnull
or returnsnull
.- See Also:
- JDK-8080418, Optional.or()
-
ifPresentOrElse
public static <T> void ifPresentOrElse(@Nonnull java.util.Optional<T> optional, @Nonnull java.util.function.Consumer<? super T> action, @Nonnull java.lang.Runnable emptyAction)
Performs an action with the value if a value is present, otherwise performs another action.- API Note:
- This method duplicates functionality in Java 9.
- Type Parameters:
T
- The type of value contained in the optional.- Parameters:
optional
- The optional to checkaction
- The action to perform if the value is present.emptyAction
- The action to perform if no value is present.- Throws:
java.lang.NullPointerException
- if a value is present and the given action isnull
, or if no value is present and the given empty action isnull
.- See Also:
- Optional.ifPresentOrElse()
-
isPresentAndEquals
public static boolean isPresentAndEquals(@Nonnull java.util.Optional<?> optional, @Nullable java.lang.Object object)
Determines whether an optional value is present and is equal to some other nullable object.- API Note:
- Note that the given object with which to compare the optional object can be
null
, as per the parameter ofObject.equals(Object)
. - Implementation Specification:
- This method is equivalent to calling
Optional.isPresent()
and if the result istrue
, callingObject.equals(Object)
on the contained object. - Parameters:
optional
- The optional to checkobject
- The object to compare for equality with the optional value.- Returns:
true
if the given optional value is present and is equal to the given object.- Throws:
java.lang.NullPointerException
- if the given optional isnull
.- See Also:
Optional.isPresent()
,Optional.get()
,Optional.equals(Object)
-
stream
public static <T> java.util.stream.Stream<T> stream(@Nonnull java.util.Optional<T> optional)
Converts an optional to a stream.- API Note:
- This method duplicates functionality in Java 9.
- Type Parameters:
T
- The type of value contained in the optional.- Parameters:
optional
- The optional to check- Returns:
- A stream, either containing the optional value, or empty if the optional is empty.
- See Also:
- Optional.stream()
-
toOptionalDouble
public static java.util.OptionalDouble toOptionalDouble(@Nonnull java.util.Optional<java.lang.Double> optional)
Converts an optional wrapperDouble
instance to a primitive containingOptionalDouble
instance.- Parameters:
optional
- TheOptional
instance to convert.- Returns:
- The equivalent primitive optional wrapper.
-
toOptionalInt
public static java.util.OptionalInt toOptionalInt(@Nonnull java.util.Optional<java.lang.Integer> optional)
Converts an optional wrapperInteger
instance to a primitive containingOptionalInt
instance.- Parameters:
optional
- TheOptional
instance to convert.- Returns:
- The equivalent primitive optional wrapper.
-
toOptionalLong
public static java.util.OptionalLong toOptionalLong(@Nonnull java.util.Optional<java.lang.Long> optional)
Converts an optional wrapperLong
instance to a primitive containingOptionalLong
instance.- Parameters:
optional
- TheOptional
instance to convert.- Returns:
- The equivalent primitive optional wrapper.
-
-