abstract class Try<T : Any!> : Serializable, MutableIterable<T>
A monadic type representing a result of some computation. It can be a Success
or a Failure
. Success
contains the computed value, Failure
contains cause of the computation failure in a form of a Throwable
.
Try
can be compared to Java's Optional, Scala's Option, Haskell's Maybe as it also allows to model optional values, but it also allows storing the information why the value is not available.
Java doesn't have means to define sealed class hierarchies, due to this limitation Try
is an abstract class with a protected constructor. This simulates a sealed hierarchy as Success
and Failure
are the only possible subclasses.
To create a Success
:
Try<String> ts = Try.success("some value");
To create a Failure
:
Try<Integer> tf = Try.failure(new Exception("some exception"));
Note: Implementation of this class was made to be extremely familiar for anyone that is used to Scala Try implementation or Vavr.io implementation.
class Failure<T : Any!> : Try<T>
Represents a failed computation that resulted in a |
|
class Success<T : Any!> : Try<T>
Represents a successful computation that resulted in some returned value. |
Try() |
abstract fun <R : Any!> cast(clazz: Class<R>): Try<R>!
Performs value type cast to provided type if possible. |
|
open static fun <R : Any!> failure(throwable: Throwable): Try<R>
Creates a |
|
open fun filter(predicate: Predicate<in T>): Try<T>!
Returns a abstract fun filter(predicate: Predicate<in T>, supplier: Supplier<out Throwable!>): Try<T>!
Returns a |
|
abstract fun <R : Any!> flatMap(function: (in T) -> out Try<out R>!): Try<R>!
Maps success value to a new Try or passes a failure. In case of success function that is passed to this method will be called on the stored value, the resulting Try will be returned by the flatMap method. In case of failure flatMap will not invoke the passed function and just pass the failure again. |
|
abstract fun get(): T
Returns success value or in case of failure throws a |
|
abstract fun getCause(): Throwable!
Gets the cause if this is a |
|
abstract fun
Returns |
|
abstract fun getOrElse(defaultValue: T?): T
Returns success value or in case of failure returns the passed value. open fun getOrElse(provider: (in Throwable!) -> T): T
Returns |
|
open fun <E : Throwable!> getOrElseThrow(): T
Returns open fun <E : Throwable!> getOrElseThrow(provider: (in Throwable!) -> E): T
Returns |
|
open fun getOrNull(): T
Returns a |
|
abstract fun ifFailure(action: Consumer1<Throwable!>): Try<T>!
Performs the provided action if the Try represents a failure. Note: If action throws, then this method may throw an Exception. |
|
abstract fun ifSuccess(action: Consumer1<in T>): Try<T>!
Performs the provided action if the Try represents a success. Note: If action throws, then this method may throw an Exception. |
|
open fun isFailure(): Boolean
|
|
abstract fun isSuccess(): Boolean
|
|
open fun iterator(): MutableIterator<T> |
|
abstract fun <R : Any!> map(function: (in T) -> out R): Try<R>!
Maps success value or passes failure. In case of success 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 Try. In case of failure map will not invoke the passed function and just pass the failure again. |
|
open static fun <R : Any!> of(supplier: Supplier<out R>): Try<R>
Creates a |
|
open static fun <R : Any!> ofChecked(supplier: CheckedSupplier<out R>): Try<R>
Creates a |
|
open static fun <R : Any!> ofFailure(supplier: Supplier<out Throwable!>): Try<R>
Creates a |
|
open static fun <R : Any!> ofNullable(value: R?): Try<R>
Creates a open static fun <R : Any!> ofNullable(value: R?, exception: Exception!): Try<R>
Creates a |
|
abstract fun <R : Any!> onBoth(other: Try<out R>, action: Consumer2<in T, in R>): Try<T>
Performs the provided action if both current and other Try represents a success. Note: If action throws, then this method may throw an Exception. abstract fun <R : Any!> onBoth(otherSupplier: Supplier<Try<R>!>, action: Consumer2<in T, in R>): Try<T>
Performs the provided action if both current and other supplied Try represents a success. Note: If action throws, then this method may throw an Exception. |
|
abstract fun orElse(defaultTry: Try<T>): Try<T>!
Returns this Try if it's a Success or the given default argument if this is a Failure. abstract fun orElse(supplier: Supplier<Try<T>!>): Try<T>!
Returns a |
|
abstract fun recover(function: (in Throwable!) -> out T): Try<T>!
Applies the given function if this is a Failure, otherwise returns a |
|
abstract fun recoverWith(function: (in Throwable!) -> Try<out T>!): Try<T>!
Applies the given function if this is a Failure, otherwise returns a Success if this is a Success. This is like a |
|
open static fun <R : Any!> success(value: R): Try<R>
Creates a |
|
open static fun <R : Any!> unit(): (R) -> Try<R>!
Returns a function that takes a value and returns a monad of that value. The returned function will always return a |
|
open static fun <R : Any!> when(condition: Boolean, value: R): Try<R>
Creates a open static fun <R : Any!> when(condition: Boolean, supplier: Supplier<out R>): Try<R>
Lazy equivalent of open static fun <R : Any!> when(conditionSupplier: Supplier<Boolean!>, valueSupplier: Supplier<out R>, errorSupplier: Supplier<out Throwable!>): Try<R>! |
|
abstract fun <R : Any!, S : Any!> zipWith(other: Try<out S>, zipper: (in T, in S) -> out R): Try<R>
Returns an instance of |
class Failure<T : Any!> : Try<T>
Represents a failed computation that resulted in a |
|
class Success<T : Any!> : Try<T>
Represents a successful computation that resulted in some returned value. |