Interface Config

All Known Implementing Classes:
ContextAwareConfig

public interface Config
Unified Configuration Access Interface 统一配置访问接口

This interface provides type-safe configuration access with support for multiple sources, type conversion, placeholder resolution, and hot reloading.

此接口提供类型安全的配置访问,支持多配置源、类型转换、占位符解析和热更新。

Features | 主要功能:

  • Type-safe configuration retrieval - 类型安全的配置检索
  • Multiple configuration sources with priority - 支持优先级的多配置源
  • Placeholder resolution ${key} - 占位符解析${key}
  • Configuration change listeners - 配置变更监听
  • Sub-configuration by prefix - 按前缀获取子配置
  • Configuration binding to POJOs/Records - 配置绑定到POJO/Record

Usage Examples | 使用示例:

// Basic retrieval
String url = config.getString("database.url");
int port = config.getInt("server.port", 8080);
Duration timeout = config.getDuration("http.timeout");

// Type-safe retrieval
LocalDate date = config.get("start.date", LocalDate.class);
List<String> hosts = config.getList("redis.hosts", String.class);

// Optional retrieval
Optional<String> apiKey = config.getOptional("api.key");

// Sub-configuration
Config dbConfig = config.getSubConfig("database");
String dbUrl = dbConfig.getString("url"); // reads database.url

// Configuration binding
DatabaseConfig dbCfg = config.bind("database", DatabaseConfig.class);

// Change listeners
config.addListener("log.level", event -> {
    System.out.println("Log level changed: " + event.newValue());
});

Performance | 性能特性:

  • Time complexity: O(1) for direct key access - 时间复杂度: 直接键访问为O(1)
  • Type conversion cached - 类型转换结果缓存
  • Immutable snapshots for thread safety - 不可变快照保证线程安全

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
  • Immutable interface - 不可变接口
