Kotlin utility classes based on Scala / it.czerwinski.kotlin.util

Package it.czerwinski.kotlin.util

Types

Either sealed class Either<out L, out R>
Disjoint union. The instance might be either Left or Right.
Failure data class Failure : Try<Nothing>
Left data class Left<out L> : Either<L, Nothing>
LeftProjection data class LeftProjection<out L, out R>
None object None : Option<Nothing>
Representation of a non-existent value.
Option sealed class Option<out T>
Representation of an optional value. The instance might be either Some value or None.
Right data class Right<out R> : Either<Nothing, R>
RightProjection data class RightProjection<out L, out R>
Some data class Some<T> : Option<T>
Representation of a value of type T.
Success data class Success<out T> : Try<T>
Try sealed class Try<out T>
Representation of an operation that might successfully return a value or throw an exception.

Extensions for External Classes

kotlin.collections.Iterable  

Functions

asOption fun <T> T?.asOption(): Option<T>
Returns Some if this is not null or None if this is null.
contains operator fun <T> Option<T>.contains(element: T): Boolean
Tests whether the Option contains the given element.
filterNotNull fun <L, R> LeftProjection<L?, R>.filterNotNull(): Either<L, R>?
Returns the same Left if its value is not null. Otherwise returns null.fun <L, R> RightProjection<L, R?>.filterNotNull(): Either<L, R>?
Returns the same Right if its value is not null. Otherwise returns null.fun <T> Try<T?>.filterNotNull(): Try<T>
Returns the same Success if its value is not null. Otherwise returns a Failure.
filterNotNullToOption fun <L, R> LeftProjection<L?, R>.filterNotNullToOption(): Option<Either<L, R>>
Returns Some containing the same Left if its value is not null. Otherwise returns None.fun <L, R> RightProjection<L, R?>.filterNotNullToOption(): Option<Either<L, R>>
Returns Some containing the same Right if its value is not null. Otherwise returns None.
flatMap fun <L, R, T> LeftProjection<L, R>.flatMap(transform: (L) -> Either<T, R>): Either<T, R>
Maps value of this Left to a new Either using transform.fun <L, R, T> RightProjection<L, R>.flatMap(transform: (R) -> Either<L, T>): Either<L, T>
Maps value of this Right to a new Either using transform.
flatten fun <T> Try<Option<T>>.flatten(): Option<T>
Extracts an Option nested in the Try to a not nested Option.fun <T> Option<Try<T>>.flatten(): Option<T>
Returns Some if this Some contains a Success. Otherwise returns None.fun <T> Option<Iterable<T>>.flatten(): List<T>
Returns nested List if this is Some. Otherwise returns an empty List.fun <T> Option<Option<T>>.flatten(): Option<T>
Transforms a nested Option to a not nested Option.fun <T> Try<Try<T>>.flatten(): Try<T>
Transforms a nested Try to a not nested Try.
getOrElse fun <L, R> LeftProjection<L, R>.getOrElse(default: () -> L): L
Gets value of this Left or default value if this is Right.fun <L, R> RightProjection<L, R>.getOrElse(default: () -> R): R
Gets value of this Right or default value if this is Left.fun <T> Option<T>.getOrElse(default: () -> T): T
Gets the value of a Some or default value if this is None.fun <T> Try<T>.getOrElse(default: () -> T): T
Gets the value of a Success or default value if this is a Failure.
joinLeft fun <L, R> Either<Either<L, R>, R>.joinLeft(): Either<L, R>
Returns this if this is Right. Otherwise returns value of Left.
joinRight fun <L, R> Either<L, Either<L, R>>.joinRight(): Either<L, R>
Returns this if this is Left. Otherwise returns value of Right.
merge fun <T> Either<T, T>.merge(): T
Merges Left and Right of the same type to a single value.
orElse fun <T> Option<T>.orElse(default: () -> Option<T>): Option<T>
Returns this Option if this is a Some or default if this is None.fun <T> Try<T>.orElse(default: () -> Try<T>): Try<T>
Returns this Try if this is a Success or default if this is a Failure.
recover fun <T> Try<T>.recover(rescue: (Throwable) -> T): Try<T>
Returns this Try if this is a Success or a Try created for the rescue operation if this is a Failure.
recoverWith fun <T> Try<T>.recoverWith(rescue: (Throwable) -> Try<T>): Try<T>
Returns this Try if this is a Success or a Try created by the rescue function if this is a Failure.