Hi, i have modified the Configuration interface to support the last two Number types we don't have yet, that's the BigInteger and the BigDecimal classes. I think it's a safe change before the 1.0 release.

Emmanuel Bourg

Index: src/java/org/apache/commons/configuration/AbstractConfiguration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/configuration/src/java/org/apache/commons/configuration/AbstractConfiguration.java,v
retrieving revision 1.3
diff -u -r1.3 AbstractConfiguration.java
--- src/java/org/apache/commons/configuration/AbstractConfiguration.java        16 Jan 
2004 14:27:57 -0000      1.3
+++ src/java/org/apache/commons/configuration/AbstractConfiguration.java        12 Feb 
2004 10:39:30 -0000
@@ -61,6 +61,8 @@
 import java.util.NoSuchElementException;
 import java.util.Properties;
 import java.util.StringTokenizer;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 
 import org.apache.commons.lang.BooleanUtils;
 
@@ -1200,6 +1202,92 @@
         {
             throw new ClassCastException(
                 '\'' + key + "' doesn't map to a Short object");
+        }
+    }
+
+    public BigDecimal getBigDecimal(String key) throws NoSuchElementException {
+        BigDecimal number = getBigDecimal(key, null);
+        if (number != null)
+        {
+            return number;
+        }
+        else
+        {
+            throw new NoSuchElementException(
+                '\'' + key + "' doesn't map to an existing object");
+        }
+    }
+
+    public BigDecimal getBigDecimal(String key, BigDecimal defaultValue) {
+        Object value = resolveContainerStore(key);
+
+        if (value instanceof BigDecimal)
+        {
+            return (BigDecimal) value;
+        }
+        else if (value instanceof String)
+        {
+            BigDecimal number = new BigDecimal((String) value);
+            return number;
+        }
+        else if (value == null)
+        {
+            if (defaults != null)
+            {
+                return defaults.getBigDecimal(key, defaultValue);
+            }
+            else
+            {
+                return defaultValue;
+            }
+        }
+        else
+        {
+            throw new ClassCastException(
+                '\'' + key + "' doesn't map to a BigDecimal object");
+        }
+    }
+
+    public BigInteger getBigInteger(String key) throws NoSuchElementException {
+        BigInteger number = getBigInteger(key, null);
+        if (number != null)
+        {
+            return number;
+        }
+        else
+        {
+            throw new NoSuchElementException(
+                '\'' + key + "' doesn't map to an existing object");
+        }
+    }
+
+    public BigInteger getBigInteger(String key, BigInteger defaultValue) {
+        Object value = resolveContainerStore(key);
+
+        if (value instanceof BigDecimal)
+        {
+            return (BigInteger) value;
+        }
+        else if (value instanceof String)
+        {
+            BigInteger number = new BigInteger((String) value);
+            return number;
+        }
+        else if (value == null)
+        {
+            if (defaults != null)
+            {
+                return defaults.getBigInteger(key, defaultValue);
+            }
+            else
+            {
+                return defaultValue;
+            }
+        }
+        else
+        {
+            throw new ClassCastException(
+                '\'' + key + "' doesn't map to a BigDecimal object");
         }
     }
 
Index: src/java/org/apache/commons/configuration/CompositeConfiguration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/configuration/src/java/org/apache/commons/configuration/CompositeConfiguration.java,v
retrieving revision 1.4
diff -u -r1.4 CompositeConfiguration.java
--- src/java/org/apache/commons/configuration/CompositeConfiguration.java       7 Feb 
2004 13:43:15 -0000       1.4
+++ src/java/org/apache/commons/configuration/CompositeConfiguration.java       12 Feb 
2004 10:39:31 -0000
@@ -53,6 +53,8 @@
  * <http://www.apache.org/>.
  */
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.LinkedList;
@@ -654,6 +656,39 @@
             return defaultValue;
         }
     }
+
+    public BigDecimal getBigDecimal(String key) throws NoSuchElementException {
+        return getFirstMatchingConfig(key).getBigDecimal(key);
+    }
+
+    public BigDecimal getBigDecimal(String key, BigDecimal defaultValue)
+    {
+        try
+        {
+            return getFirstMatchingConfig(key).getBigDecimal(key, defaultValue);
+        }
+        catch (NoSuchElementException nsee)
+        {
+            return defaultValue;
+        }
+    }
+
+    public BigInteger getBigInteger(String key) throws NoSuchElementException {
+        return getFirstMatchingConfig(key).getBigInteger(key);
+    }
+
+    public BigInteger getBigInteger(String key, BigInteger defaultValue)
+    {
+        try
+        {
+            return getFirstMatchingConfig(key).getBigInteger(key, defaultValue);
+        }
+        catch (NoSuchElementException nsee)
+        {
+            return defaultValue;
+        }
+    }
+
     /**
      * Get a string associated with the given configuration key.
      *
Index: src/java/org/apache/commons/configuration/Configuration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/configuration/src/java/org/apache/commons/configuration/Configuration.java,v
retrieving revision 1.2
diff -u -r1.2 Configuration.java
--- src/java/org/apache/commons/configuration/Configuration.java        24 Dec 2003 
14:28:22 -0000      1.2
+++ src/java/org/apache/commons/configuration/Configuration.java        12 Feb 2004 
10:39:31 -0000
@@ -57,6 +57,9 @@
 import java.util.Iterator;
 import java.util.List;
 import java.util.Properties;
+import java.util.NoSuchElementException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 
 /**
  * Configuration interface.
@@ -454,6 +457,52 @@
      * map to an existing object.
      */
     Short getShort(String key, Short defaultValue);
