A - the value type the option containspublic abstract class Option<A> extends Object implements Iterable<A>, Maybe<A>, Serializable
If it is a value it may be tested with the Maybe.isDefined() method, but
more often it is useful to either return the value or an alternative if
not set, or map or
filter.
Mapping a none of type A to type B will simply return a none of type B if performed on a none of type A. Similarly, filtering will always fail on a none.
This class is often used as an alternative to null where
null may be used to represent an optional value. There are
however some situations where null may be a legitimate value,
and that even though the option is defined, it still carries a
null inside. Specifically, this will happen if a function is
mapped across it returns null, as it is necessary to preserve the Functor composition law. Note
however, that this should be rare as functions that return null
is a bad idea anyway. Note that if a function returns null to indicate
optionality, it can be lifted into a partial function and then flat
mapped instead.
Note: while this class is public and abstract it does not expose a constructor as only the concrete internal subclasses are designed to be used.
| Modifier and Type | Method and Description |
|---|---|
static <A> Predicate<Option<A>> |
defined()
Predicate for filtering defined options only.
|
boolean |
equals(Object obj) |
boolean |
exists(Predicate<? super A> p)
Whether this is
is defined and
applying the predicate to the contained value returns true. |
Option<A> |
filter(Predicate<? super A> p)
Returns this
Option if it is nonempty
and applying the predicate to this option's value returns
true. |
<B> Option<B> |
flatMap(Function<? super A,? extends Option<? extends B>> f)
Apply
f to the value if defined. |
abstract <B> B |
fold(Supplier<? extends B> none,
Function<? super A,? extends B> some)
If this is a some value apply the some function, otherwise get the None
value.
|
boolean |
forall(Predicate<? super A> p)
Returns
true if empty or the result of the
application of the given function to the value. |
static <A> Option<A> |
fromOptional(Optional<A> optional)
|
A |
getOr(Supplier<? extends A> supplier)
Get the value
if defined or call the supplier and
return its value if not. |
<B extends A> |
getOrElse(B other)
Get the value if defined, otherwise returns
other. |
A |
getOrElse(Supplier<? extends A> supplier)
Deprecated.
|
A |
getOrNull()
Get the value if defined or null if not.
|
int |
hashCode() |
boolean |
isEmpty()
If the type does not contain a value return true.
|
Iterator<A> |
iterator()
Return an iterator for this type.
|
<B> Option<B> |
map(Function<? super A,? extends B> f)
Apply
f to the value if defined. |
static <A> Option<A> |
none()
Factory method for None instances.
|
static <A> Option<A> |
none(Class<A> type)
Factory method for None instances where the type token is handy.
|
static <A> Supplier<Option<A>> |
noneSupplier()
Supplies None as required.
|
static <A> Option<A> |
option(A a)
Factory method for
Option instances. |
Option<A> |
orElse(Option<? extends A> orElse)
If this is a some, return the same some.
|
Option<A> |
orElse(Supplier<? extends Option<? extends A>> orElse)
If this is a some, return the same some.
|
static <A> Option<A> |
some(A value)
Factory method for Some instances.
|
<X> Either<A,X> |
toLeft(Supplier<X> right)
Creates an Either from this Option.
|
abstract Optional<A> |
toOptional()
Create an
Optional from this option. |
<X> Either<X,A> |
toRight(Supplier<X> left)
Creates an Either from this Option.
|
abstract Stream<A> |
toStream()
Create a
Stream from this option. |
String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, waitget, getOrError, getOrThrow, isDefinedforEach, spliteratorforeachpublic static <A> Option<A> option(A a)
Option instances.A - the contained typea - the value to holdpublic static <A> Option<A> some(A value)
A - the contained typevalue - the value to hold, must not be nullNullPointerException - if the parameter is nullpublic static <A> Option<A> none()
A - the held typepublic static <A> Option<A> none(Class<A> type)
A - the contained typetype - token of the right type, unused, only here for the type
inferencerpublic static <A> Predicate<Option<A>> defined()
A - the contained typePredicate that returns true only for
defined optionspublic static <A> Supplier<Option<A>> noneSupplier()
A - the contained typeSupplier of None instancespublic static <A> Option<A> fromOptional(Optional<A> optional)
A - the contained typeoptional - a Optional object.Optional.isPresent() or a None
otherwise.public abstract <B> B fold(Supplier<? extends B> none, Function<? super A,? extends B> some)
B - the result typenone - the supplier of the None typesome - the function to apply if this is a Somepublic final <B extends A> A getOrElse(B other)
other.getOrElse in interface Maybe<A>B - default value typeother - value to return if this is emptyis defined, otherwise
returns otherpublic final A getOr(Supplier<? extends A> supplier)
if defined or call the supplier and
return its value if not. Replaces Maybe.getOrElse(Supplier).
Get the value if defined or call the supplier and
return its value if not.@Deprecated public final A getOrElse(Supplier<? extends A> supplier)
if defined or call the supplier and
return its value if not.public final A getOrNull()
Although the use of null is discouraged, code written to use these must often interface with code that expects and returns nulls.
public final Option<A> orElse(Option<? extends A> orElse)
orElse.orElse - option to return if this is noneorElsepublic final Option<A> orElse(Supplier<? extends Option<? extends A>> orElse)
orElse.orElse - supplier which provides the option to return if this is noneorElsepublic final boolean exists(Predicate<? super A> p)
is defined and
applying the predicate to the contained value returns true.public final boolean forall(Predicate<? super A> p)
true if empty or the result of the
application of the given function to the value.public final boolean isEmpty()
public final Iterator<A> iterator()
public final <B> Option<B> map(Function<? super A,? extends B> f)
f to the value if defined.
Transforms to an option of the result type.
B - return type of ff - function to apply to wrapped valuepublic final <B> Option<B> flatMap(Function<? super A,? extends Option<? extends B>> f)
f to the value if defined.
Transforms to an option of the result type.
B - return type of ff - function to apply to wrapped valuefpublic final Option<A> filter(Predicate<? super A> p)
Option if it is nonempty
and applying the predicate to this option's value returns
true. Otherwise, return none().p - the predicate to testpublic final <X> Either<X,A> toRight(Supplier<X> left)
Maybe.isDefined() otherwise puts the supplier's value in a left.X - the left typeleft - the Supplier to evaluate and return if this is emptytoLeft(java.util.function.Supplier<X>)public final <X> Either<A,X> toLeft(Supplier<X> right)
Maybe.isDefined() otherwise puts the supplier's value in a right.X - the right typeright - the Supplier to evaluate and return if this is emptytoRight(java.util.function.Supplier<X>)public abstract Optional<A> toOptional()
Optional from this option. Will throw a
NullPointerException if this option is defined and
contains a null value.Optional.of(Object) with the value if defined and
not null, Optional.empty() if not defined.public abstract Stream<A> toStream()
Stream from this option.Stream.of(Object) with the value if
defined, Stream.empty() if not defined.Copyright © 2017 Atlassian. All rights reserved.