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