Author: steve.ebersole(a)jboss.com
Date: 2007-08-02 23:58:13 -0400 (Thu, 02 Aug 2007)
New Revision: 12890
Modified:
core/branches/Branch_3_2/src/org/hibernate/util/PropertiesHelper.java
Log:
HHH-2761 : null and empty string consistency in PropertiesHelper
Modified: core/branches/Branch_3_2/src/org/hibernate/util/PropertiesHelper.java
===================================================================
--- core/branches/Branch_3_2/src/org/hibernate/util/PropertiesHelper.java 2007-08-03
03:57:59 UTC (rev 12889)
+++ core/branches/Branch_3_2/src/org/hibernate/util/PropertiesHelper.java 2007-08-03
03:58:13 UTC (rev 12890)
@@ -7,58 +7,178 @@
import java.util.StringTokenizer;
import java.util.Iterator;
-
+/**
+ * Collection of helper methods for dealing with {@link java.util.Properties}
+ * objects.
+ *
+ * @author Gavin King
+ * @author Steve Ebersole
+ */
public final class PropertiesHelper {
private static final String PLACEHOLDER_START = "${";
- public static boolean getBoolean(String property, Properties properties) {
- String setting = properties.getProperty(property);
- return setting != null && Boolean.valueOf( setting.trim() ).booleanValue();
+ /**
+ * Disallow instantiation
+ */
+ private PropertiesHelper() {
}
- public static boolean getBoolean(String property, Properties properties, boolean
defaultValue) {
- String setting = properties.getProperty(property);
- return setting==null ? defaultValue : Boolean.valueOf( setting.trim()
).booleanValue();
+ /**
+ * Get a property value as a string.
+ *
+ * @see #extractPropertyValue(String, java.util.Properties)
+ *
+ * @param propertyName The name of the property for which to retrieve value
+ * @param properties The properties object
+ * @param defaultValue The default property value to use.
+ * @return The property value; may be null.
+ */
+ public static String getString(String propertyName, Properties properties, String
defaultValue) {
+ String value = extractPropertyValue( propertyName, properties );
+ return value == null ? defaultValue : value;
}
- public static int getInt(String property, Properties properties, int defaultValue) {
- String propValue = properties.getProperty(property);
- return propValue==null ? defaultValue : Integer.parseInt( propValue.trim() );
+ /**
+ * Extract a property value by name from the given properties object.
+ * <p/>
+ * Both <tt>null</tt> and <tt>empty string</tt> are viewed as
the same, and return null.
+ *
+ * @param propertyName The name of the property for which to extract value
+ * @param properties The properties object
+ * @return The property value; may be null.
+ */
+ public static String extractPropertyValue(String propertyName, Properties properties) {
+ String value = properties.getProperty( propertyName );
+ if ( value == null ) {
+ return null;
+ }
+ value = value.trim();
+ if ( StringHelper.isEmpty( value ) ) {
+ return null;
+ }
+ return value;
}
- public static String getString(String property, Properties properties, String
defaultValue) {
- String propValue = properties.getProperty(property);
- return propValue==null ? defaultValue : propValue;
+ /**
+ * Get a property value as a boolean. Shorthand for calling
+ * {@link #getBoolean(String, java.util.Properties, boolean)} with
<tt>false</tt>
+ * as the default value.
+ *
+ * @param propertyName The name of the property for which to retrieve value
+ * @param properties The properties object
+ * @return The property value.
+ */
+ public static boolean getBoolean(String propertyName, Properties properties) {
+ return getBoolean( propertyName, properties, false );
}
- public static Integer getInteger(String property, Properties properties) {
- String propValue = properties.getProperty(property);
- return propValue==null ? null : Integer.valueOf( propValue.trim() );
+ /**
+ * Get a property value as a boolean.
+ * <p/>
+ * First, the string value is extracted, and then {@link Boolean#valueOf(String)} is
+ * used to determine the correct boolean value.
+ *
+ * @see #extractPropertyValue(String, java.util.Properties)
+ *
+ * @param propertyName The name of the property for which to retrieve value
+ * @param properties The properties object
+ * @param defaultValue The default property value to use.
+ * @return The property value.
+ */
+ public static boolean getBoolean(String propertyName, Properties properties, boolean
defaultValue) {
+ String value = extractPropertyValue( propertyName, properties );
+ return value == null ? defaultValue : Boolean.valueOf( value ).booleanValue();
}
- public static Map toMap(String property, String delim, Properties properties) {
+ /**
+ * Get a property value as an int.
+ * <p/>
+ * First, the string value is extracted, and then {@link Integer#parseInt(String)} is
+ * used to determine the correct int value for any non-null property values.
+ *
+ * @see #extractPropertyValue(String, java.util.Properties)
+ *
+ * @param propertyName The name of the property for which to retrieve value
+ * @param properties The properties object
+ * @param defaultValue The default property value to use.
+ * @return The property value.
+ */
+ public static int getInt(String propertyName, Properties properties, int defaultValue)
{
+ String value = extractPropertyValue( propertyName, properties );
+ return value == null ? defaultValue : Integer.parseInt( value );
+ }
+
+ /**
+ * Get a property value as an Integer.
+ * <p/>
+ * First, the string value is extracted, and then {@link Integer#valueOf(String)} is
+ * used to determine the correct boolean value for any non-null property values.
+ *
+ * @see #extractPropertyValue(String, java.util.Properties)
+ *
+ * @param propertyName The name of the property for which to retrieve value
+ * @param properties The properties object
+ * @return The property value; may be null.
+ */
+ public static Integer getInteger(String propertyName, Properties properties) {
+ String value = extractPropertyValue( propertyName, properties );
+ return value == null ? null : Integer.valueOf( value );
+ }
+
+ /**
+ * Constructs a map from a property value.
+ * <p/>
+ * The exact behavior here is largely dependant upon what is passed in as
+ * the delimiter.
+ *
+ * @see #extractPropertyValue(String, java.util.Properties)
+ *
+ * @param propertyName The name of the property for which to retrieve value
+ * @param delim The string defining tokens used as both entry and key/value delimiters.
+ * @param properties The properties object
+ * @return The resulting map; never null, though perhaps empty.
+ */
+ public static Map toMap(String propertyName, String delim, Properties properties) {
Map map = new HashMap();
- String propValue = properties.getProperty(property);
- if (propValue!=null) {
- StringTokenizer tokens = new StringTokenizer(propValue, delim);
+ String value = extractPropertyValue( propertyName, properties );
+ if ( value != null ) {
+ StringTokenizer tokens = new StringTokenizer( value, delim );
while ( tokens.hasMoreTokens() ) {
- map.put(
- tokens.nextToken(),
- tokens.hasMoreElements() ? tokens.nextToken() : ""
- );
+ map.put( tokens.nextToken(), tokens.hasMoreElements() ? tokens.nextToken() :
"" );
}
}
return map;
}
- public static String[] toStringArray(String property, String delim, Properties
properties) {
- return toStringArray( properties.getProperty(property), delim );
+ /**
+ * Get a property value as a string array.
+ *
+ * @see #extractPropertyValue(String, java.util.Properties)
+ * @see #toStringArray(String, String)
+ *
+ * @param propertyName The name of the property for which to retrieve value
+ * @param delim The delimiter used to separate individual array elements.
+ * @param properties The properties object
+ * @return The array; never null, though may be empty.
+ */
+ public static String[] toStringArray(String propertyName, String delim, Properties
properties) {
+ return toStringArray( extractPropertyValue( propertyName, properties ), delim );
}
- public static String[] toStringArray(String propValue, String delim) {
- if (propValue!=null) {
- return StringHelper.split(delim, propValue);
+ /**
+ * Convert a string to an array of strings. The assumption is that
+ * the individual array elements are delimited in the source stringForm
+ * param by the delim param.
+ *
+ * @param stringForm The string form of the string array.
+ * @param delim The delimiter used to separate individual array elements.
+ * @return The array; never null, though may be empty.
+ */
+ public static String[] toStringArray(String stringForm, String delim) {
+ // todo : move to StringHelper?
+ if ( stringForm != null ) {
+ return StringHelper.split( delim, stringForm );
}
else {
return ArrayHelper.EMPTY_STRING_ARRAY;
@@ -73,13 +193,18 @@
* @return cloned and masked properties
*/
public static Properties maskOut(Properties props, String key) {
- Properties clone = (Properties) props.clone();
- if (clone.get(key) != null) {
- clone.setProperty(key, "****");
+ Properties clone = ( Properties ) props.clone();
+ if ( clone.get( key ) != null ) {
+ clone.setProperty( key, "****" );
}
return clone;
}
+ /**
+ * Handles interpolation processing for all entries in a properties object.
+ *
+ * @param properties The properties object.
+ */
public static void resolvePlaceHolders(Properties properties) {
Iterator itr = properties.entrySet().iterator();
while ( itr.hasNext() ) {
@@ -99,6 +224,12 @@
}
}
+ /**
+ * Handles interpolation processing for a single property.
+ *
+ * @param property The property value to be processed for interpolation.
+ * @return The (possibly) interpolated property value.
+ */
public static String resolvePlaceHolder(String property) {
if ( property.indexOf( PLACEHOLDER_START ) < 0 ) {
return property;
@@ -143,7 +274,4 @@
return null;
}
}
-
-
- private PropertiesHelper() {}
}