public class Try<T> extends Object
| 构造器和说明 |
|---|
Try() |
| 限定符和类型 | 方法和说明 |
|---|---|
static <R> Try<R> |
failure(Throwable ex)
Create a failed action.
|
T |
get()
Get the result.
|
@Nullable Throwable |
getCause()
Get the cause of the exception.
|
Optional<T> |
getOptional()
Get the optional result.
|
T |
getOrElse(T defaultValue)
Get the result or the default value.
|
T |
getOrElseGet(Function<Throwable,T> function)
Get the result or the default value.
|
<X extends Throwable> |
getOrElseThrow(Function<Throwable,X> function)
Get the result or throw an exception.
|
Try<T> |
getOrElseTry(CheckedFunction<Throwable,T> function)
Get this or the other instance if this action is failed.
|
T |
getOrNull()
Get the result or null if the supplier throws an exception.
|
boolean |
isFailure()
Whether the computation is failed.
|
boolean |
isSuccess()
Whether the computation is successful.
|
static Try<Void> |
of(CheckedRunnable runnable)
Create an action that does nothing.
|
static <R> Try<R> |
of(CheckedSupplier<R> supplier)
Create an action that does nothing and returns a result.
|
void |
perform()
Perform the computation and subscribe the result and the exception.
|
void |
run()
Run the supplier.
|
<X extends Throwable> |
runOrThrow(Function<Throwable,X> function)
Run the supplier and throw an exception if the supplier throws an exception.
|
Try<T> |
subscribeFailure(Consumer<Throwable> subscriber)
Subscribe the result.
|
Try<T> |
subscribeSuccess(Consumer<T> subscriber)
Subscribe the result.
|
static <R> Try<R> |
success(R result)
Create a successful action.
|
public static Try<Void> of(CheckedRunnable runnable)
runnable - the runnablepublic static <R> Try<R> of(CheckedSupplier<R> supplier)
R - the type of the resultsupplier - the supplier of the resultpublic static <R> Try<R> success(R result)
R - the type of the resultresult - the resultpublic static <R> Try<R> failure(Throwable ex)
R - the type of the resultex - the exceptionpublic final void perform()
public boolean isSuccess()
public boolean isFailure()
public @Nullable Throwable getCause()
isFailure()public Try<T> subscribeFailure(Consumer<Throwable> subscriber)
subscriber - the subscriber of the resultIllegalStateException - if the computation has been performedpublic Try<T> subscribeSuccess(Consumer<T> subscriber)
subscriber - the subscriber of the resultIllegalStateException - if the computation has been performedpublic T get() throws Throwable
Throwable - the exception that may be thrown by the supplierpublic T getOrNull()
getOptional()public Optional<T> getOptional()
getOrNull()public T getOrElse(T defaultValue)
defaultValue - the default value to return if the supplier throws an exceptionpublic T getOrElseGet(Function<Throwable,T> function)
function - the function to return the default value if the supplier throws an exceptionpublic <X extends Throwable> T getOrElseThrow(Function<Throwable,X> function) throws X extends Throwable
X - the type of the exception to throwfunction - the function to throw an exception if the supplier throws an exceptionX - the exception thrown by the functionX extends Throwablepublic Try<T> getOrElseTry(CheckedFunction<Throwable,T> function)
function - the function to get the other instancepublic void run()
throws Throwable
Throwable - the exception that may be thrown by the supplierCopyright © 2024. All rights reserved.