/
codebase/src/net/sourceforge/pain/admin/console/command/
codebase/src/net/sourceforge/pain/data/role/
codebase/src/net/sourceforge/pain/network/console/telnet/
codebase/src/net/sourceforge/pain/network/guitool/
codebase/src/net/sourceforge/pain/plugin/
codebase/src/net/sourceforge/pain/util/
db/src/net/sourceforge/pain/util/
gui/
gui/lib/
gui/src/net/sourceforge/pain/tools/guitool/dbbrowse/
gui/src/net/sourceforge/pain/tools/guitool/dialog/
gui/src/net/sourceforge/pain/tools/guitool/menu/
gui/src/net/sourceforge/pain/tools/guitool/resources/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/
gui/src/net/sourceforge/pain/tools/guitool/resources/images/explorer/
mudlibs/tinylib/
mudlibs/tinylib/area/
mudlibs/tinylib/etc/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/prototype/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/data/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/affect/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/deploy/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/event/guitool/event/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/fn/util/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/trigger/impl/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/command/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/reset/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/shutdown/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/social/
mudlibs/tinylib/src/net/sourceforge/pain/tinylib/util/
tests/
tests/src/
tests/src/net/sourceforge/pain/db/data/
package net.sourceforge.pain.util;

import java.io.*;
import java.util.*;

/**
 * java.util.Properties wrapper.
 */
public final class PropertiesReader {

	private Properties props;
	private String fileName;
	private boolean silent = false;


	public PropertiesReader(String fileName) {
		init(fileName);
	}

	private void init(String fileName) {
		this.fileName = fileName;
		Log.debug("Reading properties from: " + fileName);
		props = new Properties();
		try {
			InputStream stream = null;
			try {
				stream = new FileInputStream(fileName);
				props.load(stream);
			} finally {
				if (stream != null) {
					stream.close();
				}
			}
		} catch (Exception e) {
			props = null;
			Log.error(e.getMessage(), e);
			throw new RuntimeException("cant read: " + fileName);
		}
		Log.debug("Properties Read");
	}

	public void setSilent(boolean silent) {
		this.silent = silent;
	}

	public String get(String key) {
		return getProperty(key);
	}

	public String get(String key, boolean notNull) {
		String result = getProperty(key);
		if (notNull && result == null) {
			throw new RuntimeException("[" + fileName + "] Null is not allowed for '" + key + "'");
		}
		return result;
	}

	public String getProperty(String key) {
		String value = props.getProperty(key);
		if (!silent) {
			Log.debug("[" + fileName + "] asking property: " + key + "=" + value);
		}
		return value;

	}

	public long getLong(String key) {
		return Long.parseLong(getProperty(key));
	}

	public long getLong(String key, long minValue) {
		long result = getLong(key);
		if (result < minValue) {
			throw new RuntimeException("[" + fileName + "]'" + key + "' must be greater then: " + minValue + " (" + result + ")");
		}
		return result;
	}

	public boolean getBoolean(String key) {
		return getProperty(key).trim().equalsIgnoreCase("true");
	}

	public boolean getBoolean(String key, boolean defaultValue) {
		String value = getProperty(key);
		if (value == null) {
			return defaultValue;
		}
		return value.trim().equalsIgnoreCase("true");
	}


	public int getInt(String key) {
		return Integer.parseInt(getProperty(key));
	}

	public int getInt(String key, int minValue) {
		int result = getInt(key);
		if (result < minValue) {
			throw new RuntimeException("[" + fileName + "]'" + key + "' must be greater then: " + minValue + " (" + result + ")");
		}
		return result;
	}

	public int getInt(String key, int minValue, int defaultValue) {
		String value = getProperty(key);
		if (value == null) {
			return defaultValue;
		}
		int result = getInt(key);
		if (result < minValue) {
			throw new RuntimeException("[" + fileName + "]'" + key + "' must be greater then: " + minValue + " (" + result + ")");
		}
		return result;
	}

	public double getDouble(String key) {
		return Double.parseDouble(getProperty(key));
	}

	public double getDouble(String key, double minValue) {
		double result = getDouble(key);
		if (result < minValue) {
			throw new RuntimeException("[" + fileName + "]'" + key + "' must be greater then: " + minValue + " (" + result + ")");
		}
		return result;
	}

	public double getDouble(String key, double minValue, double maxValue) {
		double result = getDouble(key, minValue);
		if (result > maxValue) {
			throw new RuntimeException("[" + fileName + "]'" + key + "' must be less then: " + maxValue + " (" + result + ")");
		}
		return result;
	}

	public Properties getProperties() {
		return props;
	}


}