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
Optional.
Option is a monadic container type representing the presence or absence of a value.
An instance is either a Option.Some holding a value or the singleton Option.None.
The design is similar to Optional and related types in
Haskell and
Scala.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longThe serial version UID for serialization. -
Method Summary
Modifier and TypeMethodDescriptiondefault <R> Option<R> collect(@NonNull PartialFunction<? super T, ? extends R> partialFunction) Applies apartialFunctionto the value of thisOptionif it is defined for that value, and wraps the result in a newOption.booleanClarifies that values have a proper equals() method implemented.ReturnsSome(value)if thisOptionis aSomeand the contained value satisfies the given predicate.default <U> Option<U> Transforms the value of thisOptionusing the given mapper if it is aSome.default <U> UFolds thisOptioninto a single value by applying one of two functions:ifNoneis applied if this isNonefis applied to the contained value if this isSomeget()Returns the value contained in thisSome, or throws if this isNone.default TReturns the value contained in thisSome, or the value supplied bysupplierif this isNone.default TReturns the value contained in thisSome, or the providedothervalue if this isNone.getOrElseThrow(@NonNull Supplier<X> exceptionSupplier) Returns the value contained in thisSome, or throws an exception provided byexceptionSupplierif this isNone.inthashCode()Clarifies that values have a proper hashCode() method implemented.default booleanisAsync()Indicates that anOption's value is computed synchronously.default booleanChecks whether thisOptioncontains a value.booleanisEmpty()Checks whether thisOptionis empty.default booleanisLazy()Indicates that anOption's value is computed eagerly.default booleanIndicates that anOptioncontains exactly one value.iterator()Returns a richio.vavr.collection.Iterator.default <U> Option<U> Transforms the value of thisSomeusing the given mapper and wraps it in a newSome.default <U> Option<U> mapTo(U value) Maps the underlying value to another fixed value.Maps the underlying value to Voiddefault <U> Try<U> mapTry(@NonNull CheckedFunction1<? super T, ? extends U> mapper) Converts thisOptionto aTry, then applies the given checked function if this is aTry.Success, passing the contained value to it.static <T> Option<T> Narrows a widenedOption<? extends T>toOption<T>via a type-safe cast.static <T> Option<T> none()Returns the singletonNoneinstance.static <T> Option<T> of(T value) Creates anOptionfrom the given value.static <T> Option<T> ofOptional(@NonNull Optional<? extends T> optional) Wraps aOptionalin a newOption.Returns thisOptionif it is non-empty, otherwise returns the provided alternativeOption.Returns thisOptionif it is non-empty; otherwise, returns theOptionprovided by the supplier.Executes the given action on the contained value if thisOptionis defined (Some), otherwise does nothing.Reduces multipleOptionvalues into a singleOptionby transforming anIterable<Option<? extends T>>into anOption<Seq<T>>.static <T> Option<T> some(T value) Creates aSomecontaining the given value.toString()Clarifies that values have a proper toString() method implemented.default <U> UTransforms thisOptioninto a value of typeUusing the given function.traverse(@NonNull Iterable<? extends T> values, @NonNull Function<? super T, ? extends Option<? extends U>> mapper) Maps the elements of an iterable intoOptionvalues and collects the results into a singleOption.static <T> Option<T> ReturnsSomeof the value supplied bysupplierifconditionis true, orNoneifconditionis false.static <T> Option<T> when(boolean condition, T value) ReturnsSomeof the givenvalueifconditionis true, orNoneotherwise.Methods 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 serialVersionUIDThe serial version UID for serialization.- See Also:
-
-
Method Details
-
of
Creates anOptionfrom the given value.- Type Parameters:
T- the value type- Parameters:
value- the value to wrap- Returns:
Some(value)if the value is non-null, otherwiseNone
-
sequence
Reduces multipleOptionvalues into a singleOptionby transforming anIterable<Option<? extends T>>into anOption<Seq<T>>.If any element is
Option.None, the result isNone. Otherwise, all contained values are collected into aSeqwrapped inSome.- Type Parameters:
T- the element type- Parameters:
values- an iterable ofOptionvalues- Returns:
- an
Optioncontaining aSeqof all values, orNoneif any value is empty - Throws:
NullPointerException- ifvaluesis null
-
traverse
static <T,U> Option<Seq<U>> traverse(@NonNull Iterable<? extends T> values, @NonNull Function<? super T, ? extends Option<? extends U>> mapper) Maps the elements of an iterable intoOptionvalues and collects the results into a singleOption.Each element is transformed using
mapper. If any mapped value isOption.None, the result isNone. Otherwise, all mapped values are accumulated into aSeqwrapped inSome.- Type Parameters:
T- the input element typeU- the mapped element type- Parameters:
values- an iterable of input valuesmapper- a function mapping each value to anOption- Returns:
- an
Optioncontaining aSeqof mapped values, orNoneif any mapping yieldsNone - Throws:
NullPointerException- ifvaluesormapperis null
-
some
Creates aSomecontaining the given value.Unlike
of(Object), this method preservesnull:Option.of(null); // yields None Option.some(null); // yields Some(null)
- Type Parameters:
T- the value type- Parameters:
value- the value to wrap, possiblynull- Returns:
- a
Somecontainingvalue
-
none
Returns the singletonNoneinstance.- Type Parameters:
T- the option's component type- Returns:
- the singleton
None
-
narrow
Narrows a widenedOption<? extends T>toOption<T>via a type-safe cast.This is safe because immutable/read-only types are covariant.
- Type Parameters:
T- the component type of theOption- Parameters:
option- theOptionto narrow- Returns:
- the same
Optioninstance, cast toOption<T>
-
when
ReturnsSomeof the value supplied bysupplierifconditionis true, orNoneifconditionis false.- Type Parameters:
T- the type of the optional value- Parameters:
condition- the condition to testsupplier- a supplier of the value, may returnnull- Returns:
Someof the supplied value ifconditionis true, otherwiseNone- Throws:
NullPointerException- ifsupplieris null
-
when
ReturnsSomeof the givenvalueifconditionis true, orNoneotherwise.- Type Parameters:
T- the type of the optional value- Parameters:
condition- the condition to testvalue- the value to wrap, may benull- Returns:
Someofvalueifconditionis true, otherwiseNone
-
ofOptional
Wraps aOptionalin a newOption.- Type Parameters:
T- the type of the contained value- Parameters:
optional- the JavaOptionalto wrap- Returns:
Some(optional.get())if theOptionalis present, otherwiseNone
-
collect
Applies apartialFunctionto the value of thisOptionif it is defined for that value, and wraps the result in a newOption.If the
partialFunctionis not defined for the value,Noneis returned.if (partialFunction.isDefinedAt(value)) { R newValue = partialFunction.apply(value); // wrapped in Some(newValue) }- Type Parameters:
R- the type of the mapped value- Parameters:
partialFunction- a function that may not be defined for all input values- Returns:
- a new
Optioncontaining the mapped value if defined, otherwiseNone - Throws:
NullPointerException- ifpartialFunctionis null
-
isEmpty
boolean isEmpty()Checks whether thisOptionis empty. -
onEmpty
- Parameters:
action- aRunnableto execute- Returns:
- this
Option
-
isAsync
default boolean isAsync()Indicates that anOption's value is computed synchronously. -
isDefined
default boolean isDefined()Checks whether thisOptioncontains a value.Note that
Some(null)is considered defined.- Returns:
trueif this isSome,falseif this isNone
-
isLazy
default boolean isLazy()Indicates that anOption's value is computed eagerly. -
isSingleValued
default boolean isSingleValued()Indicates that anOptioncontains exactly one value.- Specified by:
isSingleValuedin interfaceValue<T>- Returns:
true
-
get
T get()Returns the value contained in thisSome, or throws if this isNone.- Specified by:
getin interfaceValue<T>- Returns:
- the contained value
- Throws:
NoSuchElementException- if this isNone
-
getOrElse
Returns the value contained in thisSome, or the providedothervalue if this isNone.Note that
otheris evaluated eagerly. -
orElse
Returns thisOptionif it is non-empty, otherwise returns the provided alternativeOption.- Parameters:
other- an alternativeOptionto return if this isNone- Returns:
- this
Optionif defined, otherwiseother
-
orElse
Returns thisOptionif it is non-empty; otherwise, returns theOptionprovided by the supplier.- Parameters:
supplier- a supplier of an alternativeOptionif this isNone- Returns:
- this
Optionif defined, otherwise the result ofsupplier.get() - Throws:
NullPointerException- ifsupplieris null
-
getOrElse
Returns the value contained in thisSome, or the value supplied bysupplierif this isNone.The alternative value is evaluated lazily.
- Specified by:
getOrElsein interfaceValue<T>- Parameters:
supplier- a supplier of an alternative value if this isNone- Returns:
- the contained value if defined, otherwise the value returned by
supplier - Throws:
NullPointerException- ifsupplieris null
-
getOrElseThrow
Returns the value contained in thisSome, or throws an exception provided byexceptionSupplierif this isNone.- Specified by:
getOrElseThrowin interfaceValue<T>- Type Parameters:
X- the type of the exception- Parameters:
exceptionSupplier- a supplier of the exception to throw if this isNone- Returns:
- the contained value if defined
- Throws:
X- if thisOptionisNoneNullPointerException- ifexceptionSupplieris null
-
filter
ReturnsSome(value)if thisOptionis aSomeand the contained value satisfies the given predicate. Otherwise, returnsNone.- Parameters:
predicate- a predicate to test the contained value- Returns:
Some(value)if the value satisfies the predicate, otherwiseNone- Throws:
NullPointerException- ifpredicateis null
-
flatMap
Transforms the value of thisOptionusing the given mapper if it is aSome. ReturnsNoneif this isNone.- Type Parameters:
U- the type of the resultingOption's value- Parameters:
mapper- a function to transform the contained value- Returns:
- a new
Optioncontaining the mapped value, orNone - Throws:
NullPointerException- ifmapperis null
-
map
Transforms the value of thisSomeusing the given mapper and wraps it in a newSome. ReturnsNoneif this isNone.- Specified by:
mapin interfaceValue<T>- Type Parameters:
U- the type of the resultingSome's value- Parameters:
mapper- a function to transform the contained value- Returns:
- a new
Somewith the mapped value if this is defined, otherwiseNone - Throws:
NullPointerException- ifmapperis null
-
mapTo
Description copied from interface:ValueMaps the underlying value to another fixed value. -
mapToVoid
Description copied from interface:ValueMaps the underlying value to Void -
mapTry
Converts thisOptionto aTry, then applies the given checked function if this is aTry.Success, passing the contained value to it.- Type Parameters:
U- the type of the resultingTry's value- Parameters:
mapper- a checked function to transform the contained value- Returns:
- a
Trycontaining the mapped value if thisOptionis defined, otherwise aTry.Failure - Throws:
NullPointerException- ifmapperis null
-
fold
default <U> U fold(@NonNull Supplier<? extends U> ifNone, @NonNull Function<? super T, ? extends U> f) Folds thisOptioninto a single value by applying one of two functions:ifNoneis applied if this isNonefis applied to the contained value if this isSome
- Type Parameters:
U- the type of the folded result- Parameters:
ifNone- a function to produce a value if this isNonef- a function to transform the contained value if this isSome- Returns:
- the result of applying
forifNonedepending on whether this isSomeorNone - Throws:
NullPointerException- ififNoneorfis null
-
peek
Executes the given action on the contained value if thisOptionis defined (Some), otherwise does nothing.- Specified by:
peekin interfaceValue<T>- Parameters:
action- a consumer to apply to the contained value- Returns:
- this
Option - Throws:
NullPointerException- ifactionis null
-
transform
Transforms thisOptioninto a value of typeUusing the given function.- Type Parameters:
U- the type of the result- Parameters:
f- a function to transform thisOption- Returns:
- the result of applying
fto thisOption - Throws:
NullPointerException- iffis null
-
iterator
Description copied from interface:ValueReturns a richio.vavr.collection.Iterator. -
equals
Description copied from interface:ValueClarifies that values have a proper equals() method implemented. -
hashCode
int hashCode()Description copied from interface:ValueClarifies that values have a proper hashCode() method implemented.See Object.hashCode().
-
toString
String toString()Description copied from interface:ValueClarifies that values have a proper toString() method implemented.See Object.toString().
-