All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.zhengframework.configuration.source.MergeConfigurationSource Maven / Gradle / Ivy

There is a newer version: 1.8.0
Show newest version
package com.github.zhengframework.configuration.source;

import com.github.zhengframework.configuration.environment.Environment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MergeConfigurationSource implements ConfigurationSource {

  private final List sources = new ArrayList<>();

  public MergeConfigurationSource(ConfigurationSource... sources) {
    Objects.requireNonNull(sources);
    for (ConfigurationSource source : sources) {
      MergeConfigurationSource.this.sources.add(Objects.requireNonNull(source));
    }
  }

  public MergeConfigurationSource(Iterator sources) {
    Objects.requireNonNull(sources).forEachRemaining(
        configurationSource -> MergeConfigurationSource.this.sources
            .add(Objects.requireNonNull(configurationSource)));
  }

  public MergeConfigurationSource(Iterable sources) {
    Objects.requireNonNull(sources).forEach(
        configurationSource -> MergeConfigurationSource.this.sources
            .add(Objects.requireNonNull(configurationSource)));
  }

  @Override
  public void init() {
    for (ConfigurationSource source : sources) {
      source.init();
    }
  }

  @Override
  public void addListener(ConfigurationSourceListener listener) {
    for (ConfigurationSource source : sources) {
      source.addListener(listener);
    }
  }

  @Override
  public void removeListener(ConfigurationSourceListener listener) {
    for (ConfigurationSource source : sources) {
      source.removeListener(listener);
    }
  }

  @Override
  public Map getConfiguration(Environment environment) {
    Map map = new HashMap<>();
    for (ConfigurationSource source : sources) {
      Map configuration = source.getConfiguration(environment);
      map.putAll(configuration);
    }
    return map;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy