Interface Try<T>
-
- Type Parameters:
T- Value type in the case of success.
- All Superinterfaces:
java.lang.Iterable<T>,java.io.Serializable,Value<T>
- All Known Implementing Classes:
Try.Failure,Try.Success
public interface Try<T> extends Value<T>, java.io.Serializable
The Try control gives us the ability write safe code without focusing on try-catch blocks in the presence of exceptions.The following exceptions are considered to be fatal/non-recoverable:
- InterruptedException
- LinkageError
- ThreadDeath
- VirtualMachineError (i.e. OutOfMemoryError or StackOverflowError)
Important note: Try may re-throw (undeclared) exceptions, e.g. on
get(). From within a dynamic proxyInvocationHandlerthis will lead to anUndeclaredThrowableException. For more information, please read Dynamic Proxy Classes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classTry.Failure<T>A failed Try.static classTry.Success<T>A succeeded Try.static classTry.WithResources1<T1 extends java.lang.AutoCloseable>ATry-with-resources builder that operates on oneAutoCloseableresource.static classTry.WithResources2<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable>ATry-with-resources builder that operates on twoAutoCloseableresources.static classTry.WithResources3<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable>ATry-with-resources builder that operates on threeAutoCloseableresources.static classTry.WithResources4<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable>ATry-with-resources builder that operates on fourAutoCloseableresources.static classTry.WithResources5<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable>ATry-with-resources builder that operates on fiveAutoCloseableresources.static classTry.WithResources6<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable>ATry-with-resources builder that operates on sixAutoCloseableresources.static classTry.WithResources7<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable>ATry-with-resources builder that operates on sevenAutoCloseableresources.static classTry.WithResources8<T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable,T8 extends java.lang.AutoCloseable>ATry-with-resources builder that operates on eightAutoCloseableresources.
-
Field Summary
Fields Modifier and Type Field Description static longserialVersionUID
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Try<T>andFinally(java.lang.Runnable runnable)Provides try's finally behavior no matter what the result of the operation is.default Try<T>andFinallyTry(CheckedRunnable runnable)Provides try's finally behavior no matter what the result of the operation is.default Try<T>andThen(java.lang.Runnable runnable)Shortcut forandThenTry(runnable::run), seeandThenTry(CheckedRunnable).default Try<T>andThen(java.util.function.Consumer<? super T> consumer)Shortcut forandThenTry(consumer::accept), seeandThenTry(CheckedConsumer).default Try<T>andThenTry(CheckedConsumer<? super T> consumer)Passes the result to the givenconsumerif this is aSuccess.default Try<T>andThenTry(CheckedRunnable runnable)Runs the given runnable if this is aSuccess, otherwise returns thisFailure.default <R> Try<R>collect(PartialFunction<? super T,? extends R> partialFunction)Collects value that is in the domain of the givenpartialFunctionby mapping the value to typeR.booleanequals(java.lang.Object o)Clarifies that values have a proper equals() method implemented.default Try<java.lang.Throwable>failed()ReturnsSuccess(throwable)if this is aFailure(throwable), otherwise aFailure(new NoSuchElementException("Success.failed()"))if this is a Success.static <T> Try<T>failure(java.lang.Throwable exception)Creates aTry.Failurethat contains the givenexception.default Try<T>filter(java.util.function.Predicate<? super T> predicate)Shortcut forfilterTry(predicate::test), seefilterTry(CheckedPredicate)}.default Try<T>filter(java.util.function.Predicate<? super T> predicate, java.util.function.Function<? super T,? extends java.lang.Throwable> errorProvider)Shortcut forfilterTry(predicate::test, errorProvider::apply), seefilterTry(CheckedPredicate, CheckedFunction1)}.default Try<T>filter(java.util.function.Predicate<? super T> predicate, java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)Shortcut forfilterTry(predicate::test, throwableSupplier), seefilterTry(CheckedPredicate, Supplier)}.default Try<T>filterTry(CheckedPredicate<? super T> predicate)Returnsthisif this is a Failure or this is a Success and the value satisfies the predicate.default Try<T>filterTry(CheckedPredicate<? super T> predicate, CheckedFunction1<? super T,? extends java.lang.Throwable> errorProvider)Returnsthisif this is a Failure or this is a Success and the value satisfies the predicate.default Try<T>filterTry(CheckedPredicate<? super T> predicate, java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)Returnsthisif this is a Failure or this is a Success and the value satisfies the predicate.default <U> Try<U>flatMap(java.util.function.Function<? super T,? extends Try<? extends U>> mapper)Shortcut forflatMapTry(mapper::apply), seeflatMapTry(CheckedFunction1).default <U> Try<U>flatMapTry(CheckedFunction1<? super T,? extends Try<? extends U>> mapper)FlatMaps the value of a Success or returns a Failure.default <X> Xfold(java.util.function.Function<? super java.lang.Throwable,? extends X> ifFail, java.util.function.Function<? super T,? extends X> f)Folds either theFailureor theSuccessside of the Try value.Tget()Gets the result of this Try if this is aSuccessor throws if this is aFailure.java.lang.ThrowablegetCause()Gets the cause if this is a Failure or throws if this is a Success.default TgetOrElseGet(java.util.function.Function<? super java.lang.Throwable,? extends T> other)default <X extends java.lang.Throwable>
TgetOrElseThrow(java.util.function.Function<? super java.lang.Throwable,X> exceptionProvider)inthashCode()Clarifies that values have a proper hashCode() method implemented.default booleanisAsync()ATry's value is computed synchronously.booleanisEmpty()Checks whether this Try has no result, i.e.booleanisFailure()Checks if this is a Failure.default booleanisLazy()ATry's value is computed eagerly.default booleanisSingleValued()ATryis a single-valued.booleanisSuccess()Checks if this is a Success.default Iterator<T>iterator()Returns a richio.vavr.collection.Iterator.default <U> Try<U>map(java.util.function.Function<? super T,? extends U> mapper)Shortcut formapTry(mapper::apply), seemapTry(CheckedFunction1).default Try<T>mapFailure(API.Match.Case<? extends java.lang.Throwable,? extends java.lang.Throwable>... cases)Maps the cause to a new exception if this is aFailureor returns this instance if this is aSuccess.default <U> Try<U>mapTry(CheckedFunction1<? super T,? extends U> mapper)Runs the given checked function if this is aTry.Success, passing the result of the current expression to it.static <T> Try<T>narrow(Try<? extends T> t)Narrows a widenedTry<? extends T>toTry<T>by performing a type-safe cast.static <T> Try<T>of(CheckedFunction0<? extends T> supplier)Creates a Try of a CheckedFunction0.static <T> Try<T>ofCallable(java.util.concurrent.Callable<? extends T> callable)Creates a Try of a Callable.static <T> Try<T>ofSupplier(java.util.function.Supplier<? extends T> supplier)Creates a Try of a Supplier.default <X extends java.lang.Throwable>
Try<T>onFailure(java.lang.Class<X> exceptionType, java.util.function.Consumer<? super X> action)Consumes the cause if this is aTry.Failureand the cause is instance ofX.default Try<T>onFailure(java.util.function.Consumer<? super java.lang.Throwable> action)Consumes the cause if this is aTry.Failure.default Try<T>onSuccess(java.util.function.Consumer<? super T> action)Consumes the value if this is aTry.Success.default Try<T>orElse(Try<? extends T> other)default Try<T>orElse(java.util.function.Supplier<? extends Try<? extends T>> supplier)default voidorElseRun(java.util.function.Consumer<? super java.lang.Throwable> action)default Try<T>peek(java.util.function.Consumer<? super T> action)Applies the action to the value of a Success or does nothing in the case of a Failure.default <X extends java.lang.Throwable>
Try<T>recover(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,? extends T> f)Returnsthis, if this is aSuccessor this is aFailureand the cause is not assignable fromcause.getClass().default <X extends java.lang.Throwable>
Try<T>recover(java.lang.Class<X> exceptionType, T value)Returnsthis, if this is aTry.Successor this is aFailureand the cause is not assignable fromcause.getClass().default Try<T>recover(java.util.function.Function<? super java.lang.Throwable,? extends T> f)Returnsthis, if this is aSuccess, otherwise tries to recover the exception of the failure withf, i.e.default <X extends java.lang.Throwable>
Try<T>recoverWith(java.lang.Class<X> exceptionType, Try<? extends T> recovered)Recovers thisTrywith the givenrecovered, if this is aTry.Failureand the givenexceptionTypeis assignable to the underlying cause type.default <X extends java.lang.Throwable>
Try<T>recoverWith(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,Try<? extends T>> f)Returnsthis, if this is aSuccessor this is aFailureand the cause is not assignable fromcause.getClass().default Try<T>recoverWith(java.util.function.Function<? super java.lang.Throwable,? extends Try<? extends T>> f)Returnsthis, if this is a Success, otherwise tries to recover the exception of the failure withf, i.e.static Try<java.lang.Void>run(CheckedRunnable runnable)Creates a Try of a CheckedRunnable.static Try<java.lang.Void>runRunnable(java.lang.Runnable runnable)Creates a Try of a Runnable.static <T> Try<Seq<T>>sequence(java.lang.Iterable<? extends Try<? extends T>> values)Reduces manyTrys into a singleTryby transforming anIterable<Try<? extends T>>into aTry<Seq<T>>.static <T> Try<T>success(T value)Creates aTry.Successthat contains the givenvalue.default Either<java.lang.Throwable,T>toEither()Converts thisTryto anEither.java.lang.StringtoString()Clarifies that values have a proper toString() method implemented.default Validation<java.lang.Throwable,T>toValidation()Converts thisTryto aValidation.default <U> Validation<U,T>toValidation(java.util.function.Function<? super java.lang.Throwable,? extends U> throwableMapper)Converts thisTryto aValidation, converting the Throwable (if present) to another object using passedFunction.default <U> Utransform(java.util.function.Function<? super Try<T>,? extends U> f)Transforms thisTry.static <T,U>
Try<Seq<U>>traverse(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Try<? extends U>> mapper)Maps the values of an iterable to a sequence of mapped values into a singleTryby transforming anIterable<? extends T>into aTry<Seq<U>>.static <T1 extends java.lang.AutoCloseable>
Try.WithResources1<T1>withResources(CheckedFunction0<? extends T1> t1Supplier)Creates aTry-with-resources builder that operates on oneAutoCloseableresource.static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable>
Try.WithResources2<T1,T2>withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier)Creates aTry-with-resources builder that operates on twoAutoCloseableresources.static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable>
Try.WithResources3<T1,T2,T3>withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier)Creates aTry-with-resources builder that operates on threeAutoCloseableresources.static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable>
Try.WithResources4<T1,T2,T3,T4>withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier)Creates aTry-with-resources builder that operates on fourAutoCloseableresources.static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable>
Try.WithResources5<T1,T2,T3,T4,T5>withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier)Creates aTry-with-resources builder that operates on fiveAutoCloseableresources.static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable>
Try.WithResources6<T1,T2,T3,T4,T5,T6>withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier)Creates aTry-with-resources builder that operates on sixAutoCloseableresources.static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable>
Try.WithResources7<T1,T2,T3,T4,T5,T6,T7>withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier, CheckedFunction0<? extends T7> t7Supplier)Creates aTry-with-resources builder that operates on sevenAutoCloseableresources.static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable,T8 extends java.lang.AutoCloseable>
Try.WithResources8<T1,T2,T3,T4,T5,T6,T7,T8>withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier, CheckedFunction0<? extends T7> t7Supplier, CheckedFunction0<? extends T8> t8Supplier)Creates aTry-with-resources builder that operates on eightAutoCloseableresources.-
Methods inherited from interface io.vavr.Value
collect, collect, contains, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, out, out, spliterator, stderr, stdout, stringPrefix, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
-
-
-
Field Detail
-
serialVersionUID
static final long serialVersionUID
- See Also:
- Constant Field Values
-
-
Method Detail
-
of
static <T> Try<T> of(CheckedFunction0<? extends T> supplier)
Creates a Try of a CheckedFunction0.- Type Parameters:
T- Component type- Parameters:
supplier- A checked supplier- Returns:
Success(supplier.apply())if no exception occurs, otherwiseFailure(throwable)if an exception occurs callingsupplier.apply().
-
ofSupplier
static <T> Try<T> ofSupplier(java.util.function.Supplier<? extends T> supplier)
Creates a Try of a Supplier.- Type Parameters:
T- Component type- Parameters:
supplier- A supplier- Returns:
Success(supplier.get())if no exception occurs, otherwiseFailure(throwable)if an exception occurs callingsupplier.get().
-
ofCallable
static <T> Try<T> ofCallable(java.util.concurrent.Callable<? extends T> callable)
Creates a Try of a Callable.- Type Parameters:
T- Component type- Parameters:
callable- A callable- Returns:
Success(callable.call())if no exception occurs, otherwiseFailure(throwable)if an exception occurs callingcallable.call().
-
run
static Try<java.lang.Void> run(CheckedRunnable runnable)
Creates a Try of a CheckedRunnable.- Parameters:
runnable- A checked runnable- Returns:
Success(null)if no exception occurs, otherwiseFailure(throwable)if an exception occurs callingrunnable.run().
-
runRunnable
static Try<java.lang.Void> runRunnable(java.lang.Runnable runnable)
Creates a Try of a Runnable.- Parameters:
runnable- A runnable- Returns:
Success(null)if no exception occurs, otherwiseFailure(throwable)if an exception occurs callingrunnable.run().
-
sequence
static <T> Try<Seq<T>> sequence(java.lang.Iterable<? extends Try<? extends T>> values)
Reduces manyTrys into a singleTryby transforming anIterable<Try<? extends T>>into aTry<Seq<T>>. If any of theTrys areTry.Failure, then this returns aTry.Failure.- Type Parameters:
T- type of the Trys- Parameters:
values- AnIterableofTrys- Returns:
- A
Tryof aSeqof results - Throws:
java.lang.NullPointerException- ifvaluesis null
-
traverse
static <T,U> Try<Seq<U>> traverse(java.lang.Iterable<? extends T> values, java.util.function.Function<? super T,? extends Try<? extends U>> mapper)
Maps the values of an iterable to a sequence of mapped values into a singleTryby transforming anIterable<? extends T>into aTry<Seq<U>>.- Type Parameters:
T- The type of the given values.U- The mapped value type.- Parameters:
values- AnIterableof values.mapper- A mapper of values to Trys- Returns:
- A
Tryof aSeqof results. - Throws:
java.lang.NullPointerException- if values or f is null.
-
success
static <T> Try<T> success(T value)
- Type Parameters:
T- Type of the givenvalue.- Parameters:
value- A value.- Returns:
- A new
Success.
-
failure
static <T> Try<T> failure(java.lang.Throwable exception)
- Type Parameters:
T- Component type of theTry.- Parameters:
exception- An exception.- Returns:
- A new
Failure.
-
narrow
static <T> Try<T> narrow(Try<? extends T> t)
Narrows a widenedTry<? extends T>toTry<T>by performing a type-safe cast. This is eligible because immutable/read-only collections are covariant.- Type Parameters:
T- Component type of theTry.- Parameters:
t- ATry.- Returns:
- the given
tinstance as narrowed typeTry<T>.
-
andThen
default Try<T> andThen(java.util.function.Consumer<? super T> consumer)
Shortcut forandThenTry(consumer::accept), seeandThenTry(CheckedConsumer).- Parameters:
consumer- A consumer- Returns:
- this
Tryif this is aFailureor the consumer succeeded, otherwise theFailureof the consumption. - Throws:
java.lang.NullPointerException- ifconsumeris null
-
andThenTry
default Try<T> andThenTry(CheckedConsumer<? super T> consumer)
Passes the result to the givenconsumerif this is aSuccess.The main use case is chaining checked functions using method references:
Try.of(() -> 100) .andThen(i -> System.out.println(i));- Parameters:
consumer- A checked consumer- Returns:
- this
Tryif this is aFailureor the consumer succeeded, otherwise theFailureof the consumption. - Throws:
java.lang.NullPointerException- ifconsumeris null
-
andThen
default Try<T> andThen(java.lang.Runnable runnable)
Shortcut forandThenTry(runnable::run), seeandThenTry(CheckedRunnable).- Parameters:
runnable- A runnable- Returns:
- this
Tryif this is aFailureor the runnable succeeded, otherwise theFailureof the run. - Throws:
java.lang.NullPointerException- ifrunnableis null
-
andThenTry
default Try<T> andThenTry(CheckedRunnable runnable)
Runs the given runnable if this is aSuccess, otherwise returns thisFailure.The main use case is chaining runnables using method references:
Please note that these lines are semantically the same:Try.run(A::methodRef).andThen(B::methodRef).andThen(C::methodRef);Try.run(this::doStuff) .andThen(this::doMoreStuff) .andThen(this::doEvenMoreStuff); Try.run(() -> { doStuff(); doMoreStuff(); doEvenMoreStuff(); });- Parameters:
runnable- A checked runnable- Returns:
- this
Tryif this is aFailureor the runnable succeeded, otherwise theFailureof the run. - Throws:
java.lang.NullPointerException- ifrunnableis null
-
collect
default <R> Try<R> collect(PartialFunction<? super T,? extends R> partialFunction)
Collects value that is in the domain of the givenpartialFunctionby mapping the value to typeR.
If the element makes it through that filter, the mapped instance is wrapped inpartialFunction.isDefinedAt(value)TryR newValue = partialFunction.apply(value)- Type Parameters:
R- The new value type- Parameters:
partialFunction- A function that is not necessarily defined on value of this try.- Returns:
- A new
Tryinstance containing value of typeR - Throws:
java.lang.NullPointerException- ifpartialFunctionis null
-
failed
default Try<java.lang.Throwable> failed()
ReturnsSuccess(throwable)if this is aFailure(throwable), otherwise aFailure(new NoSuchElementException("Success.failed()"))if this is a Success.- Returns:
- a new Try
-
filter
default Try<T> filter(java.util.function.Predicate<? super T> predicate, java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)
Shortcut forfilterTry(predicate::test, throwableSupplier), seefilterTry(CheckedPredicate, Supplier)}.- Parameters:
predicate- A predicatethrowableSupplier- A supplier of a throwable- Returns:
- a
Tryinstance - Throws:
java.lang.NullPointerException- ifpredicateorthrowableSupplieris null
-
filter
default Try<T> filter(java.util.function.Predicate<? super T> predicate, java.util.function.Function<? super T,? extends java.lang.Throwable> errorProvider)
Shortcut forfilterTry(predicate::test, errorProvider::apply), seefilterTry(CheckedPredicate, CheckedFunction1)}.- Parameters:
predicate- A predicateerrorProvider- A function that provides some kind of Throwable for T- Returns:
- a
Tryinstance - Throws:
java.lang.NullPointerException- ifpredicateorerrorProvideris null
-
filter
default Try<T> filter(java.util.function.Predicate<? super T> predicate)
Shortcut forfilterTry(predicate::test), seefilterTry(CheckedPredicate)}.- Parameters:
predicate- A predicate- Returns:
- a
Tryinstance - Throws:
java.lang.NullPointerException- ifpredicateis null
-
filterTry
default Try<T> filterTry(CheckedPredicate<? super T> predicate, java.util.function.Supplier<? extends java.lang.Throwable> throwableSupplier)
Returnsthisif this is a Failure or this is a Success and the value satisfies the predicate.Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception occurs testing the predicate. The returned Failure wraps a Throwable instance provided by the given
throwableSupplier.- Parameters:
predicate- A checked predicatethrowableSupplier- A supplier of a throwable- Returns:
- a
Tryinstance - Throws:
java.lang.NullPointerException- ifpredicateorthrowableSupplieris null
-
filterTry
default Try<T> filterTry(CheckedPredicate<? super T> predicate, CheckedFunction1<? super T,? extends java.lang.Throwable> errorProvider)
Returnsthisif this is a Failure or this is a Success and the value satisfies the predicate.Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception occurs testing the predicate. The returned Failure wraps a Throwable instance provided by the given
errorProvider.- Parameters:
predicate- A checked predicateerrorProvider- A provider of a throwable- Returns:
- a
Tryinstance - Throws:
java.lang.NullPointerException- ifpredicateorerrorProvideris null
-
filterTry
default Try<T> filterTry(CheckedPredicate<? super T> predicate)
Returnsthisif this is a Failure or this is a Success and the value satisfies the predicate.Returns a new Failure, if this is a Success and the value does not satisfy the Predicate or an exception occurs testing the predicate. The returned Failure wraps a
NoSuchElementExceptioninstance.- Parameters:
predicate- A checked predicate- Returns:
- a
Tryinstance - Throws:
java.lang.NullPointerException- ifpredicateis null
-
flatMap
default <U> Try<U> flatMap(java.util.function.Function<? super T,? extends Try<? extends U>> mapper)
Shortcut forflatMapTry(mapper::apply), seeflatMapTry(CheckedFunction1).- Type Parameters:
U- The new component type- Parameters:
mapper- A mapper- Returns:
- a
Try - Throws:
java.lang.NullPointerException- ifmapperis null
-
flatMapTry
default <U> Try<U> flatMapTry(CheckedFunction1<? super T,? extends Try<? extends U>> mapper)
FlatMaps the value of a Success or returns a Failure.- Type Parameters:
U- The new component type- Parameters:
mapper- A mapper- Returns:
- a
Try - Throws:
java.lang.NullPointerException- ifmapperis null
-
get
T get()
Gets the result of this Try if this is aSuccessor throws if this is aFailure.IMPORTANT! If this is a
Try.Failure, the underlyingcauseof typeThrowableis thrown.The thrown exception is exactly the same as the result of
getCause().
-
getCause
java.lang.Throwable getCause()
Gets the cause if this is a Failure or throws if this is a Success.- Returns:
- The cause if this is a Failure
- Throws:
java.lang.UnsupportedOperationException- if this is a Success
-
isAsync
default boolean isAsync()
ATry's value is computed synchronously.
-
isEmpty
boolean isEmpty()
Checks whether this Try has no result, i.e. is a Failure.
-
isFailure
boolean isFailure()
Checks if this is a Failure.- Returns:
- true, if this is a Failure, otherwise false, if this is a Success
-
isLazy
default boolean isLazy()
ATry's value is computed eagerly.
-
isSingleValued
default boolean isSingleValued()
ATryis a single-valued.- Specified by:
isSingleValuedin interfaceValue<T>- Returns:
true
-
isSuccess
boolean isSuccess()
Checks if this is a Success.- Returns:
- true, if this is a Success, otherwise false, if this is a Failure
-
iterator
default Iterator<T> iterator()
Description copied from interface:ValueReturns a richio.vavr.collection.Iterator.
-
map
default <U> Try<U> map(java.util.function.Function<? super T,? extends U> mapper)
Shortcut formapTry(mapper::apply), seemapTry(CheckedFunction1).
-
mapFailure
@GwtIncompatible default Try<T> mapFailure(API.Match.Case<? extends java.lang.Throwable,? extends java.lang.Throwable>... cases)
Maps the cause to a new exception if this is aFailureor returns this instance if this is aSuccess.If none of the given cases matches the cause, the same
Failureis returned.- Parameters:
cases- A not necessarily exhaustive sequence of cases that will be matched against a cause.- Returns:
- A new
Tryif this is aFailure, otherwise this.
-
mapTry
default <U> Try<U> mapTry(CheckedFunction1<? super T,? extends U> mapper)
Runs the given checked function if this is aTry.Success, passing the result of the current expression to it. If this expression is aTry.Failurethen it'll return a newTry.Failureof type R with the original exception.The main use case is chaining checked functions using method references:
Try.of(() -> 0) .map(x -> 1 / x); // division by zero- Type Parameters:
U- The new component type- Parameters:
mapper- A checked function- Returns:
- a
Try - Throws:
java.lang.NullPointerException- ifmapperis null
-
onFailure
default Try<T> onFailure(java.util.function.Consumer<? super java.lang.Throwable> action)
Consumes the cause if this is aTry.Failure.// (does not print anything) Try.success(1).onFailure(System.out::println); // prints "java.lang.Error" Try.failure(new Error()).onFailure(System.out::println);- Parameters:
action- An exception consumer- Returns:
- this
- Throws:
java.lang.NullPointerException- ifactionis null
-
onFailure
@GwtIncompatible default <X extends java.lang.Throwable> Try<T> onFailure(java.lang.Class<X> exceptionType, java.util.function.Consumer<? super X> action)
Consumes the cause if this is aTry.Failureand the cause is instance ofX.// (does not print anything) Try.success(1).onFailure(Error.class, System.out::println); // prints "Error" Try.failure(new Error()) .onFailure(RuntimeException.class, x -> System.out.println("Runtime exception")) .onFailure(Error.class, x -> System.out.println("Error"));- Type Parameters:
X- the exception type that should be handled- Parameters:
exceptionType- the exception type that is handledaction- an excpetion consumer- Returns:
- this
- Throws:
java.lang.NullPointerException- ifexceptionTypeoractionis null
-
onSuccess
default Try<T> onSuccess(java.util.function.Consumer<? super T> action)
Consumes the value if this is aTry.Success.// prints "1" Try.success(1).onSuccess(System.out::println); // (does not print anything) Try.failure(new Error()).onSuccess(System.out::println);- Parameters:
action- A value consumer- Returns:
- this
- Throws:
java.lang.NullPointerException- ifactionis null
-
getOrElseGet
default T getOrElseGet(java.util.function.Function<? super java.lang.Throwable,? extends T> other)
-
orElseRun
default void orElseRun(java.util.function.Consumer<? super java.lang.Throwable> action)
-
getOrElseThrow
default <X extends java.lang.Throwable> T getOrElseThrow(java.util.function.Function<? super java.lang.Throwable,X> exceptionProvider) throws X extends java.lang.Throwable
- Throws:
X extends java.lang.Throwable
-
fold
default <X> X fold(java.util.function.Function<? super java.lang.Throwable,? extends X> ifFail, java.util.function.Function<? super T,? extends X> f)Folds either theFailureor theSuccessside of the Try value.- Type Parameters:
X- type of the folded value- Parameters:
ifFail- maps the left value if this is aFailuref- maps the value if this is aSuccess- Returns:
- A value of type X
-
peek
default Try<T> peek(java.util.function.Consumer<? super T> action)
Applies the action to the value of a Success or does nothing in the case of a Failure.
-
recover
@GwtIncompatible default <X extends java.lang.Throwable> Try<T> recover(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,? extends T> f)
Returnsthis, if this is aSuccessor this is aFailureand the cause is not assignable fromcause.getClass(). Otherwise tries to recover the exception of the failure withf, i.e. callingTry.of(() -> f.apply((X) getCause()).// = Success(13) Try.of(() -> 27/2).recover(ArithmeticException.class, x -> Integer.MAX_VALUE); // = Success(2147483647) Try.of(() -> 1/0) .recover(Error.class, x -> -1) .recover(ArithmeticException.class, x -> Integer.MAX_VALUE); // = Failure(java.lang.ArithmeticException: / by zero) Try.of(() -> 1/0).recover(Error.class, x -> Integer.MAX_VALUE);- Type Parameters:
X- Exception type- Parameters:
exceptionType- The specific exception type that should be handledf- A recovery function taking an exception of typeX- Returns:
- a
Try - Throws:
java.lang.NullPointerException- ifexceptionis null orfis null
-
recoverWith
@GwtIncompatible default <X extends java.lang.Throwable> Try<T> recoverWith(java.lang.Class<X> exceptionType, java.util.function.Function<? super X,Try<? extends T>> f)
Returnsthis, if this is aSuccessor this is aFailureand the cause is not assignable fromcause.getClass(). Otherwise tries to recover the exception of the failure withfwhich returns Try. IfisFailure()returned byffunction istrueit means that recovery cannot take place due to some circumstances.// = Success(13) Try.of(() -> 27/2).recoverWith(ArithmeticException.class, x -> Try.success(Integer.MAX_VALUE)); // = Success(2147483647) Try.of(() -> 1/0) .recoverWith(Error.class, x -> Try.success(-1)) .recoverWith(ArithmeticException.class, x -> Try.success(Integer.MAX_VALUE)); // = Failure(java.lang.ArithmeticException: / by zero) Try.of(() -> 1/0).recoverWith(Error.class, x -> Try.success(Integer.MAX_VALUE));- Type Parameters:
X- Exception type- Parameters:
exceptionType- The specific exception type that should be handledf- A recovery function taking an exception of typeXand returning Try as a result of recovery. If Try isisSuccess()then recovery ends up successfully. Otherwise the function was not able to recover.- Returns:
- a
Try - Throws:
java.lang.NullPointerException- ifexceptionTypeorfis null
-
recoverWith
@GwtIncompatible default <X extends java.lang.Throwable> Try<T> recoverWith(java.lang.Class<X> exceptionType, Try<? extends T> recovered)
Recovers thisTrywith the givenrecovered, if this is aTry.Failureand the givenexceptionTypeis assignable to the underlying cause type.// = Success(13) Try.of(() -> 27/2).recoverWith(ArithmeticException.class, Try.success(Integer.MAX_VALUE)); // = Success(2147483647) Try.of(() -> 1/0) .recoverWith(Error.class, Try.success(-1)) .recoverWith(ArithmeticException.class, Try.success(Integer.MAX_VALUE)); // = Failure(java.lang.ArithmeticException: / by zero) Try.of(() -> 1/0).recoverWith(Error.class, Try.success(Integer.MAX_VALUE));- Type Parameters:
X- type of the exception that should be recovered- Parameters:
exceptionType- the exception type that is recoveredrecovered- the substitute for a matchingFailure- Returns:
- the given
recoveredif this is aTry.Failureand the cause is of typeX, elsethis - Throws:
java.lang.NullPointerException- ifexceptionTypeorrecoveredis null
-
recover
@GwtIncompatible default <X extends java.lang.Throwable> Try<T> recover(java.lang.Class<X> exceptionType, T value)
Returnsthis, if this is aTry.Successor this is aFailureand the cause is not assignable fromcause.getClass(). Otherwise returns aTry.Successcontaining the givenvalue.// = Success(13) Try.of(() -> 27/2).recover(ArithmeticException.class, Integer.MAX_VALUE); // = Success(2147483647) Try.of(() -> 1/0) .recover(Error.class, -1); .recover(ArithmeticException.class, Integer.MAX_VALUE); // = Failure(java.lang.ArithmeticException: / by zero) Try.of(() -> 1/0).recover(Error.class, Integer.MAX_VALUE);- Type Parameters:
X- Exception type- Parameters:
exceptionType- The specific exception type that should be handledvalue- A value that is used in case of a recovery- Returns:
- a
Try - Throws:
java.lang.NullPointerException- ifexceptionis null
-
recover
default Try<T> recover(java.util.function.Function<? super java.lang.Throwable,? extends T> f)
Returnsthis, if this is aSuccess, otherwise tries to recover the exception of the failure withf, i.e. callingTry.of(() -> f.apply(throwable)).// = Success(13) Try.of(() -> 27/2).recover(x -> Integer.MAX_VALUE); // = Success(2147483647) Try.of(() -> 1/0).recover(x -> Integer.MAX_VALUE);- Parameters:
f- A recovery function taking a Throwable- Returns:
- a
Try - Throws:
java.lang.NullPointerException- iffis null
-
recoverWith
default Try<T> recoverWith(java.util.function.Function<? super java.lang.Throwable,? extends Try<? extends T>> f)
Returnsthis, if this is a Success, otherwise tries to recover the exception of the failure withf, i.e. callingf.apply(cause.getCause()). If an error occurs recovering a Failure, then the new Failure is returned.// = Success(13) Try.of(() -> 27/2).recoverWith(x -> Try.success(Integer.MAX_VALUE)); // = Success(2147483647) Try.of(() -> 1/0).recoverWith(x -> Try.success(Integer.MAX_VALUE));- Parameters:
f- A recovery function taking a Throwable- Returns:
- a
Try - Throws:
java.lang.NullPointerException- iffis null
-
toEither
default Either<java.lang.Throwable,T> toEither()
Converts thisTryto anEither.- Returns:
- A new
Either
-
toValidation
default Validation<java.lang.Throwable,T> toValidation()
Converts thisTryto aValidation.- Returns:
- A new
Validation
-
toValidation
default <U> Validation<U,T> toValidation(java.util.function.Function<? super java.lang.Throwable,? extends U> throwableMapper)
Converts thisTryto aValidation, converting the Throwable (if present) to another object using passedFunction.Validation<String, Integer> = Try.of(() -> 1/0).toValidation(Throwable::getMessage));- Type Parameters:
U- result type of the throwable mapper- Parameters:
throwableMapper- A transformation from throwable to desired invalid type of newValidation- Returns:
- A new
Validation - Throws:
java.lang.NullPointerException- if the giventhrowableMapperis null.
-
transform
default <U> U transform(java.util.function.Function<? super Try<T>,? extends U> f)
Transforms thisTry.- Type Parameters:
U- Type of transformation result- Parameters:
f- A transformation- Returns:
- An instance of type
U - Throws:
java.lang.NullPointerException- iffis null
-
andFinally
default Try<T> andFinally(java.lang.Runnable runnable)
Provides try's finally behavior no matter what the result of the operation is.- Parameters:
runnable- A runnable- Returns:
- this
Try. - Throws:
java.lang.NullPointerException- ifrunnableis null
-
andFinallyTry
default Try<T> andFinallyTry(CheckedRunnable runnable)
Provides try's finally behavior no matter what the result of the operation is.- Parameters:
runnable- A runnable- Returns:
- this
Try. - Throws:
java.lang.NullPointerException- ifrunnableis null
-
equals
boolean equals(java.lang.Object o)
Description copied from interface:ValueClarifies that values have a proper equals() method implemented.
-
hashCode
int hashCode()
Description copied from interface:ValueClarifies that values have a proper hashCode() method implemented.See Object.hashCode().
-
toString
java.lang.String toString()
Description copied from interface:ValueClarifies that values have a proper toString() method implemented.See Object.toString().
-
withResources
static <T1 extends java.lang.AutoCloseable> Try.WithResources1<T1> withResources(CheckedFunction0<? extends T1> t1Supplier)
Creates aTry-with-resources builder that operates on oneAutoCloseableresource.- Type Parameters:
T1- Type of the 1st resource.- Parameters:
t1Supplier- The supplier of the first resource.- Returns:
- a new
Try.WithResources1instance.
-
withResources
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable> Try.WithResources2<T1,T2> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier)
Creates aTry-with-resources builder that operates on twoAutoCloseableresources.- Type Parameters:
T1- Type of the 1st resource.T2- Type of the 2nd resource.- Parameters:
t1Supplier- The supplier of the 1st resource.t2Supplier- The supplier of the 2nd resource.- Returns:
- a new
Try.WithResources2instance.
-
withResources
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable> Try.WithResources3<T1,T2,T3> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier)
Creates aTry-with-resources builder that operates on threeAutoCloseableresources.- Type Parameters:
T1- Type of the 1st resource.T2- Type of the 2nd resource.T3- Type of the 3rd resource.- Parameters:
t1Supplier- The supplier of the 1st resource.t2Supplier- The supplier of the 2nd resource.t3Supplier- The supplier of the 3rd resource.- Returns:
- a new
Try.WithResources3instance.
-
withResources
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable> Try.WithResources4<T1,T2,T3,T4> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier)
Creates aTry-with-resources builder that operates on fourAutoCloseableresources.- Type Parameters:
T1- Type of the 1st resource.T2- Type of the 2nd resource.T3- Type of the 3rd resource.T4- Type of the 4th resource.- Parameters:
t1Supplier- The supplier of the 1st resource.t2Supplier- The supplier of the 2nd resource.t3Supplier- The supplier of the 3rd resource.t4Supplier- The supplier of the 4th resource.- Returns:
- a new
Try.WithResources4instance.
-
withResources
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable> Try.WithResources5<T1,T2,T3,T4,T5> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier)
Creates aTry-with-resources builder that operates on fiveAutoCloseableresources.- Type Parameters:
T1- Type of the 1st resource.T2- Type of the 2nd resource.T3- Type of the 3rd resource.T4- Type of the 4th resource.T5- Type of the 5th resource.- Parameters:
t1Supplier- The supplier of the 1st resource.t2Supplier- The supplier of the 2nd resource.t3Supplier- The supplier of the 3rd resource.t4Supplier- The supplier of the 4th resource.t5Supplier- The supplier of the 5th resource.- Returns:
- a new
Try.WithResources5instance.
-
withResources
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable> Try.WithResources6<T1,T2,T3,T4,T5,T6> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier)
Creates aTry-with-resources builder that operates on sixAutoCloseableresources.- Type Parameters:
T1- Type of the 1st resource.T2- Type of the 2nd resource.T3- Type of the 3rd resource.T4- Type of the 4th resource.T5- Type of the 5th resource.T6- Type of the 6th resource.- Parameters:
t1Supplier- The supplier of the 1st resource.t2Supplier- The supplier of the 2nd resource.t3Supplier- The supplier of the 3rd resource.t4Supplier- The supplier of the 4th resource.t5Supplier- The supplier of the 5th resource.t6Supplier- The supplier of the 6th resource.- Returns:
- a new
Try.WithResources6instance.
-
withResources
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable> Try.WithResources7<T1,T2,T3,T4,T5,T6,T7> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier, CheckedFunction0<? extends T7> t7Supplier)
Creates aTry-with-resources builder that operates on sevenAutoCloseableresources.- Type Parameters:
T1- Type of the 1st resource.T2- Type of the 2nd resource.T3- Type of the 3rd resource.T4- Type of the 4th resource.T5- Type of the 5th resource.T6- Type of the 6th resource.T7- Type of the 7th resource.- Parameters:
t1Supplier- The supplier of the 1st resource.t2Supplier- The supplier of the 2nd resource.t3Supplier- The supplier of the 3rd resource.t4Supplier- The supplier of the 4th resource.t5Supplier- The supplier of the 5th resource.t6Supplier- The supplier of the 6th resource.t7Supplier- The supplier of the 7th resource.- Returns:
- a new
Try.WithResources7instance.
-
withResources
static <T1 extends java.lang.AutoCloseable,T2 extends java.lang.AutoCloseable,T3 extends java.lang.AutoCloseable,T4 extends java.lang.AutoCloseable,T5 extends java.lang.AutoCloseable,T6 extends java.lang.AutoCloseable,T7 extends java.lang.AutoCloseable,T8 extends java.lang.AutoCloseable> Try.WithResources8<T1,T2,T3,T4,T5,T6,T7,T8> withResources(CheckedFunction0<? extends T1> t1Supplier, CheckedFunction0<? extends T2> t2Supplier, CheckedFunction0<? extends T3> t3Supplier, CheckedFunction0<? extends T4> t4Supplier, CheckedFunction0<? extends T5> t5Supplier, CheckedFunction0<? extends T6> t6Supplier, CheckedFunction0<? extends T7> t7Supplier, CheckedFunction0<? extends T8> t8Supplier)
Creates aTry-with-resources builder that operates on eightAutoCloseableresources.- Type Parameters:
T1- Type of the 1st resource.T2- Type of the 2nd resource.T3- Type of the 3rd resource.T4- Type of the 4th resource.T5- Type of the 5th resource.T6- Type of the 6th resource.T7- Type of the 7th resource.T8- Type of the 8th resource.- Parameters:
t1Supplier- The supplier of the 1st resource.t2Supplier- The supplier of the 2nd resource.t3Supplier- The supplier of the 3rd resource.t4Supplier- The supplier of the 4th resource.t5Supplier- The supplier of the 5th resource.t6Supplier- The supplier of the 6th resource.t7Supplier- The supplier of the 7th resource.t8Supplier- The supplier of the 8th resource.- Returns:
- a new
Try.WithResources8instance.
-
-