Class Case<T,R>
java.lang.Object
cloud.opencode.base.functional.pattern.Case<T,R>
- Type Parameters:
T- input type - 输入类型R- result type - 结果类型
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 TypeMethodDescriptionaction()Get the action 获取动作Try to apply this case to a value 尝试将此分支应用于值static <T,R> Case <T, R> Create a case for value equality matching 创建值相等匹配分支static <T,R> Case <T, R> Create a case for null matching 创建 null 匹配分支booleanCheck if this case matches the value 检查此分支是否匹配值static <T,U, R> Case <T, R> Create a case from a custom pattern 从自定义模式创建分支static <T,R> Case <T, R> Create a default case that always matches 创建始终匹配的默认分支pattern()Get the pattern 获取模式static <T,U, R> Case <T, R> Create a case for type matching 创建类型匹配分支static <T,R> Case <T, R> Create a case for predicate matching 创建谓词匹配分支
-
Method Details
-
type
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
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
-
otherwise
-
of
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
-
matches
Check if this case matches the value 检查此分支是否匹配值- Parameters:
value- value to check - 要检查的值- Returns:
- true if matches - 如果匹配返回 true
-
pattern
-
action
-