Package io.vavr.control
Class Option<T>
- java.lang.Object
-
- io.vavr.control.Option<T>
-
- Type Parameters:
T
- The type of the optional value.
- All Implemented Interfaces:
Iterable<T>
,java.io.Serializable
,java.lang.Iterable<T>
public abstract class Option<T> extends java.lang.Object implements Iterable<T>, java.io.Serializable
Replacement forOptional
.Option is a monadic container type which represents an optional value.
Most of the API is taken from
Optional
. A similar type can be found in Haskell and Scala.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description <R,A>
Rcollect(java.util.stream.Collector<? super T,A,R> collector)
Collects the underlying value (if present) using the providedcollector
.abstract boolean
equals(java.lang.Object that)
Checks if thisOption
is equal to the given objecto
.Option<T>
filter(java.util.function.Predicate<? super T> predicate)
ReturnsSome(value)
if this is aSome
and the value satisfies the given predicate.<U> Option<U>
flatMap(java.util.function.Function<? super T,Option<? extends U>> mapper)
Maps the value to a newOption
if this is aSome
, otherwise returnsNone
.<U> U
fold(java.util.function.Supplier<? extends U> ifEmpty, java.util.function.Function<? super T,? extends U> ifDefined)
Folds either theNone
or theSome
side of the Option value.abstract T
get()
Deprecated.Unsafe operation (but not marked for removal).T
getOrElse(T other)
Returns the value if this is aSome
or theother
value if this is aNone
.T
getOrElseGet(java.util.function.Supplier<? extends T> supplier)
Returns the value if this is aSome
, otherwise theother
value is returned, if this is aNone
.<X extends java.lang.Throwable>
TgetOrElseThrow(java.util.function.Supplier<X> exceptionProvider)
Returns the value if this is aSome
, otherwise throws an exception.abstract int
hashCode()
Computes the hash of thisOption
.abstract boolean
isDefined()
Returns true, if this isSome
, otherwise false, if this isNone
.abstract boolean
isEmpty()
Returns true, if this isNone
, otherwise false, if this isSome
.Iterator<T>
iterator()
Returns anIterator
that allows us to perform intermediate, sequential operations known from Vavr's collection library.<U> Option<U>
map(java.util.function.Function<? super T,? extends U> mapper)
Maps the value and wraps it in a newSome
if this is aSome
, returnsNone
.static <T> Option<T>
none()
Returns the single instance ofNone
static <T> Option<T>
of(T value)
Creates a newOption
of a given value.static <T> Option<T>
ofOptional(java.util.Optional<? extends T> optional)
Wraps a Java Optional to a new OptionOption<T>
onDefined(java.util.function.Consumer<? super T> action)
Applies an action to this value if this is defined, otherwise nothing happens.Option<T>
onEmpty(java.lang.Runnable action)
Runs a Java Runnable passed as parameter if thisOption
is empty.Option<T>
orElse(java.util.function.Supplier<? extends Option<? extends T>> supplier)
Returns thisOption
if it is nonempty, otherwise return the result of evaluating supplier.static <T> Option<T>
some(T value)
Creates a newSome
of a given value.java.util.stream.Stream<T>
stream()
Converts thisOption
to aStream
.<U> Either<U,T>
toEither(java.util.function.Supplier<? extends U> leftSupplier)
Converts thisOption
to anEither
.java.util.Optional<T>
toOptional()
Converts thisOption
to anOptional
.abstract java.lang.String
toString()
Returns a string representation of thisOption
.Try<T>
toTry(java.util.function.Supplier<? extends java.lang.Throwable> ifEmpty)
Converts thisOption
to aTry
.<U> Option<U>
transform(java.util.function.Supplier<? extends Option<? extends U>> ifEmpty, java.util.function.Function<? super T,? extends Option<? extends U>> ifDefined)
Transforms thisOption
by applying eitherifDefined
to this value or by callingifEmpty
.static <T> Option<T>
unless(boolean condition, java.util.function.Supplier<? extends T> supplier)
Unless the givencondition
is true,Some(supplier.get())
is returned.static <T> Option<T>
when(boolean condition, java.util.function.Supplier<? extends T> supplier)
When the givencondition
is true,Some(supplier.get())
is returned.
-
-
-
Method Detail
-
of
public static <T> Option<T> of(T value)
Creates a newOption
of a given value.- Type Parameters:
T
- type of the value- Parameters:
value
- A value- Returns:
- A new
Option
containing the givenvalue
if value is notnull
, otherwise the emptyOption
.
-
some
public static <T> Option<T> some(T value)
Creates a newSome
of 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
public static <T> Option<T> none()
Returns the single instance ofNone
- Type Parameters:
T
- component type- Returns:
- the single instance of
None
-
when
public static <T> Option<T> when(boolean condition, java.util.function.Supplier<? extends T> supplier)
When the givencondition
is true,Some(supplier.get())
is returned. Otherwise,None
is returned.Same as
Option.unless(!condition, supplier)
.- Type Parameters:
T
- type of the optional value- Parameters:
condition
- A boolean valuesupplier
- An optional value supplier, may supplynull
- Returns:
- a new
Option
- Throws:
java.lang.NullPointerException
- if the givensupplier
is null- See Also:
unless(boolean, Supplier)
-
unless
public static <T> Option<T> unless(boolean condition, java.util.function.Supplier<? extends T> supplier)
Unless the givencondition
is true,Some(supplier.get())
is returned. Otherwise,None
is returned.Same as
Option.when(!condition, supplier)
.- Type Parameters:
T
- type of the optional value- Parameters:
condition
- A boolean valuesupplier
- An optional value supplier, may supplynull
- Returns:
- a new
Option
- Throws:
java.lang.NullPointerException
- if the givensupplier
is null- See Also:
when(boolean, Supplier)
-
ofOptional
public 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 JavaOptional
is present,None
otherwise
-
collect
public <R,A> R collect(java.util.stream.Collector<? super T,A,R> collector)
Collects the underlying value (if present) using the providedcollector
.Shortcut for
.stream().collect(collector)
.- Type Parameters:
A
- the mutable accumulation type of the reduction operationR
- the result type of the reduction operation- Parameters:
collector
- Collector performing reduction- Returns:
- the reduction result of type
R
- Throws:
java.lang.NullPointerException
- if the givencollector
is null
-
filter
public Option<T> filter(java.util.function.Predicate<? super T> predicate)
ReturnsSome(value)
if this is aSome
and the value satisfies the given predicate. OtherwiseNone
is returned.- Parameters:
predicate
- A predicate which is used to test an optional value- Returns:
Some(value)
orNone
as specified
-
flatMap
public <U> Option<U> flatMap(java.util.function.Function<? super T,Option<? extends U>> mapper)
Maps the value to a newOption
if this is aSome
, otherwise returnsNone
.- Type Parameters:
U
- Component type of the resulting Option- Parameters:
mapper
- A mapper- Returns:
- a new
Option
-
fold
public <U> U fold(java.util.function.Supplier<? extends U> ifEmpty, java.util.function.Function<? super T,? extends U> ifDefined)
Folds either theNone
or theSome
side of the Option value.- Type Parameters:
U
- type of the folded value- Parameters:
ifEmpty
- maps the left value if this is a NoneifDefined
- maps the value if this is a Some- Returns:
- A value of type U
- Throws:
java.lang.NullPointerException
- if one of the givenifEmpty
orifDefined
is null
-
get
@Deprecated public abstract T get() throws java.util.NoSuchElementException
Deprecated.Unsafe operation (but not marked for removal). UsergetOrElse(Object)
,getOrElseGet(Supplier)
orgetOrElseThrow(Supplier)
instead. Other alternatives areonDefined(Consumer)
,Iterable.forEach(Consumer)
or iteration using a for-loop.Gets the value if this is aSome
or throws if this is aNone
.- Returns:
- the value
- Throws:
java.util.NoSuchElementException
- if this is aNone
.
-
getOrElse
public T getOrElse(T other)
Returns the value if this is aSome
or theother
value if this is aNone
.Please note, that the other value is eagerly evaluated.
- Parameters:
other
- An alternative value- Returns:
- This value, if this Option is defined or the
other
value, if this Option is empty.
-
getOrElseGet
public T getOrElseGet(java.util.function.Supplier<? extends T> supplier)
Returns the value if this is aSome
, otherwise theother
value is returned, if this is aNone
.Please note, that the other value is lazily evaluated.
- Parameters:
supplier
- An alternative value supplier- Returns:
- This value, if this Option is defined or the
other
value, if this Option is empty.
-
getOrElseThrow
public <X extends java.lang.Throwable> T getOrElseThrow(java.util.function.Supplier<X> exceptionProvider) throws X extends java.lang.Throwable
Returns the value if this is aSome
, otherwise throws an exception.- Type Parameters:
X
- A throwable- Parameters:
exceptionProvider
- An exception provider- Returns:
- This value, if this
Option
is defined, otherwise throws X - Throws:
X
- if thisOption
is emptyjava.lang.NullPointerException
- if the givenexceptionProvider
is nullX extends java.lang.Throwable
-
isDefined
public abstract 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
Option
has a defined value, false otherwise
-
isEmpty
public abstract boolean isEmpty()
Returns true, if this isNone
, otherwise false, if this isSome
.- Returns:
- true, if this
Option
is empty, false otherwise
-
iterator
public Iterator<T> iterator()
Description copied from interface:Iterable
Returns anIterator
that allows us to perform intermediate, sequential operations known from Vavr's collection library.
-
map
public <U> Option<U> map(java.util.function.Function<? super T,? extends U> mapper)
Maps the value and wraps it in a newSome
if this is aSome
, returnsNone
.- Type Parameters:
U
- The new value type- Parameters:
mapper
- A value mapper- Returns:
- a new
Some
containing the mapped value if this Option is defined, otherwiseNone
, if this is empty.
-
onEmpty
public Option<T> onEmpty(java.lang.Runnable action)
Runs a Java Runnable passed as parameter if thisOption
is empty.- Parameters:
action
- a given Runnable to be run- Returns:
- this
Option
- Throws:
java.lang.NullPointerException
- if the givenaction
is null
-
onDefined
public Option<T> onDefined(java.util.function.Consumer<? super T> action)
Applies an action to this value if this is defined, otherwise nothing happens.- Parameters:
action
- An action which can be applied to an optional value- Returns:
- this
Option
- Throws:
java.lang.NullPointerException
- if the givenaction
is null
-
orElse
public Option<T> orElse(java.util.function.Supplier<? extends Option<? extends T>> supplier)
Returns thisOption
if it is nonempty, otherwise return the result of evaluating supplier.- Parameters:
supplier
- An alternativeOption
supplier- Returns:
- this
Option
if it is nonempty, otherwise return the result of evaluating supplier.
-
stream
public java.util.stream.Stream<T> stream()
Converts thisOption
to aStream
.- Returns:
Stream.of(get()
if this is aSome
, otherwiseStream.empty()
-
toEither
public <U> Either<U,T> toEither(java.util.function.Supplier<? extends U> leftSupplier)
Converts thisOption
to anEither
.- Type Parameters:
U
- the left type of theEither
- Parameters:
leftSupplier
- a left value supplier- Returns:
Either.right(get()
if this is a definedOption
, otherwiseEither.left(leftSupplier.get())
- Throws:
java.lang.NullPointerException
- if the givenleftSupplier
is null
-
toOptional
public java.util.Optional<T> toOptional()
Converts thisOption
to anOptional
.- Returns:
Optional.ofNullable(get())
if this is defined, otherwiseOptional.empty()
-
toTry
public Try<T> toTry(java.util.function.Supplier<? extends java.lang.Throwable> ifEmpty)
Converts thisOption
to aTry
.- Parameters:
ifEmpty
- supplies aThrowable
if thisOption
is empty- Returns:
Try.success(get()
if this is a definedOption
, otherwiseTry.failure(ifEmpty.get())
- Throws:
java.lang.NullPointerException
- if the givenifEmpty
is null
-
transform
public <U> Option<U> transform(java.util.function.Supplier<? extends Option<? extends U>> ifEmpty, java.util.function.Function<? super T,? extends Option<? extends U>> ifDefined)
Transforms thisOption
by applying eitherifDefined
to this value or by callingifEmpty
.- Type Parameters:
U
- type of the transformed value- Parameters:
ifEmpty
- supplies anOption
if thisOption
is emptyifDefined
- maps the value if thisOption
is defined- Returns:
- A new
Option
instance - Throws:
java.lang.NullPointerException
- if one of the givenifDefined
orifEmpty
is null
-
equals
public abstract boolean equals(java.lang.Object that)
Checks if thisOption
is equal to the given objecto
.- Overrides:
equals
in classjava.lang.Object
- Parameters:
that
- an object, may be null- Returns:
- true, if
this
andthat
both are a definedOption
and the underlying values are equal or ifthis
andthat
both are an emptyOption
. Otherwise it returns false.
-
hashCode
public abstract int hashCode()
Computes the hash of thisOption
.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
31 + Objects.hashCode(get())
if this is aSome
, otherwise1
-
toString
public abstract java.lang.String toString()
Returns a string representation of thisOption
.- Overrides:
toString
in classjava.lang.Object
- Returns:
"Some(" + get() + ")"
if thisOption
is defined, otherwise"None"
-
-