/*
 * 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 static com.google.common.base.Preconditions.checkNotNull;

import com.github.drapostolos.typeparser.NoSuchRegisteredParserException;
import com.github.drapostolos.typeparser.TypeParser;
import com.github.drapostolos.typeparser.TypeParserException;
import pl.nort.config.source.ConfigurationSource;

import java.util.NoSuchElementException;
import java.util.Properties;

/**
 * Basic implementation of {@link ConfigurationProvider}
 */
public class SimpleConfigurationProvider implements ConfigurationProvider {

  private final ConfigurationSource configurationSource;

  /**
   * {@link ConfigurationProvider} backed by provided {@link ConfigurationSource}
   *
   * @param configurationSource source for configuration
   */
  public SimpleConfigurationProvider(ConfigurationSource configurationSource) {
    this.configurationSource = checkNotNull(configurationSource);
  }

  @Override
  public Properties allConfigurationAsProperties() {
    try {
      return configurationSource.getConfiguration();
    } catch (IllegalStateException e) {
      throw new IllegalStateException("Couldn't fetch configuration from configuration source", e);
    }
  }

  @Override
  public String getProperty(String key) {
    try {

      String property = configurationSource.getConfiguration().getProperty(key);

      if (property == null) {
        throw new NoSuchElementException("No configuration with key: " + key);
      }

      return property;

    } catch (IllegalStateException e) {
      throw new IllegalStateException("Couldn't fetch configuration from configuration source for key: " + key, e);
    }
  }

  @Override
  public <T> T getProperty(String key, Class<T> type) {
    String propertyStr = getProperty(key);

    T property;

    try {
      TypeParser parser = TypeParser.newBuilder().build();
      property = parser.parse(propertyStr, type);
    } catch (TypeParserException | NoSuchRegisteredParserException e) {
      throw new IllegalArgumentException("Unable to cast value \'" + propertyStr + "\' to " + type, e);
    }

    return property;
  }

  @Override
  public <T> T getProperty(String key, GenericType<T> genericType) {
    String propertyStr = getProperty(key);

    T property;

    try {
      TypeParser parser = TypeParser.newBuilder().build();
      property = parser.parse(propertyStr, new GenericTypeAdapter<>(genericType));
    } catch (TypeParserException | NoSuchRegisteredParserException e) {
      throw new IllegalArgumentException("Unable to cast value \'" + propertyStr + "\' to " + genericType, e);
    }

    return property;

  }
}

@@ -2379,33 +2379,16 @@
 (key);%0A%0A
-    T property;%0A%0A
     try 
@@ -2381,32 +2381,32 @@
 ey);%0A%0A    try %7B%0A
+
       TypeParser
@@ -2450,34 +2450,30 @@
 ld();%0A      
-property =
+return
  parser.pars
@@ -2675,38 +2675,16 @@
 );%0A    %7D
-%0A%0A    return property;
 %0A  %7D%0A%0A  
@@ -2810,25 +2810,8 @@
 );%0A%0A
-    T property;%0A%0A
     
@@ -2881,16 +2881,55 @@
 ;%0A      
+@SuppressWarnings(%22unchecked%22)%0A      T 
 property
@@ -2930,16 +2930,20 @@
 operty =
+ (T)
  parser.
@@ -2939,32 +2939,36 @@
 (T) parser.parse
+Type
 (propertyStr, ne
@@ -2969,13 +2969,9 @@
 tr, 
-new G
+g
 ener
@@ -2980,31 +2980,42 @@
 Type
-Adapter%3C%3E(genericType))
+.getType());%0A      return property
 ;%0A  
@@ -3199,37 +3199,14 @@
 e);%0A
+
     %7D%0A
-%0A    return property;%0A%0A
   %7D%0A

simplify SimpleConfigurationProvider