SystemProperty.java 1.38 KB
package fi.codecrew.moya.utilities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Gather all system properties in here as enums, so we have some kind of
 * understanding what kind of properties there are.
 * 
 * @author tuomari
 *
 */
public enum SystemProperty {

	MOYA_IRC_SERVER,
	MOYA_IRC_CHANNEL("#moya-debug")

	;

	private final String defaultValue;
	private final Integer defaultIntegerValue;
	private static final Logger logger = LoggerFactory.getLogger(SystemProperty.class);

	private SystemProperty() {
		defaultValue = null;
		defaultIntegerValue = null;
	}

	private SystemProperty(String value) {
		defaultValue = value;
		defaultIntegerValue = null;
	}

	private SystemProperty(Integer value) {
		defaultIntegerValue = value;
		defaultValue = null;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public String getValueOrDefault() {
		String val = System.getProperty(this.name());
		if (val == null) {
			val = defaultValue;
		}
		return val;
	}

	public Integer getIntvalueOrDefault() {
		String val = System.getProperty(this.name());
		Integer intval = null;
		try {
			intval = Integer.parseInt(val);
		} catch (Throwable t) {
			intval = null;
			logger.warn("Unable to parse system property '{}' value '{}' as integer. Defaulting to {}", name(), val, defaultIntegerValue);
		}
		if (val == null) {
			intval = defaultIntegerValue;
		}
		return intval;
	}
}