[jbosscache-commits] JBoss Cache SVN: r7473 - core/branches/flat/src/main/java/org/jboss/starobrno/util.
jbosscache-commits at lists.jboss.org
jbosscache-commits at lists.jboss.org
Wed Jan 14 11:55:34 EST 2009
Author: manik.surtani at jboss.com
Date: 2009-01-14 11:55:34 -0500 (Wed, 14 Jan 2009)
New Revision: 7473
Added:
core/branches/flat/src/main/java/org/jboss/starobrno/util/TypedProperties.java
Log:
Utility to read typed properties
Added: core/branches/flat/src/main/java/org/jboss/starobrno/util/TypedProperties.java
===================================================================
--- core/branches/flat/src/main/java/org/jboss/starobrno/util/TypedProperties.java (rev 0)
+++ core/branches/flat/src/main/java/org/jboss/starobrno/util/TypedProperties.java 2009-01-14 16:55:34 UTC (rev 7473)
@@ -0,0 +1,76 @@
+package org.jboss.starobrno.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import java.util.Properties;
+
+/**
+ * Type-aware properties. Extends the JDK {@link Properties} class to provide accessors that convert values to certain
+ * types, using default values if a conversion is not possible.
+ *
+ * @author Manik Surtani
+ */
+public class TypedProperties extends Properties
+{
+ private static final Log log = LogFactory.getLog(TypedProperties.class);
+
+ public TypedProperties(Properties p)
+ {
+ if (p != null) putAll(p);
+ }
+
+ public int getIntProperty(String key, int defaultValue)
+ {
+ String value = getProperty(key);
+ if (value == null) return defaultValue;
+ value = value.trim();
+ if (value.length() == 0) return defaultValue;
+
+ try
+ {
+ return Integer.parseInt(value);
+ }
+ catch (NumberFormatException nfe)
+ {
+ log.warn("Unable to convert string property [" + value + "] to an int! Using default value of " + defaultValue);
+ return defaultValue;
+ }
+ }
+
+ public long getLongProperty(String key, long defaultValue)
+ {
+ String value = getProperty(key);
+ if (value == null) return defaultValue;
+ value = value.trim();
+ if (value.length() == 0) return defaultValue;
+
+ try
+ {
+ return Long.parseLong(value);
+ }
+ catch (NumberFormatException nfe)
+ {
+ log.warn("Unable to convert string property [" + value + "] to a long! Using default value of " + defaultValue);
+ return defaultValue;
+ }
+ }
+
+ public boolean getBooleanProperty(String key, boolean defaultValue)
+ {
+ String value = getProperty(key);
+ if (value == null) return defaultValue;
+ value = value.trim();
+ if (value.length() == 0) return defaultValue;
+
+ try
+ {
+ return Boolean.parseBoolean(value);
+ }
+ catch (Exception e)
+ {
+ log.warn("Unable to convert string property [" + value + "] to a boolean! Using default value of " + defaultValue);
+ return defaultValue;
+ }
+ }
+}
More information about the jbosscache-commits
mailing list