Class PatternValidator

java.lang.Object
cloud.opencode.base.config.validation.PatternValidator
All Implemented Interfaces:
ConfigValidator

public class PatternValidator extends Object implements ConfigValidator
Pattern Validator - Validates configuration values match specified regex pattern 模式验证器 - 验证配置值匹配指定的正则表达式模式

Validates that configuration string values match a specified regular expression pattern. Useful for validating formats like emails, URLs, phone numbers, etc.

验证配置字符串值匹配指定的正则表达式模式。适用于验证如邮箱、URL、电话号码等格式。

Features | 主要功能:

  • Regex pattern matching - 正则表达式模式匹配
  • Pre-compiled patterns for performance - 预编译模式提高性能
  • Custom error messages - 自定义错误消息
  • Common pattern constants - 常用模式常量

Usage Examples | 使用示例:

// Email validation
PatternValidator emailValidator = new PatternValidator(
    "user.email",
    "^[A-Za-z0-9+_.-]+@(.+)$",
    "Invalid email format"
);
ValidationResult result = emailValidator.validate(config);

// URL validation with factory method
PatternValidator urlValidator = PatternValidator.url("website.url");
result = urlValidator.validate(config);

Common Patterns | 常用模式:

  • EMAIL_PATTERN - Email address validation - 邮箱地址验证
  • URL_PATTERN - HTTP/HTTPS URL validation - HTTP/HTTPS URL验证
  • IPV4_PATTERN - IPv4 address validation - IPv4地址验证
  • PHONE_PATTERN - Phone number validation - 电话号码验证

Performance | 性能特性:

  • Time complexity: O(n) where n is value length - 时间复杂度: O(n) n为值长度
  • Space complexity: O(1) - 空间复杂度: O(1)
  • Pattern compiled once at construction - 模式在构造时编译一次

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
  • Null-safe: Yes - 空值安全: 是
  • ReDoS protection: Use simple patterns - ReDoS保护: 使用简单模式
Since:
JDK 25, opencode-base-config V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

  • Constructor Details

    • PatternValidator

      public PatternValidator(String key, String regex)
      Create pattern validator with default error message 创建带默认错误消息的模式验证器
      Parameters:
      key - configuration key | 配置键
      regex - regular expression pattern | 正则表达式模式
      Throws:
      PatternSyntaxException - if regex is invalid | 如果正则表达式无效
    • PatternValidator

      public PatternValidator(String key, String regex, String errorMessage)
      Create pattern validator with custom error message 创建带自定义错误消息的模式验证器
      Parameters:
      key - configuration key | 配置键
      regex - regular expression pattern | 正则表达式模式
      errorMessage - custom error message | 自定义错误消息
      Throws:
      PatternSyntaxException - if regex is invalid | 如果正则表达式无效
    • PatternValidator

      public PatternValidator(String key, Pattern pattern, String errorMessage)
      Create pattern validator from pre-compiled Pattern 从预编译的Pattern创建模式验证器
      Parameters:
      key - configuration key | 配置键
      pattern - pre-compiled pattern | 预编译的模式
      errorMessage - custom error message | 自定义错误消息
  • Method Details

    • validate

      public ValidationResult validate(Config config)
      Description copied from interface: ConfigValidator
      Validate configuration 验证配置
      Specified by:
      validate in interface ConfigValidator
      Parameters:
      config - configuration to validate | 要验证的配置
      Returns:
      validation result | 验证结果
    • getPattern

      public Pattern getPattern()
      Get the compiled pattern 获取编译的模式
      Returns:
      compiled pattern | 编译的模式
    • getErrorMessage

      public String getErrorMessage()
      Get the error message 获取错误消息
      Returns:
      error message | 错误消息
    • email

      public static PatternValidator email(String key)
      Create email validator 创建邮箱验证器
      Parameters:
      key - configuration key | 配置键
      Returns:
      email pattern validator | 邮箱模式验证器
    • url

      public static PatternValidator url(String key)
      Create URL validator 创建URL验证器
      Parameters:
      key - configuration key | 配置键
      Returns:
      URL pattern validator | URL模式验证器
    • ipv4

      public static PatternValidator ipv4(String key)
      Create IPv4 validator 创建IPv4验证器
      Parameters:
      key - configuration key | 配置键
      Returns:
      IPv4 pattern validator | IPv4模式验证器
    • phone

      public static PatternValidator phone(String key)
      Create phone number validator 创建电话号码验证器
      Parameters:
      key - configuration key | 配置键
      Returns:
      phone pattern validator | 电话号码模式验证器