Class OpenMatch.Matcher<T>
java.lang.Object
cloud.opencode.base.functional.pattern.OpenMatch.Matcher<T>
- Type Parameters:
T- value type - 值类型
- Enclosing class:
OpenMatch
Matcher - Fluent pattern matching builder
Matcher - 流式模式匹配构建器
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
-
Method Summary
Modifier and TypeMethodDescription<U,R> OpenMatch.Matcher <T> Match by type 按类型匹配<R1,R2, Out>
OpenMatch.Matcher<T> caseRecord(Class<? extends Record> recordType, BiFunction<R1, R2, Out> action) Match record and deconstruct (2 components) 匹配 Record 并解构(2 个组件)<S,R> OpenMatch.Matcher <T> caseSealed(Class<S> sealedType, Function<S, R> action) Match sealed type 匹配密封类型<R> RGet result with type checking, throwing if wrong type 获取结果并进行类型检查,类型错误时抛出异常<R> RGet result with type checking 获取结果并进行类型检查booleanCheck if a match was found 检查是否找到匹配<R> OpenMatch.Matcher<T> Apply a Case 应用一个 Case<U,R> OpenMatch.Matcher <T> Match using a Pattern with action 使用 Pattern 进行匹配并执行动作<R> RGet result or default if no match 获取结果或默认值(如果没有匹配)<R> RorElseGet(R defaultValue) Get result or default value if no match 获取结果或默认值(如果没有匹配)<R> RGet result or throw if no match 获取结果或抛出异常(如果没有匹配)value()Get the original value 获取原始值<R> OpenMatch.Matcher<T> Match by predicate 按谓词匹配<R> OpenMatch.Matcher<T> whenEquals(T expected, Function<? super T, R> action) Match by value equality 按值相等匹配<R> OpenMatch.Matcher<T> Match null value 匹配 null 值
-
Method Details
-
caseOf
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
-
whenEquals
Match by value equality 按值相等匹配- Type Parameters:
R- result type - 结果类型- Parameters:
expected- expected value - 期望值action- action to execute - 要执行的动作- Returns:
- this Matcher for chaining
-
whenNull
Match null value 匹配 null 值- Type Parameters:
R- result type - 结果类型- Parameters:
action- action to execute - 要执行的动作- Returns:
- this Matcher for chaining
-
caseRecord
public <R1,R2, OpenMatch.Matcher<T> caseRecordOut> (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
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
Apply a Case 应用一个 Case- Type Parameters:
R- result type - 结果类型- Parameters:
matchCase- case to apply - 要应用的分支- Returns:
- this Matcher for chaining
-
match
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
-
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
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
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
-