T
- the type of the inner valuepublic class Optional<T>
extends java.lang.Object
Modifier and Type | Method and Description |
---|---|
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 value if present.
|
Optional<T> |
filter(Predicate<? super T> predicate)
Performs filtering on inner value if present.
|
<U> Optional<U> |
flatMap(Function<? super T,Optional<U>> mapper)
Invokes mapping function with
Optional result if value is present. |
T |
get()
Returns 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 |
isPresent()
Checks value present.
|
<U> Optional<U> |
map(Function<? super T,? extends U> mapper)
Invokes mapping function on inner value if present.
|
OptionalInt |
mapToInt(ToIntFunction<? 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.
|
<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() |
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-nullOptional
java.lang.NullPointerException
- 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 nullOptional
of(java.lang.Object)
public static <T> Optional<T> empty()
Optional
.T
- the type of valueOptional
public T get()
NoSuchElementException
.Optional
java.util.NoSuchElementException
- if value is not presentpublic boolean isPresent()
true
if value present, false
otherwisepublic void ifPresent(Consumer<? super T> consumer)
consumer
- consumer functionpublic void ifPresentOrElse(Consumer<? super T> consumer, 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.public Optional<T> executeIfPresent(Consumer<? super T> consumer)
ifPresent
, but does not break chainingconsumer
- consumer functionOptional
ifPresent(com.annimon.stream.function.Consumer)
public Optional<T> executeIfAbsent(java.lang.Runnable action)
action
- action that invokes if value absentOptional
public Optional<T> filter(Predicate<? super T> predicate)
predicate
- a predicate functionOptional
with value if present and matches predicate, otherwise an empty Optional
public <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 Optional
public OptionalInt mapToInt(ToIntFunction<? super T> mapper)
mapper
- mapping functionOptionalInt
with transformed value if present, otherwise an empty OptionalInt
public <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 Optional
public Stream<T> stream()
Stream
if present, otherwise returns an empty Stream
.Stream
public <R> Optional<R> select(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
public Optional<T> or(Supplier<Optional<T>> supplier)
Optional
if value is present, otherwise
returns an Optional
produced by supplier function.supplier
- supplier function that produced 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
public T orElse(T other)
other
.other
- the value to be returned if inner value is not presentother
public T orElseGet(Supplier<? extends T> other)
other
- supplier function that produced value if inner value is not presentpublic <X extends java.lang.Throwable> T orElseThrow(Supplier<? extends X> exc) throws X extends java.lang.Throwable
X
- the type of exception to be thrownexc
- supplier function that produced exception to be thrownX
- if inner value is not presentX extends java.lang.Throwable
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object