Class Try<A>
- All Implemented Interfaces:
Serializable, Iterable<A>
- Direct Known Subclasses:
Try.Delayed, Try.Failure, Try.Success
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(Checked.Function) 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.
- Since:
- 4.4.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classprivate static final classprivate static final class -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <A> Try<A> Creates a delayed Try, which will return either a Failure or a Success when evaluated.static <A> Try<A> Creates a new failurefilterOrElse(Predicate<? super A> p, Supplier<Exception> orElseSupplier) Return aSuccessif this is aSuccessand the contained values satisfies the given predicate.abstract <B> Try<B> Binds the given function across the success value if it is one.static <A> Try<A> Reduces a nested Try by a single levelabstract <B> BApplies the function to the wrapped value, applying failureF it this is a Failure and successF if this is a Success.abstract voidPerform the givenConsumer(side-effect) for the successif successvalue.abstract AReturns the contained value if this is a success otherwise call the supplier and return its value.abstract booleanReturnstrueif this failure, otherwisefalseabstract booleanReturnstrueif this success, otherwisefalseiterator()Return an iterator for this type.abstract <B> Try<B> Maps the given function to the value from this `Success` or returns this unchanged if a `Failure`.If this is a success, return the same success.If this is a success, return the same success.Applies the given function `f` if this is a `Failure` with certain exception type otherwise leaves this unchanged.Applies the given function `f` if this is a `Failure` otherwise this unchanged if a 'Success'.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.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'.Returns a success wrapping all of the values if all of the arguments were a success, otherwise this returns the first failurestatic <T,A, R> Try <R> Returns a success wrapping all of the values if all of the arguments were a success, otherwise this returns the first failurestatic <A> Try<A> successful(A value) Creates a new SuccesstoEither()Convert this Try to anEither, becoming a left if this is a failure and a right if this is a success.toOption()Convert this Try to an Option.Create aOptionalfrom this try.toStream()Create aStreamfrom this try.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
spliterator
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
-
Constructor Details
-
Try
public Try()
-
-
Method Details
-
failure
-
successful
Creates a new Success- Type Parameters:
A- the wrapped value type- Parameters:
value- a value to wrap, must not be null- Returns:
- a new Success wrapping v
-
delayed
Creates a delayed Try, which will return either a Failure or a Success when evaluated. The supplier is called only once, no matter how many times the returned delayed Try is evaluated.- Type Parameters:
A- the wrapped value type- Parameters:
supplier- a supplier that returns a Try of A.- Returns:
- a new Delayed Try wrapping the supplier.
-
sequence
Returns a success wrapping all of the values if all of the arguments were a success, otherwise this returns the first failure- Type Parameters:
A- The success type- Parameters:
trys- an iterable of try values- Returns:
- a success wrapping all of the values if all of the arguments were a success, otherwise this returns the first failure
-
sequence
Returns a success wrapping all of the values if all of the arguments were a success, otherwise this returns the first failure- Type Parameters:
T- The success typeA- The intermediate accumulator typeR- The result type- Parameters:
trys- an iterable of try valuescollector- result collector- Returns:
- a success wrapping all of the values if all of the arguments were a success, otherwise this returns the first failure
- Since:
- 4.6.0
-
flatten
-
isFailure
public abstract boolean isFailure()Returnstrueif this failure, otherwisefalseNote that for
delayed(Supplier)this is an evaluating operation.- Returns:
trueif this failure, otherwisefalse
-
isSuccess
public abstract boolean isSuccess()Returnstrueif this success, otherwisefalseNote that for
delayed(Supplier)this is an evaluating operation.- Returns:
trueif this success, otherwisefalse
-
flatMap
Binds the given function across the success value if it is one.Note that for
delayed(Supplier)this is not an evaluating operation.- Type Parameters:
B- result type- Parameters:
f- the function to bind.- Returns:
- A new Try value after binding with the function applied if this is a Success, otherwise returns this if this is a `Failure`.
-
map
Maps the given function to the value from this `Success` or returns this unchanged if a `Failure`.Note that for
delayed(Supplier)this is not an evaluating operation.- Type Parameters:
B- result type- Parameters:
f- the function to apply- Returns:
- `f` applied to the `Success`, otherwise returns this if this is a `Failure`.
-
recover
Applies the given function `f` if this is a `Failure` otherwise this unchanged if a 'Success'. This is like map for the failure.Note that for
delayed(Supplier)this is not an evaluating operation.- Parameters:
f- the function to apply- Returns:
- `f` applied to the `Failure`, otherwise returns this if this is a `Success`.
-
recover
public abstract <X extends Exception> Try<A> 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. This is like map for exceptions types.Note that for
delayed(Supplier)this is not an evaluating operation.- Type Parameters:
X- exception type- Parameters:
exceptionType- exception classf- the function to apply- Returns:
- `f` applied to the `Failure`, otherwise returns this if this is a `Success` or the exception does not match the exception type.
-
recoverWith
Binds the given function across the failure value if it is one, otherwise this unchanged if a 'Success'. This is like flatmap for the failure.Note that for
delayed(Supplier)this is not an evaluating operation.- Parameters:
f- the function to bind.- Returns:
- A new Try value after binding with the function applied if this is a `Failure`, otherwise returns this if this is a `Success`.
-
recoverWith
public abstract <X extends Exception> Try<A> 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. This is like flatmap for exceptions types.Note that for
delayed(Supplier)this is not an evaluating operation.- Type Parameters:
X- exception type- Parameters:
exceptionType- exception classf- the function to apply- Returns:
- A new Try value after binding with the function applied if this is a `Failure`, otherwise returns this if this is a `Success` or the exception does not match the exception type.
-
getOrElse
Returns the contained value if this is a success otherwise call the supplier and return its value.Note that for
delayed(Supplier)this is an evaluating operation.- Parameters:
s- called if this is a failure- Returns:
- the wrapped value or the value from the
Supplier
-
orElse
If this is a success, return the same success. Otherwise, returnorElse.Note that for
delayed(Supplier)this is not an evaluating operation.- Parameters:
orElse- try to return if this is failure- Returns:
- this or
orElse - Since:
- 4.7
-
orElse
If this is a success, return the same success. Otherwise, return value supplied byorElse.Note that for
delayed(Supplier)this is not an evaluating operation.- Parameters:
orElse- try to return if this is failure- Returns:
- this or
orElse - Since:
- 4.7
-
filterOrElse
Return aSuccessif this is aSuccessand the contained values satisfies the given predicate.If this is a
Successbut the predicate is not satisfied, return aFailurewith the value provided by the orElseSupplier.Return a
Failureif this aFailurewith the contained value.Note that for
delayed(Supplier)this is not an evaluating operation.- Parameters:
p- The predicate function to test on the right contained value.orElseSupplier- The supplier to execute when is a success, and predicate is unsatisfied- Returns:
- a new Try that will be either the existing success/failure or a failure with result of orElseSupplier
- Since:
- 4.7.0
-
fold
Applies the function to the wrapped value, applying failureF it this is a Failure and successF if this is a Success.Note that for
delayed(Supplier)this is an evaluating operation.- Type Parameters:
B- the destination type- Parameters:
failureF- the function to apply if this is a FailuresuccessF- the function to apply if this is a Success- Returns:
- the result of the applied function
-
toEither
Convert this Try to anEither, 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.- Returns:
- this value wrapped in right if a success, and the exception wrapped in a left if a failure.
-
toOption
Convert this Try to an Option. ReturnsSomewith a value if it is a success, otherwiseNone.Note that for
delayed(Supplier)this is an evaluating operation.- Returns:
- The success's value in
Someif it exists, otherwiseNone
-
toOptional
Create aOptionalfrom this try.Note that for
delayed(Supplier)this is an evaluating operation.- Returns:
Optional.of(Object)with the value if success,Optional.empty()if failure.- Since:
- 4.7
-
toStream
Create aStreamfrom this try.Note that for
delayed(Supplier)this is an evaluating operation.- Returns:
Stream.of(Object)with the value if success,Stream.empty()if failure.- Since:
- 4.7
-
forEach
Perform the givenConsumer(side-effect) for the successif successvalue.Note that for
delayed(Supplier)this is an evaluating operation. -
iterator
Return an iterator for this type. This will be an empty iterator for the failureisFailure()case, and a iterator of a single value for the successisSuccess()case.Note that for
delayed(Supplier)this is an evaluating operation.- Specified by:
iteratorin interfaceIterable<A>- Returns:
- an iterator over the contained value
if success, or an empty one otherwise. - Since:
- 4.7.1
-