Details
-
Improvement
-
Status: Resolved
-
Trivial
-
Resolution: Fixed
-
None
-
None
-
None
Description
I would like to add 2 overloading methods to EnumUtils, getEnum and getEnumIgnoreCase.
Instead of returning null, if not found. In my opinion I think allowing user to set a default result is a better approach or return an Optional.
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) { return getEnum(enumClass, enumName, null); } public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName, E defaultEnum) { if (enumName == null) { return defaultEnum; } try { return Enum.valueOf(enumClass, enumName); } catch (final IllegalArgumentException ex) { return defaultEnum; } }
public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName) { return getEnumIgnoreCase(enumClass, enumName, null); } public static <E extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName, E defaultEnum) { if (enumName == null || !enumClass.isEnum()) { return defaultEnum; } for (final E each : enumClass.getEnumConstants()) { if (each.name().equalsIgnoreCase(enumName)) { return each; } } return defaultEnum; }
Attachments
Issue Links
- links to