* These methods are very similar to others in java.lang; for example, * {@link #intValue} is nearly the same as Integer.getInteger(String,int). * The main difference is that these methods log warnings for invalid property * values. * * @author Mike Martin * @version $Revision: 1.1 $ */ public final class SystemProperty { /** Private constructor prevents instantiation. */ private SystemProperty() { } /** * Returns the value of a system property as a boolean. * If such a system property exists it is converted to a boolean with * Boolean.valueOf(String). * Returns the specified default value if no such system property exists or * the property value is invalid. * * @return * the boolean value of the specified property */ public static boolean booleanValue(String propName, boolean defaultValue) { String prop = System.getProperty(propName); boolean val; if (prop == null) val = defaultValue; else val = Boolean.valueOf(prop.trim()).booleanValue(); return val; } /** * Returns the value of a system property as an integer. * If such a system property exists it is converted to an integer with * Integer.decode(String). * Returns the specified default value if no such system property exists or * the property value is invalid. * * @return * the integer value of the specified property */ public static int intValue(String propName, int defaultValue) { String prop = System.getProperty(propName); int val; if (prop == null) val = defaultValue; else { try { val = Integer.decode(prop.trim()).intValue(); } catch (NumberFormatException e) { val = defaultValue; } } return val; } /** * Returns the value of a system property as a String. * Returns the specified default value if no such system property exists. * * @return * the String value of the specified property */ public static String stringValue(String propName, String defaultValue) { String val = System.getProperty(propName); if (val == null) val = defaultValue; return val; } }