Method cast(Class, String) builds the enum value constant of the specified Enum. The name must match exactly (case) an identifier used to declare an enum constant in the given Enum.
@SuppressWarnings({ "rawtypes", "unchecked" }) public static <E extends Enum<E>> E cast(String name, Class<E> clazz) { if (clazz == null || name == null || name.isEmpty()) { return null; } try { return (E) Enum.valueOf((Class<Enum>) clazz, name); } catch (Throwable e) { throw new RuntimeException("Enum cast error: '" + name + "' in not valid value of '" + clazz.getName() + "'"); } }This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
@SuppressWarnings({ "rawtypes", "unchecked" }) public static <E extends Enum<E>> E cast(String name, Class<E> clazz) { if (clazz == null || name == null || name.isEmpty()) { return null; } try { return (E) Enum.valueOf((Class<Enum>) clazz, name); } catch (Throwable e) { throw new RuntimeException("Enum cast error: '" + name + "' in not valid value of '" + clazz.getName() + "'"); } }