T
- The type of the optional value.public interface Option<T> extends Value<T>
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 | Method and Description |
---|---|
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 . |
T |
get()
Gets the underlying value or throws if no value is present.
|
default Option<T> |
getOption()
Gets the underlying value as Option.
|
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 |
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 |
isSingleValued()
An
Option is single-valued. |
default Iterator<T> |
iterator()
Returns a rich
javaslang.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 Option<Void> |
nothing()
Return the singleton instance of
Some<Void> . |
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> |
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> Option<T> |
when(boolean condition,
Supplier<? extends T> supplier)
Creates
Some of suppliers value if condition is true, or None in other case |
contains, corresponds, eq, exists, forAll, forEach, getOrElseTry, narrow, out, out, stderr, stdout, stringPrefix, toArray, toCharSeq, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaOptional, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toList, toMap, toOption, toQueue, toRight, toRight, toSet, toStack, toStream, toTree, toTry, toTry, toVector
spliterator
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> 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 Option<Void> nothing()
Some<Void>
.Some<Void>
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> 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
otherwiseboolean isEmpty()
None
, otherwise false, if this is Some
.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 isSingleValued()
Option
is single-valued.isSingleValued
in interface Value<T>
true
T get()
Value
default Option<T> getOption()
Value
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 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
javaslang.collection.Iterator
.boolean equals(Object o)
Value
int hashCode()
Value
See Object.hashCode().
Copyright © 2016. All Rights Reserved.