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(BiConsumer<? super T, Exception> consumer) Consumes both a result and an exception of thisTry.Consumes a result of thisTryif it is successful.boolean<U> Try<U>get()Returns a result of thisTry, possiblynullgetElseGet(Supplier<? extends T> defaultValueSupplier) @Nullable ExceptionReturns an exception of thisTry, possiblynullinthashCode()ifException(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> Try<U>map(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>Creates a new failedTrywith a given exception<U> Ureduce(BiFunction<? super T, Exception, ? extends U> fn) Applies a function to thisTry's result and an exception<U> UApplies a function to either thisTry's result or an exceptiontoEither()toString()static <T> Try<T>wrap(RunnableEx computation) Creates a newTrywhich is either successful or failed based on the result of a runnable callstatic <T> Try<T>wrap(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
- 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
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(Function<? super T, ? extends U> function, Function<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
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
-
flatMap
-
toEither
-
equals
-
hashCode
public int hashCode() -
toString
-