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-configuration.git

commit 2a20bf267662b709958c3d3c77ad8c16ce3312b9
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Mon Apr 27 09:45:24 2020 -0400

    [CONFIGURATION-789] Add ImmutableConfiguration.getEnum() methods.
---
 src/changes/changes.xml                            |  3 ++
 .../configuration2/ImmutableConfiguration.java     | 48 ++++++++++++++++++++++
 .../configuration2/ex/ConversionException.java     |  2 +-
 .../apache/commons/configuration2/EnumFixture.java | 23 +++++++++++
 .../configuration2/TestBaseConfiguration.java      | 29 ++++++++++++-
 5 files changed, 103 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 764e1af..e124773 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -27,6 +27,9 @@
        <action type="update" dev="ggregory" issue="CONFIGURATION-787" 
due-to="Gary Gregory">
          Update from Apache Commons Lang 3.9 to 3.10.
        </action>
+       <action type="add" dev="ggregory" issue="CONFIGURATION-789" 
due-to="Gary Gregory">
+         Add ImmutableConfiguration.getEnum() methods.
+       </action>
     </release>
     <release version="2.7" date="2020-03-07"
              description="Minor release with new features and updated 
dependencies.">
diff --git 
a/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java 
b/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java
index d77d0b8..d2e7d93 100644
--- 
a/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java
+++ 
b/src/main/java/org/apache/commons/configuration2/ImmutableConfiguration.java
@@ -23,6 +23,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
 
+import org.apache.commons.configuration2.ex.ConversionException;
+
 /**
  * <p>The main interface for accessing configuration data in a read-only 
fashion.</p>
  * <p>
@@ -492,6 +494,52 @@ public interface ImmutableConfiguration
     String getEncodedString(String key);
 
     /**
+     * Gets an enum associated with the given configuration key.
+     *
+     * @param <T> The enum type whose constant is to be returned.
+     * @param enumType the {@code Class} object of the enum type from which to 
return a constant
+     * @param key The configuration key.
+     * @return The associated enum.
+     *
+     * @throws org.apache.commons.configuration2.ex.ConversionException is 
thrown if the key maps to an object that
+     *         is not a String.
+     * @since 2.8
+     */
+    default <T extends Enum<T>> T getEnum(String key, Class<T> enumType) {
+        try {
+            return Enum.valueOf(enumType, getString(key));
+        } catch (IllegalArgumentException e) {
+            throw new ConversionException(e);
+        }
+    }
+
+    /**
+     * Gets the enum associated with the given configuration key. If the key 
doesn't map to an existing object, the
+     * default value is returned.
+     * 
+     * @param <T> The enum type whose constant is to be returned.
+     * @param key The configuration key.
+     * @param enumType the {@code Class} object of the enum type from which to 
return a constant
+     * @param defaultValue The default value.
+     * @return The associated enum if key is found and has valid format, 
default value otherwise.
+     *
+     * @throws org.apache.commons.configuration2.ex.ConversionException is 
thrown if the key maps to an object that is
+     *         not a Enum.
+     * @since 2.8
+     */
+    default <T extends Enum<T>> T getEnum(String key, Class<T> enumType, T 
defaultValue) {
+        final String strValue = getString(key, null);
+        if (strValue == null) {
+            return defaultValue;
+        }
+        try {
+            return Enum.valueOf(enumType, strValue);
+        } catch (IllegalArgumentException e) {
+            throw new ConversionException(e);
+        }
+    }
+
+    /**
      * Get an array of strings associated with the given configuration key.
      * If the key doesn't map to an existing object an empty array is returned
      *
diff --git 
a/src/main/java/org/apache/commons/configuration2/ex/ConversionException.java 
b/src/main/java/org/apache/commons/configuration2/ex/ConversionException.java
index 81f8f8c..6a3e3e7 100644
--- 
a/src/main/java/org/apache/commons/configuration2/ex/ConversionException.java
+++ 
b/src/main/java/org/apache/commons/configuration2/ex/ConversionException.java
@@ -19,7 +19,7 @@ package org.apache.commons.configuration2.ex;
 
 
 /**
- * Exception thrown when a property is incompatible with the type requested.
+ * Thrown when a property is incompatible with the type requested.
  *
  * @since 1.0
  *
diff --git a/src/test/java/org/apache/commons/configuration2/EnumFixture.java 
b/src/test/java/org/apache/commons/configuration2/EnumFixture.java
new file mode 100644
index 0000000..8d0688a
--- /dev/null
+++ b/src/test/java/org/apache/commons/configuration2/EnumFixture.java
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.configuration2;
+
+public enum EnumFixture {
+
+    JAVA, SMALLTALK, SCALA
+}
diff --git 
a/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java 
b/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java
index 3c56005..6d52866 100644
--- a/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestBaseConfiguration.java
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.math.BigDecimal;
 import java.math.BigInteger;
@@ -59,7 +60,6 @@ public class TestBaseConfiguration
     static final String KEY_NUMBER = "number";
 
     protected static Class<?> missingElementException = 
NoSuchElementException.class;
-
     protected static Class<?> incompatibleElementException = 
ConversionException.class;
     protected BaseConfiguration config = null;
 
@@ -527,6 +527,33 @@ public class TestBaseConfiguration
         assertEquals("Missing key with default value", defaultValue, 
config.getString("stringNotInConfig", defaultValue));
     }
 
+    @Test
+    public void testGetEnum()
+    {
+        config.setProperty("testEnum", EnumFixture.SMALLTALK.name());
+        config.setProperty("testBadEnum", "This is not an enum value.");
+        final EnumFixture enum1 = EnumFixture.SMALLTALK;
+        final EnumFixture defaultValue = EnumFixture.JAVA;
+        //
+        assertEquals("Existing key", enum1, config.getEnum("testEnum", 
EnumFixture.class));
+        assertEquals("Existing key with default value", enum1, 
config.getEnum("testEnum", EnumFixture.class, defaultValue));
+        assertEquals("Missing key with default value", defaultValue, 
config.getEnum("stringNotInConfig", EnumFixture.class, defaultValue));
+        //
+        try {
+            config.getEnum("testBadEnum", EnumFixture.class);
+            fail("Expected " + ConversionException.class);
+        } catch (ConversionException e) {
+            // expected
+        }
+        //
+        try {
+            config.getEnum("testBadEnum", EnumFixture.class, defaultValue);
+            fail("Expected " + ConversionException.class);
+        } catch (ConversionException e) {
+            // expected
+        }
+    }
+
     /**
      * Tests that the first scalar of a list is returned.
      */

Reply via email to