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 interface Effect.Applicant
foreachMethods inherited from interface Iterable
forEach, spliteratorMethods inherited from interface 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
-
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
-
noneSupplier
-
fromOptional
-
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
-
orElse
-
orElse
-
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
-
iterator
Return an iterator for this type. In most cases this takes the form of an iterator returning zero or one values. -
map
-
flatMap
-
filter
-
filterNot
-
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
-
equals
-
toString
-
valuesEqual
-