Class PropertyPlaceholder

java.lang.Object
cloud.opencode.base.yml.placeholder.PropertyPlaceholder

public final class PropertyPlaceholder extends Object
Property Placeholder - Represents a parsed placeholder 属性占位符 - 表示解析后的占位符

This class represents a parsed placeholder with key and optional default value.

此类表示解析后的占位符,包含键和可选的默认值。

Features | 主要功能:

  • Parse ${key} and ${key:default} expressions - 解析 ${key} 和 ${key:default} 表达式
  • Custom delimiter support - 自定义分隔符支持
  • Optional default value handling - 可选默认值处理

Usage Examples | 使用示例:

// Parse placeholder
PropertyPlaceholder ph = PropertyPlaceholder.parse("${server.port:8080}");
String key = ph.getKey();          // "server.port"
String def = ph.getDefaultValue(); // "8080"

// Check if has default
if (ph.hasDefaultValue()) {
    // use default
}

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构建后不可变)
  • Null-safe: No (null expression throws NullPointerException) - 空值安全: 否(空表达式抛出 NullPointerException)
Since:
JDK 25, opencode-base-yml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • parse

      public static PropertyPlaceholder parse(String expression)
      Parses a placeholder expression. 解析占位符表达式。
      Parameters:
      expression - the expression (e.g., "${key}" or "${key:default}") | 表达式
      Returns:
      the parsed placeholder | 解析后的占位符
      Throws:
      IllegalArgumentException - if expression is invalid | 如果表达式无效
    • parse

      public static PropertyPlaceholder parse(String expression, String prefix, String suffix, String separator)
      Parses a placeholder expression with custom delimiters. 使用自定义分隔符解析占位符表达式。
      Parameters:
      expression - the expression | 表达式
      prefix - the placeholder prefix | 占位符前缀
      suffix - the placeholder suffix | 占位符后缀
      separator - the default value separator | 默认值分隔符
      Returns:
      the parsed placeholder | 解析后的占位符
      Throws:
      IllegalArgumentException - if expression is invalid | 如果表达式无效
    • of

      public static PropertyPlaceholder of(String key)
      Creates a placeholder with key only. 仅使用键创建占位符。
      Parameters:
      key - the key | 键
      Returns:
      the placeholder | 占位符
    • of

      public static PropertyPlaceholder of(String key, String defaultValue)
      Creates a placeholder with key and default value. 使用键和默认值创建占位符。
      Parameters:
      key - the key | 键
      defaultValue - the default value | 默认值
      Returns:
      the placeholder | 占位符
    • getKey

      public String getKey()
      Gets the placeholder key. 获取占位符键。
      Returns:
      the key | 键
    • getDefaultValue

      public String getDefaultValue()
      Gets the default value. 获取默认值。
      Returns:
      the default value, or null if none | 默认值,如果没有则返回 null
    • defaultValue

      public Optional<String> defaultValue()
      Gets the default value as Optional. 获取默认值作为 Optional。
      Returns:
      optional containing default value | 包含默认值的 Optional
    • hasDefaultValue

      public boolean hasDefaultValue()
      Checks if this placeholder has a default value. 检查此占位符是否有默认值。
      Returns:
      true if has default value | 如果有默认值则返回 true
    • getRawExpression

      public String getRawExpression()
      Gets the raw expression. 获取原始表达式。
      Returns:
      the raw expression | 原始表达式
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object