Package javascalautils
Interface Option<T>
-
- Type Parameters:
T
- The type of the value represented by this instance
- All Superinterfaces:
java.lang.Iterable<T>
public interface Option<T> extends java.lang.Iterable<T>
Represents optional values.
Instances of Option are either an instance ofSome
orNone
.
Some
holds a non-null value whilstNone
holds no value.
The primary use case is to replace ugly null-checks.
Consider the method:
SomeData getDataIfExists(SomeInput input) { if(haveData(input) { return getSomeDataFromInternalStorage(); } return null; }
A neater way would be:Option<SomeData> getDataIfExists(SomeInput input) { if(haveData(input) { return new Some<>(getSomeDataFromInternalStorage()); } return Option.None(); }
Maps
.
Performing aget
on a Map will either yield the value of the key or null if no such key existed.
Consider the Map:
Map<String, SomeData> map = ....
To avoid null checks one could do like so:
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.
Statically importing methods from the companion class (OptionCompanion
) to Option one can get that Scala feel of declaration.
import static javascalautils.OptionCompanion.Option; Option<String> option = Option(map.get("someKey"));
- Since:
- 1.0
-
-
Field Summary
Fields Modifier and Type Field Description static None
DEFAULT_NONE
This is a singletonNone
since it anyways cannot represent a state.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <T> Option<T>
apply(T value)
Creates an instance of Option.default java.util.Optional<T>
asOptional()
Converts thisOption
to a correspondingOptional
.default boolean
contains(T other)
default int
count()
Returns the count which means1
for nonempty Option's and0
for empty.static <T> Option<T>
empty()
Returns an empty Option.boolean
exists(java.util.function.Predicate<T> predicate)
Returnstrue
if this option is nonempty and the predicate p returnstrue
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 returnstrue
.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 returnsfalse
.<R> Option<R>
flatMap(ThrowableFunction1<T,Option<R>> function)
Returns an Option consisting of the result of applying the given function to the currentSome
.default boolean
forall(java.util.function.Predicate<T> predicate)
Returnstrue
if the Option is nonempty and the predicate holdstrue
, elsefalse
.T
get()
Returns this Option's value if such exists, elseNoSuchElementException
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()
java.util.Iterator<T>
iterator()
Returns the Option's value in anIterator
if it is nonempty, or an emptyIterator
if it is empty.<R> Option<R>
map(ThrowableFunction1<T,R> function)
Returns an Option consisting of the result of applying the given function to the currentSome
.static <T> Option<T>
None()
Returns an empty Option.static <T> Option<T>
ofOptional(java.util.Optional<T> optional)
Converts theOptional
to a correspondingOption
.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, ornull
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)
-
-
-
Field Detail
-
DEFAULT_NONE
static final None DEFAULT_NONE
-
-
Method Detail
-
apply
static <T> Option<T> apply(T value)
Creates an instance of Option.
If anull
value is provided thenNone
is returned, elseSome
containing the provided value.- Type Parameters:
T
- The type for the value this Option represents- Parameters:
value
- The value this Option shall represent- Returns:
- The Option representing the provided value
- Since:
- 1.0
-
empty
static <T> Option<T> empty()
Returns an empty Option.
In practice this returns asingleton
as it anyways cannot represent a value/state.- Type Parameters:
T
- The type for the value this Option represents- Returns:
- The default
None
instance - Since:
- 1.0
-
None
static <T> Option<T> None()
Returns an empty Option.
This is the same asempty()
but with the difference it provides a more Scala like feeling if the method is statically imported.
One can useNone()
as if it was a apply method on a companion object in Scala.
E.g.import static javascalautils.Option.None; Option<String> opt = None();
- Type Parameters:
T
- The type for the value this Option represents- Returns:
- The default
None
instance - Since:
- 1.2
-
contains
default boolean contains(T other)
- Parameters:
other
- The other object to compare to- Returns:
- If this
Some
contains the provided object - Since:
- 1.0
-
count
default int count()
Returns the count which means1
for nonempty Option's and0
for empty.- Returns:
- The count
- Since:
- 1.0
-
exists
boolean exists(java.util.function.Predicate<T> predicate)
Returnstrue
if this option is nonempty and the predicate p returnstrue
when applied to this Option's value.- Parameters:
predicate
- The predicate- Returns:
- If the predicate matches
- Since:
- 1.0
-
filter
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 returnstrue
.- Parameters:
predicate
- The predicate- Returns:
- The Option representing the match
- Since:
- 1.0
-
filterNot
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 returnsfalse
.- Parameters:
predicate
- The predicate- Returns:
- The Option representing the match
- Since:
- 1.0
-
forall
default boolean forall(java.util.function.Predicate<T> predicate)
Returnstrue
if the Option is nonempty and the predicate holdstrue
, elsefalse
.
As anOption
is a zero-or-one sized collection this is in an essence exactly the same asexists(Predicate)
.- Parameters:
predicate
- The predicate- Returns:
- If the predicate matches
- Since:
- 1.0
-
get
T get()
Returns this Option's value if such exists, elseNoSuchElementException
is raised.- Returns:
- The value of the Option
- Since:
- 1.0
-
getOrElse
T getOrElse(java.util.function.Supplier<T> supplier)
Returns this Option's value if such exists, else the value provided by the supplier.- Parameters:
supplier
- The supplier to use in case this is aNone
- Returns:
- The value of the Option or the value provided by the supplier
- Since:
- 1.0
-
isDefined
default boolean isDefined()
- Returns:
true
this Option is aSome
, elsefalse
- Since:
- 1.0
-
isEmpty
default boolean isEmpty()
- Returns:
true
this Option is aNone
, elsefalse
- Since:
- 1.0
-
iterator
java.util.Iterator<T> iterator()
Returns the Option's value in anIterator
if it is nonempty, or an emptyIterator
if it is empty.- Specified by:
iterator
in interfacejava.lang.Iterable<T>
- Returns:
- The iterator for the Option
- Since:
- 1.0
-
map
<R> Option<R> map(ThrowableFunction1<T,R> function)
Returns an Option consisting of the result of applying the given function to the currentSome
.
Applying map toNone
will always yieldNone
.- Type Parameters:
R
- The type for the return value from the function- Parameters:
function
- The function to use- Returns:
- The Option containing the mapped value
- Since:
- 1.0
-
flatMap
<R> Option<R> flatMap(ThrowableFunction1<T,Option<R>> function)
Returns an Option consisting of the result of applying the given function to the currentSome
.
Applying map toNone
will always yieldNone
.- Type Parameters:
R
- The type for the return value from the function- Parameters:
function
- The function to use- Returns:
- The Option containing the mapped value
- Since:
- 1.2
-
orElse
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.- Parameters:
supplier
- The supplier to use in case ofNone
- Returns:
- This Option or the one provided by the supplier
- Since:
- 1.0
-
orNull
default T orNull()
Returns the Option's value if it is nonempty, ornull
if it is empty.- Returns:
- The value of the Option or
null
in case ofNone
- Since:
- 1.0
-
stream
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.- Returns:
- The stream for the Option
- Since:
- 1.0
-
asOptional
default java.util.Optional<T> asOptional()
Converts thisOption
to a correspondingOptional
.- Returns:
- The Optional instance
- Since:
- 1.0
-
-