Modifier and Type | Method and Description |
---|---|
static <T> T |
getFirst(Supplier<? extends T>... suppliers)
Returns the first non-null value.
|
static <T> T |
getFirst(T... values)
Returns the first non-null value.
|
static <T> void |
ifNonNull(T value,
Consumer<? super T> consumer)
If
value is non-null, invoke the specified consumer with
value , otherwise do nothing. |
static <T> void |
ifNonNullOrElse(T value,
Consumer<? super T> action,
Runnable emptyAction)
If
value is non-null, performs the given action with value ,
otherwise performs the given empty-based action. |
static <T,U> U |
map(T value,
Function<? super T,? extends U> mapper)
If
value is non-null, apply the provided mapping function to it, and
return the nullable result. |
static <T> T |
or(T value,
Supplier<? extends T> supplier)
If
value is non-null, returns value , otherwise returns a
nullable value produced by the supplying function. |
static <T> T |
orElse(T value,
T other)
Return
value if non-null, otherwise return other . |
static <T> T |
orElseGet(T value,
Supplier<? extends T> other)
Return
value if non-null, otherwise invoke other and return
the result of that invocation. |
static <T> T |
orElseThrow(T value)
If
value is non-null, returns value , otherwise throws
java.lang.NullPointerException . |
static <T,X extends Throwable> |
orElseThrow(T value,
Supplier<? extends X> exceptionSupplier)
Return
value , if non-null, otherwise throw an exception to be created
by the provided supplier. |
@Nullable @SafeVarargs public static <T> T getFirst(T... values)
T
- value typevalues
- any number of nullable values to test@Nullable @SafeVarargs public static <T> T getFirst(Supplier<? extends T>... suppliers)
suppliers
one after the other. Returns null if no value is non-null.T
- value typesuppliers
- any number of value suppliers, which values to test,
evaluated in a lazy mannerpublic static <T> void ifNonNull(@Nullable T value, Consumer<? super T> consumer)
value
is non-null, invoke the specified consumer with
value
, otherwise do nothing.T
- value typevalue
- nullable valueconsumer
- executed if value
is nullpublic static <T> void ifNonNullOrElse(@Nullable T value, Consumer<? super T> action, Runnable emptyAction)
value
is non-null, performs the given action with value
,
otherwise performs the given empty-based action.T
- value typevalue
- nullable valueaction
- the action to be performed, if value
is non-nullemptyAction
- the empty-based action to be performed, if value
is null@Nullable public static <T,U> U map(@Nullable T value, Function<? super T,? extends U> mapper)
value
is non-null, apply the provided mapping function to it, and
return the nullable result. Otherwise return null
.T
- value typeU
- the mapping functions result typevalue
- nullable valuemapper
- a mapping function to apply to value
, if non-nullvalue
, if value
is non-null, otherwise null
@Nullable public static <T> T or(@Nullable T value, Supplier<? extends T> supplier)
value
is non-null, returns value
, otherwise returns a
nullable value produced by the supplying function.T
- value typevalue
- nullable valuesupplier
- the supplying function that produces a nullable value to be
returnedvalue
, if value
is non-null, otherwise a
nullable value produced by the supplying function.public static <T> T orElse(@Nullable T value, T other)
value
if non-null, otherwise return other
.T
- value typevalue
- nullable valueother
- the value to be returned if value
is nullvalue
, if non-null, otherwise other
public static <T> T orElseGet(@Nullable T value, Supplier<? extends T> other)
value
if non-null, otherwise invoke other
and return
the result of that invocation.T
- value typevalue
- nullable valueother
- a Supplier
whose result is returned if value
is
nullvalue
if non-null otherwise the result of other.get()
public static <T> T orElseThrow(@Nullable T value)
value
is non-null, returns value
, otherwise throws
java.lang.NullPointerException
.T
- value typevalue
- nullable valuevalue
NullPointerException
- if value
is nullpublic static <T,X extends Throwable> T orElseThrow(@Nullable T value, Supplier<? extends X> exceptionSupplier) throws X extends Throwable
value
, if non-null, otherwise throw an exception to be created
by the provided supplier.T
- value typeX
- Type of the exception to be thrownvalue
- nullable valueexceptionSupplier
- The supplier which will return the exception to be
thrownX
- if value
is nullX extends Throwable
Copyright © 2025. All rights reserved.