package fi.bittiraha.util;

import java.util.*;

/**
 * Property file reader which contains handy content validation and
 * shortcuts for common data types. Best practise is to load
 * configuration file and then set defaults for each parameter in use
 * in a try block to catch validation errors. Then you can safely use
 * getters without fear of validation errors.
 */ 
public class ConfigFile extends Properties {

    public ConfigFile() {
    }
    
    public ConfigFile(Properties defaults) {
	super(defaults);
    }

    public void defaultBoolean(String k, boolean def) {
	try {
	    getBoolean(k);
	} catch (ConfigFieldNullException _) {
	    super.setProperty(k, def ? "1" : "0");
	}
    }

    public boolean getBoolean(String k) {
	String v = super.getProperty(k);
	if (v == null) throw new ConfigFieldNullException(k);
	switch (v.toLowerCase()) {
	case "1": return true;
	case "0": return false;
	case "true": return true;
	case "false": return false;
	default: throw new ConfigFieldException(k,"is not boolean (true, false, 0, or 1)");
	}

    }

    public void defaultString(String k, String def) {
	try {
	    getString(k);
	} catch (ConfigFieldNullException _) {
	    super.setProperty(k, def);
	}
    }

    public String getString(String k) {
	String v = super.getProperty(k);
	if (v == null) throw new ConfigFieldNullException(k);
	return v;
    }
    
    /**
     * Validation exception. This is runtime exception so remember to
     * check exceptions if you don't want to exit in case of an error.
     */
    public static class ConfigFieldException extends RuntimeException {
	public ConfigFieldException(String k, String msg) {
	    super("Key "+k+" "+msg+" in configuration file");
	}
    }

    /**
     * Validation exception. This is used when null value is
     * encountered.
     */
    public static class ConfigFieldNullException extends ConfigFieldException {
	public ConfigFieldNullException(String k) {
	    super(k,"is null");
	}
    }
}

@@ -21,16 +21,45 @@
 .util;%0A%0A
+import java.math.BigDecimal;%0A
 import j
@@ -1077,36 +1077,941 @@
 )%22);%0A%09%7D%0A%0A    %7D%0A%0A
-    
+%09public void defaultInteger(String k,int def) %7B%0A%09%09try %7B%0A%09%09%09getString(k);%0A%09%09%7D catch (ConfigFieldNullException _) %7B%0A%09%09%09super.setProperty(k,Integer.toString(def));%0A%09%09%7D%0A%09%7D%0A%0A%09public int getInteger(String k) %7B%0A%09%09String v = super.getProperty(k);%0A%09%09if (v == null) throw new ConfigFieldNullException(k);%0A%09%09try %7B%0A%09%09%09return Integer.parseInt(v);%0A%09%09%7D catch (NumberFormatException _) %7B%0A%09%09%09throw new ConfigFieldException(k,%22Invalid format for Integer%22);%0A%09%09%7D%0A%09%7D%0A%0A%09public void defaultBigDecimal(String k,BigDecimal def) %7B%0A%09%09try %7B%0A%09%09%09getString(k);%0A%09%09%7D catch (ConfigFieldNullException _) %7B%0A%09%09%09super.setProperty(k,def.toString());%0A%09%09%7D%0A%09%7D%0A%0A%09public BigDecimal getBigDecimal(String k) %7B%0A%09%09String v = super.getProperty(k);%0A%09%09if (v == null) throw new ConfigFieldNullException(k);%0A%09%09try %7B%0A%09%09%09return new BigDecimal(v);%0A%09%09%7D catch (NumberFormatException _) %7B%0A%09%09%09throw new ConfigFieldException(k,%22Invalid format for BigDecimal%22);%0A%09%09%7D%0A%09%7D%0A%0A%09
 public void defa

Add Integer and BigDecimal support to ConfigFile