public abstract class Try<A> extends Object implements Serializable, Iterable<A>
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(Function) 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(Function).
Note that since 4.7.0, all API methods describe whether or not they will
result in a Delayed Try being evaluated. In addition to
this, any action to Serialize a Delayed Try will result in it being evaluated, and the underlying Try
Success or Failure result being
serialized.
| Constructor and Description |
|---|
Try() |
| Modifier and Type | Method and Description |
|---|---|
static <A> Try<A> |
delayed(Supplier<Try<A>> supplier)
Creates a delayed Try, which will return either a Failure or a Success when
evaluated.
|
static <A> Try<A> |
failure(Exception e)
Creates a new failure
|
abstract Try<A> |
filterOrElse(Predicate<? super A> p,
Supplier<Exception> orElseSupplier)
Return a
Success if this is a Success and the
contained values satisfies the given predicate. |
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
Failure and successF if this is a Success.
|
abstract void |
forEach(Consumer<? super A> action)
Perform the given
Consumer (side-effect) for the
success if success value. |
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 |
Iterator<A> |
iterator()
Return an iterator for this type.
|
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 Try<A> |
orElse(Supplier<? extends Try<? extends A>> orElse)
If this is a success, return the same success.
|
Try<A> |
orElse(Try<? extends A> orElse)
If this is a success, return the same success.
|
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 <T,A,R> Try<R> |
sequence(Iterable<Try<T>> trys,
Collector<T,A,R> collector)
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.
|
abstract Optional<A> |
toOptional()
Create a
Optional from this try. |
abstract Stream<A> |
toStream()
Create a
Stream from this try. |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitspliteratorpublic 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<A> delayed(Supplier<Try<A>> supplier)
A - the wrapped value typesupplier - a supplier that returns a Try of A.public static <A> Try<Iterable<A>> sequence(Iterable<Try<A>> trys)
A - The success typetrys - an iterable of try valuespublic static <T,A,R> Try<R> sequence(Iterable<Try<T>> trys, Collector<T,A,R> collector)
T - The success typeA - The intermediate accumulator typeR - The result typetrys - an iterable of try valuescollector - result collectorpublic static <A> Try<A> flatten(Try<Try<A>> t)
A - The success typet - A nested Trypublic abstract boolean isFailure()
true if this failure, otherwise false
Note that for delayed(Supplier) this is an
evaluating operation.
true if this failure, otherwise falsepublic abstract boolean isSuccess()
true if this success, otherwise false
Note that for delayed(Supplier) this is an
evaluating operation.
true if this success, otherwise falsepublic abstract <B> Try<B> flatMap(Function<? super A,Try<B>> f)
Note that for delayed(Supplier) this is not an evaluating
operation.
B - result typef - the function to bind.public abstract <B> Try<B> map(Function<? super A,? extends B> f)
Note that for delayed(Supplier) this is not an evaluating
operation.
B - result typef - the function to applypublic abstract Try<A> recover(Function<? super Exception,A> f)
Note that for delayed(Supplier) this is not an evaluating
operation.
f - the function to applypublic abstract <X extends Exception> Try<A> recover(Class<X> exceptionType, Function<? super X,A> f)
Note that for delayed(Supplier) this is not an evaluating
operation.
X - exception typeexceptionType - exception classf - the function to applypublic abstract Try<A> recoverWith(Function<? super Exception,Try<A>> f)
Note that for delayed(Supplier) this is not an evaluating
operation.
f - the function to bind.public abstract <X extends Exception> Try<A> recoverWith(Class<X> exceptionType, Function<? super X,Try<A>> f)
Note that for delayed(Supplier) this is not an evaluating
operation.
X - exception typeexceptionType - exception classf - the function to applypublic abstract A getOrElse(Supplier<A> s)
Note that for delayed(Supplier) this is an
evaluating operation.
s - called if this is a failureSupplierpublic final Try<A> orElse(Try<? extends A> orElse)
orElse.
Note that for delayed(Supplier) this is not an evaluating
operation.
orElse - try to return if this is failureorElsepublic abstract Try<A> orElse(Supplier<? extends Try<? extends A>> orElse)
orElse.
Note that for delayed(Supplier) this is not an evaluating
operation.
orElse - try to return if this is failureorElsepublic abstract Try<A> filterOrElse(Predicate<? super A> p, Supplier<Exception> orElseSupplier)
Success if this is a Success and the
contained values satisfies the given predicate.
If this is a Success but the predicate is not satisfied,
return a Failure with the value provided by the
orElseSupplier.
Return a Failure if this a Failure with the
contained value.
Note that for delayed(Supplier) this is not an evaluating
operation.
p - The predicate function to test on the right contained value.orElseSupplier - The supplier to execute when is a success, and
predicate is unsatisfiedpublic abstract <B> B fold(Function<? super Exception,B> failureF, Function<A,B> successF)
Note that for delayed(Supplier) this is an
evaluating operation.
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.
Note that for delayed(Supplier) this is an
evaluating operation.
public abstract Option<A> toOption()
Some with a value if it
is a success, otherwise None.
Note that for delayed(Supplier) this is an
evaluating operation.
Some if it exists, otherwise
Nonepublic abstract Optional<A> toOptional()
Optional from this try.
Note that for delayed(Supplier) this is an
evaluating operation.
Optional.of(Object) with the value if success,
Optional.empty() if failure.public abstract Stream<A> toStream()
Stream from this try.
Note that for delayed(Supplier) this is an
evaluating operation.
Stream.of(Object) with the value if
success, Stream.empty() if failure.public abstract void forEach(Consumer<? super A> action)
Consumer (side-effect) for the
success if success value.
Note that for delayed(Supplier) this is an
evaluating operation.
public final Iterator<A> iterator()
isFailure() case, and a iterator of a single value for the
success isSuccess() case.
Note that for delayed(Supplier) this is an
evaluating operation.
iterator in interface Iterable<A>if
success, or an empty one otherwise.Copyright © 2018 Atlassian. All rights reserved.