Class Option<A>
- Type Parameters:
A- the value type the option contains
- All Implemented Interfaces:
Effect.Applicant<A>,Maybe<A>,Serializable,Iterable<A>
- Direct Known Subclasses:
Option.None,Option.Some
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.
- Since:
- 1.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classOne of the big two actual implementation classes.(package private) static final classOne of the big two actual implementation classes. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final SerializableDeprecated.private static final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondefined()Predicate for filtering defined options only.final booleanfinal booleanWhether this isis definedand applying the predicate to the contained value returns true.Returns thisOptionif it is nonempty and applying the predicate to this option's value returns true.Returns thisOptionif it is nonempty and applying the predicate to this option's value returns false.final <B> Option<B> Applyfto the value if defined.abstract <B> BIf this is a some value apply the some function, otherwise get the None value.final booleanReturnstrueif emptyor the result of the application of the given function to the value.static <A> Option<A> fromOptional(Optional<A> optional) final AGet the valueif definedor call the supplier and return its value if not.getOrElse(B other) Get the value if defined, otherwise returnsother.final ADeprecated.final AGet the value if defined or null if not.final inthashCode()final booleanisEmpty()If the type does not contain a value return true.iterator()Return an iterator for this type.final <B> Option<B> Applyfto the value if defined.static <A> Option<A> none()Factory method for None instances.static <A> Option<A> Factory method for None instances where the type token is handy.Supplies None as required.static <A> Option<A> option(A a) Factory method forOptioninstances.If this is a some, return the same some.If this is a some, return the same some.static <A> Option<A> some(A value) Factory method for Some instances.Creates an Either from this Option.Create anOptionalfrom this option.Creates an Either from this Option.toStream()Create aStreamfrom this option.final StringtoString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface io.atlassian.fugue.Effect.Applicant
foreachMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface io.atlassian.fugue.Maybe
contains, get, getOrError, getOrThrow, isDefined
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
NONE_STRING
-
NONE_HASH
-
SOME_STRING
-
SOME_HASH
-
NONE
Deprecated.Backwards compatibility requires us to have a class Option$1 so we can deserialize it into Option$None.- Since:
- 4.2.0
-
-
Constructor Details
-
Option
Option()do not constructor
-
-
Method Details
-
option
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
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:
NullPointerException- if the parameter is null
-
none
Factory method for None instances.- Type Parameters:
A- the held type- Returns:
- a None
-
none
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
Predicate for filtering defined options only.- Type Parameters:
A- the contained type- Returns:
- a
Predicatethat returns true only for defined options
-
noneSupplier
Supplies None as required. Useful as the zero value for folds.- Type Parameters:
A- the contained type- Returns:
- a
Supplierof None instances
-
fromOptional
- Type Parameters:
A- the contained type- Parameters:
optional- aOptionalobject.- Returns:
- a Some if
Optional.isPresent()or a None otherwise.
-
fold
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
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
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.Get the valueif definedor call the supplier and return its value if not. -
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
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
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
Whether this isis definedand applying the predicate to the contained value returns true. -
forall
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
Return an iterator for this type. In most cases this takes the form of an iterator returning zero or one values. -
map
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
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
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
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
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
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:
-
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
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() -
equals
-
toString
-
valuesEqual
-