IntegerUtil { public static int tryParse(String value, int defaultValue) { try { return parseInt(value); } catch (NumberFormatException e) { return defaultValue; } }  static int tryParse(String value, int defaultValue); static boolean isInteger(String value); static String parseValidIntegersAndDefaultInvalidOnesToEmptyString(String value);  }
@Test public void shouldParseIntSafely() throws Exception { assertEquals(0, IntegerUtil.tryParse("", 0)); assertEquals(2, IntegerUtil.tryParse(null, 2)); assertEquals(0, IntegerUtil.tryParse("0", 1)); assertEquals(3, IntegerUtil.tryParse("3", 3)); }