diff --git a/src/main/java/org/apache/commons/lang3/EnumUtils.java b/src/main/java/org/apache/commons/lang3/EnumUtils.java index 0ebca28ee..88b2c3c53 100644 --- a/src/main/java/org/apache/commons/lang3/EnumUtils.java +++ b/src/main/java/org/apache/commons/lang3/EnumUtils.java @@ -119,13 +119,30 @@ public class EnumUtils { * @return the enum, null if not found */ public static > E getEnum(final Class enumClass, final String enumName) { + return getEnum(enumClass, enumName, null); + } + + /** + *

Gets the enum for the class, returning {@code defaultEnum} if not found.

+ * + *

This method differs from {@link Enum#valueOf} in that it does not throw an exception + * for an invalid enum name.

+ * + * @param the type of the enumeration + * @param enumClass the class of the enum to query, not null + * @param enumName the enum name, null returns default enum + * @param defaultEnum the default enum + * @return the enum, default enum if not found + * @since 3.10 + */ + public static > E getEnum(final Class enumClass, final String enumName, final E defaultEnum) { if (enumName == null) { - return null; + return defaultEnum; } try { return Enum.valueOf(enumClass, enumName); } catch (final IllegalArgumentException ex) { - return null; + return defaultEnum; } } @@ -142,15 +159,32 @@ public class EnumUtils { * @since 3.8 */ public static > E getEnumIgnoreCase(final Class enumClass, final String enumName) { + return getEnumIgnoreCase(enumClass, enumName, null); + } + + /** + *

Gets the enum for the class, returning {@code defaultEnum} if not found.

+ * + *

This method differs from {@link Enum#valueOf} in that it does not throw an exception + * for an invalid enum name and performs case insensitive matching of the name.

+ * + * @param the type of the enumeration + * @param enumClass the class of the enum to query, not null + * @param enumName the enum name, null returns default enum + * @param defaultEnum the default enum + * @return the enum, default enum if not found + * @since 3.10 + */ + public static > E getEnumIgnoreCase(final Class enumClass, final String enumName, final E defaultEnum) { if (enumName == null || !enumClass.isEnum()) { - return null; + return defaultEnum; } for (final E each : enumClass.getEnumConstants()) { if (each.name().equalsIgnoreCase(enumName)) { return each; } } - return null; + return defaultEnum; } /** diff --git a/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java b/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java index 8cf13b44e..90e8f51b9 100644 --- a/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java @@ -115,6 +115,20 @@ public class EnumUtilsTest { assertThrows(NullPointerException.class, () -> EnumUtils.getEnum((Class) null, "PURPLE")); } + @Test + public void test_getEnum_defaultEnum() { + assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED", Traffic.AMBER)); + assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "AMBER", Traffic.GREEN)); + assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "GREEN", Traffic.RED)); + assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.AMBER)); + assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.GREEN)); + assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "PURPLE", Traffic.RED)); + assertEquals(Traffic.AMBER, EnumUtils.getEnum(Traffic.class, null, Traffic.AMBER)); + assertEquals(Traffic.GREEN, EnumUtils.getEnum(Traffic.class, null, Traffic.GREEN)); + assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, null, Traffic.RED)); + assertNull(EnumUtils.getEnum(Traffic.class, "PURPLE", null)); + } + @Test public void test_getEnumIgnoreCase() { assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red")); @@ -135,6 +149,20 @@ public class EnumUtilsTest { assertThrows(NullPointerException.class, () -> EnumUtils.getEnumIgnoreCase((Class) null, "PURPLE")); } + @Test + public void test_getEnumIgnoreCase_defaultEnum() { + assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red", Traffic.AMBER)); + assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "Amber", Traffic.GREEN)); + assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "grEEn", Traffic.RED)); + assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", Traffic.AMBER)); + assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "purple", Traffic.GREEN)); + assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "pUrPlE", Traffic.RED)); + assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.AMBER)); + assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.GREEN)); + assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, null, Traffic.RED)); + assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "PURPLE", null)); + } + @Test public void test_generateBitVector_nullClass() { assertThrows(NullPointerException.class, () -> EnumUtils.generateBitVector(null, EnumSet.of(Traffic.RED)));