// 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/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/validator/BindingValidator.java
// public class BindingValidator {
// 
//   private static final Logger LOG = LoggerFactory.getLogger(BindingValidator.class);
// 
//   /**
//    * Validate if the {@code configurationBean} object was bound successfully
//    *
//    * @param configurationBean configurationBean to validate
//    * @param type              interface used to access {@code configurationBean}
//    * @param <T>               type of the validated bean
//    * @throws IllegalStateException    when unable to access one of the methods
//    * @throws NoSuchElementException   when an invocation of one of the {@code configurationBean} methods failed with an
//    *                                  underlying exception of type {@link NoSuchElementException}
//    * @throws IllegalArgumentException when an invocation of one of the {@code configurationBean} methods failed with an
//    *                                  underlying exception of type {@link IllegalArgumentException}
//    */
//   public <T> void validate(T configurationBean, Class<T> type) {
//     LOG.debug("Validating configuration bean of type " + type);
// 
//     for (Method declaredMethod : type.getDeclaredMethods()) {
//       try {
//         LOG.debug("Validating method: " + declaredMethod.getName());
// 
//         declaredMethod.invoke(configurationBean);
// 
//       } catch (InvocationTargetException e) {
// 
//         if (e.getCause() != null) {
//           if (e.getCause() instanceof NoSuchElementException) {
//             throw (NoSuchElementException) e.getCause();
//           }
// 
//           if (e.getCause() instanceof IllegalArgumentException) {
//             throw (IllegalArgumentException) e.getCause();
//           }
//         }
// 
//         throw new IllegalStateException("Can't bind method " + declaredMethod.getName(), e);
// 
//       } catch (IllegalAccessException e) {
//         throw new IllegalStateException("Can't bind method " + declaredMethod.getName(), e);
//       }
//     }
//   }
// }


import static java.util.Objects.requireNonNull;
import com.github.drapostolos.typeparser.NoSuchRegisteredParserException;
import com.github.drapostolos.typeparser.TypeParser;
import com.github.drapostolos.typeparser.TypeParserException;
import org.cfg4j.source.ConfigurationSource;
import org.cfg4j.source.context.environment.Environment;
import org.cfg4j.source.context.environment.MissingEnvironmentException;
import org.cfg4j.validator.BindingValidator;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.NoSuchElementException;
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.provider;




/**
 * Basic implementation of {@link ConfigurationProvider}. To construct this provider use {@link ConfigurationProviderBuilder}.
 */
class SimpleConfigurationProvider implements ConfigurationProvider {

  private final ConfigurationSource configurationSource;
  private final Environment environment;

  /**
   * {@link ConfigurationProvider} backed by provided {@link ConfigurationSource} and using {@code environment}
   * to select environment. To construct this provider use {@link ConfigurationProviderBuilder}.
   *
   * @param configurationSource source for configuration
   * @param environment         {@link Environment} to use
   */
  SimpleConfigurationProvider(ConfigurationSource configurationSource, Environment environment) {
    this.configurationSource = requireNonNull(configurationSource);
    this.environment = requireNonNull(environment);
  }

  @Override
  public Properties allConfigurationAsProperties() {
    try {
      return configurationSource.getConfiguration(environment);
// 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/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/validator/BindingValidator.java
// public class BindingValidator {
// 
//   private static final Logger LOG = LoggerFactory.getLogger(BindingValidator.class);
// 
//   /**
//    * Validate if the {@code configurationBean} object was bound successfully
//    *
//    * @param configurationBean configurationBean to validate
//    * @param type              interface used to access {@code configurationBean}
//    * @param <T>               type of the validated bean
//    * @throws IllegalStateException    when unable to access one of the methods
//    * @throws NoSuchElementException   when an invocation of one of the {@code configurationBean} methods failed with an
//    *                                  underlying exception of type {@link NoSuchElementException}
//    * @throws IllegalArgumentException when an invocation of one of the {@code configurationBean} methods failed with an
//    *                                  underlying exception of type {@link IllegalArgumentException}
//    */
//   public <T> void validate(T configurationBean, Class<T> type) {
//     LOG.debug("Validating configuration bean of type " + type);
// 
//     for (Method declaredMethod : type.getDeclaredMethods()) {
//       try {
//         LOG.debug("Validating method: " + declaredMethod.getName());
// 
//         declaredMethod.invoke(configurationBean);
// 
//       } catch (InvocationTargetException e) {
// 
//         if (e.getCause() != null) {
//           if (e.getCause() instanceof NoSuchElementException) {
//             throw (NoSuchElementException) e.getCause();
//           }
// 
//           if (e.getCause() instanceof IllegalArgumentException) {
//             throw (IllegalArgumentException) e.getCause();
//           }
//         }
// 
//         throw new IllegalStateException("Can't bind method " + declaredMethod.getName(), e);
// 
//       } catch (IllegalAccessException e) {
//         throw new IllegalStateException("Can't bind method " + declaredMethod.getName(), e);
//       }
//     }
//   }
// }


// Path: cfg4j-core/src/main/java/org/cfg4j/provider/SimpleConfigurationProvider.java
import static java.util.Objects.requireNonNull;
import com.github.drapostolos.typeparser.NoSuchRegisteredParserException;
import com.github.drapostolos.typeparser.TypeParser;
import com.github.drapostolos.typeparser.TypeParserException;
import org.cfg4j.source.ConfigurationSource;
import org.cfg4j.source.context.environment.Environment;
import org.cfg4j.source.context.environment.MissingEnvironmentException;
import org.cfg4j.validator.BindingValidator;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.NoSuchElementException;
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.provider;




/**
 * Basic implementation of {@link ConfigurationProvider}. To construct this provider use {@link ConfigurationProviderBuilder}.
 */
class SimpleConfigurationProvider implements ConfigurationProvider {

  private final ConfigurationSource configurationSource;
  private final Environment environment;

  /**
   * {@link ConfigurationProvider} backed by provided {@link ConfigurationSource} and using {@code environment}
   * to select environment. To construct this provider use {@link ConfigurationProviderBuilder}.
   *
   * @param configurationSource source for configuration
   * @param environment         {@link Environment} to use
   */
  SimpleConfigurationProvider(ConfigurationSource configurationSource, Environment environment) {
    this.configurationSource = requireNonNull(configurationSource);
    this.environment = requireNonNull(environment);
  }

  @Override
  public Properties allConfigurationAsProperties() {
    try {
      return configurationSource.getConfiguration(environment);
    } catch (IllegalStateException | MissingEnvironmentException e) {