Class TaskResult<T>
java.lang.Object
cloud.opencode.base.parallel.structured.TaskResult<T>
- Type Parameters:
T- the result type - 结果类型
Task Result - Structured Task Result Wrapper
任务结果 - 结构化任务结果包装器
Represents the result of a structured task execution, containing either a success value or a failure exception.
表示结构化任务执行的结果,包含成功值或失败异常。
Example | 示例:
TaskResult<String> result = TaskResult.success("data");
result.ifSuccess(data -> process(data))
.ifFailure(ex -> log.error("Failed", ex));
String value = result.getOrDefault("fallback");
Features | 主要功能:
- Success/Failure/Cancelled states - 成功/失败/取消状态
- Functional transformation (map, flatMap, recover) - 函数式转换
- Side effect handling (ifSuccess, ifFailure) - 副作用处理
- Optional and default value access - Optional和默认值访问
Security | 安全性:
- Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
- Since:
- JDK 25, opencode-base-parallel V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TaskResult<T> Creates a cancelled result.booleanstatic <T> TaskResult<T> Creates a failed result.<R> TaskResult<R> flatMap(Function<T, TaskResult<R>> mapper) Flat maps the success value.get()Gets the value, throwing if failed.Gets the exception.Gets the exception as Optional.getOrDefault(T defaultValue) Gets the value or default.Gets the value or computes from exception.Gets the value or null.getState()Gets the state.inthashCode()ifCancelled(Runnable action) Executes action if cancelled.Executes action if failure.Executes action if success.booleanChecks if the task was cancelled.booleanChecks if the task failed.booleanChecks if the task succeeded.<R> TaskResult<R> Maps the success value.static <T> TaskResult<T> Creates a result from a callable.Recovers from failure.static <T> TaskResult<T> success(T value) Creates a successful result.Gets the value as Optional.toString()
-
Method Details
-
success
Creates a successful result. 创建成功结果。- Type Parameters:
T- the result type - 结果类型- Parameters:
value- the value - 值- Returns:
- the result - 结果
-
failure
Creates a failed result. 创建失败结果。- Type Parameters:
T- the result type - 结果类型- Parameters:
exception- the exception - 异常- Returns:
- the result - 结果
-
cancelled
Creates a cancelled result. 创建取消结果。- Type Parameters:
T- the result type - 结果类型- Returns:
- the result - 结果
-
of
Creates a result from a callable. 从 Callable 创建结果。- Type Parameters:
T- the result type - 结果类型- Parameters:
callable- the callable - Callable- Returns:
- the result - 结果
-
isSuccess
public boolean isSuccess()Checks if the task succeeded. 检查任务是否成功。- Returns:
- true if success - 如果成功返回 true
-
isFailure
public boolean isFailure()Checks if the task failed. 检查任务是否失败。- Returns:
- true if failure - 如果失败返回 true
-
isCancelled
public boolean isCancelled()Checks if the task was cancelled. 检查任务是否被取消。- Returns:
- true if cancelled - 如果取消返回 true
-
getState
-
get
Gets the value, throwing if failed. 获取值,如果失败则抛出异常。- Returns:
- the value - 值
- Throws:
IllegalStateException- if not success - 如果不是成功状态
-
getOrNull
-
getOrDefault
-
getOrElse
-
toOptional
-
getException
-
getExceptionOptional
-
map
Maps the success value. 映射成功值。- Type Parameters:
R- the result type - 结果类型- Parameters:
mapper- the mapper function - 映射函数- Returns:
- the mapped result - 映射结果
-
flatMap
Flat maps the success value. 扁平映射成功值。- Type Parameters:
R- the result type - 结果类型- Parameters:
mapper- the mapper function - 映射函数- Returns:
- the flat mapped result - 扁平映射结果
-
recover
Recovers from failure. 从失败中恢复。- Parameters:
recovery- the recovery function - 恢复函数- Returns:
- the recovered result - 恢复结果
-
ifSuccess
Executes action if success. 如果成功执行动作。- Parameters:
action- the action - 动作- Returns:
- this result - 此结果
-
ifFailure
Executes action if failure. 如果失败执行动作。- Parameters:
action- the action - 动作- Returns:
- this result - 此结果
-
ifCancelled
Executes action if cancelled. 如果取消执行动作。- Parameters:
action- the action - 动作- Returns:
- this result - 此结果
-
equals
-
hashCode
-
toString
-