From 0b70b01a9f1d9664bb5eac737175f58f06979b60 Mon Sep 17 00:00:00 2001 From: Ruslan Sibgatullin Date: Thu, 14 Sep 2017 23:19:41 +0300 Subject: [PATCH] LANG-1352: EnumUtils.getEnumIgnoreCase and isValidEnumIgnoreCase methods added (closes #286) --- .../org/apache/commons/lang3/EnumUtils.java | 49 +++++++++++++++---- .../apache/commons/lang3/EnumUtilsTest.java | 44 ++++++++++++++++- 2 files changed, 82 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/commons/lang3/EnumUtils.java b/src/main/java/org/apache/commons/lang3/EnumUtils.java index ab062ad05..5aa76689d 100644 --- a/src/main/java/org/apache/commons/lang3/EnumUtils.java +++ b/src/main/java/org/apache/commons/lang3/EnumUtils.java @@ -87,15 +87,23 @@ public class EnumUtils { * @return true if the enum name is valid, otherwise false */ public static > boolean isValidEnum(final Class enumClass, final String enumName) { - if (enumName == null) { - return false; - } - try { - Enum.valueOf(enumClass, enumName); - return true; - } catch (final IllegalArgumentException ex) { - return false; - } + return getEnum(enumClass, enumName) != null; + } + + /** + *

Checks if the specified name is a valid enum for the class.

+ * + *

This method differs from {@link Enum#valueOf} in that checks if the name is + * a valid enum without needing to catch the exception + * 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 false + * @return true if the enum name is valid, otherwise false + */ + public static > boolean isValidEnumIgnoreCase(final Class enumClass, final String enumName) { + return getEnumIgnoreCase(enumClass, enumName) != null; } /** @@ -120,6 +128,29 @@ public class EnumUtils { } } + /** + *

Gets the enum for the class, returning {@code null} 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 null + * @return the enum, null if not found + */ + public static > E getEnumIgnoreCase(final Class enumClass, final String enumName) { + if (enumName == null || !enumClass.isEnum()) { + return null; + } + for (final E each : enumClass.getEnumConstants()) { + if (each.name().equalsIgnoreCase(enumName)) { + return each; + } + } + return null; + } + /** *

Creates a long bit vector representation of the given subset of an Enum.

* diff --git a/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java b/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java index f17dddae6..7c732b28a 100644 --- a/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java @@ -67,7 +67,7 @@ public class EnumUtilsTest { } @Test - public void test_isEnum() { + public void test_isValidEnum() { assertTrue(EnumUtils.isValidEnum(Traffic.class, "RED")); assertTrue(EnumUtils.isValidEnum(Traffic.class, "AMBER")); assertTrue(EnumUtils.isValidEnum(Traffic.class, "GREEN")); @@ -76,10 +76,24 @@ public class EnumUtilsTest { } @Test(expected=NullPointerException.class) - public void test_isEnum_nullClass() { + public void test_isValidEnum_nullClass() { EnumUtils.isValidEnum(null, "PURPLE"); } + @Test + public void test_isValidEnumIgnoreCase() { + assertTrue(EnumUtils.isValidEnumIgnoreCase(Traffic.class, "red")); + assertTrue(EnumUtils.isValidEnumIgnoreCase(Traffic.class, "Amber")); + assertTrue(EnumUtils.isValidEnumIgnoreCase(Traffic.class, "grEEn")); + assertFalse(EnumUtils.isValidEnumIgnoreCase(Traffic.class, "purple")); + assertFalse(EnumUtils.isValidEnumIgnoreCase(Traffic.class, null)); + } + + @Test(expected=NullPointerException.class) + public void test_isValidEnumIgnoreCase_nullClass() { + EnumUtils.isValidEnumIgnoreCase(null, "PURPLE"); + } + @Test public void test_getEnum() { assertEquals(Traffic.RED, EnumUtils.getEnum(Traffic.class, "RED")); @@ -89,11 +103,37 @@ public class EnumUtilsTest { assertNull(EnumUtils.getEnum(Traffic.class, null)); } + @Test + public void test_getEnum_nonEnumClass() { + final Class rawType = Object.class; + assertNull(EnumUtils.getEnum(rawType, "rawType")); + } + @Test(expected=NullPointerException.class) public void test_getEnum_nullClass() { EnumUtils.getEnum((Class) null, "PURPLE"); } + @Test + public void test_getEnumIgnoreCase() { + assertEquals(Traffic.RED, EnumUtils.getEnumIgnoreCase(Traffic.class, "red")); + assertEquals(Traffic.AMBER, EnumUtils.getEnumIgnoreCase(Traffic.class, "Amber")); + assertEquals(Traffic.GREEN, EnumUtils.getEnumIgnoreCase(Traffic.class, "grEEn")); + assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, "purple")); + assertNull(EnumUtils.getEnumIgnoreCase(Traffic.class, null)); + } + + @Test + public void test_getEnumIgnoreCase_nonEnumClass() { + final Class rawType = Object.class; + assertNull(EnumUtils.getEnumIgnoreCase(rawType, "rawType")); + } + + @Test(expected=NullPointerException.class) + public void test_getEnumIgnoreCase_nullClass() { + EnumUtils.getEnumIgnoreCase((Class) null, "PURPLE"); + } + @Test(expected=NullPointerException.class) public void test_generateBitVector_nullClass() { EnumUtils.generateBitVector(null, EnumSet.of(Traffic.RED));