Interface Pattern<T,R>
- Type Parameters:
T- input type to match - 要匹配的输入类型R- extracted result type - 提取的结果类型
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Pattern - Pattern interface for matching values
Pattern - 用于匹配值的模式接口
Defines the contract for patterns that can match against values and extract results.
定义可以匹配值并提取结果的模式契约。
Features | 主要功能:
- Type-safe pattern matching - 类型安全的模式匹配
- Composable patterns - 可组合的模式
- Works with OpenMatch - 与 OpenMatch 配合使用
Usage Examples | 使用示例:
// Type pattern
Pattern<Object, String> stringPattern = Pattern.type(String.class);
// Predicate pattern
Pattern<Integer, Integer> positive = Pattern.when(n -> n > 0);
// Value pattern
Pattern<String, String> hello = Pattern.equals("hello");
// Using with OpenMatch
OpenMatch.of(value)
.match(stringPattern, s -> "String: " + s)
.match(positive, n -> "Positive: " + n);
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Patterns handle null - 空值安全: 模式处理 null
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionCombine with another pattern (AND) 与另一个模式组合(AND)static <T> Pattern<T, T> any()Create a pattern that always matches 创建始终匹配的模式static <T> Pattern<T, T> equalTo(T expected) Create an equality matching pattern 创建相等匹配模式Add a guard condition 添加守卫条件static <T> Pattern<T, T> isNull()Create a null matching pattern 创建 null 匹配模式Transform the matched result 转换匹配的结果Try to match the value 尝试匹配值default booleanCheck if pattern matches without extracting 检查是否匹配而不提取Combine with another pattern (OR) 与另一个模式组合(OR)static <T,R> Pattern <T, R> Create a type matching pattern 创建类型匹配模式static <T> Pattern<T, T> Create a predicate matching pattern 创建谓词匹配模式
-
Method Details
-
match
-
matches
Check if pattern matches without extracting 检查是否匹配而不提取- Parameters:
value- value to check - 要检查的值- Returns:
- true if matches - 如果匹配返回 true
-
type
-
equalTo
Create an equality matching pattern 创建相等匹配模式- Type Parameters:
T- value type - 值类型- Parameters:
expected- expected value - 期望值- Returns:
- equality pattern
-
when
-
isNull
Create a null matching pattern 创建 null 匹配模式- Type Parameters:
T- value type - 值类型- Returns:
- null pattern
-
any
Create a pattern that always matches 创建始终匹配的模式- Type Parameters:
T- value type - 值类型- Returns:
- wildcard pattern
-
and
-
or
-
map
-
filter
-