This commit is contained in:
Gary Gregory 2019-10-28 14:29:00 -04:00
commit be1536fe56
2 changed files with 66 additions and 4 deletions

View File

@ -119,13 +119,30 @@ public class EnumUtils {
* @return the enum, null if not found
*/
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) {
return getEnum(enumClass, enumName, null);
}
/**
* <p>Gets the enum for the class, returning {@code defaultEnum} if not found.</p>
*
* <p>This method differs from {@link Enum#valueOf} in that it does not throw an exception
* for an invalid enum name.</p>
*
* @param <E> 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 extends Enum<E>> E getEnum(final Class<E> 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 extends Enum<E>> E getEnumIgnoreCase(final Class<E> enumClass, final String enumName) {
return getEnumIgnoreCase(enumClass, enumName, null);
}
/**
* <p>Gets the enum for the class, returning {@code defaultEnum} if not found.</p>
*
* <p>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.</p>
*
* @param <E> 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 extends Enum<E>> E getEnumIgnoreCase(final Class<E> 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;
}
/**

View File

@ -115,6 +115,20 @@ public class EnumUtilsTest {
assertThrows(NullPointerException.class, () -> EnumUtils.getEnum((Class<Traffic>) 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<Traffic>) 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)));