T
- The type of the optional value.public interface Option<T> extends Value<T>, Serializable
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.
Modifier and Type | Interface and Description |
---|---|
static class |
Option.None<T>
None is a singleton representation of the undefined
Option . |
static class |
Option.Some<T>
Some represents a defined
Option . |
Modifier and Type | Field and Description |
---|---|
static long |
serialVersionUID |
Modifier and Type | Method and Description |
---|---|
default <R> Option<R> |
collect(PartialFunction<? super T,? extends R> partialFunction)
Collects value that is in the domain of the given
partialFunction by mapping the value to type R . |
boolean |
equals(Object o)
Clarifies that values have a proper equals() method implemented.
|
default Option<T> |
filter(Predicate<? super T> predicate)
Returns
Some(value) if this is a Some and the value satisfies the given predicate. |
default <U> Option<U> |
flatMap(Function<? super T,? extends Option<? extends U>> mapper)
Maps the value to a new
Option if this is a Some , otherwise returns None . |
default <U> U |
fold(Supplier<? extends U> ifNone,
Function<? super T,? extends U> f)
Folds either the
None or the Some side of the Option value. |
T |
get()
Gets the value if this is a
Some or throws if this is a None . |
default T |
getOrElse(Supplier<? extends T> supplier)
Returns the value if this is a
Some , otherwise the other value is returned,
if this is a None . |
default T |
getOrElse(T other)
Returns the value if this is a
Some or the other value if this is a None . |
default <X extends Throwable> |
getOrElseThrow(Supplier<X> exceptionSupplier)
Returns the value if this is a
Some , otherwise throws an exception. |
int |
hashCode()
Clarifies that values have a proper hashCode() method implemented.
|
default boolean |
isAsync()
An
Option 's value is computed synchronously. |
default boolean |
isDefined()
Returns true, if this is
Some , otherwise false, if this is None . |
boolean |
isEmpty()
Returns true, if this is
None , otherwise false, if this is Some . |
default boolean |
isLazy()
An
Option 's value is computed eagerly. |
default boolean |
isSingleValued()
An
Option is single-valued. |
default Iterator<T> |
iterator()
Returns a rich
io.vavr.collection.Iterator . |
default <U> Option<U> |
map(Function<? super T,? extends U> mapper)
Maps the value and wraps it in a new
Some if this is a Some , returns None . |
static <T> Option<T> |
narrow(Option<? extends T> option)
Narrows a widened
Option<? extends T> to Option<T>
by performing a type-safe cast. |
static <T> Option<T> |
none()
Returns the single instance of
None |
static <T> Option<T> |
of(T value)
Creates a new
Option of a given value. |
static <T> Option<T> |
ofOptional(Optional<? extends T> optional)
Wraps a Java Optional to a new Option
|
default Option<T> |
onEmpty(Runnable action)
Runs a Java Runnable passed as parameter if this
Option is empty. |
default Option<T> |
orElse(Option<? extends T> other)
Returns this
Option if it is nonempty, otherwise return the alternative. |
default Option<T> |
orElse(Supplier<? extends Option<? extends T>> supplier)
Returns this
Option if it is nonempty, otherwise return the result of evaluating supplier. |
default Option<T> |
peek(Consumer<? super T> action)
Applies an action to this value, if this option is defined, otherwise does nothing.
|
static <T> Option<Seq<T>> |
sequence(Iterable<? extends Option<? extends T>> values)
Reduces many
Option s into a single Option by transforming an
Iterable<Option<? extends T>> into a Option<Seq<T>> . |
static <T> Option<T> |
some(T value)
Creates a new
Some of a given value. |
String |
toString()
Clarifies that values have a proper toString() method implemented.
|
default <U> U |
transform(Function<? super Option<T>,? extends U> f)
Transforms this
Option . |
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 single
Option by
transforming an Iterable<? extends T> into a Option<Seq<U>> . |
static <T> Option<T> |
when(boolean condition,
Supplier<? extends T> supplier)
Creates
Some of suppliers value if condition is true, or None in other case |
static <T> Option<T> |
when(boolean condition,
T value)
Creates
Some of value if condition is true, or None in other case |
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElseTry, getOrNull, narrow, 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
static final long serialVersionUID
static <T> Option<T> of(T value)
Option
of a given value.T
- type of the valuevalue
- A valueSome(value)
if value is not null
, None
otherwisestatic <T> Option<Seq<T>> sequence(Iterable<? extends Option<? extends T>> values)
Option
s into a single Option
by transforming an
Iterable<Option<? extends T>>
into a Option<Seq<T>>
. If any of
the Options are Option.None
, then this returns Option.None
.T
- type of the Optionsvalues
- An Iterable
of Option
sOption
of a Seq
of resultsNullPointerException
- if values
is nullstatic <T,U> Option<Seq<U>> traverse(Iterable<? extends T> values, Function<? super T,? extends Option<? extends U>> mapper)
Option
by
transforming an Iterable<? extends T>
into a Option<Seq<U>>
.
T
- The type of the given values.U
- The mapped value type.values
- An Iterable
of values.mapper
- A mapper of values to OptionsOption
of a Seq
of results.NullPointerException
- if values or f is null.static <T> Option<T> some(T value)
Some
of a given value.
The only difference to of(Object)
is, when called with argument null
.
Option.of(null); // = None
Option.some(null); // = Some(null)
T
- type of the valuevalue
- A valueSome(value)
static <T> Option<T> none()
None
T
- component typeNone
static <T> Option<T> narrow(Option<? extends T> option)
Option<? extends T>
to Option<T>
by performing a type-safe cast. This is eligible because immutable/read-only
collections are covariant.T
- Component type of the Option
.option
- A Option
.option
instance as narrowed type Option<T>
.static <T> Option<T> when(boolean condition, Supplier<? extends T> supplier)
Some
of suppliers value if condition is true, or None
in other caseT
- type of the optional valuecondition
- A boolean valuesupplier
- An optional value supplier, may supply null
Some
of supplier's value if condition is true, or None
in other caseNullPointerException
- if the given supplier
is nullstatic <T> Option<T> when(boolean condition, T value)
Some
of value if condition is true, or None
in other caseT
- type of the optional valuecondition
- A boolean valuevalue
- An optional value, may be null
Some
of value if condition is true, or None
in other casestatic <T> Option<T> ofOptional(Optional<? extends T> optional)
T
- type of the valueoptional
- a given optional to wrap in Option
Some(optional.get())
if value is Java Optional
is present, None
otherwisedefault <R> Option<R> collect(PartialFunction<? super T,? extends R> partialFunction)
partialFunction
by mapping the value to type R
.
partialFunction.isDefinedAt(value)
If the element makes it through that filter, the mapped instance is wrapped in Option
R newValue = partialFunction.apply(value)
R
- The new value typepartialFunction
- A function that is not necessarily defined on value of this option.Option
instance containing value of type R
NullPointerException
- if partialFunction
is nullboolean isEmpty()
None
, otherwise false, if this is Some
.default Option<T> onEmpty(Runnable action)
Option
is empty.action
- a given Runnable to be runOption
default boolean isAsync()
Option
's value is computed synchronously.default boolean isDefined()
Some
, otherwise false, if this is None
.
Please note that it is possible to create new Some(null)
, which is defined.
Option
has a defined value, false otherwisedefault boolean isLazy()
Option
's value is computed eagerly.default boolean isSingleValued()
Option
is single-valued.isSingleValued
in interface Value<T>
true
T get()
Some
or throws if this is a None
.get
in interface Value<T>
NoSuchElementException
- if this is a None
.default T getOrElse(T other)
Some
or the other
value if this is a None
.
Please note, that the other value is eagerly evaluated.
default Option<T> orElse(Option<? extends T> other)
Option
if it is nonempty, otherwise return the alternative.other
- An alternative Option
Option
if it is nonempty, otherwise return the alternative.default Option<T> orElse(Supplier<? extends Option<? extends T>> supplier)
Option
if it is nonempty, otherwise return the result of evaluating supplier.supplier
- An alternative Option
supplierOption
if it is nonempty, otherwise return the result of evaluating supplier.default T getOrElse(Supplier<? extends T> supplier)
Some
, otherwise the other
value is returned,
if this is a None
.
Please note, that the other value is lazily evaluated.
default <X extends Throwable> T getOrElseThrow(Supplier<X> exceptionSupplier) throws X extends Throwable
Some
, otherwise throws an exception.getOrElseThrow
in interface Value<T>
X
- A throwableexceptionSupplier
- An exception supplierX
- a throwableX extends Throwable
default Option<T> filter(Predicate<? super T> predicate)
Some(value)
if this is a Some
and the value satisfies the given predicate.
Otherwise None
is returned.predicate
- A predicate which is used to test an optional valueSome(value)
or None
as specifieddefault <U> Option<U> flatMap(Function<? super T,? extends Option<? extends U>> mapper)
Option
if this is a Some
, otherwise returns None
.U
- Component type of the resulting Optionmapper
- A mapperOption
default <U> Option<U> map(Function<? super T,? extends U> mapper)
Some
if this is a Some
, returns None
.default <U> U fold(Supplier<? extends U> ifNone, Function<? super T,? extends U> f)
None
or the Some
side of the Option value.U
- type of the folded valueifNone
- maps the left value if this is a Nonef
- maps the value if this is a Somedefault Option<T> peek(Consumer<? super T> action)
default <U> U transform(Function<? super Option<T>,? extends U> f)
Option
.U
- Type of transformation resultf
- A transformationU
NullPointerException
- if f
is nulldefault Iterator<T> iterator()
Value
io.vavr.collection.Iterator
.boolean equals(Object o)
Value
int hashCode()
Value
See Object.hashCode().
Copyright © 2019. All Rights Reserved.