// Path: cfg4j-core/src/main/java/org/cfg4j/source/ConfigurationSource.java
// public interface ConfigurationSource {
// 
//   /**
//    * Get configuration set for a given {@code environment} from this source in a form of {@link Properties}.
//    * Provided {@link Environment} will be used to determine which environment to use.
//    *
//    * @param environment environment to use
//    * @return configuration set for {@code environment}
//    * @throws MissingEnvironmentException when requested environment couldn't be found
//    * @throws IllegalStateException       when unable to fetch configuration
//    */
//   Properties getConfiguration(Environment environment);
// 
//   /**
//    * Initialize this source. This method has to be called before any other method of this instance.
//    *
//    * @throws IllegalStateException        when source was improperly configured
//    * @throws SourceCommunicationException when unable to communicate with source
//    */
//   void init();
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/SourceCommunicationException.java
// public class SourceCommunicationException extends RuntimeException {
// 
//   /**
//    * Indicates problem when communicating with {@link ConfigurationSource}.
//    *
//    * @param msg the detail message
//    * @param e   the cause
//    */
//   public SourceCommunicationException(String msg, Exception e) {
//     super("Unable to communicate with configuration source: " + msg, e);
//   }
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/context/environment/Environment.java
// public interface Environment {
// 
//   /**
//    * Name of the environment. Should not be null.
//    *
//    * @return environment name. Not null.
//    */
//   String getName();
// 
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/context/environment/MissingEnvironmentException.java
// public class MissingEnvironmentException extends RuntimeException {
// 
//   private static final String MISSING_ENV_MSG = "Missing environment: ";
// 
//   /**
//    * Environment named {@code envName} is missing.
//    *
//    * @param envName environment name
//    */
//   public MissingEnvironmentException(String envName) {
//     super(MISSING_ENV_MSG + envName);
//   }
// 
//   /**
//    * Environment named {@code envName} is missing.
//    *
//    * @param envName environment name
//    * @param cause root cause
//    */
//   public MissingEnvironmentException(String envName, Throwable cause) {
//     super(MISSING_ENV_MSG + envName, cause);
//   }
// }


import static java.util.Objects.requireNonNull;
import org.cfg4j.source.ConfigurationSource;
import org.cfg4j.source.SourceCommunicationException;
import org.cfg4j.source.context.environment.Environment;
import org.cfg4j.source.context.environment.MissingEnvironmentException;
import java.util.Arrays;
import java.util.Properties;
/*
 * Copyright 2015-2018 Norbert Potocki (norbert.potocki@nort.pl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cfg4j.source.compose;




/**
 * Combines multiple {@link ConfigurationSource}s in a fallback chain. When one of the sources is not available
 * another one is used for providing configuration.
 */
public class FallbackConfigurationSource implements ConfigurationSource {

  private final ConfigurationSource[] sources;

  /**
   * Create a fallback chain of {@link ConfigurationSource}s
   *
   * @param sources configuration sources to use
   */
  public FallbackConfigurationSource(ConfigurationSource... sources) {
    this.sources = requireNonNull(sources);

    for (ConfigurationSource source : sources) {
      requireNonNull(source);
    }
  }

  /**
   * Get configuration set for a given {@code environment} from this source in a form of {@link Properties}.
   * The configuration set is a result of the first {@link ConfigurationSource#getConfiguration(Environment)}
   * call to underlying sources that succeeds. Sources are called in a provided order.
   *
   * @param environment environment to use
   * @return configuration set for {@code environment} from the first source that works
   * @throws MissingEnvironmentException when requested environment couldn't be found in any of the underlying source
   * @throws IllegalStateException       when unable to fetch configuration from any of the underlying sources
   */
  @Override
// Path: cfg4j-core/src/main/java/org/cfg4j/source/ConfigurationSource.java
// public interface ConfigurationSource {
// 
//   /**
//    * Get configuration set for a given {@code environment} from this source in a form of {@link Properties}.
//    * Provided {@link Environment} will be used to determine which environment to use.
//    *
//    * @param environment environment to use
//    * @return configuration set for {@code environment}
//    * @throws MissingEnvironmentException when requested environment couldn't be found
//    * @throws IllegalStateException       when unable to fetch configuration
//    */
//   Properties getConfiguration(Environment environment);
// 
//   /**
//    * Initialize this source. This method has to be called before any other method of this instance.
//    *
//    * @throws IllegalStateException        when source was improperly configured
//    * @throws SourceCommunicationException when unable to communicate with source
//    */
//   void init();
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/SourceCommunicationException.java
// public class SourceCommunicationException extends RuntimeException {
// 
//   /**
//    * Indicates problem when communicating with {@link ConfigurationSource}.
//    *
//    * @param msg the detail message
//    * @param e   the cause
//    */
//   public SourceCommunicationException(String msg, Exception e) {
//     super("Unable to communicate with configuration source: " + msg, e);
//   }
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/context/environment/Environment.java
// public interface Environment {
// 
//   /**
//    * Name of the environment. Should not be null.
//    *
//    * @return environment name. Not null.
//    */
//   String getName();
// 
// }
// 
// Path: cfg4j-core/src/main/java/org/cfg4j/source/context/environment/MissingEnvironmentException.java
// public class MissingEnvironmentException extends RuntimeException {
// 
//   private static final String MISSING_ENV_MSG = "Missing environment: ";
// 
//   /**
//    * Environment named {@code envName} is missing.
//    *
//    * @param envName environment name
//    */
//   public MissingEnvironmentException(String envName) {
//     super(MISSING_ENV_MSG + envName);
//   }
// 
//   /**
//    * Environment named {@code envName} is missing.
//    *
//    * @param envName environment name
//    * @param cause root cause
//    */
//   public MissingEnvironmentException(String envName, Throwable cause) {
//     super(MISSING_ENV_MSG + envName, cause);
//   }
// }


// Path: cfg4j-core/src/main/java/org/cfg4j/source/compose/FallbackConfigurationSource.java
import static java.util.Objects.requireNonNull;
import org.cfg4j.source.ConfigurationSource;
import org.cfg4j.source.SourceCommunicationException;
import org.cfg4j.source.context.environment.Environment;
import org.cfg4j.source.context.environment.MissingEnvironmentException;
import java.util.Arrays;
import java.util.Properties;

/*
 * Copyright 2015-2018 Norbert Potocki (norbert.potocki@nort.pl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cfg4j.source.compose;




/**
 * Combines multiple {@link ConfigurationSource}s in a fallback chain. When one of the sources is not available
 * another one is used for providing configuration.
 */
public class FallbackConfigurationSource implements ConfigurationSource {

  private final ConfigurationSource[] sources;

  /**
   * Create a fallback chain of {@link ConfigurationSource}s
   *
   * @param sources configuration sources to use
   */
  public FallbackConfigurationSource(ConfigurationSource... sources) {
    this.sources = requireNonNull(sources);

    for (ConfigurationSource source : sources) {
      requireNonNull(source);
    }
  }

  /**
   * Get configuration set for a given {@code environment} from this source in a form of {@link Properties}.
   * The configuration set is a result of the first {@link ConfigurationSource#getConfiguration(Environment)}
   * call to underlying sources that succeeds. Sources are called in a provided order.
   *
   * @param environment environment to use
   * @return configuration set for {@code environment} from the first source that works
   * @throws MissingEnvironmentException when requested environment couldn't be found in any of the underlying source
   * @throws IllegalStateException       when unable to fetch configuration from any of the underlying sources
   */
  @Override
  public Properties getConfiguration(Environment environment) {