T - the type of the inner valuepublic class Optional<T> extends 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(Object obj) |
Optional<T> |
executeIfAbsent(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,
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 Throwable> |
orElseThrow(Supplier<? extends X> exc)
Returns inner value if present, otherwise throws the exception provided by supplier function.
|
<R> Optional<R> |
select(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. |
String |
toString() |
public static <T> Optional<T> of(T value)
Optional with the specified present non-null value.T - the type of valuevalue - the value to be present, must be non-nullOptionalNullPointerException - if value is nullofNullable(java.lang.Object)public static <T> Optional<T> ofNullable(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)public static <T> Optional<T> empty()
Optional.T - the type of valueOptionalpublic T get()
NoSuchElementException.
Since 1.2.0 prefer orElseThrow() method as it has readable name.OptionalNoSuchElementException - 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(Consumer<? super T> consumer)
consumer - the consumer functionpublic void ifPresentOrElse(Consumer<? super T> consumer, 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 presentNullPointerException - 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.public Optional<T> executeIfPresent(Consumer<? super T> consumer)
ifPresent, but does not breaks chainingconsumer - consumer functionOptionalifPresent(com.annimon.stream.function.Consumer)public Optional<T> executeIfAbsent(Runnable action)
action - action that invokes if value absentOptionalpublic <R> R custom(Function<Optional<T>,R> function)
Optional.R - the type of the resultfunction - a transforming functionNullPointerException - if function is nullpublic Optional<T> filter(Predicate<? super T> predicate)
predicate - a predicate functionOptional if the value is present and matches predicate,
otherwise an empty Optionalpublic Optional<T> filterNot(Predicate<? super T> predicate)
predicate - a predicate functionOptional if the value is present and doesn't matches predicate,
otherwise an empty Optionalpublic <U> Optional<U> map(Function<? super T,? extends U> mapper)
U - the type of result valuemapper - mapping functionOptional with transformed value if present,
otherwise an empty OptionalNullPointerException - if value is present and
mapper is nullpublic OptionalInt mapToInt(ToIntFunction<? super T> mapper)
mapper - mapping functionOptionalInt with transformed value if present,
otherwise an empty OptionalIntNullPointerException - if value is present and
mapper is nullpublic OptionalLong mapToLong(ToLongFunction<? super T> mapper)
mapper - mapping functionOptionalLong with transformed value if present,
otherwise an empty OptionalLongNullPointerException - if value is present and
mapper is nullpublic OptionalDouble mapToDouble(ToDoubleFunction<? super T> mapper)
mapper - mapping functionOptionalDouble with transformed value if present,
otherwise an empty OptionalDoubleNullPointerException - if value is present and
mapper is nullpublic OptionalBoolean mapToBoolean(ToBooleanFunction<? super T> mapper)
mapper - mapping functionOptionalBoolean with transformed value if present,
otherwise an empty OptionalBooleanNullPointerException - if value is present and
mapper is nullpublic <U> Optional<U> flatMap(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 Optionalpublic Stream<T> stream()
Stream if present, otherwise returns an empty Stream.Streampublic <R> Optional<R> select(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 Optionalpublic Optional<T> or(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 functionNullPointerException - if value is not present and
supplier or value produced by it is nullpublic T orElse(T other)
other.other - the value to be returned if inner value is not presentotherpublic T orElseGet(Supplier<? extends T> other)
other - supplier function that produces value if inner value is not presentpublic T orElseThrow()
NoSuchElementException.NoSuchElementException - if inner value is not presentpublic <X extends Throwable> T orElseThrow(Supplier<? extends X> exc) throws X extends 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 ThrowableCopyright © 2018. All rights reserved.