Class CompositeConfigSource

java.lang.Object
cloud.opencode.base.config.source.CompositeConfigSource
All Implemented Interfaces:
ConfigSource

public class CompositeConfigSource extends Object implements ConfigSource
Composite Configuration Source 组合配置源

Combines multiple configuration sources with priority-based merging. Higher priority sources override lower priority ones.

组合多个配置源并基于优先级合并。高优先级源覆盖低优先级源。

Features | 主要功能:

  • Priority-based merging - 基于优先级的合并
  • Multiple source aggregation - 多源聚合
  • Automatic source ordering - 自动源排序
  • Hot reload support - 热重载支持

Usage Examples | 使用示例:

List<ConfigSource> sources = List.of(
    new PropertiesConfigSource("defaults.properties"),  // Priority: 50
    new EnvironmentConfigSource(),                      // Priority: 100
    new CommandLineConfigSource(args)                   // Priority: 200
);

CompositeConfigSource composite = new CompositeConfigSource(sources);
// Command line > Environment > Properties

Priority Order | 优先级顺序:

Higher number = Higher priority
200: Command Line
100: Environment Variables
50:  Properties/YAML files
10:  In-Memory defaults

Performance | 性能特性:

  • Time complexity: O(n*m) for merging - 时间复杂度: 合并为O(n*m)
  • Merged map cached - 合并的映射被缓存
  • Reload triggers re-merge - 重载触发重新合并

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Immutable merged properties - 不可变的合并属性
Since:
JDK 25, opencode-base-config V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • CompositeConfigSource

      public CompositeConfigSource(List<ConfigSource> sources)
      Create composite config source 创建组合配置源
      Parameters:
      sources - configuration sources | 配置源列表
  • Method Details

    • getName

      public String getName()
      Description copied from interface: ConfigSource
      Get configuration source name 获取配置源名称
      Specified by:
      getName in interface ConfigSource
      Returns:
      source name | 源名称
    • getProperties

      public Map<String,String> getProperties()
      Description copied from interface: ConfigSource
      Get all configuration properties 获取所有配置属性

      Performance | 性能:

      This method should return a cached immutable map for best performance.

      此方法应返回缓存的不可变映射以获得最佳性能。

      Specified by:
      getProperties in interface ConfigSource
      Returns:
      configuration properties map | 配置属性映射
    • getPriority

      public int getPriority()
      Description copied from interface: ConfigSource
      Get configuration source priority (higher number = higher priority) 获取配置源优先级(数值越大优先级越高)

      Default Priorities | 默认优先级:

      • CommandLine: 200
      • Environment: 100
      • Properties/YAML: 50
      • InMemory: 10
      Specified by:
      getPriority in interface ConfigSource
      Returns:
      priority value | 优先级值
    • supportsReload

      public boolean supportsReload()
      Description copied from interface: ConfigSource
      Check if source supports hot reload 检查源是否支持热重载
      Specified by:
      supportsReload in interface ConfigSource
      Returns:
      true if hot reload supported | 如果支持热重载返回true
    • reload

      public void reload()
      Description copied from interface: ConfigSource
      Reload configuration from source 从源重新加载配置

      This method is called when hot reload is enabled.

      启用热重载时会调用此方法。

      Specified by:
      reload in interface ConfigSource
    • getSources

      public List<ConfigSource> getSources()
      Get all underlying sources 获取所有底层源
      Returns:
      list of sources | 源列表