T - the type of the inner valuepublic class Optional<T>
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
<R> R |
custom(Function<Optional<T>,R> function)
Applies custom operator on
Optional. |
static <T> Optional<T> |
empty()
Returns an empty
Optional. |
boolean |
equals(java.lang.Object obj) |
Optional<T> |
executeIfAbsent(java.lang.Runnable action)
Invokes action function if value is absent.
|
Optional<T> |
executeIfPresent(Consumer<? super T> consumer)
Invokes consumer function with the value if present.
|
Optional<T> |
filter(Predicate<? super T> predicate)
Performs filtering on inner value if it is present.
|
Optional<T> |
filterNot(Predicate<? super T> predicate)
Performs negated filtering on inner value if it is present.
|
<U> Optional<U> |
flatMap(Function<? super T,Optional<U>> mapper)
Invokes mapping function with
Optional result if value is present. |
T |
get()
Returns an inner value if present, otherwise throws
NoSuchElementException. |
int |
hashCode() |
void |
ifPresent(Consumer<? super T> consumer)
Invokes consumer function with value if present.
|
void |
ifPresentOrElse(Consumer<? super T> consumer,
java.lang.Runnable emptyAction)
If a value is present, performs the given action with the value, otherwise performs the given empty-based action.
|
boolean |
isEmpty()
Checks the value is not present.
|
boolean |
isPresent()
Checks value present.
|
<U> Optional<U> |
map(Function<? super T,? extends U> mapper)
Invokes the given mapping function on inner value if present.
|
OptionalBoolean |
mapToBoolean(ToBooleanFunction<? super T> mapper)
Invokes mapping function on inner value if present.
|
OptionalDouble |
mapToDouble(ToDoubleFunction<? super T> mapper)
Invokes mapping function on inner value if present.
|
OptionalInt |
mapToInt(ToIntFunction<? super T> mapper)
Invokes the given mapping function on inner value if present.
|
OptionalLong |
mapToLong(ToLongFunction<? super T> mapper)
Invokes mapping function on inner value if present.
|
static <T> Optional<T> |
of(T value)
Returns an
Optional with the specified present non-null value. |
static <T> Optional<T> |
ofNullable(T value)
Returns an
Optional with the specified value, or empty Optional if value is null. |
Optional<T> |
or(Supplier<Optional<T>> supplier)
Returns current
Optional if value is present, otherwise
returns an Optional produced by supplier function. |
T |
orElse(T other)
Returns inner value if present, otherwise returns
other. |
T |
orElseGet(Supplier<? extends T> other)
Returns inner value if present, otherwise returns value produced by supplier function.
|
T |
orElseThrow()
Returns inner value if present, otherwise throws
NoSuchElementException. |
<X extends java.lang.Throwable> |
orElseThrow(Supplier<? extends X> exc)
Returns inner value if present, otherwise throws the exception provided by supplier function.
|
<R> Optional<R> |
select(java.lang.Class<R> clazz)
Keeps inner value only if is present and instance of given class.
|
Stream<T> |
stream()
Wraps a value into
Stream if present, otherwise returns an empty Stream. |
java.lang.String |
toString() |
@NotNull @Contract(value="_ -> new") public static <T> Optional<T> of(@NotNull T value)
Optional with the specified present non-null value.T - the type of valuevalue - the value to be present, must be non-nullOptionaljava.lang.NullPointerException - if value is nullofNullable(java.lang.Object)@NotNull public static <T> Optional<T> ofNullable(@Nullable T value)
Optional with the specified value, or empty Optional if value is null.T - the type of valuevalue - the value which can be nullOptionalof(java.lang.Object)@NotNull @Contract(pure=true) public static <T> Optional<T> empty()
Optional.T - the type of valueOptional@NotNull public T get()
NoSuchElementException.
Since 1.2.0 prefer orElseThrow() method as it has readable name.Optionaljava.util.NoSuchElementException - if value is not presentorElseThrow()public boolean isPresent()
true if a value present, false otherwisepublic boolean isEmpty()
true if a value is not present, false otherwisepublic void ifPresent(@NotNull
Consumer<? super T> consumer)
consumer - the consumer functionpublic void ifPresentOrElse(@NotNull
Consumer<? super T> consumer,
@NotNull
java.lang.Runnable emptyAction)
consumer - the consumer function to be executed, if a value is presentemptyAction - the empty-based action to be performed, if no value is presentjava.lang.NullPointerException - if a value is present and the given consumer function is null,
or no value is present and the given empty-based action is null.@NotNull public Optional<T> executeIfPresent(@NotNull Consumer<? super T> consumer)
ifPresent, but does not breaks chainingconsumer - consumer functionOptionalifPresent(com.annimon.stream.function.Consumer)@NotNull public Optional<T> executeIfAbsent(@NotNull java.lang.Runnable action)
action - action that invokes if value absentOptional@Nullable
public <R> R custom(@NotNull
Function<Optional<T>,R> function)
Optional.R - the type of the resultfunction - a transforming functionjava.lang.NullPointerException - if function is null@NotNull public Optional<T> filter(@NotNull Predicate<? super T> predicate)
predicate - a predicate functionOptional if the value is present and matches predicate,
otherwise an empty Optional@NotNull public Optional<T> filterNot(@NotNull Predicate<? super T> predicate)
predicate - a predicate functionOptional if the value is present and doesn't matches predicate,
otherwise an empty Optional@NotNull public <U> Optional<U> map(@NotNull Function<? super T,? extends U> mapper)
U - the type of result valuemapper - mapping functionOptional with transformed value if present,
otherwise an empty Optionaljava.lang.NullPointerException - if value is present and
mapper is null@NotNull public OptionalInt mapToInt(@NotNull ToIntFunction<? super T> mapper)
mapper - mapping functionOptionalInt with transformed value if present,
otherwise an empty OptionalIntjava.lang.NullPointerException - if value is present and
mapper is null@NotNull public OptionalLong mapToLong(@NotNull ToLongFunction<? super T> mapper)
mapper - mapping functionOptionalLong with transformed value if present,
otherwise an empty OptionalLongjava.lang.NullPointerException - if value is present and
mapper is null@NotNull public OptionalDouble mapToDouble(@NotNull ToDoubleFunction<? super T> mapper)
mapper - mapping functionOptionalDouble with transformed value if present,
otherwise an empty OptionalDoublejava.lang.NullPointerException - if value is present and
mapper is null@NotNull public OptionalBoolean mapToBoolean(@NotNull ToBooleanFunction<? super T> mapper)
mapper - mapping functionOptionalBoolean with transformed value if present,
otherwise an empty OptionalBooleanjava.lang.NullPointerException - if value is present and
mapper is null@NotNull public <U> Optional<U> flatMap(@NotNull Function<? super T,Optional<U>> mapper)
Optional result if value is present.U - the type of result valuemapper - mapping functionOptional with transformed value if present, otherwise an empty Optional@NotNull public Stream<T> stream()
Stream if present, otherwise returns an empty Stream.Stream@NotNull public <R> Optional<R> select(@NotNull java.lang.Class<R> clazz)
R - a type of instance to select.clazz - a class which instance should be selectedOptional with value of type class if present, otherwise an empty Optional@NotNull public Optional<T> or(@NotNull Supplier<Optional<T>> supplier)
Optional if value is present, otherwise
returns an Optional produced by supplier function.supplier - supplier function that produces an Optional to be returnedOptional if value is present, otherwise
an Optional produced by supplier functionjava.lang.NullPointerException - if value is not present and
supplier or value produced by it is null@Nullable public T orElse(@Nullable T other)
other.other - the value to be returned if inner value is not presentother@Nullable public T orElseGet(@NotNull Supplier<? extends T> other)
other - supplier function that produces value if inner value is not present@NotNull public T orElseThrow()
NoSuchElementException.java.util.NoSuchElementException - if inner value is not present@NotNull public <X extends java.lang.Throwable> T orElseThrow(@NotNull Supplier<? extends X> exc) throws X extends java.lang.Throwable
X - the type of exception to be thrownexc - supplier function that produces an exception to be thrownX - if inner value is not presentX extends java.lang.Throwablepublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic int hashCode()
hashCode in class java.lang.Object@NotNull public java.lang.String toString()
toString in class java.lang.Object