Modifier and Type | Method and Description |
---|---|
static <T extends Comparable<T>> |
comparator()
|
static <T> Comparator<? super Optional<? extends T>> |
comparator(Comparator<? super T> comparator)
Wraps a
Comparator to allow compare Optional s. |
static <T,U extends Comparable<U>> |
comparator(Function<T,Optional<U>> keyExtractor)
Wraps a
Comparator to allow compare Optional s. |
static <T> OptionalDouble |
flatMapToDouble(Optional<T> optional,
Function<? super T,OptionalDouble> mapper)
If a value is present, apply the provided
OptionalDouble -bearing
mapping function to it, return that result, otherwise return an empty
OptionalDouble . |
static <T> OptionalInt |
flatMapToInt(Optional<T> optional,
Function<? super T,OptionalInt> mapper)
If a value is present, apply the provided
OptionalInt -bearing mapping
function to it, return that result, otherwise return an empty
OptionalInt . |
static <T> OptionalLong |
flatMapToLong(Optional<T> optional,
Function<? super T,OptionalLong> mapper)
If a value is present, apply the provided
OptionalLong -bearing
mapping function to it, return that result, otherwise return an empty
OptionalLong . |
static <T> Optional<T> |
getFirst(Optional<T>... values)
Returns the first present value.
|
static <T> Optional<T> |
getFirst(Predicate<? super T> isPresent,
Supplier<? extends T>... suppliers)
Returns an
Optional describing the first value matching the predicate
isPresent . |
static <T> Optional<T> |
getFirst(Supplier<? extends Optional<T>>... suppliers)
Returns the first present value.
|
static <T> Optional<T> |
getFirstValue(Predicate<? super T> isPresent,
T... values)
Returns an
Optional describing the first value matching the predicate
isPresent . |
static OptionalDouble |
mapToDouble(Optional<Double> optional)
If a value is present, returns an
OptionalDouble describing the
result. |
static <T> OptionalDouble |
mapToDouble(Optional<T> optional,
ToDoubleFunction<? super T> mapper)
If a value is present, applies
mapper to optional and returns
an OptionalDouble describing the result. |
static OptionalInt |
mapToInt(Optional<Integer> optional)
If a value is present, returns an
OptionalInt describing the result. |
static <T> OptionalInt |
mapToInt(Optional<T> optional,
ToIntFunction<? super T> mapper)
|
static OptionalLong |
mapToLong(Optional<Long> optional)
If a value is present, returns an
OptionalLong describing the result. |
static <T> OptionalLong |
mapToLong(Optional<T> optional,
ToLongFunction<? super T> mapper)
If a value is present, applies
mapper to optional and returns
an OptionalLong describing the result. |
static <T> Optional<T> |
ofNon(Predicate<? super T> isEmpty,
T value)
|
static Optional<String> |
ofNonBlank(String string)
|
static Optional<String> |
ofNonEmpty(String string)
|
static <T extends Collection<?>> |
ofNonEmpty(T collection)
|
static <T> Optional<T[]> |
ofNonEmpty(T[] array)
|
static OptionalInt |
ofSingle(CharSequence string)
Returns an
OptionalInt describing the specified strings first
character, if present, otherwise returns an empty OptionalInt . |
static <T> Optional<T> |
ofSingle(Iterable<T> iterable)
|
static <T> Optional<T> |
ofSingle(Iterator<T> iterator)
|
static <T> Optional<T> |
ofSingle(Stream<T> stream)
|
static <T> Optional<T> |
ofSingle(T[] array)
|
static <T> Stream<T> |
stream(Optional<T>... optionals)
Returns a stream consisting of present elements.
|
public static <T extends Comparable<T>> Comparator<? super Optional<? extends T>> comparator()
T
- optional value typepublic static <T> Comparator<? super Optional<? extends T>> comparator(Comparator<? super T> comparator)
Comparator
to allow compare Optional
s.
When comparing comparator
is called with the optionals object if
present or null
. Therefore it needs to handle null values
appropriately. Use Comparator.nullsFirst(Comparator)
and
Comparator.nullsLast(Comparator)
if your comparator does not.
T
- optional value typecomparator
- optional value comparatorpublic static <T,U extends Comparable<U>> Comparator<T> comparator(Function<T,Optional<U>> keyExtractor)
Comparator
to allow compare Optional
s.T
- the type of element to be comparedU
- the type of the sort keykeyExtractor
- method to extract the optional sort keypublic static <T> OptionalDouble flatMapToDouble(Optional<T> optional, Function<? super T,OptionalDouble> mapper)
OptionalDouble
-bearing
mapping function to it, return that result, otherwise return an empty
OptionalDouble
. This method is similar to
mapToDouble(Optional, ToDoubleFunction)
, but the provided mapper is
one whose result is already an OptionalDouble
, and if invoked,
flatMapToDouble
does not wrap it with an additional
OptionalDouble
.T
- element typeoptional
- optional valuemapper
- a mapping function to apply to the value, if present the
mapping functionOptionalDouble
-bearing mapping
function to the value of this OptionalDouble
, if a value is
present, otherwise an empty OptionalDouble
public static <T> OptionalInt flatMapToInt(Optional<T> optional, Function<? super T,OptionalInt> mapper)
OptionalInt
-bearing mapping
function to it, return that result, otherwise return an empty
OptionalInt
. This method is similar to
mapToInt(Optional, ToIntFunction)
, but the provided mapper is one
whose result is already an OptionalInt
, and if invoked,
flatMap
does not wrap it with an additional OptionalInt
.T
- element typeoptional
- optional valuemapper
- a mapping function to apply to the value, if present the
mapping functionOptionalInt
-bearing mapping
function to the value of this OptionalInt
, if a value is
present, otherwise an empty OptionalInt
public static <T> OptionalLong flatMapToLong(Optional<T> optional, Function<? super T,OptionalLong> mapper)
OptionalLong
-bearing
mapping function to it, return that result, otherwise return an empty
OptionalLong
. This method is similar to
mapToLong(Optional, ToLongFunction)
, but the provided mapper is one
whose result is already an OptionalLong
, and if invoked,
flatMapToDouble
does not wrap it with an additional
OptionalLong
.T
- element typeoptional
- optional valuemapper
- a mapping function to apply to the value, if present the
mapping functionOptionalLong
-bearing mapping
function to the value of this OptionalLong
, if a value is
present, otherwise an empty OptionalLong
@SafeVarargs public static <T> Optional<T> getFirst(Optional<T>... values)
T
- value typevalues
- any number of optional values to test@SafeVarargs public static <T> Optional<T> getFirst(Supplier<? extends Optional<T>>... suppliers)
suppliers
one after the other. Returns empty if no value is present.T
- value typesuppliers
- any number of value suppliers, which values to test,
evaluated in a lazy manner@SafeVarargs public static <T> Optional<T> getFirst(Predicate<? super T> isPresent, Supplier<? extends T>... suppliers)
Optional
describing the first value matching the predicate
isPresent
. Values are created on-demand by calling suppliers
one after the other. Returns an empty Optional
if no element matches.T
- type of the return valueisPresent
- predicate to matchsuppliers
- any number of value suppliers, which values to test,
evaluated in a lazy mannerOptional
describing the first value matching the predicate
isPresent
, an empty Optional
if no element matches@SafeVarargs public static <T> Optional<T> getFirstValue(Predicate<? super T> isPresent, T... values)
public static OptionalDouble mapToDouble(Optional<Double> optional)
OptionalDouble
describing the
result. Otherwise returns an empty OptionalDouble
.optional
- optional valueOptionalDouble
describing the value of optional
,
if a value is present, otherwise an empty OptionalDouble
public static <T> OptionalDouble mapToDouble(Optional<T> optional, ToDoubleFunction<? super T> mapper)
mapper
to optional
and returns
an OptionalDouble
describing the result. Otherwise returns an empty
OptionalDouble
.T
- element typeoptional
- optional valuemapper
- a mapping function to apply to the value, if presentOptionalDouble
describing the result of applying
mapper
function to the value of optional
, if a value
is present, otherwise an empty OptionalDouble
public static OptionalInt mapToInt(Optional<Integer> optional)
OptionalInt
describing the result.
Otherwise returns an empty OptionalInt
.optional
- optional valueOptionalInt
describing the value of optional
, if a
value is present, otherwise an empty OptionalInt
public static <T> OptionalInt mapToInt(Optional<T> optional, ToIntFunction<? super T> mapper)
mapper
to optional
and returns
an OptionalInt
describing the result. Otherwise returns an empty
OptionalInt
.T
- element typeoptional
- optional valuemapper
- a mapping function to apply to the value, if presentOptionalInt
describing the result of applying
mapper
function to the value of optional
, if a value
is present, otherwise an empty OptionalInt
public static OptionalLong mapToLong(Optional<Long> optional)
OptionalLong
describing the result.
Otherwise returns an empty OptionalLong
.optional
- optional valueOptionalLong
describing the value of optional
, if
a value is present, otherwise an empty OptionalLong
public static <T> OptionalLong mapToLong(Optional<T> optional, ToLongFunction<? super T> mapper)
mapper
to optional
and returns
an OptionalLong
describing the result. Otherwise returns an empty
OptionalLong
.T
- element typeoptional
- optional valuemapper
- a mapping function to apply to the value, if presentOptionalLong
describing the result of applying
mapper
function to the value of optional
, if a value
is present, otherwise an empty OptionalLong
public static <T> Optional<T[]> ofNonEmpty(@Nullable T[] array)
public static <T extends Collection<?>> Optional<T> ofNonEmpty(@Nullable T collection)
public static <T> Optional<T> ofSingle(T[] array)
Optional
describing the specified arrays first element, if
present, otherwise returns an empty Optional
. Throws a
TooManyElementsException
if array
contains more than one
element.T
- array element typearray
- the array describing the elementOptional
describing the specified arrays first element, if
present, otherwise returns an empty Optional
TooManyElementsException
- if array
contains more than one
elementpublic static <T> Optional<T> ofSingle(Iterable<T> iterable)
Optional
describing the specified iterables first element,
if present, otherwise returns an empty Optional
. Throws a
TooManyElementsException
if iterable
contains more than one
element.T
- iterable element typeiterable
- the iterable describing the elementOptional
describing the specified iterables first element,
if present, otherwise returns an empty Optional
TooManyElementsException
- if iterable
contains more than one
elementpublic static <T> Optional<T> ofSingle(Iterator<T> iterator)
Optional
describing the specified iterators first element,
if present, otherwise returns an empty Optional
. Throws a
TooManyElementsException
if iterator
contains more than one
element.T
- iterator element typeiterator
- the iterator describing the elementOptional
describing the specified iterators first element,
if present, otherwise returns an empty Optional
TooManyElementsException
- if iterator
contains more than one
elementpublic static <T> Optional<T> ofSingle(Stream<T> stream)
Optional
describing the specified streams first element,
if present, otherwise returns an empty Optional
. Throws a
TooManyElementsException
if stream
contains more than one
element.
This is a terminal operation.
T
- stream element typestream
- the stream describing the elementOptional
describing the specified streams first element,
if present, otherwise returns an empty Optional
TooManyElementsException
- if stream
contains more than one
elementpublic static OptionalInt ofSingle(CharSequence string)
OptionalInt
describing the specified strings first
character, if present, otherwise returns an empty OptionalInt
. Throws
a TooManyElementsException
if string
contains more than one
character, ignoring trailing whitespaces.
Whitespace characters are recognized using
Characters.isAsciiWhitespace(char)
.
string
- the string describing the characterOptionalInt
describing the specified strings first
character, if present, otherwise returns an empty OptionalInt
TooManyElementsException
- if string
contains more than one
character, ignoring trailing whitespaces@SafeVarargs public static <T> Stream<T> stream(Optional<T>... optionals)
T
- element typeoptionals
- array of optional elementsCopyright © 2025. All rights reserved.