Class Case<T,R>

java.lang.Object
cloud.opencode.base.functional.pattern.Case<T,R>
Type Parameters:
T - input type - 输入类型
R - result type - 结果类型

public final class Case<T,R> extends Object
Case - Match case definition combining pattern and action Case - 组合模式和动作的匹配分支定义

Represents a single case in pattern matching, consisting of a pattern to match against and an action to execute on match.

表示模式匹配中的单个分支,由要匹配的模式和匹配时执行的动作组成。

Features | 主要功能:

  • Pattern + action bundling - 模式 + 动作绑定
  • Type-safe case creation - 类型安全的分支创建
  • Factory methods for common cases - 常见分支的工厂方法

Usage Examples | 使用示例:

// Type case
Case<Object, String> stringCase = Case.type(String.class, s -> "String: " + s);

// Predicate case
Case<Integer, String> positive = Case.when(n -> n > 0, n -> "Positive: " + n);

// Default case
Case<Object, String> defaultCase = Case.otherwise(o -> "Unknown: " + o);

// Apply case
Optional<String> result = stringCase.apply("hello");

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是 (不可变)
  • Null-safe: Handles null input - 空值安全: 处理 null 输入
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Function<? super T, ? extends R>
    Get the action 获取动作
    apply(T value)
    Try to apply this case to a value 尝试将此分支应用于值
    static <T,R> Case<T,R>
    equals(T expected, Function<? super T, ? extends R> action)
    Create a case for value equality matching 创建值相等匹配分支
    static <T,R> Case<T,R>
    isNull(Function<? super T, ? extends R> action)
    Create a case for null matching 创建 null 匹配分支
    boolean
    matches(T value)
    Check if this case matches the value 检查此分支是否匹配值
    static <T,U,R> Case<T,R>
    of(Pattern<T,U> pattern, Function<? super U, ? extends R> action)
    Create a case from a custom pattern 从自定义模式创建分支
    static <T,R> Case<T,R>
    otherwise(Function<? super T, ? extends R> action)
    Create a default case that always matches 创建始终匹配的默认分支
    Get the pattern 获取模式
    static <T,U,R> Case<T,R>
    type(Class<U> type, Function<? super U, ? extends R> action)
    Create a case for type matching 创建类型匹配分支
    static <T,R> Case<T,R>
    when(Predicate<? super T> predicate, Function<? super T, ? extends R> action)
    Create a case for predicate matching 创建谓词匹配分支

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • type

      public static <T,U,R> Case<T,R> type(Class<U> type, Function<? super U, ? extends R> action)
      Create a case for type matching 创建类型匹配分支
      Type Parameters:
      T - input type - 输入类型
      U - matched type - 匹配的类型
      R - result type - 结果类型
      Parameters:
      type - type to match - 要匹配的类型
      action - action to execute - 要执行的动作
      Returns:
      type case
    • when

      public static <T,R> Case<T,R> when(Predicate<? super T> predicate, Function<? super T, ? extends R> action)
      Create a case for predicate matching 创建谓词匹配分支
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      predicate - condition to match - 匹配条件
      action - action to execute - 要执行的动作
      Returns:
      predicate case
    • equals

      public static <T,R> Case<T,R> equals(T expected, Function<? super T, ? extends R> action)
      Create a case for value equality matching 创建值相等匹配分支
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      expected - expected value - 期望值
      action - action to execute - 要执行的动作
      Returns:
      equality case
    • isNull

      public static <T,R> Case<T,R> isNull(Function<? super T, ? extends R> action)
      Create a case for null matching 创建 null 匹配分支
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      action - action to execute - 要执行的动作
      Returns:
      null case
    • otherwise

      public static <T,R> Case<T,R> otherwise(Function<? super T, ? extends R> action)
      Create a default case that always matches 创建始终匹配的默认分支
      Type Parameters:
      T - input type - 输入类型
      R - result type - 结果类型
      Parameters:
      action - action to execute - 要执行的动作
      Returns:
      default case
    • of

      public static <T,U,R> Case<T,R> of(Pattern<T,U> pattern, Function<? super U, ? extends R> action)
      Create a case from a custom pattern 从自定义模式创建分支
      Type Parameters:
      T - input type - 输入类型
      U - pattern result type - 模式结果类型
      R - result type - 结果类型
      Parameters:
      pattern - custom pattern - 自定义模式
      action - action to execute - 要执行的动作
      Returns:
      custom case
    • apply

      public Optional<R> apply(T value)
      Try to apply this case to a value 尝试将此分支应用于值
      Parameters:
      value - value to match - 要匹配的值
      Returns:
      Optional containing result if matched
    • matches

      public boolean matches(T value)
      Check if this case matches the value 检查此分支是否匹配值
      Parameters:
      value - value to check - 要检查的值
      Returns:
      true if matches - 如果匹配返回 true
    • pattern

      public Pattern<T,?> pattern()
      Get the pattern 获取模式
      Returns:
      the pattern - 模式
    • action

      public Function<? super T, ? extends R> action()
      Get the action 获取动作
      Returns:
      the action - 动作