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> com.google.common.base.Predicate<Option<A>> |
defined()
Predicate for filtering defined options only.
|
boolean |
equals(Object obj) |
boolean |
exists(com.google.common.base.Predicate<? super A> p)
Whether this is
is defined and
applying the predicate to the contained value returns true. |
Option<A> |
filter(com.google.common.base.Predicate<? super A> p)
Returns this
Option if it is nonempty and applying
the predicate to this option's value returns true. |
static <A> Iterable<Option<A>> |
filterNone(Iterable<Option<A>> options)
Deprecated.
since 1.1 use
Options.filterNone(Iterable) instead |
static <A> Option<A> |
find(Iterable<Option<A>> options)
Deprecated.
since 1.1 use
Options.find(Iterable) instead |
<B> Option<B> |
flatMap(com.google.common.base.Function<? super A,? extends Option<? extends B>> f)
Apply
f to the value if defined. |
abstract <B> B |
fold(com.google.common.base.Supplier<? extends B> none,
com.google.common.base.Function<? super A,? extends B> some)
If this is a some value apply the some function, otherwise get the None
value.
|
boolean |
forall(com.google.common.base.Predicate<? super A> p)
Returns
true if empty or the result of the
application of the given function to the value. |
<B extends A> |
getOrElse(B other)
Get the value if defined, otherwise returns
other . |
A |
getOrElse(com.google.common.base.Supplier<? extends A> supplier)
Get the value
if defined or call the supplier and
return its value if not. |
A |
getOrNull()
Get the value if defined or null if not.
|
int |
hashCode() |
boolean |
isEmpty() |
Iterator<A> |
iterator() |
<B> Option<B> |
map(com.google.common.base.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> com.google.common.base.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(com.google.common.base.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(com.google.common.base.Supplier<X> right)
Creates an Either from this Option.
|
<X> Either<X,A> |
toRight(com.google.common.base.Supplier<X> left)
Creates an Either from this Option.
|
String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
get, getOrError, getOrThrow, isDefined
foreach
public 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 holdNullPointerException
- 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> com.google.common.base.Predicate<Option<A>> defined()
A
- the contained typePredicate
that returns true only for defined optionspublic static <A> com.google.common.base.Supplier<Option<A>> noneSupplier()
A
- the contained typeSupplier
of None instances@Deprecated public static <A> Option<A> find(Iterable<Option<A>> options)
Options.find(Iterable)
insteadA
- the contained typeoptions
- an Iterable of options to search through@Deprecated public static <A> Iterable<Option<A>> filterNone(Iterable<Option<A>> options)
Options.filterNone(Iterable)
insteadA
- the contained typeoptions
- many options that may or may not be definedpublic abstract <B> B fold(com.google.common.base.Supplier<? extends B> none, com.google.common.base.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)
Maybe
other
.getOrElse
in interface Maybe<A>
B
- the destination typeother
- value to return if this is empty
is defined
, otherwise
returns other
public final A getOrElse(com.google.common.base.Supplier<? extends A> supplier)
Maybe
if defined
or call the supplier and
return its value if not.public final A getOrNull()
Maybe
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 noneorElse
public final Option<A> orElse(com.google.common.base.Supplier<? extends Option<? extends A>> orElse)
orElse
.orElse
- supplier which provides the option to return if this is noneorElse
public final boolean exists(com.google.common.base.Predicate<? super A> p)
Maybe
is defined
and
applying the predicate to the contained value returns true.public boolean forall(com.google.common.base.Predicate<? super A> p)
Maybe
true
if empty
or the result of the
application of the given function to the value.public final boolean isEmpty()
public final <B> Option<B> map(com.google.common.base.Function<? super A,? extends B> f)
f
to the value if defined.
Transforms to an option of the result type.
B
- return type of f
f
- function to apply to wrapped valuepublic final <B> Option<B> flatMap(com.google.common.base.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 f
f
- function to apply to wrapped valuef
public final Option<A> filter(com.google.common.base.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(com.google.common.base.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
public final <X> Either<A,X> toLeft(com.google.common.base.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
Copyright © 2015 Atlassian. All rights reserved.