HHH-2761 : null and empty string consistency in PropertiesHelper

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@12878 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2007-08-02 02:45:31 +00:00
parent e071f348a9
commit e9065e13a0
1 changed files with 42 additions and 34 deletions

View File

@ -7,58 +7,65 @@ import java.util.Properties;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Iterator; import java.util.Iterator;
public final class PropertiesHelper { public final class PropertiesHelper {
private static final String PLACEHOLDER_START = "${"; private static final String PLACEHOLDER_START = "${";
public static String getString(String propertyName, Properties properties, String defaultValue) {
String value = extractPropertyValue( propertyName, properties );
return value == null ? defaultValue : value;
}
private 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 boolean getBoolean(String property, Properties properties) { public static boolean getBoolean(String property, Properties properties) {
String setting = properties.getProperty(property); return getBoolean( property, properties, false );
return setting != null && Boolean.valueOf( setting.trim() ).booleanValue();
} }
public static boolean getBoolean(String property, Properties properties, boolean defaultValue) { public static boolean getBoolean(String propertyName, Properties properties, boolean defaultValue) {
String setting = properties.getProperty(property); String value = extractPropertyValue( propertyName, properties );
return setting==null ? defaultValue : Boolean.valueOf( setting.trim() ).booleanValue(); return value == null ? defaultValue : Boolean.valueOf( value ).booleanValue();
} }
public static int getInt(String property, Properties properties, int defaultValue) { public static int getInt(String propertyName, Properties properties, int defaultValue) {
String propValue = properties.getProperty(property); String value = extractPropertyValue( propertyName, properties );
return propValue==null ? defaultValue : Integer.parseInt( propValue.trim() ); return value == null ? defaultValue : Integer.parseInt( value );
} }
public static String getString(String property, Properties properties, String defaultValue) { public static Integer getInteger(String propertyName, Properties properties) {
String propValue = properties.getProperty(property); String value = extractPropertyValue( propertyName, properties );
return propValue==null ? defaultValue : propValue; return value == null ? null : Integer.valueOf( value );
} }
public static Integer getInteger(String property, Properties properties) { public static Map toMap(String propertyName, String delim, Properties properties) {
String propValue = properties.getProperty(property);
return propValue==null ? null : Integer.valueOf( propValue.trim() );
}
public static Map toMap(String property, String delim, Properties properties) {
Map map = new HashMap(); Map map = new HashMap();
String propValue = properties.getProperty(property); String value = extractPropertyValue( propertyName, properties );
if (propValue!=null) { if ( value != null ) {
StringTokenizer tokens = new StringTokenizer(propValue, delim); StringTokenizer tokens = new StringTokenizer( value, delim );
while ( tokens.hasMoreTokens() ) { while ( tokens.hasMoreTokens() ) {
map.put( map.put( tokens.nextToken(), tokens.hasMoreElements() ? tokens.nextToken() : "" );
tokens.nextToken(),
tokens.hasMoreElements() ? tokens.nextToken() : ""
);
} }
} }
return map; return map;
} }
public static String[] toStringArray(String property, String delim, Properties properties) { public static String[] toStringArray(String propertyName, String delim, Properties properties) {
return toStringArray( properties.getProperty(property), delim ); return toStringArray( extractPropertyValue( propertyName, properties ), delim );
} }
public static String[] toStringArray(String propValue, String delim) { public static String[] toStringArray(String stringForm, String delim) {
if (propValue!=null) { if ( stringForm != null ) {
return StringHelper.split(delim, propValue); return StringHelper.split( delim, stringForm );
} }
else { else {
return ArrayHelper.EMPTY_STRING_ARRAY; return ArrayHelper.EMPTY_STRING_ARRAY;
@ -145,5 +152,6 @@ public final class PropertiesHelper {
} }
private PropertiesHelper() {} private PropertiesHelper() {
}
} }