Since:
JDK 25, opencode-base-config V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Add configuration change listener 添加配置变更监听器
    void
    Add listener for specific configuration key 添加指定键的监听器
    <T> T
    bind(String prefix, Class<T> type)
    Bind configuration to POJO/Record 绑定配置到POJO/Record
    <T> void
    bindTo(String prefix, T target)
    Bind configuration to existing object 绑定配置到现有对象
    <T> T
    get(String key, Class<T> type)
    Get typed configuration value 获取指定类型配置值
    <T> T
    get(String key, Class<T> type, T defaultValue)
    Get typed configuration value with default 获取指定类型配置值(带默认值)
    boolean
    Get boolean configuration value 获取布尔配置值
    boolean
    getBoolean(String key, boolean defaultValue)
    Get boolean configuration value with default 获取布尔配置值(带默认值)
    Get all configurations with specified prefix 获取指定前缀的所有配置
    double
    Get double configuration value 获取双精度配置值
    double
    getDouble(String key, double defaultValue)
    Get double configuration value with default 获取双精度配置值(带默认值)
    Get Duration configuration value 获取Duration配置值
    getDuration(String key, Duration defaultValue)
    Get Duration configuration value with default 获取Duration配置值(带默认值)
    int
    Get integer configuration value 获取整数配置值
    int
    getInt(String key, int defaultValue)
    Get integer configuration value with default 获取整数配置值(带默认值)
    Get all configuration keys 获取所有配置键
    <T> List<T>
    getList(String key, Class<T> elementType)
    Get list configuration value 获取列表配置值
    long
    Get long configuration value 获取长整数配置值
    long
    getLong(String key, long defaultValue)
    Get long configuration value with default 获取长整数配置值(带默认值)
    <K,V> Map<K,V>
    getMap(String key, Class<K> keyType, Class<V> valueType)
    Get map configuration value 获取映射配置值
    Get optional string configuration value 获取Optional字符串配置值
    <T> Optional<T>
    getOptional(String key, Class<T> type)
    Get optional typed configuration value 获取Optional类型配置值
    Get string configuration value 获取字符串配置值
    getString(String key, String defaultValue)
    Get string configuration value with default 获取字符串配置值(带默认值)
    Get sub-configuration by prefix 根据前缀获取子配置
    boolean
    Check if configuration key exists 检查配置键是否存在
    void
    Remove configuration change listener 移除配置变更监听器
  • Method Details

    • getString

      String getString(String key)
      Get string configuration value 获取字符串配置值
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
      Throws:
      OpenConfigException - if key not found | 如果键未找到
    • getString

      String getString(String key, String defaultValue)
      Get string configuration value with default 获取字符串配置值(带默认值)
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value if key not found | 键未找到时的默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getInt

      int getInt(String key)
      Get integer configuration value 获取整数配置值
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
      Throws:
      OpenConfigException - if key not found or conversion failed | 如果键未找到或转换失败
    • getInt

      int getInt(String key, int defaultValue)
      Get integer configuration value with default 获取整数配置值(带默认值)
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getLong

      long getLong(String key)
      Get long configuration value 获取长整数配置值
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
      Throws:
      OpenConfigException - if key not found or conversion failed | 如果键未找到或转换失败
    • getLong

      long getLong(String key, long defaultValue)
      Get long configuration value with default 获取长整数配置值(带默认值)
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getDouble

      double getDouble(String key)
      Get double configuration value 获取双精度配置值
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
      Throws:
      OpenConfigException - if key not found or conversion failed | 如果键未找到或转换失败
    • getDouble

      double getDouble(String key, double defaultValue)
      Get double configuration value with default 获取双精度配置值(带默认值)
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getBoolean

      boolean getBoolean(String key)
      Get boolean configuration value 获取布尔配置值
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
      Throws:
      OpenConfigException - if key not found or conversion failed | 如果键未找到或转换失败
    • getBoolean

      boolean getBoolean(String key, boolean defaultValue)
      Get boolean configuration value with default 获取布尔配置值(带默认值)
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getDuration

      Duration getDuration(String key)
      Get Duration configuration value 获取Duration配置值
      Parameters:
      key - configuration key | 配置键
      Returns:
      configuration value | 配置值
      Throws:
      OpenConfigException - if key not found or conversion failed | 如果键未找到或转换失败
    • getDuration

      Duration getDuration(String key, Duration defaultValue)
      Get Duration configuration value with default 获取Duration配置值(带默认值)
      Parameters:
      key - configuration key | 配置键
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • get

      <T> T get(String key, Class<T> type)
      Get typed configuration value 获取指定类型配置值
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      key - configuration key | 配置键
      type - target class | 目标类
      Returns:
      configuration value | 配置值
      Throws:
      OpenConfigException - if key not found or conversion failed | 如果键未找到或转换失败
    • get

      <T> T get(String key, Class<T> type, T defaultValue)
      Get typed configuration value with default 获取指定类型配置值(带默认值)
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      key - configuration key | 配置键
      type - target class | 目标类
      defaultValue - default value | 默认值
      Returns:
      configuration value or default | 配置值或默认值
    • getList

      <T> List<T> getList(String key, Class<T> elementType)
      Get list configuration value 获取列表配置值
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      key - configuration key | 配置键
      elementType - element class | 元素类
      Returns:
      configuration value list | 配置值列表
    • getMap

      <K,V> Map<K,V> getMap(String key, Class<K> keyType, Class<V> valueType)
      Get map configuration value 获取映射配置值
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      key - configuration key | 配置键
      keyType - key class | 键类
      valueType - value class | 值类
      Returns:
      configuration value map | 配置值映射
    • getOptional

      Optional<String> getOptional(String key)
      Get optional string configuration value 获取Optional字符串配置值
      Parameters:
      key - configuration key | 配置键
      Returns:
      optional configuration value | Optional配置值
    • getOptional

      <T> Optional<T> getOptional(String key, Class<T> type)
      Get optional typed configuration value 获取Optional类型配置值
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      key - configuration key | 配置键
      type - target class | 目标类
      Returns:
      optional configuration value | Optional配置值
    • getSubConfig

      Config getSubConfig(String prefix)
      Get sub-configuration by prefix 根据前缀获取子配置
      Parameters:
      prefix - configuration prefix | 配置前缀
      Returns:
      sub-configuration | 子配置
    • getByPrefix

      Map<String,String> getByPrefix(String prefix)
      Get all configurations with specified prefix 获取指定前缀的所有配置
      Parameters:
      prefix - configuration prefix | 配置前缀
      Returns:
      configuration map | 配置映射
    • hasKey

      boolean hasKey(String key)
      Check if configuration key exists 检查配置键是否存在
      Parameters:
      key - configuration key | 配置键
      Returns:
      true if exists | 如果存在返回true
    • getKeys

      Set<String> getKeys()
      Get all configuration keys 获取所有配置键
      Returns:
      set of all keys | 所有键的集合
    • addListener

      void addListener(ConfigListener listener)
      Add configuration change listener 添加配置变更监听器
      Parameters:
      listener - configuration listener | 配置监听器
    • addListener

      void addListener(String key, ConfigListener listener)
      Add listener for specific configuration key 添加指定键的监听器
      Parameters:
      key - configuration key | 配置键
      listener - configuration listener | 配置监听器
    • removeListener

      void removeListener(ConfigListener listener)
      Remove configuration change listener 移除配置变更监听器
      Parameters:
      listener - configuration listener | 配置监听器
    • bind

      <T> T bind(String prefix, Class<T> type)
      Bind configuration to POJO/Record 绑定配置到POJO/Record
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      prefix - configuration prefix | 配置前缀
      type - target class | 目标类
      Returns:
      bound instance | 绑定的实例
      Throws:
      OpenConfigException - if binding failed | 如果绑定失败
    • bindTo

      <T> void bindTo(String prefix, T target)
      Bind configuration to existing object 绑定配置到现有对象
      Type Parameters:
      T - target type | 目标类型
      Parameters:
      prefix - configuration prefix | 配置前缀
      target - target object | 目标对象
      Throws:
      OpenConfigException - if binding failed | 如果绑定失败