This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-lang.git
commit 700be0d8c1abff745020bbe6ed44a2c4c855a7b3 Author: Gary Gregory <[email protected]> AuthorDate: Wed Mar 17 08:55:26 2021 -0400 EnumUtils.getEnumSystemProperty(...). --- src/changes/changes.xml | 2 ++ .../java/org/apache/commons/lang3/EnumUtils.java | 22 ++++++++++++++++++++++ .../org/apache/commons/lang3/EnumUtilsTest.java | 15 +++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 92274b1..018e119 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -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)."> diff --git a/src/main/java/org/apache/commons/lang3/EnumUtils.java b/src/main/java/org/apache/commons/lang3/EnumUtils.java index d748538..bd6f8a2 100644 --- a/src/main/java/org/apache/commons/lang3/EnumUtils.java +++ b/src/main/java/org/apache/commons/lang3/EnumUtils.java @@ -295,6 +295,28 @@ public class EnumUtils { } /** + * <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> * * <p>This method differs from {@link Enum#valueOf} in that checks if the name is diff --git a/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java b/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java index c522fd4..e9d8e86 100644 --- a/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/EnumUtilsTest.java @@ -344,6 +344,21 @@ public class EnumUtilsTest { } @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")); assertTrue(EnumUtils.isValidEnum(Traffic.class, "AMBER"));
