commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r1183508 - in /commons/proper/configuration/trunk/src: changes/ main/java/org/apache/commons/configuration/ test/java/org/apache/commons/configuration/
Date Fri, 14 Oct 2011 21:33:20 GMT
Author: ebourg
Date: Fri Oct 14 21:33:19 2011
New Revision: 1183508

URL: http://svn.apache.org/viewvc?rev=1183508&view=rev
Log:
Added support for binary literals (CONFIGURATION-466)

Modified:
    commons/proper/configuration/trunk/src/changes/changes.xml
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java

Modified: commons/proper/configuration/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/changes/changes.xml?rev=1183508&r1=1183507&r2=1183508&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Fri Oct 14 21:33:19 2011
@@ -27,6 +27,9 @@
   <body>
     <release version="1.8" date="in SVN"
       description="TBD">
+      <action dev="ebourg" type="add" issue="CONFIGURATION-466">
+        Binary literals are now supported (i.e Ob11010001).
+      </action>
       <action dev="oheger" type="update" issue="CONFIGURATION-461">
         The project now uses standard Maven directory layout.
       </action>

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java?rev=1183508&r1=1183507&r2=1183508&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/PropertyConverter.java
Fri Oct 14 21:33:19 2011
@@ -66,6 +66,12 @@ public final class PropertyConverter
     /** Constant for the radix of hex numbers.*/
     private static final int HEX_RADIX = 16;
 
+    /** Constant for the prefix of binary numbers.*/
+    private static final String BIN_PREFIX = "0b";
+
+    /** Constant for the radix of binary numbers.*/
+    private static final int BIN_RADIX = 2;
+
     /** Constant for the Java version 1.5.*/
     private static final float JAVA_VERSION_1_5 = 1.5f;
 
@@ -402,7 +408,21 @@ public final class PropertyConverter
                             + "! Invalid hex number.", nex);
                 }
             }
-
+            
+            if (str.startsWith(BIN_PREFIX))
+            {
+                try
+                {
+                    return new BigInteger(str.substring(BIN_PREFIX.length()), BIN_RADIX);
+                }
+                catch (NumberFormatException nex)
+                {
+                    throw new ConversionException("Could not convert " + str
+                            + " to " + targetClass.getName()
+                            + "! Invalid binary number.", nex);
+                }
+            }
+            
             try
             {
                 Constructor constr = targetClass.getConstructor(CONSTR_ARGS);

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseConfiguration.java?rev=1183508&r1=1183507&r2=1183508&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestBaseConfiguration.java
Fri Oct 14 21:33:19 2011
@@ -688,6 +688,23 @@ public class TestBaseConfiguration exten
         assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config.getBigInteger("number").longValue());
     }
 
+    public void testGetBinaryValue()
+    {
+        config.setProperty("number", "0b11111111");
+        assertEquals("byte value", (byte) 0xFF, config.getByte("number"));
+
+        config.setProperty("number", "0b1111111111111111");
+        assertEquals("short value", (short) 0xFFFF, config.getShort("number"));
+
+        config.setProperty("number", "0b11111111111111111111111111111111");
+        assertEquals("int value", 0xFFFFFFFF, config.getInt("number"));
+
+        config.setProperty("number", "0b1111111111111111111111111111111111111111111111111111111111111111");
+        assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config.getLong("number"));
+
+        assertEquals("long value", 0xFFFFFFFFFFFFFFFFL, config.getBigInteger("number").longValue());
+    }
+
     public void testResolveContainerStore()
     {
         AbstractConfiguration config = new BaseConfiguration();

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java?rev=1183508&r1=1183507&r2=1183508&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/TestPropertyConverter.java
Fri Oct 14 21:33:19 2011
@@ -241,8 +241,8 @@ public class TestPropertyConverter exten
     }
 
     /**
-     * Tests conversion to numbers when an invalid Hex value is passed in. This
-     * should cause an exception.
+     * Tests conversion to numbers when an invalid Hex value is passed in.
+     * This should cause an exception.
      */
     public void testToNumberFromInvalidHexString()
     {
@@ -258,6 +258,33 @@ public class TestPropertyConverter exten
     }
 
     /**
+     * Tests conversion to numbers when the passed in objects are strings with
+     * prefixes for special radices.
+     */
+    public void testToNumberFromBinaryString()
+    {
+        Number n = PropertyConverter.toNumber("0b1111", Integer.class);
+        assertEquals("Incorrect Integer value", 15, n.intValue());
+    }
+
+    /**
+     * Tests conversion to numbers when an invalid binary value is passed in.
+     * This should cause an exception.
+     */
+    public void testToNumberFromInvalidBinaryString()
+    {
+        try
+        {
+            PropertyConverter.toNumber("0bNotABinValue", Integer.class);
+            fail("Could convert invalid binary value!");
+        }
+        catch (ConversionException cex)
+        {
+            // ok
+        }
+    }
+
+    /**
      * Tests conversion to numbers when the passed in objects have no numeric
      * String representation. This should cause an exception.
      */



Mime
View raw message