Class SealedDispatcher.Builder<T,R>

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

public static final class SealedDispatcher.Builder<T,R> extends Object
Builder for SealedDispatcher SealedDispatcher 构建器

Registers handlers for each permitted subclass and validates exhaustiveness at build time.

为每个许可子类注册处理器,并在构建时验证穷举性。

Since:
JDK 25, opencode-base-reflect V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • on

      public <S extends T> SealedDispatcher.Builder<T,R> on(Class<S> subtype, Function<? super S, ? extends R> handler)
      Registers a handler for a specific subtype 为特定子类型注册处理器
      Type Parameters:
      S - the subtype | 子类型
      Parameters:
      subtype - the subtype class | 子类型类
      handler - the handler function | 处理函数
      Returns:
      this builder | 此构建器
      Throws:
      OpenReflectException - if subtype is not assignable from sealed type | 如果子类型不可从密封类型赋值
    • orElse

      public SealedDispatcher.Builder<T,R> orElse(Function<? super T, ? extends R> defaultHandler)
      Registers a default handler as fallback 注册默认处理器作为回退

      When a default handler is provided, exhaustiveness checking is skipped at build time.

      当提供默认处理器时,构建时跳过穷举性检查。

      Parameters:
      defaultHandler - the default handler | 默认处理器
      Returns:
      this builder | 此构建器
    • build

      public SealedDispatcher<T,R> build()
      Builds the dispatcher 构建分发器

      If no default handler is registered, validates that all concrete leaf classes in the sealed hierarchy have handlers. Throws OpenReflectException if any are missing.

      如果未注册默认处理器,验证密封层次结构中的所有具体叶类都有处理器。 如果有缺失则抛出 OpenReflectException

      Returns:
      the built dispatcher | 构建的分发器
      Throws:
      OpenReflectException - if exhaustiveness check fails | 如果穷举性检查失败