Helper class to avoid managing NumberFormatException and similar code
and ensure consistent error messages across Configuration parsing problems.
| Method from org.hibernate.search.backend.configuration.ConfigurationParseHelper Detail: |
public static final int getIntValue(Properties cfg,
String key,
int defValue) {
String propValue = cfg.getProperty( key );
return parseInt( propValue, defValue, "Unable to parse " + key + ": " + propValue );
}
Looks for a numeric value in the Properties, returning
defValue if not found or if an empty string is found.
When the key the value is found but not in valid format
a standard error message is generated. |
public static final boolean parseBoolean(String value,
String errorMsgOnParseFailure) {
// avoiding Boolean.valueOf() to have more checks: makes it easy to spot wrong type in cfg.
if ( value == null ) {
throw new SearchException( errorMsgOnParseFailure );
}
else if ( "false".equalsIgnoreCase( value.trim() ) ) {
return false;
}
else if ( "true".equalsIgnoreCase( value.trim() ) ) {
return true;
}
else {
throw new SearchException( errorMsgOnParseFailure );
}
}
Parses a string to recognize exactly either "true" or "false". |
public static final int parseInt(String value,
String errorMsgOnParseFailure) {
if ( value == null ) {
throw new SearchException( errorMsgOnParseFailure );
}
else {
try {
return Integer.parseInt( value.trim() );
} catch (NumberFormatException nfe) {
throw new SearchException( errorMsgOnParseFailure, nfe );
}
}
}
Parses a String to get an int value. |
public static final int parseInt(String value,
int defValue,
String errorMsgOnParseFailure) {
if ( StringHelper.isEmpty( value ) ) {
return defValue;
}
else {
return parseInt( value, errorMsgOnParseFailure );
}
}
In case value is null or an empty string the defValue is returned |