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 givenpartialFunctionby mapping the value to typeR.booleanClarifies that values have a proper equals() method implemented.ReturnsSome(value)if this is aSomeand the value satisfies the given predicate.default <U> Option<U> Maps the value to a newOptionif this is aSome, otherwise returnsNone.default <U> UFolds either theNoneor theSomeside of the Option value.get()Gets the value if this is aSomeor throws if this is aNone.default TReturns the value if this is aSome, otherwise theothervalue is returned, if this is aNone.default TReturns the value if this is aSomeor theothervalue if this is aNone.getOrElseThrow(Supplier<X> exceptionSupplier) Returns the value if this is aSome, otherwise throws an exception.inthashCode()Clarifies that values have a proper hashCode() method implemented.default booleanisAsync()AnOption's value is computed synchronously.default booleanReturns true, if this isSome, otherwise false, if this isNone.booleanisEmpty()Returns true, if this isNone, otherwise false, if this isSome.default booleanisLazy()AnOption's value is computed eagerly.default booleanAnOptionis single-valued.iterator()Returns a richio.vavr.collection.Iterator.default <U> Option<U> Maps the value and wraps it in a newSomeif 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 ofNonestatic <T> Option<T> of(T value) Creates a newOptionof 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 thisOptionis empty.Returns thisOptionif it is nonempty, otherwise return the alternative.Returns thisOptionif 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 manyOptions into a singleOptionby transforming anIterable<Option<? extends T>>into aOption<Seq<T>>.static <T> Option<T> some(T value) Creates a newSomeof a given value.toString()Clarifies that values have a proper toString() method implemented.default <U> UTransforms thisOption.Maps the values of an iterable to a sequence of mapped values into a singleOptionby transforming anIterable<? extends T>into aOption<Seq<U>>.static <T> Option<T> CreatesSomeof suppliers value if condition is true, orNonein other casestatic <T> Option<T> when(boolean condition, T value) CreatesSomeof value if condition is true, orNonein other caseMethods inherited from interface 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 newOptionof a given value.- Type Parameters:
T- type of the value- Parameters:
value- A value- Returns:
Some(value)if value is notnull,Noneotherwise
-
sequence
Reduces manyOptions into a singleOptionby 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- AnIterableofOptions- Returns:
- An
Optionof aSeqof results - Throws:
NullPointerException- ifvaluesis 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 singleOptionby transforming anIterable<? extends T>into aOption<Seq<U>>.- Type Parameters:
T- The type of the given values.U- The mapped value type.- Parameters:
values- AnIterableof values.mapper- A mapper of values to Options- Returns:
- A
Optionof aSeqof results. - Throws:
NullPointerException- if values or f is null.
-
some
Creates a newSomeof 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
optioninstance as narrowed typeOption<T>.
-
when
CreatesSomeof suppliers value if condition is true, orNonein other case- Type Parameters:
T- type of the optional value- Parameters:
condition- A boolean valuesupplier- An optional value supplier, may supplynull- Returns:
- return
Someof supplier's value if condition is true, orNonein other case - Throws:
NullPointerException- if the givensupplieris null
-
when
CreatesSomeof value if condition is true, orNonein other case- Type Parameters:
T- type of the optional value- Parameters:
condition- A boolean valuevalue- An optional value, may benull- Returns:
- return
Someof value if condition is true, orNonein other case
-
ofOptional
-
collect
Collects value that is in the domain of the givenpartialFunctionby mapping the value to typeR.
If the element makes it through that filter, the mapped instance is wrapped inpartialFunction.isDefinedAt(value)OptionR 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
Optioninstance containing value of typeR - Throws:
NullPointerException- ifpartialFunctionis null
-
isEmpty
-
onEmpty
-
isAsync
-
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
Optionhas a defined value, false otherwise
-
isLazy
-
isSingleValued
default boolean isSingleValued()AnOptionis single-valued.- Specified by:
isSingleValuedin interfaceValue<T>- Returns:
true
-
get
T get()Gets the value if this is aSomeor throws if this is aNone.- Specified by:
getin interfaceValue<T>- Returns:
- the value
- Throws:
NoSuchElementException- if this is aNone.
-
getOrElse
Returns the value if this is aSomeor theothervalue if this is aNone.Please note, that the other value is eagerly evaluated.
-
orElse
-
orElse
Returns thisOptionif it is nonempty, otherwise return the result of evaluating supplier.- Parameters:
supplier- An alternativeOptionsupplier- Returns:
- this
Optionif it is nonempty, otherwise return the result of evaluating supplier.
-
getOrElse
Returns the value if this is aSome, otherwise theothervalue 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:
getOrElseThrowin 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
-
flatMap
-
map
Maps the value and wraps it in a newSomeif this is aSome, returnsNone. -
fold
Folds either theNoneor theSomeside 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
-
transform
Transforms thisOption.- Type Parameters:
U- Type of transformation result- Parameters:
f- A transformation- Returns:
- An instance of type
U - Throws:
NullPointerException- iffis null
-
iterator
-
equals
Description copied from interface:ValueClarifies that values have a proper equals() method implemented. -
hashCode
-
toString
-