Class OpenMatch.Matcher<T>

java.lang.Object
cloud.opencode.base.functional.pattern.OpenMatch.Matcher<T>
Type Parameters:
T - value type - 值类型
Enclosing class:
OpenMatch

public static final class OpenMatch.Matcher<T> extends Object
Matcher - Fluent pattern matching builder Matcher - 流式模式匹配构建器
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
  • Method Details

    • caseOf

      public <U,R> OpenMatch.Matcher<T> caseOf(Class<U> type, Function<? super U, R> action)
      Match by type 按类型匹配
      Type Parameters:
      U - matched type - 匹配的类型
      R - result type - 结果类型
      Parameters:
      type - type to match - 要匹配的类型
      action - action to execute - 要执行的动作
      Returns:
      this Matcher for chaining
    • when

      public <R> OpenMatch.Matcher<T> when(Predicate<? super T> predicate, Function<? super T, R> action)
      Match by predicate 按谓词匹配
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      predicate - condition to match - 匹配条件
      action - action to execute - 要执行的动作
      Returns:
      this Matcher for chaining
    • whenEquals

      public <R> OpenMatch.Matcher<T> whenEquals(T expected, Function<? super T, R> action)
      Match by value equality 按值相等匹配
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      expected - expected value - 期望值
      action - action to execute - 要执行的动作
      Returns:
      this Matcher for chaining
    • whenNull

      public <R> OpenMatch.Matcher<T> whenNull(Function<? super T, R> action)
      Match null value 匹配 null 值
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      action - action to execute - 要执行的动作
      Returns:
      this Matcher for chaining
    • caseRecord

      public <R1,R2,Out> OpenMatch.Matcher<T> caseRecord(Class<? extends Record> recordType, BiFunction<R1,R2,Out> action)
      Match record and deconstruct (2 components) 匹配 Record 并解构(2 个组件)
      Type Parameters:
      R1 - first component type - 第一个组件类型
      R2 - second component type - 第二个组件类型
      Out - result type - 结果类型
      Parameters:
      recordType - record type to match - 要匹配的 Record 类型
      action - action receiving deconstructed components - 接收解构组件的动作
      Returns:
      this Matcher for chaining
    • caseSealed

      public <S,R> OpenMatch.Matcher<T> caseSealed(Class<S> sealedType, Function<S,R> action)
      Match sealed type 匹配密封类型
      Type Parameters:
      S - sealed type - 密封类型
      R - result type - 结果类型
      Parameters:
      sealedType - sealed type to match - 要匹配的密封类型
      action - action to execute - 要执行的动作
      Returns:
      this Matcher for chaining
    • match

      public <R> OpenMatch.Matcher<T> match(Case<T,R> matchCase)
      Apply a Case 应用一个 Case
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      matchCase - case to apply - 要应用的分支
      Returns:
      this Matcher for chaining
    • match

      public <U,R> OpenMatch.Matcher<T> match(Pattern<T,U> pattern, Function<? super U, R> action)
      Match using a Pattern with action 使用 Pattern 进行匹配并执行动作

      Example | 示例:

      Pattern<Object, String> stringPattern = Pattern.type(String.class);
      String result = OpenMatch.of(value)
          .match(stringPattern, s -> "String: " + s)
          .orElse(o -> "Unknown");
      
      Type Parameters:
      U - pattern result type - 模式结果类型
      R - result type - 结果类型
      Parameters:
      pattern - pattern to match - 要匹配的模式
      action - action to execute on match - 匹配时执行的动作
      Returns:
      this Matcher for chaining
    • orElse

      public <R> R orElse(Function<? super T, R> defaultAction)
      Get result or default if no match 获取结果或默认值(如果没有匹配)
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      defaultAction - default action - 默认动作
      Returns:
      result or default
    • orElseGet

      public <R> R orElseGet(R defaultValue)
      Get result or default value if no match 获取结果或默认值(如果没有匹配)
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      defaultValue - default value - 默认值
      Returns:
      result or default
    • orElseThrow

      public <R> R orElseThrow()
      Get result or throw if no match 获取结果或抛出异常(如果没有匹配)
      Type Parameters:
      R - result type - 结果类型
      Returns:
      result
      Throws:
      OpenMatchException - if no match
    • getAs

      public <R> R getAs(Class<R> resultType, R defaultValue)
      Get result with type checking 获取结果并进行类型检查

      This is the type-safe version of orElseGet/orElse. Use this when you want to ensure the result is of the expected type at runtime.

      这是 orElseGet/orElse 的类型安全版本。当您想在运行时确保结果是预期类型时使用。

      Type Parameters:
      R - result type - 结果类型
      Parameters:
      resultType - expected result type - 期望的结果类型
      defaultValue - default value if no match or wrong type - 无匹配或类型错误时的默认值
      Returns:
      result or default value
    • getAs

      public <R> R getAs(Class<R> resultType)
      Get result with type checking, throwing if wrong type 获取结果并进行类型检查,类型错误时抛出异常
      Type Parameters:
      R - result type - 结果类型
      Parameters:
      resultType - expected result type - 期望的结果类型
      Returns:
      result
      Throws:
      OpenMatchException - if no match or wrong type
    • isMatched

      public boolean isMatched()
      Check if a match was found 检查是否找到匹配
      Returns:
      true if matched - 如果匹配返回 true
    • value

      public T value()
      Get the original value 获取原始值
      Returns:
      the value being matched - 被匹配的值