You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/07/13 21:25:37 UTC

svn commit: r1502844 - in /commons/proper/configuration/trunk/src: changes/changes.xml main/java/org/apache/commons/configuration/PropertyConverter.java test/java/org/apache/commons/configuration/TestPropertyConverter.java

Author: oheger
Date: Sat Jul 13 19:25:36 2013
New Revision: 1502844

URL: http://svn.apache.org/r1502844
Log:
[CONFIGURATION-550] PropertyConverter now supports conversion to Character.

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/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=1502844&r1=1502843&r2=1502844&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/changes/changes.xml (original)
+++ commons/proper/configuration/trunk/src/changes/changes.xml Sat Jul 13 19:25:36 2013
@@ -27,6 +27,9 @@
   <body>
     <release version="2.0" date="in SVN"
       description="TBD">
+      <action dev="oheger" type="add" issue="CONFIGURATION-550">
+        Conversion to Character is now supported.
+      </action>
       <action dev="oheger" type="update" issue="CONFIGURATION-546" due-to="Justin Couch">
         BeanHelper can now process BeanDefinitions initializing properties of
         collection types of their target beans.

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=1502844&r1=1502843&r2=1502844&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 Sat Jul 13 19:25:36 2013
@@ -101,6 +101,10 @@ public final class PropertyConverter
         {
             return toBoolean(value);
         }
+        else if (Character.class.equals(cls) || Character.TYPE.equals(cls))
+        {
+            return toCharacter(value);
+        }
         else if (Number.class.isAssignableFrom(cls) || cls.isPrimitive())
         {
             if (Integer.class.equals(cls) || Integer.TYPE.equals(cls))
@@ -208,6 +212,32 @@ public final class PropertyConverter
     }
 
     /**
+     * Converts the specified value object to a {@code Character}. This method
+     * converts the passed in object to a string. If the string has exactly one
+     * character, this character is returned as result. Otherwise, conversion
+     * fails.
+     *
+     * @param value the value to be converted
+     * @return the resulting {@code Character} object
+     * @throws ConversionException if the conversion is not possible
+     */
+    public static Character toCharacter(Object value) throws ConversionException
+    {
+        String strValue = String.valueOf(value);
+        if (strValue.length() == 1)
+        {
+            return Character.valueOf(strValue.charAt(0));
+        }
+        else
+        {
+            throw new ConversionException(
+                    String.format(
+                            "The value '%s' cannot be converted to a Character object!",
+                            strValue));
+        }
+    }
+
+    /**
      * Convert the specified object into a Byte.
      *
      * @param value the value to convert

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=1502844&r1=1502843&r2=1502844&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 Sat Jul 13 19:25:36 2013
@@ -260,4 +260,42 @@ public class TestPropertyConverter
         assertEquals("Wrong conversion result", value,
                 PropertyConverter.to(String.class, value, null));
     }
+
+    /**
+     * Tests whether a conversion to character is possible.
+     */
+    @Test
+    public void testToCharSuccess()
+    {
+        assertEquals("Wrong conversion result", Character.valueOf('t'),
+                PropertyConverter.to(Character.class, "t", null));
+    }
+
+    /**
+     * Tests whether other objects implementing a toString() method can be
+     * converted to character.
+     */
+    @Test
+    public void testToCharViaToString()
+    {
+        Object value = new Object()
+        {
+            @Override
+            public String toString()
+            {
+                return "X";
+            }
+        };
+        assertEquals("Wrong conversion result", Character.valueOf('X'),
+                PropertyConverter.to(Character.TYPE, value, null));
+    }
+
+    /**
+     * Tests a failed conversion to character.
+     */
+    @Test(expected = ConversionException.class)
+    public void testToCharFailed()
+    {
+        PropertyConverter.to(Character.TYPE, "FF", null);
+    }
 }