/** * Returns the parameter as a string or the * {@code defaultValue} if the parameter is {@code null}. * @param propValue the property value or {@code null} * @param defaultValue the default string value */ public static String toString(Object propValue, String defaultValue) { propValue = toObject(propValue); return (propValue != null) ? propValue.toString() : defaultValue; }
/** * Returns the parameter as a string or the * {@code defaultValue} if the parameter is {@code null}. * @param propValue the property value or {@code null} * @param defaultValue the default string value */ public static String toString(Object propValue, String defaultValue) { propValue = toObject(propValue); return (propValue != null) ? propValue.toString() : defaultValue; }
/** * Returns the parameter as a string or the * {@code defaultValue} if the parameter is {@code null}. * @param propValue the property value or {@code null} * @param defaultValue the default string value */ public static String toString(Object propValue, String defaultValue) { propValue = toObject(propValue); return (propValue != null) ? propValue.toString() : defaultValue; }
/** * Returns the parameter as an integer or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not an {@code Integer} and cannot be converted to * an {@code Integer} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default integer value */ public static int toInteger(Object propValue, int defaultValue) { propValue = toObject(propValue); if (propValue instanceof Integer) { return (Integer) propValue; } else if (propValue != null) { try { return Integer.parseInt(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
/** * Returns the boolean value of the parameter or the * {@code defaultValue} if the parameter is {@code null}. * If the parameter is not a {@code Boolean} it is converted * by calling {@code Boolean.valueOf} on the string value of the * object. * * @param propValue the property value or {@code null} * @param defaultValue the default boolean value */ public static boolean toBoolean(Object propValue, boolean defaultValue) { propValue = toObject(propValue); if (propValue instanceof Boolean) { return (Boolean) propValue; } else if (propValue != null) { return Boolean.parseBoolean(String.valueOf(propValue)); } return defaultValue; }
/** * Returns the parameter as a double or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not a {@code Double} and cannot be converted to * a {@code Double} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default double value */ public static double toDouble(Object propValue, double defaultValue) { propValue = toObject(propValue); if (propValue instanceof Double) { return (Double) propValue; } else if (propValue != null) { try { return Double.parseDouble(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
/** * Returns the parameter as a long or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not a {@code Long} and cannot be converted to * a {@code Long} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default long value */ public static long toLong(Object propValue, long defaultValue) { propValue = toObject(propValue); if (propValue instanceof Long) { return (Long) propValue; } else if (propValue != null) { try { return Long.parseLong(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
/** * Returns the boolean value of the parameter or the * {@code defaultValue} if the parameter is {@code null}. * If the parameter is not a {@code Boolean} it is converted * by calling {@code Boolean.valueOf} on the string value of the * object. * * @param propValue the property value or {@code null} * @param defaultValue the default boolean value */ public static boolean toBoolean(Object propValue, boolean defaultValue) { propValue = toObject(propValue); if (propValue instanceof Boolean) { return (Boolean) propValue; } else if (propValue != null) { return Boolean.parseBoolean(String.valueOf(propValue)); } return defaultValue; }
/** * Returns the boolean value of the parameter or the * {@code defaultValue} if the parameter is {@code null}. * If the parameter is not a {@code Boolean} it is converted * by calling {@code Boolean.valueOf} on the string value of the * object. * * @param propValue the property value or {@code null} * @param defaultValue the default boolean value */ public static boolean toBoolean(Object propValue, boolean defaultValue) { propValue = toObject(propValue); if (propValue instanceof Boolean) { return (Boolean) propValue; } else if (propValue != null) { return Boolean.parseBoolean(String.valueOf(propValue)); } return defaultValue; }
/** * Returns the parameter as a long or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not a {@code Long} and cannot be converted to * a {@code Long} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default long value */ public static long toLong(Object propValue, long defaultValue) { propValue = toObject(propValue); if (propValue instanceof Long) { return (Long) propValue; } else if (propValue != null) { try { return Long.parseLong(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
/** * Returns the parameter as a double or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not a {@code Double} and cannot be converted to * a {@code Double} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default double value */ public static double toDouble(Object propValue, double defaultValue) { propValue = toObject(propValue); if (propValue instanceof Double) { return (Double) propValue; } else if (propValue != null) { try { return Double.parseDouble(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
/** * Returns the parameter as a double or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not a {@code Double} and cannot be converted to * a {@code Double} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default double value */ public static double toDouble(Object propValue, double defaultValue) { propValue = toObject(propValue); if (propValue instanceof Double) { return (Double) propValue; } else if (propValue != null) { try { return Double.parseDouble(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
/** * Returns the parameter as a long or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not a {@code Long} and cannot be converted to * a {@code Long} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default long value */ public static long toLong(Object propValue, long defaultValue) { propValue = toObject(propValue); if (propValue instanceof Long) { return (Long) propValue; } else if (propValue != null) { try { return Long.parseLong(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
/** * Returns the parameter as an integer or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not an {@code Integer} and cannot be converted to * an {@code Integer} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default integer value */ public static int toInteger(Object propValue, int defaultValue) { propValue = toObject(propValue); if (propValue instanceof Integer) { return (Integer) propValue; } else if (propValue != null) { try { return Integer.parseInt(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
/** * Returns the parameter as an integer or the * {@code defaultValue} if the parameter is {@code null} or if * the parameter is not an {@code Integer} and cannot be converted to * an {@code Integer} from the parameter's string value. * * @param propValue the property value or {@code null} * @param defaultValue the default integer value */ public static int toInteger(Object propValue, int defaultValue) { propValue = toObject(propValue); if (propValue instanceof Integer) { return (Integer) propValue; } else if (propValue != null) { try { return Integer.parseInt(String.valueOf(propValue)); } catch (NumberFormatException nfe) { // don't care, fall through to default value } } return defaultValue; }
@Test public void testToObject() { assertEquals("hallo", PropertiesUtil.toObject("hallo")); assertEquals("1", PropertiesUtil.toObject(new String[]{"1", "2"})); assertEquals(null, PropertiesUtil.toObject(null)); assertEquals(null, PropertiesUtil.toObject(new String[]{})); final List<String> l = new ArrayList<String>(); assertEquals(null, PropertiesUtil.toObject(l)); l.add("1"); assertEquals("1", PropertiesUtil.toObject(l)); l.add("2"); assertEquals("1", PropertiesUtil.toObject(l)); final Map<String, Object> m = new HashMap<String, Object>(); assertEquals(m, PropertiesUtil.toObject(m)); }