Class Option<A>
- java.lang.Object
-
- io.atlassian.fugue.Option<A>
-
- Type Parameters:
A- the value type the option contains
- All Implemented Interfaces:
Effect.Applicant<A>,Maybe<A>,java.io.Serializable,java.lang.Iterable<A>
- Direct Known Subclasses:
Option.None,Option.Some
public abstract class Option<A> extends java.lang.Object implements java.lang.Iterable<A>, Maybe<A>, java.io.Serializable
A class that encapsulates missing values. An Option may be either some value or none.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 ifnot set, ormaporfilter.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
nullwherenullmay be used to represent an optional value. There are however some situations wherenullmay be a legitimate value, and that even though the option is defined, it still carries anullinside. 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 returnnullis a bad idea anyway. Note that if a function returns null to indicate optionality, it can beliftedinto a partial function and thenflat mappedinstead.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.
- Since:
- 1.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classOption.NoneOne of the big two actual implementation classes.(package private) static classOption.Some<A>One of the big two actual implementation classes.
-
Field Summary
Fields Modifier and Type Field Description private static java.io.SerializableNONEDeprecated.private static java.util.function.Supplier<java.lang.Integer>NONE_HASHprivate static java.util.function.Supplier<java.lang.String>NONE_STRINGprivate static longserialVersionUIDprivate static java.util.function.Function<java.lang.Object,java.lang.Integer>SOME_HASHprivate static java.util.function.Function<java.lang.Object,java.lang.String>SOME_STRING
-
Constructor Summary
Constructors Constructor Description Option()do not constructor
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <A> java.util.function.Predicate<Option<A>>defined()Predicate for filtering defined options only.booleanequals(java.lang.Object obj)booleanexists(java.util.function.Predicate<? super A> p)Whether this isis definedand applying the predicate to the contained value returns true.Option<A>filter(java.util.function.Predicate<? super A> p)Returns thisOptionif it is nonempty and applying the predicate to this option's value returns true.Option<A>filterNot(java.util.function.Predicate<? super A> p)Returns thisOptionif it is nonempty and applying the predicate to this option's value returns false.<B> Option<B>flatMap(java.util.function.Function<? super A,? extends Option<? extends B>> f)Applyfto the value if defined.abstract <B> Bfold(java.util.function.Supplier<? extends B> none, java.util.function.Function<? super A,? extends B> some)If this is a some value apply the some function, otherwise get the None value.booleanforall(java.util.function.Predicate<? super A> p)Returnstrueif emptyor the result of the application of the given function to the value.static <A> Option<A>fromOptional(java.util.Optional<A> optional)Factory method forOptioninstances fromOptionalinstances.AgetOr(java.util.function.Supplier<? extends A> supplier)Get the valueif definedor call the supplier and return its value if not.<B extends A>
AgetOrElse(B other)Get the value if defined, otherwise returnsother.AgetOrElse(java.util.function.Supplier<? extends A> supplier)Deprecated.AgetOrNull()Get the value if defined or null if not.inthashCode()booleanisEmpty()If the type does not contain a value return true.java.util.Iterator<A>iterator()Return an iterator for this type.<B> Option<B>map(java.util.function.Function<? super A,? extends B> f)Applyfto the value if defined.static <A> Option<A>none()Factory method for None instances.static <A> Option<A>none(java.lang.Class<A> type)Factory method for None instances where the type token is handy.static <A> java.util.function.Supplier<Option<A>>noneSupplier()Supplies None as required.static <A> Option<A>option(A a)Factory method forOptioninstances.Option<A>orElse(Option<? extends A> orElse)If this is a some, return the same some.Option<A>orElse(java.util.function.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(java.util.function.Supplier<X> right)Creates an Either from this Option.abstract java.util.Optional<A>toOptional()Create anOptionalfrom this option.<X> Either<X,A>toRight(java.util.function.Supplier<X> left)Creates an Either from this Option.abstract java.util.stream.Stream<A>toStream()Create aStreamfrom this option.java.lang.StringtoString()private java.util.function.Function<java.lang.Object,java.lang.Boolean>valuesEqual()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.atlassian.fugue.Effect.Applicant
foreach
-
Methods inherited from interface io.atlassian.fugue.Maybe
contains, get, getOrError, getOrThrow, isDefined
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
NONE_STRING
private static final java.util.function.Supplier<java.lang.String> NONE_STRING
-
NONE_HASH
private static final java.util.function.Supplier<java.lang.Integer> NONE_HASH
-
SOME_STRING
private static final java.util.function.Function<java.lang.Object,java.lang.String> SOME_STRING
-
SOME_HASH
private static final java.util.function.Function<java.lang.Object,java.lang.Integer> SOME_HASH
-
NONE
@Deprecated private static final java.io.Serializable NONE
Deprecated.Backwards compatibility requires us to have a class Option$1 so we can deserialize it into Option$None.- Since:
- 4.2.0
-
-
Method Detail
-
option
public static <A> Option<A> option(A a)
Factory method forOptioninstances.- Type Parameters:
A- the contained type- Parameters:
a- the value to hold- Returns:
- a Some if the parameter is not null or a None if it is
-
some
public static <A> Option<A> some(A value)
Factory method for Some instances.- Type Parameters:
A- the contained type- Parameters:
value- the value to hold, must not be null- Returns:
- a Some if the parameter is not null
- Throws:
java.lang.NullPointerException- if the parameter is null
-
none
public static <A> Option<A> none()
Factory method for None instances.- Type Parameters:
A- the held type- Returns:
- a None
-
none
public static <A> Option<A> none(java.lang.Class<A> type)
Factory method for None instances where the type token is handy. Allows calling in-line where the type inferencer would otherwise complain.- Type Parameters:
A- the contained type- Parameters:
type- token of the right type, unused, only here for the type inferencer- Returns:
- a None
-
defined
public static <A> java.util.function.Predicate<Option<A>> defined()
Predicate for filtering defined options only.- Type Parameters:
A- the contained type- Returns:
- a
Predicatethat returns true only for defined options
-
noneSupplier
public static <A> java.util.function.Supplier<Option<A>> noneSupplier()
Supplies None as required. Useful as the zero value for folds.- Type Parameters:
A- the contained type- Returns:
- a
Supplierof None instances
-
fromOptional
public static <A> Option<A> fromOptional(java.util.Optional<A> optional)
Factory method forOptioninstances fromOptionalinstances.- Type Parameters:
A- the contained type- Parameters:
optional- aOptionalobject.- Returns:
- a Some if
Optional.isPresent()or a None otherwise.
-
fold
public abstract <B> B fold(java.util.function.Supplier<? extends B> none, java.util.function.Function<? super A,? extends B> some)If this is a some value apply the some function, otherwise get the None value.- Type Parameters:
B- the result type- Parameters:
none- the supplier of the None typesome- the function to apply if this is a Some- Returns:
- the appropriate value
-
getOrElse
public final <B extends A> A getOrElse(B other)
Get the value if defined, otherwise returnsother.- Specified by:
getOrElsein interfaceMaybe<A>- Type Parameters:
B- default value type- Parameters:
other- value to return if thisis empty- Returns:
- wrapped value if this
is defined, otherwise returnsother
-
getOr
public final A getOr(java.util.function.Supplier<? extends A> supplier)
Get the valueif definedor call the supplier and return its value if not. ReplacesMaybe.getOrElse(Supplier). Get the valueif definedor call the supplier and return its value if not.
-
getOrElse
@Deprecated public final A getOrElse(java.util.function.Supplier<? extends A> supplier)
Deprecated.Get the valueif definedor call the supplier and return its value if not.
-
getOrNull
public final A getOrNull()
Get the value if defined or null if not.Although the use of null is discouraged, code written to use these must often interface with code that expects and returns nulls.
-
orElse
public final Option<A> orElse(Option<? extends A> orElse)
If this is a some, return the same some. Otherwise, returnorElse.- Parameters:
orElse- option to return if this is none- Returns:
- this or
orElse - Since:
- 1.1
-
orElse
public final Option<A> orElse(java.util.function.Supplier<? extends Option<? extends A>> orElse)
If this is a some, return the same some. Otherwise, return value supplied byorElse.- Parameters:
orElse- supplier which provides the option to return if this is none- Returns:
- this or value supplied by
orElse - Since:
- 1.1
-
exists
public final boolean exists(java.util.function.Predicate<? super A> p)
Whether this isis definedand applying the predicate to the contained value returns true.
-
forall
public final boolean forall(java.util.function.Predicate<? super A> p)
Returnstrueif emptyor the result of the application of the given function to the value.
-
isEmpty
public final boolean isEmpty()
If the type does not contain a value return true.
-
iterator
public final java.util.Iterator<A> iterator()
Return an iterator for this type. In most cases this takes the form of an iterator returning zero or one values.- Specified by:
iteratorin interfacejava.lang.Iterable<A>- Specified by:
iteratorin interfaceMaybe<A>- Returns:
- an iterator over the contained value
if defined, or an empty one otherwise.
-
map
public final <B> Option<B> map(java.util.function.Function<? super A,? extends B> f)
Applyfto the value if defined.Transforms to an option of the result type.
- Type Parameters:
B- return type off- Parameters:
f- function to apply to wrapped value- Returns:
- new wrapped value
-
flatMap
public final <B> Option<B> flatMap(java.util.function.Function<? super A,? extends Option<? extends B>> f)
Applyfto the value if defined.Transforms to an option of the result type.
- Type Parameters:
B- return type off- Parameters:
f- function to apply to wrapped value- Returns:
- value returned from
f
-
filter
public final Option<A> filter(java.util.function.Predicate<? super A> p)
Returns thisOptionif it is nonempty and applying the predicate to this option's value returns true. Otherwise, returnnone().- Parameters:
p- the predicate to test- Returns:
- this option, or none
-
filterNot
public final Option<A> filterNot(java.util.function.Predicate<? super A> p)
Returns thisOptionif it is nonempty and applying the predicate to this option's value returns false. Otherwise, returnnone().- Parameters:
p- the predicate to test- Returns:
- this option, or none
- Since:
- 6.1
-
toRight
public final <X> Either<X,A> toRight(java.util.function.Supplier<X> left)
Creates an Either from this Option. Puts the contained value in a right ifMaybe.isDefined()otherwise puts the supplier's value in a left.- Type Parameters:
X- the left type- Parameters:
left- the Supplier to evaluate and return if this is empty- Returns:
- the content of this option if defined as a right, or the supplier's content as a left if not
- See Also:
toLeft(java.util.function.Supplier<X>)
-
toLeft
public final <X> Either<A,X> toLeft(java.util.function.Supplier<X> right)
Creates an Either from this Option. Puts the contained value in a left ifMaybe.isDefined()otherwise puts the supplier's value in a right.- Type Parameters:
X- the right type- Parameters:
right- the Supplier to evaluate and return if this is empty- Returns:
- the content of this option if defined as a left, or the supplier's content as a right if not defined.
- See Also:
toRight(java.util.function.Supplier<X>)
-
toOptional
public abstract java.util.Optional<A> toOptional()
Create anOptionalfrom this option. Will throw aNullPointerExceptionif this option is defined and contains a null value.- Returns:
Optional.of(Object)with the value if defined and not null,Optional.empty()if not defined.
-
toStream
public abstract java.util.stream.Stream<A> toStream()
Create aStreamfrom this option.- Returns:
Stream.of(Object)with the value if defined,Stream.empty()if not defined.- Since:
- 4.5.0
-
hashCode
public final int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public final boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public final java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
valuesEqual
private java.util.function.Function<java.lang.Object,java.lang.Boolean> valuesEqual()
-
-