T
- The type of the value represented by this instancepublic interface Option<T> extends Iterable<T>
Some
or None
. Some
holds a non-null value whilst None
holds no value. The above method causes the user to do null-checks in case the method returned a proper value or not.SomeData getDataIfExists(SomeInput input) { if(haveData(input) { return getSomeDataFromInternalStorage(); } return null; }
Another example is the usage ofOption<SomeData> getDataIfExists(SomeInput input) { if(haveData(input) { return new Some<>(getSomeDataFromInternalStorage()); } return Option.None(); }
Maps
. get
on a Map will either yield the value of the key or null if no such key existed. Map<String, SomeData> map = ....
Option<SomeData> option = Option.apply(map.get("someKey"));Now you have a guaranteed non-null value with which you can work with without performing constant null-checks.
OptionCompanion
) to Option one can get that Scala feel of declaration. import static javascalautils.OptionCompanion.Option; Option<String> option = Option(map.get("someKey"));
Modifier and Type | Field and Description |
---|---|
static None |
DEFAULT_NONE
This is a singleton
None since it anyways cannot represent a state. |
Modifier and Type | Method and Description |
---|---|
static <T> Option<T> |
apply(T value)
Creates an instance of Option.
|
default Optional<T> |
asOptional()
|
default boolean |
contains(T other)
|
default int |
count()
Returns the count which means
1 for nonempty Option's and 0 for empty. |
static <T> Option<T> |
empty()
Returns an empty Option.
|
boolean |
exists(java.util.function.Predicate<T> predicate)
Returns
true if this option is nonempty and the predicate p returns true when applied to this Option's value. |
default Option<T> |
filter(java.util.function.Predicate<T> predicate)
Returns this Option if it is nonempty and applying the predicate p to this Option's value returns
true . |
default Option<T> |
filterNot(java.util.function.Predicate<T> predicate)
Returns this Option if it is nonempty and applying the predicate p to this Option's value returns
false . |
<R> Option<R> |
flatMap(java.util.function.Function<T,Option<R>> function)
Returns an Option consisting of the result of applying the given function to the current
Some . |
default boolean |
forall(java.util.function.Predicate<T> predicate)
Returns
true if the Option is nonempty and the predicate holds true , else false . |
T |
get()
Returns this Option's value if such exists, else
NoSuchElementException is raised. |
T |
getOrElse(java.util.function.Supplier<T> supplier)
Returns this Option's value if such exists, else the value provided by the supplier.
|
default boolean |
isDefined()
|
default boolean |
isEmpty()
|
Iterator<T> |
iterator()
|
<R> Option<R> |
map(java.util.function.Function<T,R> function)
Returns an Option consisting of the result of applying the given function to the current
Some . |
static <T> Option<T> |
None()
Returns an empty Option.
|
static <T> Option<T> |
ofOptional(Optional<T> optional)
|
Option<T> |
orElse(java.util.function.Supplier<Option<T>> supplier)
Returns this Option if it is nonempty, otherwise return the result of provided by the supplier.
|
default T |
orNull()
Returns the Option's value if it is nonempty, or
null if it is empty. |
java.util.stream.Stream<T> |
stream()
Returns the Option's value in a Stream if it is nonempty, or an empty Stream if it is empty.
|
<R> Either<T,R> |
toLeft(java.util.function.Supplier<R> right)
|
<L> Either<L,T> |
toRight(java.util.function.Supplier<L> left)
|
forEach, spliterator
static final None DEFAULT_NONE
static <T> Option<T> apply(T value)
null
value is provided then None
is returned, else Some
containing the provided value.T
- The type for the value this Option representsvalue
- The value this Option shall representstatic <T> Option<T> empty()
singleton
as it anyways cannot represent a value/state.T
- The type for the value this Option representsNone
instancestatic <T> Option<T> None()
empty()
but with the difference it provides a more Scala like feeling if the method is statically imported. None()
as if it was a apply method on a companion object in Scala. import static javascalautils.Option.None; Option<String> opt = None();
T
- The type for the value this Option representsNone
instancedefault boolean contains(T other)
other
- The other object to compare toSome
contains the provided objectdefault int count()
1
for nonempty Option's and 0
for empty.boolean exists(java.util.function.Predicate<T> predicate)
true
if this option is nonempty and the predicate p returns true
when applied to this Option's value.predicate
- The predicatedefault Option<T> filter(java.util.function.Predicate<T> predicate)
true
.predicate
- The predicatedefault Option<T> filterNot(java.util.function.Predicate<T> predicate)
false
.predicate
- The predicatedefault boolean forall(java.util.function.Predicate<T> predicate)
true
if the Option is nonempty and the predicate holds true
, else false
. Option
is a zero-or-one sized collection this is in an essence exactly the same as exists(Predicate)
.predicate
- The predicateT get()
NoSuchElementException
is raised.T getOrElse(java.util.function.Supplier<T> supplier)
supplier
- The supplier to use in case this is a None
default boolean isDefined()
true
this Option is a Some
, else false
default boolean isEmpty()
true
this Option is a None
, else false
<R> Option<R> map(java.util.function.Function<T,R> function)
Some
. None
will always yield None
.R
- The type for the return value from the functionfunction
- The function to use<R> Option<R> flatMap(java.util.function.Function<T,Option<R>> function)
Some
. None
will always yield None
.R
- The type for the return value from the functionfunction
- The function to useOption<T> orElse(java.util.function.Supplier<Option<T>> supplier)
supplier
- The supplier to use in case of None
default T orNull()
null
if it is empty.null
in case of None
java.util.stream.Stream<T> stream()
<R> Either<T,R> toLeft(java.util.function.Supplier<R> right)
<L> Either<L,T> toRight(java.util.function.Supplier<L> left)
Copyright © 2015, Peter Nerg Apache License v2.0