Util { public static <T extends Enum<T>> T valueOfOrDefault(String name, Class<T> type, T def) { T ret; try { ret = Enum.valueOf(type, name); } catch (Exception e) { ret = def; } return ret; } private  Util(); static T valueOfOrNull(String name, Class<T> type); static T valueOfOrDefault(String name, Class<T> type, T def); static String nameOfOrNull(String name, Class<T> type);  }
@Test public void testValueOfOrDefault() throws Exception { assertThat(Util.valueOfOrDefault("collapsed", Block.class, Block.expanded), is(Block.collapsed)); assertThat(Util.valueOfOrDefault("null", Block.class, Block.expanded), is(Block.expanded)); }