Package io.vavr.control
Interface Option<T>
- Type Parameters:
T
- The type of the optional value.
- All Superinterfaces:
Iterable<T>
,Serializable
,Value<T>
- All Known Implementing Classes:
Option.None
,Option.Some
Replacement for
Optional
.
Option is a monadic container type which
represents an optional value. Instances of Option are either an instance of Option.Some
or the
singleton Option.None
.
Most of the API is taken from Optional
. A similar type can be found in Haskell and Scala.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptiondefault <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
.boolean
Clarifies that values have a proper equals() method implemented.ReturnsSome(value)
if this is aSome
and the value satisfies the given predicate.default <U> Option
<U> Maps the value to a newOption
if this is aSome
, otherwise returnsNone
.default <U> U
Folds either theNone
or theSome
side of the Option value.get()
Gets the value if this is aSome
or throws if this is aNone
.default T
Returns the value if this is aSome
, otherwise theother
value is returned, if this is aNone
.default T
Returns the value if this is aSome
or theother
value if this is aNone
.getOrElseThrow
(Supplier<X> exceptionSupplier) Returns the value if this is aSome
, otherwise throws an exception.int
hashCode()
Clarifies that values have a proper hashCode() method implemented.default boolean
isAsync()
AnOption
's value is computed synchronously.default boolean
Returns true, if this isSome
, otherwise false, if this isNone
.boolean
isEmpty()
Returns true, if this isNone
, otherwise false, if this isSome
.default boolean
isLazy()
AnOption
's value is computed eagerly.default boolean
AnOption
is single-valued.iterator()
Returns a richio.vavr.collection.Iterator
.default <U> Option
<U> Maps the value and wraps it in a newSome
if this is aSome
, returnsNone
.static <T> Option
<T> 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
(Optional<? extends T> optional) Wraps a Java Optional to a new OptionRuns a Java Runnable passed as parameter if thisOption
is empty.Returns thisOption
if it is nonempty, otherwise return the alternative.Returns thisOption
if it is nonempty, otherwise return the result of evaluating supplier.Applies an action to this value, if this option is defined, otherwise does nothing.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.toString()
Clarifies that values have a proper toString() method implemented.default <U> U
Transforms thisOption
.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> 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 caseMethods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElseTry, getOrNull, 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, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
Field Details
-
serialVersionUID
static final long serialVersionUID- See Also:
-
-
Method Details
-
of
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
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:
NullPointerException
- ifvalues
is null
-
traverse
static <T,U> Option<Seq<U>> traverse(Iterable<? extends T> values, 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:
NullPointerException
- if values or f is null.
-
some
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
Returns the single instance ofNone
- Type Parameters:
T
- component type- Returns:
- the single instance of
None
-
narrow
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
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:
NullPointerException
- if the givensupplier
is null
-
when
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
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
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:
NullPointerException
- ifpartialFunction
is null
-
isEmpty
boolean isEmpty()Returns true, if this isNone
, otherwise false, if this isSome
. -
onEmpty
Runs a Java Runnable passed as parameter if thisOption
is empty.- Parameters:
action
- a given Runnable to be run- Returns:
- this
Option
-
isAsync
default boolean isAsync()AnOption
's value is computed synchronously. -
isDefined
default 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
default boolean isLazy()AnOption
's value is computed eagerly. -
isSingleValued
default boolean isSingleValued()AnOption
is single-valued.- Specified by:
isSingleValued
in interfaceValue<T>
- Returns:
true
-
get
T get()Gets the value if this is aSome
or throws if this is aNone
.- Specified by:
get
in interfaceValue<T>
- Returns:
- the value
- Throws:
NoSuchElementException
- if this is aNone
.
-
getOrElse
Returns the value if this is aSome
or theother
value if this is aNone
.Please note, that the other value is eagerly evaluated.
-
orElse
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
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
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
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 throwable
-
filter
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
-
flatMap
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
Maps the value and wraps it in a newSome
if this is aSome
, returnsNone
. -
fold
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
Applies an action to this value, if this option is defined, otherwise does nothing. -
transform
Transforms thisOption
.- Type Parameters:
U
- Type of transformation result- Parameters:
f
- A transformation- Returns:
- An instance of type
U
- Throws:
NullPointerException
- iff
is null
-
iterator
Description copied from interface:Value
Returns a richio.vavr.collection.Iterator
. -
equals
Description copied from interface:Value
Clarifies that values have a proper equals() method implemented. -
hashCode
int hashCode()Description copied from interface:Value
Clarifies that values have a proper hashCode() method implemented.See Object.hashCode().
-
toString
String toString()Description copied from interface:Value
Clarifies that values have a proper toString() method implemented.See Object.toString().
-