Package io.vavr.control
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
Replacement forOptional.Option is a monadic container type which represents an optional value. Instances of Option are either an instance of
Option.Someor the singletonOption.None.Most of the API is taken from
Optional. A similar type can be found 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 longserialVersionUID
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default <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.booleanequals(java.lang.Object o)Clarifies that values have a proper equals() method implemented.default Option<T>filter(java.util.function.Predicate<? super T> predicate)ReturnsSome(value)if this is aSomeand the value satisfies the given predicate.default <U> Option<U>flatMap(java.util.function.Function<? super T,? extends Option<? extends U>> mapper)Maps the value to a newOptionif this is aSome, otherwise returnsNone.default <U> Ufold(java.util.function.Supplier<? extends U> ifNone, java.util.function.Function<? super T,? extends U> f)Folds either theNoneor theSomeside of the Option value.Tget()Gets the value if this is aSomeor throws if this is aNone.default TgetOrElse(java.util.function.Supplier<? extends T> supplier)Returns the value if this is aSome, otherwise theothervalue is returned, if this is aNone.default TgetOrElse(T other)Returns the value if this is aSomeor theothervalue if this is aNone.default <X extends java.lang.Throwable>
TgetOrElseThrow(java.util.function.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 booleanisDefined()Returns 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 booleanisSingleValued()AnOptionis single-valued.default Iterator<T>iterator()Returns a richio.vavr.collection.Iterator.default <U> Option<U>map(java.util.function.Function<? super T,? extends U> mapper)Maps the value and wraps it in a newSomeif this is aSome, returnsNone.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 ofNonestatic <T> Option<T>of(T value)Creates a newOptionof a given value.static <T> Option<T>ofOptional(java.util.Optional<? extends T> optional)Wraps a Java Optional to a new Optiondefault Option<T>onEmpty(java.lang.Runnable action)Runs a Java Runnable passed as parameter if thisOptionis empty.default Option<T>orElse(Option<? extends T> other)Returns thisOptionif it is nonempty, otherwise return the alternative.default Option<T>orElse(java.util.function.Supplier<? extends Option<? extends T>> supplier)Returns thisOptionif it is nonempty, otherwise return the result of evaluating supplier.default 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 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.java.lang.StringtoString()Clarifies that values have a proper toString() method implemented.default <U> Utransform(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 singleOptionby transforming anIterable<? extends T>into aOption<Seq<U>>.static <T> Option<T>when(boolean condition, java.util.function.Supplier<? extends T> supplier)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 case-
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
- See Also:
- Constant Field Values
-
-
Method Detail
-
of
static <T> Option<T> of(T value)
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
static <T> Option<Seq<T>> sequence(java.lang.Iterable<? extends Option<? extends T>> values)
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:
java.lang.NullPointerException- ifvaluesis null
-
traverse
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 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:
java.lang.NullPointerException- if values or f is null.
-
some
static <T> Option<T> some(T value)
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
static <T> Option<T> none()
Returns the single instance ofNone- Type Parameters:
T- component type- Returns:
- the single instance of
None
-
narrow
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
optioninstance as narrowed typeOption<T>.
-
when
static <T> Option<T> when(boolean condition, java.util.function.Supplier<? extends T> supplier)
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:
java.lang.NullPointerException- if the givensupplieris null
-
when
static <T> Option<T> when(boolean condition, T value)
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
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 JavaOptionalis present,Noneotherwise
-
collect
default <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.
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:
java.lang.NullPointerException- ifpartialFunctionis null
-
isEmpty
boolean isEmpty()
Returns true, if this isNone, otherwise false, if this isSome.
-
onEmpty
default Option<T> onEmpty(java.lang.Runnable action)
Runs a Java Runnable passed as parameter if thisOptionis 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
Optionhas a defined value, false otherwise
-
isLazy
default boolean isLazy()
AnOption's value is computed eagerly.
-
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.
-
getOrElse
default T getOrElse(T other)
Returns the value if this is aSomeor theothervalue if this is aNone.Please note, that the other value is eagerly evaluated.
-
orElse
default Option<T> orElse(Option<? extends T> other)
Returns thisOptionif it is nonempty, otherwise return the alternative.- Parameters:
other- An alternativeOption- Returns:
- this
Optionif it is nonempty, otherwise return the alternative.
-
orElse
default Option<T> orElse(java.util.function.Supplier<? extends Option<? extends T>> supplier)
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
default T getOrElse(java.util.function.Supplier<? extends T> supplier)
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
default <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:
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 throwableX extends java.lang.Throwable
-
filter
default Option<T> filter(java.util.function.Predicate<? super T> predicate)
ReturnsSome(value)if this is aSomeand the value satisfies the given predicate. OtherwiseNoneis returned.- Parameters:
predicate- A predicate which is used to test an optional value- Returns:
Some(value)orNoneas specified
-
flatMap
default <U> Option<U> flatMap(java.util.function.Function<? super T,? extends Option<? extends U>> mapper)
Maps the value to a newOptionif this is aSome, otherwise returnsNone.- Type Parameters:
U- Component type of the resulting Option- Parameters:
mapper- A mapper- Returns:
- a new
Option
-
map
default <U> Option<U> map(java.util.function.Function<? super T,? extends U> mapper)
Maps the value and wraps it in a newSomeif this is aSome, returnsNone.
-
fold
default <U> U fold(java.util.function.Supplier<? extends U> ifNone, java.util.function.Function<? super T,? extends U> f)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
default 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
default <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- iffis null
-
iterator
default 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().
-
-