/**
 * Copyright 2015 Delft University of Technology
 *
 * 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 nl.tudelft.graphalytics.configuration;

import org.apache.commons.configuration.Configuration;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ConfigurationUtil {
	private static final Logger LOG = LogManager.getLogger();

	public static void ensureConfigurationKeyExists(Configuration config, String property)
			throws InvalidConfigurationException {
		if (!config.containsKey(property)) {
			throw new InvalidConfigurationException("Missing property: \"" + property + "\".");
		}
	}
	
	public static String getString(Configuration config, String property)
			throws InvalidConfigurationException {
		ensureConfigurationKeyExists(config, property);
		return config.getString(property);
	}
	
	public static String[] getStringArray(Configuration config, String property)
			throws InvalidConfigurationException {
		ensureConfigurationKeyExists(config, property);
		return config.getStringArray(property);
	}
	
	public static boolean getBoolean(Configuration config, String property)
			throws InvalidConfigurationException {
		ensureConfigurationKeyExists(config, property);
		Boolean value = config.getBoolean(property, null);
		if (value == null) {
			throw new InvalidConfigurationException("Invalid value for property \"" + property +
					"\": \"" + config.getString(property) + "\", expected a boolean value.");
		}
		return value.booleanValue();
	}
	
	public static int getInteger(Configuration config, String property)
			throws InvalidConfigurationException {
		ensureConfigurationKeyExists(config, property);
		Integer value = config.getInteger(property, null);
		if (value == null) {
			throw new InvalidConfigurationException("Invalid value for property \"" + property +
					"\": \"" + config.getString(property) + "\", expected an integer value.");
		}
		return value.intValue();
	}
	
	public static long getLong(Configuration config, String property)
			throws InvalidConfigurationException {
		ensureConfigurationKeyExists(config, property);
		Long value = config.getLong(property, null);
		if (value == null) {
			throw new InvalidConfigurationException("Invalid value for property \"" + property +
					"\": \"" + config.getString(property) + "\", expected a long value.");
		}
		return value.longValue();
	}

	public static long getLongOrWarn(Configuration config, String property, long defaultValue) {
		try {
			return getLong(config, property);
		} catch (InvalidConfigurationException ex) {
			LOG.warn("Failed to read property \"" + property + "\", defaulting to " + defaultValue + ".", ex);
			return defaultValue;
		}
	}
	
	public static float getFloat(Configuration config, String property)
			throws InvalidConfigurationException {
		ensureConfigurationKeyExists(config, property);
		Float value = config.getFloat(property, null);
		if (value == null) {
			throw new InvalidConfigurationException("Invalid value for property \"" + property +
					"\": \"" + config.getString(property) + "\", expected a floating point value.");
		}
		return value.floatValue();
	}
	
}

@@ -715,16 +715,77 @@
 ration;%0A
+import org.apache.commons.configuration.ConversionException;%0A
 import o
@@ -820,16 +820,16 @@
 anager;%0A
-
 import o
@@ -1812,23 +1812,23 @@
 ;%0A%09%09
-Boolean value =
+try %7B%0A%09%09%09return
  con
@@ -1854,36 +1854,48 @@
 erty
-, null);%0A%09%09if (value == null
+);%0A%09%09%7D catch (ConversionException ignore
 ) %7B%0A
@@ -2069,39 +2069,8 @@
 %09%09%7D%0A
-%09%09return value.booleanValue();%0A
 %09%7D%0A%09
@@ -2237,23 +2237,23 @@
 ;%0A%09%09
-Integer value =
+try %7B%0A%09%09%09return
  con
@@ -2262,20 +2262,16 @@
 g.getInt
-eger
 (propert
@@ -2275,36 +2275,48 @@
 erty
-, null);%0A%09%09if (value == null
+);%0A%09%09%7D catch (ConversionException ignore
 ) %7B%0A
@@ -2491,35 +2491,8 @@
 %09%09%7D%0A
-%09%09return value.intValue();%0A
 %09%7D%0A%09
@@ -2657,20 +2657,23 @@
 ;%0A%09%09
-Long value =
+try %7B%0A%09%09%09return
  con
@@ -2696,36 +2696,48 @@
 erty
-, null);%0A%09%09if (value == null
+);%0A%09%09%7D catch (ConversionException ignore
 ) %7B%0A
@@ -2908,36 +2908,8 @@
 %09%09%7D%0A
-%09%09return value.longValue();%0A
 %09%7D%0A%0A
@@ -3396,21 +3396,23 @@
 ;%0A%09%09
-Float value =
+try %7B%0A%09%09%09return
  con
@@ -3436,36 +3436,48 @@
 erty
-, null);%0A%09%09if (value == null
+);%0A%09%09%7D catch (ConversionException ignore
 ) %7B%0A
@@ -3654,41 +3654,12 @@
 %22);%0A
+
 %09%09%7D%0A
-%09%09return value.floatValue();%0A
 %09%7D%0A%09

Fix exception type on incorrect value in configuration