@Test public void testFromValues() { Function<String, EnumTest> nameLookup = EnumUtils.lookupMap(EnumTest.values(), e -> e.getName()); Function<Integer, EnumTest> numLookup = EnumUtils.lookupMap(EnumTest.values(), e -> e.getNum()); for (EnumTest cur : EnumTest.values()) { assertEquals(cur, nameLookup.apply(cur.getName())); assertEquals(cur, numLookup.apply(cur.getNum())); } assertEquals(EnumTest.ZERO, nameLookup.apply("zero")); assertEquals(EnumTest.ZERO, numLookup.apply(0)); }
public static <T, E extends Enum<E>> Function<T, E> lookupMap(Class<E> clazz, Function<E, T> mapper) { @SuppressWarnings("unchecked") E[] emptyArray = (E[]) Array.newInstance(clazz, 0); return lookupMap(EnumSet.allOf(clazz).toArray(emptyArray), mapper); }
EnumUtils { public static <T, E extends Enum<E>> Function<T, E> lookupMap(Class<E> clazz, Function<E, T> mapper) { @SuppressWarnings("unchecked") E[] emptyArray = (E[]) Array.newInstance(clazz, 0); return lookupMap(EnumSet.allOf(clazz).toArray(emptyArray), mapper); } }
EnumUtils { public static <T, E extends Enum<E>> Function<T, E> lookupMap(Class<E> clazz, Function<E, T> mapper) { @SuppressWarnings("unchecked") E[] emptyArray = (E[]) Array.newInstance(clazz, 0); return lookupMap(EnumSet.allOf(clazz).toArray(emptyArray), mapper); }  }
EnumUtils { public static <T, E extends Enum<E>> Function<T, E> lookupMap(Class<E> clazz, Function<E, T> mapper) { @SuppressWarnings("unchecked") E[] emptyArray = (E[]) Array.newInstance(clazz, 0); return lookupMap(EnumSet.allOf(clazz).toArray(emptyArray), mapper); }  static Function<T, E> lookupMap(Class<E> clazz, Function<E, T> mapper); static Function<T, E> lookupMap(E[] values, Function<E, T> mapper); }
EnumUtils { public static <T, E extends Enum<E>> Function<T, E> lookupMap(Class<E> clazz, Function<E, T> mapper) { @SuppressWarnings("unchecked") E[] emptyArray = (E[]) Array.newInstance(clazz, 0); return lookupMap(EnumSet.allOf(clazz).toArray(emptyArray), mapper); }  static Function<T, E> lookupMap(Class<E> clazz, Function<E, T> mapper); static Function<T, E> lookupMap(E[] values, Function<E, T> mapper);  }