commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r471625 - in /jakarta/commons/proper/beanutils/trunk/src: java/org/apache/commons/beanutils/converters/ test/org/apache/commons/beanutils/converters/
Date Mon, 06 Nov 2006 03:55:13 GMT
Author: niallp
Date: Sun Nov  5 19:55:10 2006
New Revision: 471625

URL: http://svn.apache.org/viewvc?view=rev&rev=471625
Log:
BEANUTILS-258 Add standard support for conversion to StringBuffer. Plus small refactoring
in ArrayConverter

Modified:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/AbstractConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DateTimeConverter.java
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/AbstractConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/AbstractConverter.java?view=diff&rev=471625&r1=471624&r2=471625
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/AbstractConverter.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/AbstractConverter.java
Sun Nov  5 19:55:10 2006
@@ -150,9 +150,21 @@
         if (value == null) {
             return handleMissing(targetType);
 
-        // Convert --> String
-        } else if (targetType.equals(String.class)) {
-            return convertToString(value);
+        // Convert --> String or StringBuffer
+        } else if (targetType.equals(String.class) ||
+                targetType.equals(StringBuffer.class)) {
+            Object result = convertToString(value);
+            if (result == null) {
+                return getDefault(targetType);
+            } else if (type.equals(StringBuffer.class)) {
+                if (result instanceof StringBuffer) {
+                    return result;
+                } else {
+                    return new StringBuffer(result.toString());
+                }
+            } else {
+                return result.toString();
+            }
 
         // No conversion necessary
         } else if (targetType.equals(sourceType)) {
@@ -179,18 +191,18 @@
     }
 
     /**
-     * Convert the input object into a String.
+     * Convert the input object into a String (or StringBuffer).
      * <p>
-     * <b>N.B.</b>This implementation simply uses the value's
-     * <code>toString()</code> method and should be overriden if a
-     * more sophisticated mechanism for <i>conversion to a String</i>
-     * is required.
+     * <b>N.B.</b>This implementation simply returns the value unchanged.
+     * The {@link AbstractConverter#convert(Class, Object)} method
+     * checks the value returned by this method, converting to either
+     * a String or StringBuffer as required.
      *
      * @param value The input value to be converted.
      * @return the converted String value.
      */