+
+    /**
+     * Get a BigDecimal associated with the given configuration key.
+     *
+     * @param key The configuration key.
+     *
+     * @return The associated BigDecimal if key is found and has valid format
+     *
+     * @exception NoSuchElementException is thrown if the key doesn't
+     * map to an existing object.
+     */
+    BigDecimal getBigDecimal(String key) throws NoSuchElementException;
+
+    /**
+     * Get a BigDecimal associated with the given configuration key.
+     *
+     * @param key          The configuration key.
+     * @param defaultValue The default value.
+     *
+     * @return The associated BigDecimal if key is found and has valid
+     * format, default value otherwise.
+     */
+    BigDecimal getBigDecimal(String key, BigDecimal defaultValue);
+
+    /**
+     * Get a BigInteger associated with the given configuration key.
+     *
+     * @param key The configuration key.
+     *
+     * @return The associated BigInteger if key is found and has valid format
+     *
+     * @exception NoSuchElementException is thrown if the key doesn't
+     * map to an existing object.
+     */
+    BigInteger getBigInteger(String key) throws NoSuchElementException;
+
+    /**
+     * Get a BigInteger associated with the given configuration key.
+     *
+     * @param key          The configuration key.
+     * @param defaultValue The default value.
+     *
+     * @return The associated BigInteger if key is found and has valid
+     * format, default value otherwise.
+     */
+    BigInteger getBigInteger(String key, BigInteger defaultValue);
 
     /**
      * Get a string associated with the given configuration key.
Index: src/test/org/apache/commons/configuration/TestBaseConfiguration.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestBaseConfiguration.java,v
retrieving revision 1.3
diff -u -r1.3 TestBaseConfiguration.java
--- src/test/org/apache/commons/configuration/TestBaseConfiguration.java        16 Jan 
2004 14:23:39 -0000      1.3
+++ src/test/org/apache/commons/configuration/TestBaseConfiguration.java        12 Feb 
2004 10:39:31 -0000
@@ -54,9 +54,11 @@
  * <http://www.apache.org/>.
  */
 
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Properties;
-import java.util.List;
 
 import junit.framework.TestCase;
 
@@ -160,6 +162,26 @@
         assertEquals("This returns 1(Double)",
                      eprop.getDouble("numberD",new Double("2")),
                      new Double(oneD));
+    }
+
+    public void testGetBigDecimal() {
+        eprop.setProperty("numberBigD", "123.456");
+        BigDecimal number = new BigDecimal("123.456");
+        BigDecimal defaultValue = new BigDecimal("654.321");
+
+        assertEquals("Existing key", number, eprop.getBigDecimal("numberBigD"));
+        assertEquals("Existing key with default value", number, 
eprop.getBigDecimal("numberBigD", defaultValue));
+        assertEquals("Missing key with default value", defaultValue, 
eprop.getBigDecimal("numberNotInConfig", defaultValue));
+    }
+
+    public void testGetBigInteger() {
+        eprop.setProperty("numberBigI", "1234567890");
+        BigInteger number = new BigInteger("1234567890");
+        BigInteger defaultValue = new BigInteger("654321");
+
+        assertEquals("Existing key", number, eprop.getBigInteger("numberBigI"));
+        assertEquals("Existing key with default value", number, 
eprop.getBigInteger("numberBigI", defaultValue));
+        assertEquals("Missing key with default value", defaultValue, 
eprop.getBigInteger("numberNotInConfig", defaultValue));
     }
 
     public void testGetBoolean() {
Index: xdocs/changes.xml
===================================================================
RCS file: /home/cvspublic/jakarta-commons/configuration/xdocs/changes.xml,v
retrieving revision 1.7
diff -u -r1.7 changes.xml
--- xdocs/changes.xml   30 Jan 2004 14:53:11 -0000      1.7
+++ xdocs/changes.xml   12 Feb 2004 10:39:32 -0000
@@ -7,6 +7,9 @@
 
   <body>
     <release version="1.0-dev-4" date="">
+      <action dev="ebourg" type="add">
+        The Configuration interface now supports BigDecimal and BigInteger numbers.
+      </action>
         <action dev="epugh" type="add">
        ConfigurationException is now thrown by public methods instead of Exception or
        IOException or whatnot.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to