Class SealedDispatcher<T,R>

java.lang.Object
cloud.opencode.base.reflect.sealed.SealedDispatcher<T,R>
Type Parameters:
T - the sealed type | 密封类型
R - the result type | 结果类型

public final class SealedDispatcher<T,R> extends Object
Type-Safe Sealed Class Dispatcher 类型安全的密封类分发器

Provides exhaustive, type-safe dispatch over sealed class hierarchies, similar to pattern matching but with compile-time handler registration and runtime exhaustiveness validation.

提供密封类层次结构上的穷举式、类型安全分发, 类似于模式匹配,但具有编译时处理器注册和运行时穷举性验证。

Features | 主要功能:

  • Exhaustive dispatch validation - 穷举分发验证
  • Multi-level sealed hierarchy support - 多层密封层次结构支持
  • Safe dispatch with Optional return - 安全分发(返回Optional)
  • Default handler fallback - 默认处理器回退

Usage Examples | 使用示例:

sealed interface Shape permits Circle, Rectangle {}
record Circle(double radius) implements Shape {}
record Rectangle(double w, double h) implements Shape {}

var dispatcher = SealedDispatcher.builder(Shape.class, Double.class)
    .on(Circle.class, c -> Math.PI * c.radius() * c.radius())
    .on(Rectangle.class, r -> r.w() * r.h())
    .build();

double area = dispatcher.dispatch(new Circle(5.0)); // 78.54...

Security | 安全性:

  • Thread-safe: Yes (immutable after build) - 线程安全: 是(构建后不可变)
  • Null-safe: No (dispatch throws NullPointerException for null input) - 空值安全: 否(null输入抛出NullPointerException)

Performance | 性能特性:

  • Time complexity: O(h) per dispatch where h is the hierarchy depth - 时间复杂度: 每次分发 O(h),h为层次结构深度
  • Space complexity: O(n) where n is the number of handlers - 空间复杂度: O(n),n为处理器数量
Since:
JDK 25, opencode-base-reflect V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static <T,R> SealedDispatcher.Builder<T,R> builder(Class<T> sealedType, Class<R> resultType)
      Creates a new builder for the given sealed type and result type 为给定的密封类型和结果类型创建新的构建器
      Type Parameters:
      T - the sealed type | 密封类型
      R - the result type | 结果类型
      Parameters:
      sealedType - the sealed class | 密封类
      resultType - the result class | 结果类
      Returns:
      a new builder | 新的构建器
      Throws:
      OpenReflectException - if sealedType is not sealed | 如果sealedType不是密封类
    • dispatch

      public R dispatch(T value)
      Dispatches the value to the matching handler 将值分发到匹配的处理器

      Looks up the handler by the runtime class of the value. For multi-level sealed hierarchies, walks up the class hierarchy checking supertypes if no direct handler is found.

      通过值的运行时类查找处理器。 对于多层密封层次结构,如果没有直接匹配的处理器,会向上遍历类层次结构。

      Parameters:
      value - the value to dispatch | 要分发的值
      Returns:
      the result | 结果
      Throws:
      NullPointerException - if value is null | 如果值为null
      OpenReflectException - if no handler matches | 如果没有匹配的处理器
    • dispatchSafe

      public Optional<R> dispatchSafe(T value)
      Dispatches the value safely, returning an Optional 安全分发值,返回Optional

      Similar to dispatch(Object) but returns Optional.empty() instead of throwing when no handler matches.

      dispatch(Object) 类似,但在没有匹配处理器时返回 Optional.empty() 而不是抛出异常。

      Parameters:
      value - the value to dispatch | 要分发的值
      Returns:
      Optional containing the result, or empty | 包含结果的Optional,或空
      Throws:
      NullPointerException - if value is null | 如果值为null