DataUtil { public static double getDouble(Object value, double defValue) { if (value == null) { return defValue; } double result; if (value instanceof Number) { result = ((Number)value).doubleValue(); } else if (value instanceof String) { String str = (String)value; try { result = Double.parseDouble(str.trim()); } catch (Exception ex) { result = defValue; } } else if (value instanceof Boolean) { Boolean b = (Boolean)value; return b ? 1d : 0d; } else { result = defValue; } return result; }  static boolean getBoolean(Object value, boolean defValue); static Boolean doGetBoolean(Object value, Boolean defValue); static boolean toBoolean(String value, boolean defValue); static Boolean convertToBoolean(String value, Boolean defValue); static boolean toBoolean(String value); static int getInt(Object value, int defValue); static long getLong(Object value, long defValue); static double getDouble(Object value, double defValue); static String getString(Object value, String defValue); static String[] getStringArray(Object object, String[] defValue); static int[] getIntArray(Object object, int[] defValue); static List<String> getStringList(Object object);  }
@Test public void getDouble() throws Exception { assertEquals(DataUtil.getDouble(null, 8), 8, 0.1); assertEquals(DataUtil.getDouble(Boolean.TRUE, 8), 1, 0.1); assertEquals(DataUtil.getDouble(Boolean.FALSE, 9), 0, 0.1); assertEquals(DataUtil.getDouble("True", 8), 8, 0.1); assertEquals(DataUtil.getDouble("99", 88), 99, 0.1); assertEquals(DataUtil.getDouble(" 88 ", 99), 88, 0.1); assertEquals(DataUtil.getDouble("DDDDD", 999), 999, 0.1); assertEquals(DataUtil.getDouble(new Object(), 333), 333, 0.1); assertEquals(DataUtil.getDouble(888l, 333), 888, 0.1); }