EnumUtils.getEnumSystemProperty(...).
This commit is contained in:
parent
64e6ddbc6b
commit
700be0d8c1
|
@ -49,6 +49,8 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
<!-- FIX -->
|
||||
<action issue="LANG-1645" type="fix" dev="aherbert" due-to="Alex Herbert">NumberUtils.createNumber to recognise hex integers prefixed with +.</action>
|
||||
<action issue="LANG-1646" type="fix" dev="aherbert" due-to="Alex Herbert">NumberUtils.createNumber to return requested floating point type for zero.</action>
|
||||
<!-- ADD -->
|
||||
<action type="add" dev="ggregory" due-to="Gary Gregory">EnumUtils.getEnumSystemProperty(...).</action>
|
||||
</release>
|
||||
|
||||
<release version="3.12.0" date="2021-02-26" description="New features and bug fixes (Java 8).">
|
||||
|
|
|
@ -294,6 +294,28 @@ public class EnumUtils {
|
|||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Gets the enum for the class in a system property, 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 propName the system property key for the enum name, null returns default enum
|
||||
* @param defaultEnum the default enum
|
||||
* @return the enum, default enum if not found
|
||||
* @since 3.13.0
|
||||
*/
|
||||
public static <E extends Enum<E>> E getEnumSystemProperty(final Class<E> enumClass, final String propName,
|
||||
final E defaultEnum) {
|
||||
return enumClass == null || propName == null ? defaultEnum
|
||||
: getEnum(enumClass, System.getProperty(propName), defaultEnum);
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>Checks if the specified name is a valid enum for the class.</p>
|
||||
*
|
||||
|
|
|
@ -343,6 +343,21 @@ public class EnumUtilsTest {
|
|||
assertFalse(test.containsKey("PURPLE"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_getEnumSystemProperty() {
|
||||
final String key = getClass().getName();
|
||||
System.setProperty(key, Traffic.RED.toString());
|
||||
try {
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(Traffic.class, key, null));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(Traffic.class, "?", Traffic.RED));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(null, null, Traffic.RED));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(null, "?", Traffic.RED));
|
||||
assertEquals(Traffic.RED, EnumUtils.getEnumSystemProperty(Traffic.class, null, Traffic.RED));
|
||||
} finally {
|
||||
System.getProperties().remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_isValidEnum() {
|
||||
assertTrue(EnumUtils.isValidEnum(Traffic.class, "RED"));
|
||||
|
|
Loading…
Reference in New Issue