T
- public final class Optional<T>
extends java.lang.Object
implements java.io.Serializable
Modifier and Type | Field and Description |
---|---|
static Optional<java.lang.Double> |
EMPTY_DOUBLE |
static Optional<java.lang.Double[]> |
EMPTY_DOUBLE_ARRAY |
static Optional<java.lang.Long> |
EMPTY_LONG |
static Optional<java.lang.Long[]> |
EMPTY_LONG_ARRAY |
static Optional<java.util.Map<java.lang.String,java.lang.Object>> |
EMPTY_MAP |
static Optional<java.lang.String> |
EMPTY_STRING |
static Optional<java.lang.String[]> |
EMPTY_STRING_ARRAY |
Modifier and Type | Method and Description |
---|---|
static <T> Optional<T> |
empty()
Create an empty Optional value.
|
boolean |
equals(java.lang.Object obj) |
Optional<T> |
filter(Predicate<? super T> predicate)
If a value is present, and the value matches the given predicate, return an Optional describing the value, otherwise return an empty Optional.
|
<U> Optional<U> |
flatMap(Function<? super T,Optional<U>> mapper)
If a value is present, apply the provided Optional-bearing mapping function to it, return that result, otherwise return an empty Optional.
|
T |
get()
If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
|
int |
hashCode() |
void |
ifPresent(Consumer<? super T> consumer)
If a value is present, invoke the specified consumer with the value, otherwise do nothing.
|
boolean |
isPresent()
Returns true if there is a value present, otherwise false.
|
<U> Optional<U> |
map(Function<? super T,? extends U> mapper)
If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Optional describing the result.
|
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 describing the specified value, if non-null, otherwise returns an empty Optional.
|
T |
orElse(T other)
Return the value if present, otherwise return other.
|
T |
orElseGet(Supplier<? extends T> other)
Return the value if present, otherwise invoke other and return the result of that invocation.
|
<X extends java.lang.Throwable> |
orElseThrow(Supplier<? extends X> exceptionSupplier)
Return the contained value, if present, otherwise throw an exception to be created by the provided supplier.
|
java.lang.String |
toString() |
public static final Optional<java.lang.String> EMPTY_STRING
public static final Optional<java.lang.String[]> EMPTY_STRING_ARRAY
public static final Optional<java.lang.Long> EMPTY_LONG
public static final Optional<java.lang.Long[]> EMPTY_LONG_ARRAY
public static final Optional<java.lang.Double> EMPTY_DOUBLE
public static final Optional<java.lang.Double[]> EMPTY_DOUBLE_ARRAY
public static final Optional<java.util.Map<java.lang.String,java.lang.Object>> EMPTY_MAP
public static <T> Optional<T> empty()
public static <T> Optional<T> of(T value)
T
- the class of the valuevalue
- the value to be present, which must be non-nullpublic static <T> Optional<T> ofNullable(T value)
T
- the class of the valuevalue
- the optional value to createpublic boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public Optional<T> filter(Predicate<? super T> predicate)
predicate
- a predicate to apply to the value, if presentpublic boolean isPresent()
public <U> Optional<U> flatMap(Function<? super T,Optional<U>> mapper)
U
- The type parameter to the Optional returned bymapper
- a mapping function to apply to the value, if present the mapping functionpublic T get()
public int hashCode()
hashCode
in class java.lang.Object
public void ifPresent(Consumer<? super T> consumer)
consumer
- block to be executed if a value is presentpublic <U> Optional<U> map(Function<? super T,? extends U> mapper)
U
- The type of the result of the mapping functionmapper
- a mapping function to apply to the value, if presentpublic T orElse(T other)
other
- the value to be returned if there is no value present, may be nullpublic T orElseGet(Supplier<? extends T> other)
other
- a Supplier whose result is returned if no value is presentpublic <X extends java.lang.Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X extends java.lang.Throwable
X
- Type of the exception to be thrownexceptionSupplier
- The supplier which will return the exception to be thrownX
- if there is no value presentX extends java.lang.Throwable
public java.lang.String toString()
toString
in class java.lang.Object