DataUtil { public static long getLong(Object value, long defValue) { if (value == null) { return defValue; } long result; if (value instanceof Number) { result = ((Number)value).longValue(); } else if (value instanceof String) { String str = (String)value; try { result = Long.parseLong(str.trim(), 10); } catch (Exception ex) { result = defValue; } } else if (value instanceof Boolean) { Boolean b = (Boolean)value; return b ? 1l : 0l; } 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 getLong() throws Exception { assertEquals(DataUtil.getLong(null, 8), 8); assertEquals(DataUtil.getLong(Boolean.TRUE, 8), 1); assertEquals(DataUtil.getLong(Boolean.FALSE, 9), 0); assertEquals(DataUtil.getLong("True", 8), 8); assertEquals(DataUtil.getLong("99", 88), 99); assertEquals(DataUtil.getLong(" 88 ", 99), 88); assertEquals(DataUtil.getLong("DDDDD", 999), 999); assertEquals(DataUtil.getLong(new Object(), 333), 333); assertEquals(DataUtil.getLong(888l, 333), 888); }