-
public final class ResultKt
-
-
Method Summary
Modifier and Type Method Description final static <VALUE extends Any> Result<VALUE>tryCatch(Function0<VALUE> block)Calls the specified function block and returns Success with encapsulated result if invocation was successful, catching and returning Error with any thrown exception except CancellationException. final static <VALUE extends Any> VALUEgetOrNull(Result<VALUE> $self)Returns the encapsulated value if this instance represents Success or null for encapsulated exception if it is Error. final static <VALUE extends Any> VALUEgetOrDefault(Result<VALUE> $self, VALUE defaultValue)Returns the encapsulated value if this instance represents Success or defaultValue for encapsulated exception if it is Error. final static <VALUE extends Any> VALUEgetOrElse(Result<VALUE> $self, Function1<Throwable, VALUE> onError)Returns the encapsulated value if this instance represents Success or result of onError for encapsulated exception if it is Error. final static <VALUE extends Any> VALUEgetOrThrow(Result<VALUE> $self, Function1<Throwable, Unit> doBeforeThrow)Returns the encapsulated value if this instance represents Success or throws the encapsulated exception if it is Error. final static <VALUE extends Any> VALUEgetOrCancel(Result<VALUE> $self, Function1<Throwable, Unit> doBeforeThrow)Returns the encapsulated value if this instance represents Success or throws CancellationException with Error.error as its cause if it is Error. final static <VALUE extends Any, NEW_VALUE extends Any> Result<NEW_VALUE>map(Result<VALUE> $self, Function1<VALUE, NEW_VALUE> transform)Returns the encapsulated result of the given transform function applied to encapsulated value if this instance represents Success or the original encapsulated exception if it is Error. final static <VALUE extends Any, NEW_VALUE extends Any> Result<NEW_VALUE>mapCatching(Result<VALUE> $self, Function1<VALUE, NEW_VALUE> transform)Returns the encapsulated result of the given transform function applied to encapsulated value if this instance represents Success or the original encapsulated exception if it is Error. final static <VALUE extends Any> Result<VALUE>recover(Result<VALUE> $self, Function1<Throwable, VALUE> transform)Returns the encapsulated result of the given transform function applied to encapsulated exception if this instance represents Error or the original encapsulated value if it is Success. final static <VALUE extends Any> Result<VALUE>recoverCatching(Result<VALUE> $self, Function1<Throwable, VALUE> transform)Returns the encapsulated result of the given transform function applied to encapsulated exception if this instance represents Error or the original encapsulated value if it is Success. final static <VALUE extends Any, NEW_VALUE extends Any> NEW_VALUEfold(Result<VALUE> $self, Function1<VALUE, NEW_VALUE> onSuccess, Function1<Throwable, NEW_VALUE> onError)Returns the the result of onSuccess for encapsulated value if this instance represents Success or the result of onError function for encapsulated exception if it is Error. -
-
Method Detail
-
getOrNull
final static <VALUE extends Any> VALUE getOrNull(Result<VALUE> $self)
Returns the encapsulated value if this instance represents Success or null for encapsulated exception if it is Error.
If Error.error is CancellationException then this error is rethrown.
-
getOrDefault
final static <VALUE extends Any> VALUE getOrDefault(Result<VALUE> $self, VALUE defaultValue)
Returns the encapsulated value if this instance represents Success or defaultValue for encapsulated exception if it is Error.
If Error.error is CancellationException then this error is rethrown.
-
getOrElse
final static <VALUE extends Any> VALUE getOrElse(Result<VALUE> $self, Function1<Throwable, VALUE> onError)
Returns the encapsulated value if this instance represents Success or result of onError for encapsulated exception if it is Error.
If Error.error is CancellationException then this error is rethrown.
-
getOrThrow
final static <VALUE extends Any> VALUE getOrThrow(Result<VALUE> $self, Function1<Throwable, Unit> doBeforeThrow)
Returns the encapsulated value if this instance represents Success or throws the encapsulated exception if it is Error.
If Error.error is not CancellationException then doBeforeThrow is called before throw.
-
getOrCancel
final static <VALUE extends Any> VALUE getOrCancel(Result<VALUE> $self, Function1<Throwable, Unit> doBeforeThrow)
Returns the encapsulated value if this instance represents Success or throws CancellationException with Error.error as its cause if it is Error.
This method should be used in the case when you want to safely cancel your coroutine from inside.
If Error.error is not CancellationException then doBeforeThrow is called before throw.
-
map
final static <VALUE extends Any, NEW_VALUE extends Any> Result<NEW_VALUE> map(Result<VALUE> $self, Function1<VALUE, NEW_VALUE> transform)
Returns the encapsulated result of the given transform function applied to encapsulated value if this instance represents Success or the original encapsulated exception if it is Error.
Note, that an exception thrown by transform function is rethrown by this function. See mapCatching for an alternative that encapsulates exceptions.
-
mapCatching
final static <VALUE extends Any, NEW_VALUE extends Any> Result<NEW_VALUE> mapCatching(Result<VALUE> $self, Function1<VALUE, NEW_VALUE> transform)
Returns the encapsulated result of the given transform function applied to encapsulated value if this instance represents Success or the original encapsulated exception if it is Error.
Any exception thrown by transform function is caught, encapsulated as a failure and returned by this function. See map for an alternative that rethrows exceptions.
-
recover
final static <VALUE extends Any> Result<VALUE> recover(Result<VALUE> $self, Function1<Throwable, VALUE> transform)
Returns the encapsulated result of the given transform function applied to encapsulated exception if this instance represents Error or the original encapsulated value if it is Success.
If Error.error is CancellationException then this error is rethrown.
Note, that an exception thrown by transform function is rethrown by this function. See recoverCatching for an alternative that encapsulates exceptions.
-
recoverCatching
final static <VALUE extends Any> Result<VALUE> recoverCatching(Result<VALUE> $self, Function1<Throwable, VALUE> transform)
Returns the encapsulated result of the given transform function applied to encapsulated exception if this instance represents Error or the original encapsulated value if it is Success.
If Error.error is CancellationException then this error is rethrown.
Any other exception thrown by transform function is caught, encapsulated as a failure and returned by this function. See recover for an alternative that rethrows exceptions.
-
fold
final static <VALUE extends Any, NEW_VALUE extends Any> NEW_VALUE fold(Result<VALUE> $self, Function1<VALUE, NEW_VALUE> onSuccess, Function1<Throwable, NEW_VALUE> onError)
Returns the the result of onSuccess for encapsulated value if this instance represents Success or the result of onError function for encapsulated exception if it is Error.
If Error.error is CancellationException then this error is rethrown.
-
-
-
-