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:
  • 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

      default T getOrNull()
      Get the value or null if not present 获取值,如果不存在则返回null
      Returns:
      the value or null | 值或null
    • getOrThrow

      default T getOrThrow()
      Get the value or throw if not present 获取值,如果不存在则抛出异常
      Returns:
      the value | 值
      Throws:
      NoSuchElementException - if no value present | 如果没有值抛出异常
    • getOrThrow

      default <X extends Throwable> T getOrThrow(Supplier<? extends X> exceptionSupplier) throws X
      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

      default T getOrElse(T defaultValue)
      Get the value or a default value 获取值,如果不存在则返回默认值
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      the value or default | 值或默认值
    • getOrElseGet

      default T getOrElseGet(Supplier<? extends T> defaultSupplier)
      Get the value or compute a default value 获取值,如果不存在则计算默认值
      Parameters:
      defaultSupplier - the default value supplier | 默认值提供者
      Returns:
      the value or computed default | 值或计算的默认值
    • map

      default <U> TreeResult<U> map(Function<? super T, ? extends U> mapper)
      Transform the value if present 如果存在则转换值
      Type Parameters:
      U - the target type | 目标类型
      Parameters:
      mapper - the mapping function | 映射函数
      Returns:
      the transformed result | 转换后的结果
    • flatMap

      default <U> TreeResult<U> flatMap(Function<? super T, ? extends TreeResult<U>> mapper)
      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

      default TreeResult<T> recover(Function<? super String, ? extends T> recoveryFunction)
      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

      default TreeResult<T> onSuccess(Consumer<? super T> action)
      Execute action if success 如果成功则执行操作
      Parameters:
      action - the action to execute | 要执行的操作
      Returns:
      this result for chaining | 此结果用于链式调用
    • onFailure

      default TreeResult<T> onFailure(Consumer<? super String> action)
      Execute action if failure 如果失败则执行操作
      Parameters:
      action - the action to execute with error message | 要执行的操作,带错误消息
      Returns:
      this result for chaining | 此结果用于链式调用
    • onEmpty

      default TreeResult<T> onEmpty(Runnable action)
      Execute action if empty 如果为空则执行操作
      Parameters:
      action - the action to execute | 要执行的操作
      Returns:
      this result for chaining | 此结果用于链式调用
    • onValidation

      default TreeResult<T> onValidation(Consumer<? super List<TreeResult.Violation>> action)
      Execute action if validation failure 如果验证失败则执行操作
      Parameters:
      action - the action to execute with violations | 要执行的操作,带违规列表
      Returns:
      this result for chaining | 此结果用于链式调用
    • toOptional

      default Optional<T> toOptional()
      Convert to Optional 转换为Optional
      Returns:
      the Optional | Optional对象
    • 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

      static <T> TreeResult<T> success(T data)
      Create a success result 创建成功结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      data - the data | 数据
      Returns:
      the success result | 成功结果
    • failure

      static <T> TreeResult<T> failure(String message)
      Create a failure result 创建失败结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      message - the error message | 错误消息
      Returns:
      the failure result | 失败结果
    • failure

      static <T> TreeResult<T> failure(String message, Throwable cause)
      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

      static <T> TreeResult<T> empty()
      Create an empty result 创建空结果
      Type Parameters:
      T - the data type | 数据类型
      Returns:
      the empty result | 空结果
    • validation

      static <T> TreeResult<T> validation(List<TreeResult.Violation> violations)
      Create a validation result 创建验证结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      violations - the validation violations | 验证违规列表
      Returns:
      the validation result | 验证结果
    • validation

      static <T> TreeResult<T> validation(TreeResult.Violation violation)
      Create a validation result with single violation 创建单个违规的验证结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      violation - the validation violation | 验证违规
      Returns:
      the validation result | 验证结果
    • fromOptional

      static <T> TreeResult<T> fromOptional(Optional<T> optional)
      Create a result from Optional 从Optional创建结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      optional - the optional | Optional对象
      Returns:
      the result | 结果
    • fromNullable

      static <T> TreeResult<T> fromNullable(T value)
      Create a result from nullable value 从可空值创建结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      value - the nullable value | 可空值
      Returns:
      the result | 结果
    • fromCallable

      static <T> TreeResult<T> fromCallable(Callable<T> callable)
      Create a result from a callable 从Callable创建结果
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      callable - the callable | Callable对象
      Returns:
      the result | 结果