Interface Option<T>
-
- Type Parameters:
T- the type of the optional value
- All Superinterfaces:
java.lang.Iterable<T>,java.io.Serializable,Value<T>
- All Known Implementing Classes:
Option.None,Option.Some
public interface Option<T> extends Value<T>, java.io.Serializable
A replacement forOptional.Optionis a monadic container type representing the presence or absence of a value. An instance is either aOption.Someholding a value or the singletonOption.None.The design is similar to
Optionaland related types in Haskell and Scala.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classOption.None<T>None is a singleton representation of the undefinedOption.static classOption.Some<T>Some represents a definedOption.
-
Field Summary
Fields Modifier and Type Field Description static longserialVersionUIDThe serial version UID for serialization.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <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.booleanequals(java.lang.Object o)Clarifies that values have a proper equals() method implemented.default Option<T>filter(@NonNull java.util.function.Predicate<? super T> predicate)ReturnsSome(value)if thisOptionis aSomeand the contained value satisfies the given predicate.default <U> Option<U>flatMap(@NonNull java.util.function.Function<? super T,? extends Option<? extends U>> mapper)Transforms the value of thisOptionusing the given mapper if it is aSome.default <U> Ufold(@NonNull java.util.function.Supplier<? extends U> ifNone, @NonNull java.util.function.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 isSomeTget()Returns the value contained in thisSome, or throws if this isNone.default TgetOrElse(@NonNull java.util.function.Supplier<? extends T> supplier)Returns the value contained in thisSome, or the value supplied bysupplierif this isNone.default TgetOrElse(T other)Returns the value contained in thisSome, or the providedothervalue if this isNone.default <X extends java.lang.Throwable>
TgetOrElseThrow(@NonNull java.util.function.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 booleanisDefined()Checks whether thisOptioncontains a value.booleanisEmpty()Checks whether thisOptionis empty.default booleanisLazy()Indicates that anOption's value is computed eagerly.default booleanisSingleValued()Indicates that anOptioncontains exactly one value.default @NonNull Iterator<T>iterator()Returns a richio.vavr.collection.Iterator.default <U> Option<U>map(@NonNull java.util.function.Function<? super T,? extends U> mapper)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.default Option<java.lang.Void>mapToVoid()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>narrow(@NonNull Option<? extends T> option)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 java.util.Optional<? extends T> optional)Wraps aOptionalin a newOption.default Option<T>onEmpty(@NonNull java.lang.Runnable action)Executes the givenRunnableif thisOptionis empty (None).default Option<T>orElse(@NonNull Option<? extends T> other)Returns thisOptionif it is non-empty, otherwise returns the provided alternativeOption.default Option<T>orElse(@NonNull java.util.function.Supplier<? extends Option<? extends T>> supplier)Returns thisOptionif it is non-empty; otherwise, returns theOptionprovided by the supplier.default Option<T>peek(@NonNull java.util.function.Consumer<? super T> action)Executes the given action on the contained value if thisOptionis defined (Some), otherwise does nothing.static <T> Option<Seq<T>>sequence(@NonNull java.lang.Iterable<? extends Option<? extends T>> values)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.java.lang.StringtoString()Clarifies that values have a proper toString() method implemented.default <U> Utransform(@NonNull java.util.function.Function<? super Option<T>,? extends U> f)Transforms thisOptioninto a value of typeUusing the given function.static <T,U>
Option<Seq<U>>traverse(@NonNull java.lang.Iterable<? extends T> values, @NonNull java.util.function.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>when(boolean condition, @NonNull java.util.function.Supplier<? extends T> supplier)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 Detail
-
serialVersionUID
static final long serialVersionUID
The serial version UID for serialization.- See Also:
- Constant Field Values
-
-
Method Detail
-
of
static <T> Option<T> of(T value)
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
static <T> Option<Seq<T>> sequence(@NonNull java.lang.Iterable<? extends Option<? extends T>> values)
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:
java.lang.NullPointerException- ifvaluesis null
-
traverse
static <T,U> Option<Seq<U>> traverse(@NonNull java.lang.Iterable<? extends T> values, @NonNull java.util.function.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:
java.lang.NullPointerException- ifvaluesormapperis null
-
some
static <T> Option<T> some(T value)
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
static <T> Option<T> none()
Returns the singletonNoneinstance.- Type Parameters:
T- the option's component type- Returns:
- the singleton
None
-
narrow
static <T> Option<T> narrow(@NonNull Option<? extends T> option)
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
static <T> Option<T> when(boolean condition, @NonNull java.util.function.Supplier<? extends T> supplier)
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:
java.lang.NullPointerException- ifsupplieris null
-
when
static <T> Option<T> when(boolean condition, T value)
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
static <T> Option<T> ofOptional(@NonNull java.util.Optional<? extends T> optional)
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
default <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.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:
java.lang.NullPointerException- ifpartialFunctionis null
-
isEmpty
boolean isEmpty()
Checks whether thisOptionis empty.
-
onEmpty
default Option<T> onEmpty(@NonNull java.lang.Runnable action)
Executes the givenRunnableif thisOptionis empty (None).- 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.
-
getOrElse
default T getOrElse(T other)
Returns the value contained in thisSome, or the providedothervalue if this isNone.Note that
otheris evaluated eagerly.
-
orElse
default Option<T> orElse(@NonNull Option<? extends T> other)
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
default Option<T> orElse(@NonNull java.util.function.Supplier<? extends Option<? extends T>> supplier)
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:
java.lang.NullPointerException- ifsupplieris null
-
getOrElse
default T getOrElse(@NonNull java.util.function.Supplier<? extends T> supplier)
Returns the value contained in thisSome, or the value supplied bysupplierif this isNone.The alternative value is evaluated lazily.
-
getOrElseThrow
default <X extends java.lang.Throwable> T getOrElseThrow(@NonNull java.util.function.Supplier<X> exceptionSupplier) throws X extends java.lang.Throwable
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 thisOptionisNonejava.lang.NullPointerException- ifexceptionSupplieris nullX extends java.lang.Throwable
-
filter
default Option<T> filter(@NonNull java.util.function.Predicate<? super T> predicate)
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:
java.lang.NullPointerException- ifpredicateis null
-
flatMap
default <U> Option<U> flatMap(@NonNull java.util.function.Function<? super T,? extends Option<? extends U>> mapper)
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:
java.lang.NullPointerException- ifmapperis null
-
map
default <U> Option<U> map(@NonNull java.util.function.Function<? super T,? extends U> mapper)
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:
java.lang.NullPointerException- ifmapperis null
-
mapTo
default <U> Option<U> mapTo(U value)
Description copied from interface:ValueMaps the underlying value to another fixed value.
-
mapToVoid
default Option<java.lang.Void> mapToVoid()
Description copied from interface:ValueMaps the underlying value to Void
-
mapTry
default <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.- 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:
java.lang.NullPointerException- ifmapperis null
-
fold
default <U> U fold(@NonNull java.util.function.Supplier<? extends U> ifNone, @NonNull java.util.function.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:
java.lang.NullPointerException- ififNoneorfis null
-
peek
default Option<T> peek(@NonNull java.util.function.Consumer<? super T> action)
Executes the given action on the contained value if thisOptionis defined (Some), otherwise does nothing.
-
transform
default <U> U transform(@NonNull java.util.function.Function<? super Option<T>,? extends U> f)
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:
java.lang.NullPointerException- iffis null
-
iterator
default @NonNull Iterator<T> iterator()
Description copied from interface:ValueReturns a richio.vavr.collection.Iterator.
-
equals
boolean equals(java.lang.Object o)
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
java.lang.String toString()
Description copied from interface:ValueClarifies that values have a proper toString() method implemented.See Object.toString().
-
-