Interface TreeResult<T>
- Type Parameters:
T- the result data type | 结果数据类型
- All Known Implementing Classes:
TreeResult.Empty, TreeResult.Failure, TreeResult.Success, TreeResult.Validation
public sealed interface TreeResult<T>
permits TreeResult.Success<T>, TreeResult.Failure<T>, TreeResult.Empty<T>, TreeResult.Validation<T>
Tree Result
树操作结果
A sealed interface representing the result of tree operations. Supports pattern matching with switch expressions for exhaustive handling.
表示树操作结果的密封接口。支持使用switch表达式进行模式匹配以实现完整处理。
Usage Examples | 使用示例
// Pattern matching with switch
String message = switch (result) {
case TreeResult.Success(var data) -> "Found: " + data;
case TreeResult.Failure(var msg, var cause) -> "Error: " + msg;
case TreeResult.Empty() -> "No data found";
case TreeResult.Validation(var violations) -> "Invalid: " + violations;
};
// Functional style
result.map(node -> node.getData())
.onSuccess(data -> System.out.println(data))
.onFailure(error -> log.error(error));
Features | 主要功能:
- Sealed interface with pattern matching support - 密封接口支持模式匹配
- Success, Failure, Empty, and Validation variants - 成功、失败、空和验证变体
- Functional operations (map, flatMap, recover) - 函数式操作
- Violation tracking with severity levels - 带严重程度级别的违规跟踪
Security | 安全性:
- Thread-safe: Yes (immutable records) - 是(不可变记录)
- Null-safe: Partial (data can be null in Success) - 部分(Success中数据可为null)
- Since:
- JDK 25, opencode-base-tree V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordEmpty Result 空结果static final recordFailure Result 失败结果static final recordSuccess Result 成功结果static final recordValidation Result 验证结果static final recordViolation 违规 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TreeResult<T> empty()Create an empty result 创建空结果static <T> TreeResult<T> Create a failure result 创建失败结果static <T> TreeResult<T> Create a failure result with cause 创建带原因的失败结果default <U> TreeResult<U> flatMap(Function<? super T, ? extends TreeResult<U>> mapper) Transform the value with a function that returns a TreeResult 使用返回TreeResult的函数转换值default <U> Ufold(Function<? super T, ? extends U> onSuccess, Function<? super String, ? extends U> onFailure, Supplier<? extends U> onEmpty, Function<? super List<TreeResult.Violation>, ? extends U> onValidation) Fold the result into a single value 将结果折叠为单个值static <T> TreeResult<T> fromCallable(Callable<T> callable) Create a result from a callable 从Callable创建结果static <T> TreeResult<T> fromNullable(T value) Create a result from nullable value 从可空值创建结果static <T> TreeResult<T> fromOptional(Optional<T> optional) Create a result from Optional 从Optional创建结果default TGet the value or a default value 获取值,如果不存在则返回默认值default TgetOrElseGet(Supplier<? extends T> defaultSupplier) Get the value or compute a default value 获取值,如果不存在则计算默认值default TGet the value or null if not present 获取值,如果不存在则返回nulldefault TGet the value or throw if not present 获取值,如果不存在则抛出异常getOrThrow(Supplier<? extends X> exceptionSupplier) Get the value or throw a custom exception 获取值,如果不存在则抛出自定义异常default booleanisEmpty()Check if this result represents an empty result 检查此结果是否表示空结果default booleanisFailed()Check if this result represents a failure 检查此结果是否表示失败default booleanCheck if this result represents a success 检查此结果是否表示成功default booleanCheck if this result represents a validation failure 检查此结果是否表示验证失败default <U> TreeResult<U> Transform the value if present 如果存在则转换值default TreeResult<T> Execute action if empty 如果为空则执行操作default TreeResult<T> Execute action if failure 如果失败则执行操作default TreeResult<T> Execute action if success 如果成功则执行操作default TreeResult<T> onValidation(Consumer<? super List<TreeResult.Violation>> action) Execute action if validation failure 如果验证失败则执行操作default TreeResult<T> Recover from failure with an alternative value 从失败中恢复为替代值default TreeResult<T> recoverWith(Function<? super String, ? extends TreeResult<T>> recoveryFunction) Recover from failure with an alternative TreeResult 从失败中恢复为替代TreeResultstatic <T> TreeResult<T> success(T data) Create a success result 创建成功结果Convert to Optional 转换为Optionalstatic <T> TreeResult<T> validation(TreeResult.Violation violation) Create a validation result with single violation 创建单个违规的验证结果static <T> TreeResult<T> validation(List<TreeResult.Violation> violations) Create a validation result 创建验证结果
-
Method Details
-
isSuccess
default boolean isSuccess()Check if this result represents a success 检查此结果是否表示成功- Returns:
- true if success | 如果成功返回true
-
isFailed
default boolean isFailed()Check if this result represents a failure 检查此结果是否表示失败- Returns:
- true if failure | 如果失败返回true
-
isEmpty
default boolean isEmpty()Check if this result represents an empty result 检查此结果是否表示空结果- Returns:
- true if empty | 如果为空返回true
-
isValidation
default boolean isValidation()Check if this result represents a validation failure 检查此结果是否表示验证失败- Returns:
- true if validation failure | 如果验证失败返回true
-
getOrNull
Get the value or null if not present 获取值,如果不存在则返回null- Returns:
- the value or null | 值或null
-
getOrThrow
Get the value or throw if not present 获取值,如果不存在则抛出异常- Returns:
- the value | 值
- Throws:
NoSuchElementException- if no value present | 如果没有值抛出异常
-
getOrThrow
Get the value or throw a custom exception 获取值,如果不存在则抛出自定义异常- Type Parameters:
X- the exception type | 异常类型- Parameters:
exceptionSupplier- the exception supplier | 异常提供者- Returns:
- the value | 值
- Throws:
X- if no value present | 如果没有值抛出异常
-
getOrElse
-
getOrElseGet
-
map
Transform the value if present 如果存在则转换值- Type Parameters:
U- the target type | 目标类型- Parameters:
mapper- the mapping function | 映射函数- Returns:
- the transformed result | 转换后的结果
-
flatMap
Transform the value with a function that returns a TreeResult 使用返回TreeResult的函数转换值- Type Parameters:
U- the target type | 目标类型- Parameters:
mapper- the mapping function | 映射函数- Returns:
- the transformed result | 转换后的结果
-
recover
Recover from failure with an alternative value 从失败中恢复为替代值- Parameters:
recoveryFunction- the recovery function | 恢复函数- Returns:
- the recovered result | 恢复后的结果
-
recoverWith
default TreeResult<T> recoverWith(Function<? super String, ? extends TreeResult<T>> recoveryFunction) Recover from failure with an alternative TreeResult 从失败中恢复为替代TreeResult- Parameters:
recoveryFunction- the recovery function | 恢复函数- Returns:
- the recovered result | 恢复后的结果
-
onSuccess
Execute action if success 如果成功则执行操作- Parameters:
action- the action to execute | 要执行的操作- Returns:
- this result for chaining | 此结果用于链式调用
-
onFailure
Execute action if failure 如果失败则执行操作- Parameters:
action- the action to execute with error message | 要执行的操作,带错误消息- Returns:
- this result for chaining | 此结果用于链式调用
-
onEmpty
Execute action if empty 如果为空则执行操作- Parameters:
action- the action to execute | 要执行的操作- Returns:
- this result for chaining | 此结果用于链式调用
-
onValidation
Execute action if validation failure 如果验证失败则执行操作- Parameters:
action- the action to execute with violations | 要执行的操作,带违规列表- Returns:
- this result for chaining | 此结果用于链式调用
-
toOptional
-
fold
default <U> U fold(Function<? super T, ? extends U> onSuccess, Function<? super String, ? extends U> onFailure, Supplier<? extends U> onEmpty, Function<? super List<TreeResult.Violation>, ? extends U> onValidation) Fold the result into a single value 将结果折叠为单个值- Type Parameters:
U- the result type | 结果类型- Parameters:
onSuccess- function to apply on success | 成功时应用的函数onFailure- function to apply on failure | 失败时应用的函数onEmpty- supplier for empty case | 空情况的提供者onValidation- function to apply on validation failure | 验证失败时应用的函数- Returns:
- the folded value | 折叠后的值
-
success
Create a success result 创建成功结果- Type Parameters:
T- the data type | 数据类型- Parameters:
data- the data | 数据- Returns:
- the success result | 成功结果
-
failure
Create a failure result 创建失败结果- Type Parameters:
T- the data type | 数据类型- Parameters:
message- the error message | 错误消息- Returns:
- the failure result | 失败结果
-
failure
Create a failure result with cause 创建带原因的失败结果- Type Parameters:
T- the data type | 数据类型- Parameters:
message- the error message | 错误消息cause- the cause | 原因- Returns:
- the failure result | 失败结果
-
empty
Create an empty result 创建空结果- Type Parameters:
T- the data type | 数据类型- Returns:
- the empty result | 空结果
-
validation
Create a validation result 创建验证结果- Type Parameters:
T- the data type | 数据类型- Parameters:
violations- the validation violations | 验证违规列表- Returns:
- the validation result | 验证结果
-
validation
Create a validation result with single violation 创建单个违规的验证结果- Type Parameters:
T- the data type | 数据类型- Parameters:
violation- the validation violation | 验证违规- Returns:
- the validation result | 验证结果
-
fromOptional
Create a result from Optional 从Optional创建结果- Type Parameters:
T- the data type | 数据类型- Parameters:
optional- the optional | Optional对象- Returns:
- the result | 结果
-
fromNullable
Create a result from nullable value 从可空值创建结果- Type Parameters:
T- the data type | 数据类型- Parameters:
value- the nullable value | 可空值- Returns:
- the result | 结果
-
fromCallable
Create a result from a callable 从Callable创建结果- Type Parameters:
T- the data type | 数据类型- Parameters:
callable- the callable | Callable对象- Returns:
- the result | 结果
-