sdk-common / com.tomtom.online.sdk.common.functional / Maybe

Maybe

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.

Types

Just

class Just<A : Any!> : Maybe<A>

Nothing

class Nothing<A : Any!> : Maybe<A>

Constructors

<init>

Maybe()

Functions

filter

abstract fun filter(predicate: Predicate<in A>): Maybe<A>

Returns a Nothing if this Maybe represents a missing value. Returns a Nothing if the value does not satisfy the predicate or an Exception is thrown when testing the predicate. Returns Just otherwise.

flatMap

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.

get

abstract fun get(): A

Returns Just value or in case of Nothing throws an RuntimeException.

getOrElse

abstract fun getOrElse(defaultValue: A?): A

Returns value stored in Just or default value in case of Nothing.

getOrNull

open fun getOrNull(): A?

Returns value stored in Just or null value in case of Nothing.

getOrThrow

open fun <E : Exception!> getOrThrow(exception: Exception): A

Returns Just value or in case of Nothing throws an exception specified as the argument.

ifJust

abstract fun ifJust(action: Consumer1<A>): Maybe<A>

Performs the provided action if the Maybe represents a Just. The stored value is not changed in any way.

ifNothing

abstract fun ifNothing(action: Consumer0): Maybe<A>

Performs the provided action if the Maybe represents a Nothing. Note: If action throws, then this method may throw an Exception.

isJust

abstract fun isJust(): Boolean

Checks if the Maybe is a Just.

isNothing

open fun isNothing(): Boolean

Checks if the Maybe is a Nothing.

iterator

open fun iterator(): MutableIterator<A>

just

open static fun <B : Any!> just(value: B): Maybe<B>

Creates a Just from the given value. Please be aware that if the value is null then a null will get stored. If you don't want to store or process a potential null value please use Maybe#ofNullable(Object) instead.

map

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.

nothing

open static fun <B : Any!> nothing(): Maybe<B>

Creates a Nothing.

ofNullable

open static fun <B : Any!> ofNullable(value: B?): Maybe<B>

Creates a Maybe from a given value. If the value is null a Nothing will be created.

orElse

abstract fun orElse(defaultMaybe: Maybe<A>): Maybe<A>

Returns this Maybe if it's a Just or the given default argument if this is a Nothing.

abstract fun orElse(supplier: Supplier<Maybe<A>!>): Maybe<A>

Returns a Just or a value returned by the Supplier wrapped in a Maybe.

unit

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 Just regardless of the passed value (even null!).

Inheritors

Just

class Just<A : Any!> : Maybe<A>

Nothing

class Nothing<A : Any!> : Maybe<A>