public final class TryCompanion extends Object
Try
/Success
/Failure
. import static javascalautils.TryCompanion.Failure; import static javascalautils.TryCompanion.Success; import static javascalautils.TryCompanion.Try; Try<Integer> t = Try(() -> 9 / 3); Try<String> ts = Success("Peter was here"); Try<String> tf = Failure(new Exception("Bad mojo!")); //code that performs a side effect but has not return type can be written like this Try<Unit> t = Try(() -> { database.delete(someId); });
Modifier and Type | Method and Description |
---|---|
static <T> Failure<T> |
Failure(Throwable throwable)
Creates an instance of
Failure wrapping the provided throwable. |
static <T> Success<T> |
Success(T value)
Creates an instance of
Success wrapping the provided value. |
static <T> Try<T> |
Try(ThrowableFunction0<T> function)
Creates an instance of
Try wrapping the result of the provided function. |
static Try<Unit> |
Try(VoidFunction0 function)
|
public static Try<Unit> Try(VoidFunction0 function)
Try
with a Unit
return type. Success
/Failure
.
Best used in conjunction with statically importing this method.
import static javascalautils.TryCompanion.Try; Try<Unit> t = Try(() -> { database.delete(someId); });
function
- The function to render either the value T or raise an exception.Unit
or an exceptionpublic static <T> Try<T> Try(ThrowableFunction0<T> function)
Try
wrapping the result of the provided function. import static javascalautils.TryCompanion.Try; Try<Integer> t = Try(() -> 9 / 3);
T
- The type for the Tryfunction
- The function to render either the value T or raise an exception.Try.apply(ThrowableFunction0)
public static <T> Failure<T> Failure(Throwable throwable)
Failure
wrapping the provided throwable. import static javascalautils.TryCompanion.Failure; Try<String> tf = Failure(new Exception("Bad mojo!"));
T
- The type for the Trythrowable
- The throwable to wrapFailure.Failure(Throwable)
public static <T> Success<T> Success(T value)
Success
wrapping the provided value. import static javascalautils.TryCompanion.Success; Try<String> ts = Success("Peter was here");
T
- The type for the Tryvalue
- The value to wrapSuccess.Success(Object)
Copyright © 2016, Peter Nerg Apache License v2.0