-    protected String convertToString(Object value) {
-        return value.toString();
+    protected Object convertToString(Object value) {
+        return value;
     }
 
     /**
@@ -265,7 +277,7 @@
      */
     protected Object handleMissing(Class type) {
 
-        if (useDefault || type.equals(String.class)) {
+        if (useDefault || type.equals(String.class) || type.equals(StringBuffer.class)) {
             Object value = getDefault(type);
             if (useDefault && value != null && !(type.equals(value.getClass())))
{
                 try {
@@ -306,7 +318,13 @@
      * @return The default value for the specified type.
      */
     protected Object getDefault(Class type) {
-        return (type.equals(String.class) ? null : defaultValue);
+        if (type.equals(String.class)) {
+            return null;
+        } else if (type.equals(StringBuffer.class)) {
+            return new StringBuffer();
+        } else {
+            return defaultValue;
+        }
     }
 
     // ----------------------------------------------------------- Package Methods

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java?view=diff&rev=471625&r1=471624&r2=471625
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
Sun Nov  5 19:55:10 2006
@@ -83,7 +83,7 @@
  *    ArrayConverter arrayConverter = new ArrayConverter(int[].class, integerConverter);
  *
  *    // Construct a "Matrix" Converter which converts arrays of integer arrays using
- *    // the pre-ceeding ArrayConverter as the element Converter. 
+ *    // the pre-ceeding ArrayConverter as the element Converter.
  *    // N.B. Uses a semi-colon (i.e. ";") as the delimiter to separate the different sets
of numbers.
  *    //      Also the delimiter used by the first ArrayConverter needs to be added to the
  *    //      "allowed characters" for this one.
@@ -176,7 +176,7 @@
      * @param value The value to be converted.
      * @return the converted String value.
      */
-    protected String convertToString(Object value) {
+    protected Object convertToString(Object value) {
 
         Class type = value.getClass();
         if (!type.isArray()) {
@@ -185,9 +185,10 @@
 
         int size = Array.getLength(value);
         if (size == 0) {
-            return (String)getDefault(String.class);
+            return "";
         }
 
+        // Create a StringBuffer containing a delimited list of the values
         StringBuffer buffer = new StringBuffer();
         for (int i = 0; i < size; i++) {
             if (i > 0) {
@@ -200,7 +201,7 @@
             }
         }
 
-        return buffer.toString();
+        return buffer;
 
     }
 
@@ -215,27 +216,31 @@
      */
     protected Object convertToType(Class type, Object value) throws Exception {
 
-        Class sourceType    = value.getClass();
-        Class componentType = type.getComponentType();
+        if (!type.isArray()) {
+            throw new ConversionException("Not an array: " + toString(type));
+        }
 
-        Collection collection = null;
+        // Handle the source
         int size = 0;
-
-        if (sourceType.isArray()) {
+        Iterator iterator = null;
+        if (value.getClass().isArray()) {
             size = Array.getLength(value);
-        } else if (value instanceof Collection) {
-            collection = (Collection)value;
-            size = collection.size();
         } else {
-            collection =  parseElements(value.toString().trim());
+            Collection collection = null;
+            if (value instanceof Collection) {
+                collection = (Collection)value;
+            } else {
+                collection = parseElements(value.toString().trim());
+            }
             size = collection.size();
+            iterator = collection.iterator();
         }
 
         // Allocate a new Array
+        Class componentType = type.getComponentType();
         Object newArray = Array.newInstance(componentType, size);
 
         // Convert and set each element in the new Array
-        Iterator iterator = collection == null ? null : collection.iterator();
         for (int i = 0; i < size; i++) {
             Object element = iterator == null ? Array.get(value, i) : iterator.next();
             element = elementConverter.convert(componentType, element);
@@ -322,7 +327,7 @@
                     if (list == null) {
                         list = new ArrayList();
                     }
-                    list.add(st.sval);
+                    list.add(st.sval.trim());
                 } else if (ttype == StreamTokenizer.TT_EOF) {
                     break;
                 } else {

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java?view=diff&rev=471625&r1=471624&r2=471625
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/CharacterConverter.java
Sun Nov  5 19:55:10 2006
@@ -57,7 +57,7 @@
      * @param value The input value to be converted
      * @return the converted String value.
      */
-    protected String convertToString(Object value) {
+    protected Object convertToString(Object value) {
         String strValue = value.toString();
         return strValue.length() == 0 ? "" : strValue.substring(0, 1);
     }

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java?view=diff&rev=471625&r1=471624&r2=471625
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ClassConverter.java
Sun Nov  5 19:55:10 2006
@@ -61,8 +61,8 @@
      * @param value The input value to be converted
      * @return the converted String value.
      */
-    protected String convertToString(Object value) {
-        return (value instanceof Class) ? ((Class)value).getName() : value.toString();
+    protected Object convertToString(Object value) {
+        return (value instanceof Class) ? ((Class)value).getName() : value;
     }
 
     /**

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DateTimeConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DateTimeConverter.java?view=diff&rev=471625&r1=471624&r2=471625
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DateTimeConverter.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/DateTimeConverter.java
Sun Nov  5 19:55:10 2006
@@ -191,9 +191,9 @@
      * @param value The input value to be converted
      * @return the converted String value.
      */
-    protected String convertToString(Object value) {
+    protected Object convertToString(Object value) {
 
-        Date date     = null;
+        Date date = null;
         if (value instanceof Date) {
             date = (Date)value;
         } else if (value instanceof Calendar) {
@@ -202,7 +202,7 @@
             date = new Date(((Long)value).longValue());
         }
 
-        String result = null;
+        Object result = value;
         if (useLocaleFormat && date != null) {
             DateFormat format = null;
             if (patterns != null && patterns.length > 0) {
@@ -214,11 +214,6 @@
             result = format.format(date);
             if (log().isDebugEnabled()) {
                 log().debug("    Converted  to String using format '" + result + "'");
-            }
-        } else {
-            result = value.toString();
-            if (log().isDebugEnabled()) {
-                log().debug("    Converted  to String using toString() '" + result + "'");
             }
         }
         return result;

Modified: jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java?view=diff&rev=471625&r1=471624&r2=471625
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/NumberConverter.java
Sun Nov  5 19:55:10 2006
@@ -167,20 +167,15 @@
      * @param value The input value to be converted
      * @return the converted String value.
      */
-    protected String convertToString(Object value) {
+    protected Object convertToString(Object value) {
 
-        String result = null;
+        Object result = value;
         if (useLocaleFormat && value instanceof Number) {
             NumberFormat format = getFormat();
             format.setGroupingUsed(false);
             result = format.format(value);
             if (log().isDebugEnabled()) {
                 log().debug("    Converted  to String using format '" + result + "'");
-            }
-        } else {
-            result = value.toString();
-            if (log().isDebugEnabled()) {
-                log().debug("    Converted  to String using toString() '" + result + "'");
             }
         }
         return result;

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java?view=diff&rev=471625&r1=471624&r2=471625
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
Sun Nov  5 19:55:10 2006
@@ -147,6 +147,16 @@
             fail(msg + " failed " + e);
         }
 
+        // LONG[] --> StringBuffer
+        try {
+            msg = "LONG[] --> StringBuffer";
+            Object result = arrayConverter.convert(StringBuffer.class, LONGArray);
+            assertEquals(msg, StringBuffer.class, result.getClass());
+            assertEquals(msg, stringB, result.toString());
+        } catch (Exception e) {
+            fail(msg + " failed " + e);
+        }
+
         // LONG[] --> String[]
         try {
             msg = "long[] --> String[]";

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java?view=diff&rev=471625&r1=471624&r2=471625
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
(original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ClassConverterTestCase.java
Sun Nov  5 19:55:10 2006
@@ -68,6 +68,14 @@
         assertEquals("Value Test", "foo", converter.convert(String.class, "foo"));
         assertEquals("Value Test", "bar", converter.convert(String.class, new StringBuffer("bar")));
         assertEquals("Null Test",   null, converter.convert(String.class, null));
+
+        // StringBuffer Tests
+        Object result = converter.convert(StringBuffer.class, Integer.class);
+        assertEquals("StringBuffer Test", StringBuffer.class, result.getClass());
+        assertEquals("StringBuffer Test", "java.lang.Integer", result.toString());
+        result = converter.convert(StringBuffer.class, null);
+        assertEquals("StringBuffer Null", StringBuffer.class, result.getClass());
+        assertEquals("StringBuffer Null", "", result.toString());
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message