T - the element typepublic abstract static class Lang.Option<T> extends Object implements Iterable<T>, Serializable
Defines an option of element T. This class can be used to implement the else-if semantic in functional programming and eliminate the null value
| Modifier and Type | Class and Description |
|---|---|
class |
Lang.Option.f
The runtime/instance function namespace
|
| Modifier and Type | Field and Description |
|---|---|
Lang.Option.f |
f
A reference to this runtime function namesapce
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object obj) |
Lang.Option<T> |
filter(Lang.Function<? super T,Boolean> predicate)
If a value is present, and the value matches the given predicate, return an
Option describing the value, otherwise return Lang.NONE. |
<B> Lang.Option<B> |
flatMap(Lang.Function<? super T,Lang.Option<B>> mapper)
If a value is present, apply the provided
Option-bearing mapping function to it, return that result, otherwise return Lang.NONE. |
abstract T |
get()
If a value is present in this
Option, returns the value, otherwise throws NoSuchElementException. |
int |
hashCode() |
boolean |
isDefined()
|
<B> Lang.Option<B> |
map(Lang.Function<? super T,? extends B> mapper)
If a value is present, apply the provided mapping function to it, and if the result is non-null, return an
Option describing the result. |
static <T> Lang.None<T> |
none() |
boolean |
notDefined()
Negate of
isDefined() |
static <T> Lang.Option<T> |
of(T value)
|
T |
orElse(Lang.Func0<? extends T> other)
Return the value if present, otherwise invoke
other and return the result of that invocation. |
T |
orElse(T other)
Return the value if present, otherwise return
other. |
void |
runWith(Lang.Visitor<? super T> consumer) |
static <T> Lang.Some<T> |
some(T value)
Returns an
Option with the specified present non-null value. |
abstract String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitforEach, iterator, spliteratorpublic final Lang.Option.f f
A reference to this runtime function namesapce
public final boolean isDefined()
Returns true if this Option is not Lang.NONE
true if there is a value present, otherwise falsepublic boolean notDefined()
Negate of isDefined()
true if this option is not definedpublic abstract T get() throws NoSuchElementException
If a value is present in this Option, returns the value, otherwise throws NoSuchElementException.
OptionNoSuchElementException - if this Option is Lang.NONEpublic final Lang.Option<T> filter(Lang.Function<? super T,Boolean> predicate)
If a value is present, and the value matches the given predicate, return an Option describing the value, otherwise return Lang.NONE.
predicate - the function to test the value held by this OptionOption describing the value of this Option if a value is present and the value matches the given predicate, otherwise Lang.NONEpublic final <B> Lang.Option<B> map(Lang.Function<? super T,? extends B> mapper)
If a value is present, apply the provided mapping function to it, and if the result is non-null, return an Option describing the result. Otherwise return Lang.NONE.
B - The type of the result of the mapping functionmapper - a mapping function to apply to the value, if presentOption, if a value is present, otherwise Lang.NONENullPointerException - if the mapper function is nullpublic final <B> Lang.Option<B> flatMap(Lang.Function<? super T,Lang.Option<B>> mapper)
If a value is present, apply the provided Option-bearing mapping function to it, return that result, otherwise return Lang.NONE. This method is similar to #map(Function), but the provided mapper is one whose result is already an Option, and if invoked, flatMap does not wrap it with an additional Option.
B - The type parameter to the Option returned bymapper - a mapping function to apply to the value,Option-bearing mapping function to the value of this Option, if a value is present, otherwise Lang.NONENullPointerException - if the mapping function is null or returns a null resultpublic final T orElse(T other)
Return the value if present, otherwise return other.
other - the value to be returned if there is no value present, may be nullotherpublic final T orElse(Lang.Func0<? extends T> other)
Return the value if present, otherwise invoke other and return the result of that invocation.
other - the function that is applied when no value is presentedother.apply()NullPointerException - if value is not present and other is nullpublic final void runWith(Lang.Visitor<? super T> consumer)
public static <T> Lang.None<T> none()
public static <T> Lang.Some<T> some(T value)
Returns an Option with the specified present non-null value.
T - the type of the valuevalue - the value that cannot be nullNullPointerException - if the value specified is nullpublic static <T> Lang.Option<T> of(T value)
Returns an Option with the specified present value if it is not null or Lang.NONE otherwise.
T - the type of the valuevalue - the valueOption describing the value if it is not null or Lang.NONE if the value is nullCopyright © 2014–2019 OSGL (Open Source General Library). All rights reserved.