commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r539812 - in /jakarta/commons/proper/beanutils/trunk/src: java/org/apache/commons/beanutils/converters/ArrayConverter.java test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java
Date Sun, 20 May 2007 01:13:30 GMT
Author: niallp
Date: Sat May 19 18:13:29 2007
New Revision: 539812

URL: http://svn.apache.org/viewvc?view=rev&rev=539812
Log:
BEANUTILS-242 - number of small improvements:
- add handling for conversion to String of Collections and non-arrays
- make conversion to String configurable (either first element or delimited list)
- improve conversion to array for primitive and date values

Modified:
    jakarta/commons/proper/beanutils/trunk/src/java/org/apache/commons/beanutils/converters/ArrayConverter.java
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/ArrayConverterTestCase.java

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=539812&r1=539811&r2=539812
==============================================================================
--- 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
Sat May 19 18:13:29 2007
@@ -105,6 +105,7 @@
     private int defaultSize;
     private char delimiter    = ',';
     private char[] allowedChars = new char[] {'.', '-'};
+    private boolean onlyFirstToString = true;
 
     // ----------------------------------------------------------- Constructors
 
@@ -171,6 +172,19 @@
     }
 
     /**
+     * Indicates whether converting to a String should create
+     * a delimited list or just convert the first value.
+     *
+     * @param onlyFirstToString <code>true</code> converts only
+     * the first value in the array to a String, <code>false</code>
+     * converts all values in the array into a delimited list (default
+     * is <code>true</code> 
+     */
+    public void setOnlyFirstToString(boolean onlyFirstToString) {
+        this.onlyFirstToString = onlyFirstToString;
+    }
+
+    /**
      * Convert the input object into a String.
      *
      * @param value The value to be converted.
@@ -178,25 +192,35 @@
      */
     protected String convertToString(Object value) {
 
+        int size = 0;
+        Iterator iterator = null;
         Class type = value.getClass();
-        if (!type.isArray()) {
-            throw new ConversionException(toString(getClass())
-                    + " cannot handle conversion from '"
-                    + toString(type) + "' to String (not an array).");
+        if (type.isArray()) {
+            size = Array.getLength(value);
+        } else if (value instanceof Collection) {
+            Collection collection = (Collection)value;
+            size = collection.size();
+            iterator = collection.iterator();
+        } else {
+            Object converted = elementConverter.convert(String.class, value);
+            return (converted == null ? (String)getDefault(String.class) : converted.toString());
         }
 
-        int size = Array.getLength(value);
         if (size == 0) {
             return (String)getDefault(String.class);
         }
 
+        if (onlyFirstToString) {
+            size = 1;
+        }
+
         // Create a StringBuffer containing a delimited list of the values
         StringBuffer buffer = new StringBuffer();
         for (int i = 0; i < size; i++) {
             if (i > 0) {
                 buffer.append(delimiter);
             }
-            Object element = Array.get(value, i);
+            Object element = iterator == null ? Array.get(value, i) : iterator.next();
             element = elementConverter.convert(String.class, element);
             if (element != null) {
                 buffer.append(element);
@@ -230,12 +254,7 @@
         if (value.getClass().isArray()) {
             size = Array.getLength(value);
         } else {
-            Collection collection = null;
-            if (value instanceof Collection) {
-                collection = (Collection)value;
-            } else {
-                collection = parseElements(type, value.toString().trim());
-            }
+            Collection collection = convertToCollection(type, value);
             size = collection.size();
             iterator = collection.iterator();
         }
@@ -254,6 +273,28 @@
         }
 
         return newArray;
+    }
+
+    /**
+     * Convert an Object into a Collection.
+     *
+     * @param type The type to convert the value to
+     * @param value value to be converted
+     * @return List of parsed elements.
+     */
+    protected Collection convertToCollection(Class type, Object value) {
+        if (value instanceof Collection) {
+            return (Collection)value;
+        }
+        if (value instanceof Number ||
+            value instanceof Boolean ||
+            value instanceof java.util.Date) {
+            List list = new ArrayList(1);
+            list.add(value);
+            return list;
+        }
+        
+        return parseElements(type, value.toString());
     }
 
     /**

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=539812&r1=539811&r2=539812
==============================================================================
--- 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
Sat May 19 18:13:29 2007
@@ -131,6 +131,14 @@
             fail(msg + " failed " + e);
         }
 
+        // Long --> int[]
+        try {
+            msg = "Long --> int[]";
+            checkArray(msg, new int[] {LONGArray[0].intValue()}, arrayConverter.convert(int[].class,
LONGArray[0]));
+        } catch (Exception e) {
+            fail(msg + " failed " + e);
+        }
+
         // LONG[] --> int[]
         try {
             msg = "LONG[] --> int[]";
@@ -139,14 +147,39 @@
             fail(msg + " failed " + e);
         }
 
-        // LONG[] --> String
+        // Long --> String
+        try {
+            msg = "Long --> String";
+            assertEquals(msg, LONGArray[0] + "", arrayConverter.convert(String.class, LONGArray[0]));
+        } catch (Exception e) {
+            fail(msg + " failed " + e);
+        }
+
+        // LONG[] --> String (first)
         try {
-            msg = "LONG[] --> String";
+            msg = "LONG[] --> String (first)";
+            assertEquals(msg, LONGArray[0] + "", arrayConverter.convert(String.class, LONGArray));
+        } catch (Exception e) {
+            fail(msg + " failed " + e);
+        }
+
+        // LONG[] --> String (all)
+        try {
+            msg = "LONG[] --> String (all)";
+            arrayConverter.setOnlyFirstToString(false);
             assertEquals(msg, stringB, arrayConverter.convert(String.class, LONGArray));
         } catch (Exception e) {
             fail(msg + " failed " + e);
         }
 
+        // Collection of Long --> String
+        try {
+            msg = "Collection of Long --> String";
+            assertEquals(msg, stringB, arrayConverter.convert(String.class, longList));
+        } catch (Exception e) {
+            fail(msg + " failed " + e);
+        }
+
         // LONG[] --> String[]
         try {
             msg = "long[] --> String[]";
@@ -323,12 +356,6 @@
             new ArrayConverter(int[].class, null);
             fail("Component Converter missing - expected IllegalArgumentException");
         } catch (IllegalArgumentException e) {
-            // expected result
-        }
-        try {
-            new ArrayConverter(int[].class, new IntegerConverter()).convert(String.class,
"ABC");
-            fail("Non-array to String, expected ConversionException");
-        } catch (ConversionException e) {
             // expected result
         }
     }



---------------------------------------------------------------------
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