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> |
filter(Predicate<? super T> predicate)
Performs 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 |
isPresent()
Checks value present.
|
<U> Optional<U> |
map(Function<? super T,? extends U> mapper)
Invokes the given 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. |
T |
or(T other)
Returns inner value if present, otherwise returns
other . |
T |
orGet(Supplier<? extends T> other)
Returns inner value if present, otherwise returns value produced by supplier function.
|
<X extends java.lang.Throwable> |
orThrow(Supplier<? extends X> exc)
Returns inner value if present, otherwise throws the exception provided by supplier function.
|
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 a value present, false
otherwisepublic void ifPresent(Consumer<? super T> consumer)
consumer
- the 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> filter(Predicate<? super T> predicate)
predicate
- a predicate functionOptional
if the value is 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
java.lang.NullPointerException
- if value is present and
mapper
is null
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 T or(T other)
other
.other
- the value to be returned if inner value is not presentother
public T orGet(Supplier<? extends T> other)
other
- supplier function that produces value if inner value is not presentpublic <X extends java.lang.Throwable> T orThrow(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.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