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.

@FunctionalInterface public interface Pattern<T,R>
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 Type
    Method
    Description
    default Pattern<T,R>
    and(Pattern<? super R, R> other)
    Combine 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 创建相等匹配模式
    default Pattern<T,R>
    filter(Predicate<? super R> predicate)
    Add a guard condition 添加守卫条件
    static <T> Pattern<T,T>
    Create a null matching pattern 创建 null 匹配模式
    default <U> Pattern<T,U>
    map(Function<? super R, ? extends U> mapper)
    Transform the matched result 转换匹配的结果
    match(T value)
    Try to match the value 尝试匹配值
    default boolean
    matches(T value)
    Check if pattern matches without extracting 检查是否匹配而不提取
    default Pattern<T,R>
    or(Pattern<T,R> other)
    Combine with another pattern (OR) 与另一个模式组合(OR)
    static <T,R> Pattern<T,R>
    type(Class<R> type)
    Create a type matching pattern 创建类型匹配模式
    static <T> Pattern<T,T>
    when(Predicate<T> predicate)
    Create a predicate matching pattern 创建谓词匹配模式
  • Method Details

    • match

      Optional<R> match(T value)
      Try to match the value 尝试匹配值
      Parameters:
      value - value to match - 要匹配的值
      Returns:
      Optional containing extracted value if matched, empty otherwise
    • matches

      default boolean matches(T value)
      Check if pattern matches without extracting 检查是否匹配而不提取
      Parameters:
      value - value to check - 要检查的值
      Returns:
      true if matches - 如果匹配返回 true
    • type

      static <T,R> Pattern<T,R> type(Class<R> type)
      Create a type matching pattern 创建类型匹配模式
      Type Parameters:
      T - input type - 输入类型
      R - target type - 目标类型
      Parameters:
      type - type to match - 要匹配的类型
      Returns:
      type pattern
    • equalTo

      static <T> Pattern<T,T> equalTo(T expected)
      Create an equality matching pattern 创建相等匹配模式
      Type Parameters:
      T - value type - 值类型
      Parameters:
      expected - expected value - 期望值
      Returns:
      equality pattern
    • when

      static <T> Pattern<T,T> when(Predicate<T> predicate)
      Create a predicate matching pattern 创建谓词匹配模式
      Type Parameters:
      T - value type - 值类型
      Parameters:
      predicate - condition to match - 匹配条件
      Returns:
      predicate pattern
    • isNull

      static <T> Pattern<T,T> isNull()
      Create a null matching pattern 创建 null 匹配模式
      Type Parameters:
      T - value type - 值类型
      Returns:
      null pattern
    • any

      static <T> Pattern<T,T> any()
      Create a pattern that always matches 创建始终匹配的模式
      Type Parameters:
      T - value type - 值类型
      Returns:
      wildcard pattern
    • and

      default Pattern<T,R> and(Pattern<? super R, R> other)
      Combine with another pattern (AND) 与另一个模式组合(AND)
      Parameters:
      other - other pattern - 其他模式
      Returns:
      combined pattern
    • or

      default Pattern<T,R> or(Pattern<T,R> other)
      Combine with another pattern (OR) 与另一个模式组合(OR)
      Parameters:
      other - alternative pattern - 备选模式
      Returns:
      combined pattern
    • map

      default <U> Pattern<T,U> map(Function<? super R, ? extends U> mapper)
      Transform the matched result 转换匹配的结果
      Type Parameters:
      U - result type - 结果类型
      Parameters:
      mapper - transformation function - 转换函数
      Returns:
      transformed pattern
    • filter

      default Pattern<T,R> filter(Predicate<? super R> predicate)
      Add a guard condition 添加守卫条件
      Parameters:
      predicate - guard condition - 守卫条件
      Returns:
      guarded pattern