public abstract class Try<A> extends Object
Try represents a computation that may either throw an
exception or return a value. A Try will either be Success
wrapping a value or Failure which wraps an exception.
This class is similar to Either, but is explicit about having a
success and failure case. Unless method level javadoc says otherwise, methods
will not automatically catch exceptions thrown by function arguments. In
particular map will not catch automatically catch thrown exceptions, instead
you should use Checked.lift(io.atlassian.fugue.Checked.Function<A, B, E>) to to make the function explicitly return
a Try and the use flatmap.
| Constructor and Description |
|---|
Try() |
| Modifier and Type | Method and Description |
|---|---|
static <A> Try<A> |
failure(Exception e)
Creates a new failure
|
abstract <B> Try<B> |
flatMap(Function<? super A,Try<B>> f)
Binds the given function across the success value if it is one.
|
static <A> Try<A> |
flatten(Try<Try<A>> t)
Reduces a nested Try by a single level
|
abstract <B> B |
fold(Function<? super Exception,B> failureF,
Function<A,B> successF)
Applies the function to the wrapped value, applying failureF it this is a
Left and successF if this is a Right.
|
abstract A |
getOrElse(Supplier<A> s)
Returns the contained value if this is a success otherwise call the
supplier and return its value.
|
abstract boolean |
isFailure()
Returns
true if this failure, otherwise false |
abstract boolean |
isSuccess()
Returns
true if this success, otherwise false |
abstract <B> Try<B> |
map(Function<? super A,? extends B> f)
Maps the given function to the value from this `Success` or returns this
unchanged if a `Failure`.
|
abstract <X extends Exception> |
recover(Class<X> exceptionType,
Function<? super X,A> f)
Applies the given function `f` if this is a `Failure` with certain
exception type otherwise leaves this unchanged.
|
abstract Try<A> |
recover(Function<? super Exception,A> f)
Applies the given function `f` if this is a `Failure` otherwise this
unchanged if a 'Success'.
|
abstract <X extends Exception> |
recoverWith(Class<X> exceptionType,
Function<? super X,Try<A>> f)
Binds the given function across certain exception type if it is one,
otherwise this unchanged.
|
abstract Try<A> |
recoverWith(Function<? super Exception,Try<A>> f)
Binds the given function across the failure value if it is one, otherwise
this unchanged if a 'Success'.
|
static <A> Try<Iterable<A>> |
sequence(Iterable<Try<A>> trys)
Returns a success wrapping all of the values if all of the arguments were a
success, otherwise this returns the first failure
|
static <A> Try<A> |
successful(A value)
Creates a new Success
|
abstract Either<Exception,A> |
toEither()
Convert this Try to an
Either, becoming a left if this is a failure
and a right if this is a success. |
abstract Option<A> |
toOption()
Convert this Try to an Option.
|
public static <A> Try<A> failure(Exception e)
A - the success typee - an exception to wrap, must not be null.public static <A> Try<A> successful(A value)
A - the wrapped value typevalue - a value to wrap, must not be nullpublic static <A> Try<Iterable<A>> sequence(Iterable<Try<A>> trys)
A - The success typetrys - an iterable of try valuespublic static <A> Try<A> flatten(Try<Try<A>> t)
A - The success typet - A nested Trypublic abstract boolean isFailure()
true if this failure, otherwise falsetrue if this failure, otherwise falsepublic abstract boolean isSuccess()
true if this success, otherwise falsetrue if this success, otherwise falsepublic abstract <B> Try<B> flatMap(Function<? super A,Try<B>> f)
B - result typef - the function to bind.public abstract <B> Try<B> map(Function<? super A,? extends B> f)
B - result typef - the function to applypublic abstract Try<A> recover(Function<? super Exception,A> f)
f - the function to applypublic abstract <X extends Exception> Try<A> recover(Class<X> exceptionType, Function<? super X,A> f)
X - exception typeexceptionType - exception classf - the function to applypublic abstract Try<A> recoverWith(Function<? super Exception,Try<A>> f)
f - the function to bind.public abstract <X extends Exception> Try<A> recoverWith(Class<X> exceptionType, Function<? super X,Try<A>> f)
X - exception typeexceptionType - exception classf - the function to applypublic abstract A getOrElse(Supplier<A> s)
s - called if this is a failureSupplierpublic abstract <B> B fold(Function<? super Exception,B> failureF, Function<A,B> successF)
B - the destination typefailureF - the function to apply if this is a FailuresuccessF - the function to apply if this is a Successpublic abstract Either<Exception,A> toEither()
Either, becoming a left if this is a failure
and a right if this is a success.Copyright © 2017 Atlassian. All rights reserved.