abstract class Maybe<A : Any!> : Serializable, MutableIterable<A>
A monadic type representing a result of some computation. It can be a Just
or a Nothing
. Just
contains the computed value, Nothing
indicates that result for the computations is unavailable (no other information is passed).
If information about the cause of computation failure is needed please check Try
instead.
Maybe
can be compared to Java's Optional, Scala's Option or Haskell's Maybe type.
Java doesn't have means to define sealed class hierarchies, due to this limitation Maybe
is an abstract class with a protected constructor. This simulates a sealed hierarchy as Just
and Nothing
are the only possible subclasses.
To create a Just
:
Maybe<String> mj = Maybe.just("some value");
To create a Nothing
:
Maybe<Integer> mn = Maybe.nothing();
Let's look at a usage example. There is a function that returns a Maybe String, we want to process the probable String by changing it to a number (it's size) and then checking if the value is larger then a certain threshold.
// we have a partial function that returns a Maybe result
final Maybe<String> result = somePartialFunction(args);
// we then want to process the result Maybe
final Maybe<Integer> processed =
// first we change the string to it's length
result.map(string -> string.length())
// then we check if the size is above the desired threshold
.filter(size -> size > THRESHOLD);
// At this stage Maybe can be Just Integer or Nothing. It's possible to react
// to it in two ways:
if (processed.isJust()) {
// do something if the value exist
}
if (processed.isNothing()) {
// do something if the value does not exist
}
// We can also do the same conditions in chain rather then explicit if statement:
processed
.ifJust(value -> {
// do something if the value exist
})
.ifNothing(nothing -> {
// do something if the value does not exist
});
Note: Implementation of this class was made to be extremely familiar for anyone that is used to Java Optional, Scala Option or Vavr.io Option.
class Just<A : Any!> : Maybe<A> |
|
class Nothing<A : Any!> : Maybe<A> |
Maybe() |
abstract fun filter(predicate: Predicate<in A>): Maybe<A>
Returns a |
|
abstract fun <B : Any!> flatMap(function: (in A) -> out Maybe<out B>!): Maybe<B>
Maps Just value or to a new Maybe or passes a Nothing. In case of a Just, function that is passed to this method will be called on the stored value, the resulting Maybe will be returned by the flatMap method. In case of Nothing flatMap will not invoke the passed function and just pass the Nothing again. |
|
abstract fun get(): A
Returns |
|
abstract fun getOrElse(defaultValue: A?): A
Returns value stored in |
|
open fun getOrNull(): A?
Returns value stored in |
|
open fun <E : Exception!> getOrThrow(exception: Exception): A
Returns |
|
abstract fun ifJust(action: Consumer1<A>): Maybe<A>
Performs the provided action if the |
|
abstract fun ifNothing(action: Consumer0): Maybe<A>
Performs the provided action if the |
|
abstract fun isJust(): Boolean
|
|
open fun isNothing(): Boolean
|
|
open fun iterator(): MutableIterator<A> |
|
open static fun <B : Any!> just(value: B): Maybe<B>
Creates a |
|
abstract fun <B : Any!> map(function: (in A) -> out B): Maybe<B>
Maps Just value or passes Nothing. In case of Just, function that is passed to this method will be called with the stored value, the result of this operation will be assigned to a new Maybe. In case of Nothing, map will not invoke the passed function and just pass the Nothing again. |
|
open static fun <B : Any!> nothing(): Maybe<B>
Creates a |
|
open static fun <B : Any!> ofNullable(value: B?): Maybe<B>
Creates a |
|
abstract fun orElse(defaultMaybe: Maybe<A>): Maybe<A>
Returns this abstract fun orElse(supplier: Supplier<Maybe<A>!>): Maybe<A>
Returns a |
|
open static fun <R : Any!> unit(): (R) -> Maybe<R>!
Returns a function that takes a value and returns a monad of that value. The returned function will always return a |
class Just<A : Any!> : Maybe<A> |
|
class Nothing<A : Any!> : Maybe<A> |