/*
 * Copyright 2015 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 pl.nort.config.provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.nort.config.source.ConfigurationSource;
import pl.nort.config.source.EmptyConfigurationSource;
import pl.nort.config.source.refresh.RefreshStrategy;
import pl.nort.config.source.refresh.strategy.OnInitRefreshStrategy;

/**
 * A builder producing {@link ConfigurationProvider}s. If you don't specify the value for one the fields
 * then the default value will be provided - read the constructor's documentation to learn
 * what the default values are.
 */
public class ConfigurationProviderBuilder {

  private static final Logger LOG = LoggerFactory.getLogger(ConfigurationProviderBuilder.class);

  private ConfigurationSource configurationSource;
  private RefreshStrategy refreshStrategy;

  /**
   * Construct {@link ConfigurationProvider}s builder
   * <p>
   * Default setup (override using with*() methods)
   * <ul>
   * <li>ConfigurationSource: {@link EmptyConfigurationSource}</li>
   * <li>RefreshStrategy: {@link OnInitRefreshStrategy}</li>
   * </ul>
   */
  public ConfigurationProviderBuilder() {
    configurationSource = new EmptyConfigurationSource();
    refreshStrategy = new OnInitRefreshStrategy();
  }

  /**
   * Set {@link ConfigurationSource} for {@link ConfigurationProvider}s built by this builder
   *
   * @param configurationSource {@link ConfigurationSource} to use
   * @return this builder with {@link ConfigurationSource} set to {@code configurationSource}
   */
  public ConfigurationProviderBuilder withConfigurationSource(ConfigurationSource configurationSource) {
    this.configurationSource = configurationSource;
    return this;
  }

  /**
   * Set {@link RefreshStrategy} for {@link ConfigurationProvider}s built by this builder
   *
   * @param refreshStrategy {@link RefreshStrategy} to use
   * @return this builder with {@link RefreshStrategy} set to {@code refreshStrategy}
   */
  public ConfigurationProviderBuilder withRefreshStrategy(RefreshStrategy refreshStrategy) {
    this.refreshStrategy = refreshStrategy;
    return this;
  }

  /**
   * Build a {@link ConfigurationProvider} using this builder's configuration
   *
   * @return new {@link ConfigurationProvider}
   */
  public ConfigurationProvider build() {
    LOG.info("Initializing ConfigurationProvider with " + configurationSource.getClass() + " source and " +
        refreshStrategy.getClass() + " refresh strategy.");

    refreshStrategy.init(configurationSource);

    return new SimpleConfigurationProvider(configurationSource);
  }

}

@@ -802,32 +802,157 @@
 gurationSource;%0A
+import pl.nort.config.source.context.DefaultEnvSelectionStrategy;%0Aimport pl.nort.config.source.context.EnvSelectionStrategy;%0A
 import pl.nort.c
@@ -1531,16 +1531,69 @@
 trategy;
+%0A  private EnvSelectionStrategy envSelectionStrategy;
 %0A%0A  /**%0A
@@ -1842,24 +1842,96 @@
 ategy%7D%3C/li%3E%0A
+   * %3Cli%3EEnvSelectionStrategy: %7B@link DefaultEnvSelectionStrategy%7D%3C/li%3E%0A
    * %3C/ul%3E%0A 
@@ -2086,16 +2086,78 @@
 tegy();%0A
+    envSelectionStrategy = new DefaultEnvSelectionStrategy();%0A
   %7D%0A%0A  /
@@ -3007,32 +3007,494 @@
 turn this;%0A  %7D%0A%0A
+  /**%0A   * Set %7B@link EnvSelectionStrategy%7D for %7B@link ConfigurationProviders%7Ds built by this builder%0A   *%0A   * @param envSelectionStrategy %7B@link EnvSelectionStrategy%7D to use%0A   * @return this builder with %7B@link EnvSelectionStrategy%7D set to %7B@code envSelectionStrategy%7D%0A   */%0A  public ConfigurationProviderBuilder withEnvSelectionStrategy(EnvSelectionStrategy envSelectionStrategy) %7B%0A    this.envSelectionStrategy = envSelectionStrategy;%0A    return this;%0A  %7D%0A%0A
   /**%0A   * Build
@@ -3759,20 +3759,17 @@
 %22 source
- and
+,
  %22 +%0A   
@@ -3812,16 +3812,103 @@
 refresh 
+strategy and %22 + envSelectionStrategy.getClass() + %22 environment%22 +%0A        %22selection 
 strategy
@@ -4023,16 +4023,38 @@
 onSource
+, envSelectionStrategy
 );%0A  %7D%0A%0A

support EnvSelectionStrategy in ConfigurationProviderBuilder