HTraceConfiguration { public int getInt(String key, int defaultVal) { String val = get(key); if (val == null || val.trim().isEmpty()) { return defaultVal; } try { return Integer.parseInt(val); } catch (NumberFormatException nfe) { throw new IllegalArgumentException("Bad value for '" + key + "': should be int"); } }  static HTraceConfiguration fromMap(Map<String, String> conf); static HTraceConfiguration fromKeyValuePairs(String... pairs); abstract String get(String key); abstract String get(String key, String defaultValue); boolean getBoolean(String key, boolean defaultValue); int getInt(String key, int defaultVal); static final HTraceConfiguration EMPTY; }
@Test public void testGetInt() throws Exception { Map<String, String> m = new HashMap<String, String>(); m.put("a", "100"); m.put("b", "0"); m.put("c", "-100"); m.put("d", "5"); HTraceConfiguration configuration = HTraceConfiguration.fromMap(m); assertEquals(100, configuration.getInt("a", -999)); assertEquals(0, configuration.getInt("b", -999)); assertEquals(-100, configuration.getInt("c", -999)); assertEquals(5, configuration.getInt("d", -999)); assertEquals(-999, configuration.getInt("absent", -999)); }