Package io.activej.common.collection
Class Try<T>
java.lang.Object
io.activej.common.collection.Try<T>
- Type Parameters:
T- type of result
A compound type that represents either a result or an exception
Semantically close to Either<T, Exception>
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionconsume(@NotNull BiConsumer<? super T, Exception> consumer) Consumes both a result and an exception of thisTry.consume(@NotNull Consumer<? super T> resultConsumer, @NotNull Consumer<Exception> exceptionConsumer) Consumes a result of thisTryif it is successful.boolean<U> @NotNull Try<U>get()Returns a result of thisTry, possiblynullgetElseGet(@NotNull Supplier<? extends T> defaultValueSupplier) @Nullable ExceptionReturns an exception of thisTry, possiblynullinthashCode()ifException(@NotNull Consumer<Exception> exceptionConsumer) Consumes an exception of thisTryif it is failed.Consumes a result of thisTryif it is successful.booleanReturns whether thisTryis failedbooleanReturns whether thisTryis successful<U> @NotNull Try<U>map(@NotNull FunctionEx<T, U> function) static <T> Try<T>of(T result) Creates a new successfulTrywith a resultstatic <T> Try<T>Creates a newTrywhich is either successful and has a result or is failed and has an exceptionstatic <T> Try<T>ofException(@NotNull Exception e) Creates a new failedTrywith a given exception<U> Ureduce(@NotNull BiFunction<? super T, Exception, ? extends U> fn) Applies a function to thisTry's result and an exception<U> Ureduce(@NotNull Function<? super T, ? extends U> function, @NotNull Function<@NotNull Exception, ? extends U> exceptionFunction) Applies a function to either thisTry's result or an exceptiontoEither()toString()static <T> Try<T>wrap(@NotNull RunnableEx computation) Creates a newTrywhich is either successful or failed based on the result of a runnable callstatic <T> Try<T>wrap(@NotNull SupplierEx<T> computation) Creates a newTrywhich is either successful or failed based on the result of a supplier callstatic <T> Try<T>Creates a newTrywhich is either successful or failed based on the result of a callable call
-
Method Details
-
of
Creates a new successfulTrywith a result -
of
Creates a newTrywhich is either successful and has a result or is failed and has an exception -
ofException
Creates a new failedTrywith a given exception -
wrap
-
wrap
Creates a newTrywhich is either successful or failed based on the result of a runnable callIf runnable throws exception a
Tryis failed. Otherwise, it will havenullas a result value of aTry- Type Parameters:
T- a type of result- Parameters:
computation- a throwing runnable- Returns:
- a new instance of a
Trythat is either successful or failed
-
wrap
-
isSuccess
@Contract(pure=true) public boolean isSuccess()Returns whether thisTryis successful -
isException
@Contract(pure=true) public boolean isException()Returns whether thisTryis failed -
get
Returns a result of thisTry, possiblynull -
getElse
-
getElseGet
-
getException
Returns an exception of thisTry, possiblynull -
ifSuccess
- Parameters:
resultConsumer- a consumer of a result- Returns:
- this
Try
-
ifException
@NotNull public @NotNull Try<T> ifException(@NotNull @NotNull Consumer<Exception> exceptionConsumer) - Parameters:
exceptionConsumer- a consumer of an exception- Returns:
- this
Try
-
consume
- Parameters:
consumer- a consumer of a result and an exception- Returns:
- this
Try
-
consume
@NotNull public @NotNull Try<T> consume(@NotNull @NotNull Consumer<? super T> resultConsumer, @NotNull @NotNull Consumer<Exception> exceptionConsumer) Consumes a result of thisTryif it is successful. Otherwise, consumes an exceptionAlways returns this
Try- Parameters:
resultConsumer- a consumer of a resultexceptionConsumer- a consumer of an exception- Returns:
- this
Try
-
reduce
@Contract(pure=true) public <U> U reduce(@NotNull @NotNull Function<? super T, ? extends U> function, @NotNull @NotNull Function<@NotNull Exception, ? extends U> exceptionFunction) Applies a function to either thisTry's result or an exception- Type Parameters:
U- a type of mapping result- Parameters:
function- a function to map a result valueexceptionFunction- a function to map an exception- Returns:
- a result of mapping of either a result or an exception of a
Try
-
reduce
@Contract(pure=true) public <U> U reduce(@NotNull @NotNull BiFunction<? super T, Exception, ? extends U> fn) Applies a function to thisTry's result and an exception- Type Parameters:
U- a type of mapping result- Parameters:
fn- a function to map a result value and an exception- Returns:
- a result of mapping of a result and an exception of a
Try
-
map
@Contract(pure=true) @NotNull public <U> @NotNull Try<U> map(@NotNull @NotNull FunctionEx<T, U> function) -
flatMap
-
toEither
-
equals
-
hashCode
public int hashCode() -
toString
-