almost.functional
public final class Optional<T> extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T> Optional<T> |
empty()
Returns an empty Optional instance.
|
Optional<T> |
filter(Predicate<? super T> predicate)
Apply a predicate to the optional, if the optional is present, and the predicate is true, return the optional, otherwise
return empty.
|
T |
get()
If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
|
Optional<T> |
ifPresent(Consumer<? super T> consumer)
If a value is present, invoke the specified consumer with the value, otherwise do nothing.
|
boolean |
isPresent()
Return true if there is a value present, otherwise false.
|
<V> Optional<V> |
map(Function<T,V> function)
If a value is present, map is with function, and if the result is non-null,
return an Optional describing the result.
|
static <T> Optional<T> |
of(T optionalValue)
Returns an Optional with the specified present non-null value.
|
static <T> Optional<T> |
ofNullable(T value)
Returns an Optional of a value which might be null.
|
T |
orElse(T other)
Return the value if present, otherwise return other.
|
Optional<T> |
orElseRun(java.lang.Runnable run)
If the optional is empty run the Runnable.
|
T |
orElseSupplier(Supplier<T> other)
Return the value if present, otherwise get the value from the Supplier.
|
T |
orElseThrow(java.lang.String msg)
If optional is not empty return it's value, if empty throw a NoSuchElementException with message.
|
public static <T> Optional<T> empty()
T - Type of the non-existent valuepublic static <T> Optional<T> of(T optionalValue)
T - the class of the valueoptionalValue - the value to be present, which must be non-nullpublic static <T> Optional<T> ofNullable(T value)
T - the type of the optionalvalue - the valuepublic Optional<T> filter(Predicate<? super T> predicate)
predicate - the predicate to applypublic T get() throws java.util.NoSuchElementException
java.util.NoSuchElementExceptionpublic boolean isPresent()
public Optional<T> ifPresent(Consumer<? super T> consumer)
consumer - consumer to be invoked if present.public T orElse(T other)
other - the value to be returned if there is no value present, may be null.public Optional<T> orElseRun(java.lang.Runnable run)
run - the runnable to run.public T orElseSupplier(Supplier<T> other)
other - a Supplierpublic T orElseThrow(java.lang.String msg)
msg - message for exceptionjava.util.NoSuchElementException - thrown if optional is emptypublic <V> Optional<V> map(Function<T,V> function)
V - The type of the result of the mapping functionfunction - a map function to apply to the value, if present