Package io.vavr.control
Class Option<T>
- java.lang.Object
-
- io.vavr.control.Option<T>
-
- Type Parameters:
T
- The type of the optional value.
- Direct Known Subclasses:
Option.None
,Option.Some
public abstract class Option<T> extends java.lang.Object implements Iterable<T>, Value<T>, java.io.Serializable
Replacement forOptional
.Option is a monadic container type which represents an optional value. Instances of Option are either an instance of
Option.Some
or the singletonOption.None
.Most of the API is taken from
Optional
. A similar type can be found in Haskell and Scala.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Option.None<T>
Deprecated.will be removed from the public APIstatic class
Option.Some<T>
Deprecated.will be removed from the public API
-
Method Summary
Modifier and Type Method Description <R> Option<R>
collect(PartialFunction<? super T,? extends R> partialFunction)
Collects value that is in the domain of the givenpartialFunction
by mapping the value to typeR
.Option<T>
filter(java.util.function.Predicate<? super T> predicate)
ReturnsSome(value)
if this is aSome
and the value satisfies the given predicate.Option<T>
filterNot(java.util.function.Predicate<? super T> predicate)
ReturnsSome(value)
if this is aSome
and the value not satisfies the given predicate.<U> Option<U>
flatMap(java.util.function.Function<? super T,? extends Option<? extends U>> mapper)
Maps the value to a newOption
if this is aSome
, otherwise returnsNone
.<U> U
fold(java.util.function.Supplier<? extends U> ifNone, java.util.function.Function<? super T,? extends U> f)
Folds either theNone
or theSome
side of the Option value.abstract T
get()
Gets the value if this is aSome
or throws if this is aNone
.T
getOrElse(java.util.function.Supplier<? extends T> supplier)
Returns the value if this is aSome
, otherwise theother
value is returned, if this is aNone
.T
getOrElse(T other)
Returns the value if this is aSome
or theother
value if this is aNone
.<X extends java.lang.Throwable>
TgetOrElseThrow(java.util.function.Supplier<X> exceptionSupplier)
Returns the value if this is aSome
, otherwise throws an exception.boolean
isAsync()
AnOption
's value is computed synchronously.boolean
isDefined()
Returns true, if this isSome
, otherwise false, if this isNone
.abstract boolean
isEmpty()
Returns true, if this isNone
, otherwise false, if this isSome
.boolean
isLazy()
AnOption
's value is computed eagerly.boolean
isSingleValued()
AnOption
is single-valued.Iterator<T>
iterator()
Returns a richIterator
that allows us to perform intermediate, sequential operations known from Vavr's collection library.<U> Option<U>
map(java.util.function.Function<? super T,? extends U> mapper)
Maps the value and wraps it in a newSome
if this is aSome
, otherwise returns aNone
.static <T> Option<T>
narrow(Option<? extends T> option)
Narrows a widenedOption<? extends T>
toOption<T>
by performing a type-safe cast.static <T> Option<T>
none()
Returns the single instance ofNone
static <T> Option<T>
of(T value)
Creates a newOption
of a given value.static <T> Option<T>
ofOptional(java.util.Optional<? extends T> optional)
Wraps a Java Optional to a new OptionOption<T>
onEmpty(java.lang.Runnable action)
Runs a Java Runnable passed as parameter if thisOption
is empty.Option<T>
orElse(Option<? extends T> other)
Returns thisOption
if it is nonempty, otherwise return the alternative.Option<T>
orElse(java.util.function.Supplier<? extends Option<? extends T>> supplier)
Returns thisOption
if it is nonempty, otherwise return the result of evaluating supplier.Option<T>
peek(java.util.function.Consumer<? super T> action)
Applies an action to this value, if this option is defined, otherwise does nothing.static <T> Option<Seq<T>>
sequence(java.lang.Iterable<? extends Option<? extends T>> values)
Reduces manyOption
s into a singleOption
by transforming anIterable<Option<? extends T>>
into aOption<Seq<T>>
.static <T> Option<T>
some(T value)
Creates a newSome
of a given value.<U> U
transform(java.util.function.Function<? super Option<T>,? extends U> f)
Transforms thisOption
.static <T,U>
Option<Seq<U>>traverse(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Option<? extends U>> mapper)
Maps the values of an iterable to a sequence of mapped values into a singleOption
by transforming anIterable<? extends T>
into aOption<Seq<U>>
.static <T> Option<T>
when(boolean condition, java.util.function.Supplier<? extends T> supplier)
CreatesSome
of suppliers value if condition is true, orNone
in other casestatic <T> Option<T>
when(boolean condition, T value)
CreatesSome
of value if condition is true, orNone
in other case-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, equals, exists, forAll, forEach, getOrElseTry, getOrNull, hashCode, out, out, spliterator, stderr, stdout, stringPrefix, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toString, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
-
-
-
Method Detail
-
of
public static <T> Option<T> of(T value)
Creates a newOption
of a given value.- Type Parameters:
T
- type of the value- Parameters:
value
- A value- Returns:
Some(value)
if value is notnull
,None
otherwise
-
sequence
public static <T> Option<Seq<T>> sequence(java.lang.Iterable<? extends Option<? extends T>> values)
Reduces manyOption
s into a singleOption
by transforming anIterable<Option<? extends T>>
into aOption<Seq<T>>
. If any of the Options areOption.None
, then this returnsOption.None
.- Type Parameters:
T
- type of the Options- Parameters:
values
- AnIterable
ofOption
s- Returns:
- An
Option
of aSeq
of results - Throws:
java.lang.NullPointerException
- ifvalues
is null
-
traverse
public static <T,U> Option<Seq<U>> traverse(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Option<? extends U>> mapper)
Maps the values of an iterable to a sequence of mapped values into a singleOption
by transforming anIterable<? extends T>
into aOption<Seq<U>>
.- Type Parameters:
T
- The type of the given values.U
- The mapped value type.- Parameters:
values
- AnIterable
of values.mapper
- A mapper of values to Options- Returns:
- A
Option
of aSeq
of results. - Throws:
java.lang.NullPointerException
- if values or f is null.
-
some
public static <T> Option<T> some(T value)
Creates a newSome
of a given value.The only difference to
of(Object)
is, when called with argumentnull
.Option.of(null); // = None Option.some(null); // = Some(null)
- Type Parameters:
T
- type of the value- Parameters:
value
- A value- Returns:
Some(value)
-
none
public static <T> Option<T> none()
Returns the single instance ofNone
- Type Parameters:
T
- component type- Returns:
- the single instance of
None
-
narrow
public static <T> Option<T> narrow(Option<? extends T> option)
Narrows a widenedOption<? extends T>
toOption<T>
by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
T
- Component type of theOption
.- Parameters:
option
- AOption
.- Returns:
- the given
option
instance as narrowed typeOption<T>
.
-
when
public static <T> Option<T> when(boolean condition, java.util.function.Supplier<? extends T> supplier)
CreatesSome
of suppliers value if condition is true, orNone
in other case- Type Parameters:
T
- type of the optional value- Parameters:
condition
- A boolean valuesupplier
- An optional value supplier, may supplynull
- Returns:
- return
Some
of supplier's value if condition is true, orNone
in other case - Throws:
java.lang.NullPointerException
- if the givensupplier
is null
-
when
public static <T> Option<T> when(boolean condition, T value)
CreatesSome
of value if condition is true, orNone
in other case- Type Parameters:
T
- type of the optional value- Parameters:
condition
- A boolean valuevalue
- An optional value, may benull
- Returns:
- return
Some
of value if condition is true, orNone
in other case
-
ofOptional
public static <T> Option<T> ofOptional(java.util.Optional<? extends T> optional)
Wraps a Java Optional to a new Option- Type Parameters:
T
- type of the value- Parameters:
optional
- a given optional to wrap inOption
- Returns:
Some(optional.get())
if value is JavaOptional
is present,None
otherwise
-
collect
public final <R> Option<R> collect(PartialFunction<? super T,? extends R> partialFunction)
Collects value that is in the domain of the givenpartialFunction
by mapping the value to typeR
.
If the element makes it through that filter, the mapped instance is wrapped inpartialFunction.isDefinedAt(value)
Option
R newValue = partialFunction.apply(value)
- Type Parameters:
R
- The new value type- Parameters:
partialFunction
- A function that is not necessarily defined on value of this option.- Returns:
- A new
Option
instance containing value of typeR
- Throws:
java.lang.NullPointerException
- ifpartialFunction
is null
-
isEmpty
public abstract boolean isEmpty()
Returns true, if this isNone
, otherwise false, if this isSome
.
-
onEmpty
public final Option<T> onEmpty(java.lang.Runnable action)
Runs a Java Runnable passed as parameter if thisOption
is empty.- Parameters:
action
- a given Runnable to be run- Returns:
- this
Option
-
isAsync
public final boolean isAsync()
AnOption
's value is computed synchronously.
-
isDefined
public final boolean isDefined()
Returns true, if this isSome
, otherwise false, if this isNone
.Please note that it is possible to create
new Some(null)
, which is defined.- Returns:
- true, if this
Option
has a defined value, false otherwise
-
isLazy
public final boolean isLazy()
AnOption
's value is computed eagerly.
-
isSingleValued
public final boolean isSingleValued()
AnOption
is single-valued.- Specified by:
isSingleValued
in interfaceValue<T>
- Returns:
true
-
get
public abstract T get()
Gets the value if this is aSome
or throws if this is aNone
.
-
getOrElse
public final T getOrElse(T other)
Returns the value if this is aSome
or theother
value if this is aNone
.Please note, that the other value is eagerly evaluated.
-
orElse
public final Option<T> orElse(Option<? extends T> other)
Returns thisOption
if it is nonempty, otherwise return the alternative.- Parameters:
other
- An alternativeOption
- Returns:
- this
Option
if it is nonempty, otherwise return the alternative.
-
orElse
public final Option<T> orElse(java.util.function.Supplier<? extends Option<? extends T>> supplier)
Returns thisOption
if it is nonempty, otherwise return the result of evaluating supplier.- Parameters:
supplier
- An alternativeOption
supplier- Returns:
- this
Option
if it is nonempty, otherwise return the result of evaluating supplier.
-
getOrElse
public final T getOrElse(java.util.function.Supplier<? extends T> supplier)
Returns the value if this is aSome
, otherwise theother
value is returned, if this is aNone
.Please note, that the other value is lazily evaluated.
-
getOrElseThrow
public final <X extends java.lang.Throwable> T getOrElseThrow(java.util.function.Supplier<X> exceptionSupplier) throws X extends java.lang.Throwable
Returns the value if this is aSome
, otherwise throws an exception.- Specified by:
getOrElseThrow
in interfaceValue<T>
- Type Parameters:
X
- A throwable- Parameters:
exceptionSupplier
- An exception supplier- Returns:
- This value, if this Option is defined, otherwise throws X
- Throws:
X
- a throwableX extends java.lang.Throwable
-
filter
public final Option<T> filter(java.util.function.Predicate<? super T> predicate)
ReturnsSome(value)
if this is aSome
and the value satisfies the given predicate. OtherwiseNone
is returned.- Parameters:
predicate
- A predicate which is used to test an optional value- Returns:
Some(value)
orNone
as specified
-
filterNot
public final Option<T> filterNot(java.util.function.Predicate<? super T> predicate)
ReturnsSome(value)
if this is aSome
and the value not satisfies the given predicate. OtherwiseNone
is returned.- Parameters:
predicate
- A predicate which is used to test an optional value- Returns:
Some(value)
orNone
as specified
-
flatMap
public final <U> Option<U> flatMap(java.util.function.Function<? super T,? extends Option<? extends U>> mapper)
Maps the value to a newOption
if this is aSome
, otherwise returnsNone
.- Type Parameters:
U
- Component type of the resulting Option- Parameters:
mapper
- A mapper- Returns:
- a new
Option
-
map
public final <U> Option<U> map(java.util.function.Function<? super T,? extends U> mapper)
Maps the value and wraps it in a newSome
if this is aSome
, otherwise returns aNone
.
-
fold
public final <U> U fold(java.util.function.Supplier<? extends U> ifNone, java.util.function.Function<? super T,? extends U> f)
Folds either theNone
or theSome
side of the Option value.- Type Parameters:
U
- type of the folded value- Parameters:
ifNone
- maps the left value if this is a Nonef
- maps the value if this is a Some- Returns:
- A value of type U
-
peek
public final Option<T> peek(java.util.function.Consumer<? super T> action)
Applies an action to this value, if this option is defined, otherwise does nothing.
-
transform
public final <U> U transform(java.util.function.Function<? super Option<T>,? extends U> f)
Transforms thisOption
.- Type Parameters:
U
- Type of transformation result- Parameters:
f
- A transformation- Returns:
- An instance of type
U
- Throws:
java.lang.NullPointerException
- iff
is null
-
-