You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ni...@apache.org on 2007/05/25 17:34:21 UTC

svn commit: r541692 - in /jakarta/commons/proper/beanutils/trunk/src: java/org/apache/commons/beanutils/converters/ test/org/apache/commons/beanutils/converters/

Author: niallp
Date: Fri May 25 08:34:19 2007
New Revision: 541692

URL: http://svn.apache.org/viewvc?view=rev&rev=541692
Log:
BEANUTILS-258 - enhance AbstractConverter to handle Array-->non-Array conversion, but exclude behaviour for 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/test/org/apache/commons/beanutils/converters/NumberConverterTestBase.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=541692&r1=541691&r2=541692
==============================================================================
--- 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 Fri May 25 08:34:19 2007
@@ -16,6 +16,8 @@
  */ 
 package org.apache.commons.beanutils.converters;
 
+import java.lang.reflect.Array;
+import java.util.Collection;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.beanutils.BeanUtils;
@@ -130,11 +132,15 @@
                     + " value '" + value + "' to type '" + toString(targetType) + "'");
         }
 
+        value = convertArray(value);
+
         // Missing Value
         if (value == null) {
             return handleMissing(targetType);
         }
 
+        sourceType = value.getClass();
+
         try {
             // Convert --> String
             if (targetType.equals(String.class)) {
@@ -192,6 +198,38 @@
      * @throws Throwable if an error occurs converting to the specified type
      */
     protected abstract Object convertToType(Class type, Object value) throws Throwable;
+
+    /**
+     * Return the first element from an Array (or Collection)
+     * or the value unchanged if not an Array (or Collection).
+     *
+     * N.B. This needs to be overriden for array/Collection converters.
+     *
+     * @param value The value to convert
+     * @return The first element in an Array (or Collection)
+     * or the value unchanged if not an Array (or Collection)
+     */
+    protected Object convertArray(Object value) {
+        if (value == null) {
+            return null;
+        }
+        if (value.getClass().isArray()) {
+            if (Array.getLength(value) > 0) {
+                return Array.get(value, 0);
+            } else {
+                return null;
+            }
+        }
+        if (value instanceof Collection) {
+            Collection collection = (Collection)value;
+            if (collection.size() > 0) {
+                return collection.iterator().next();
+            } else {
+                return null;
+            }
+        }
+        return value;
+    }
 
     /**
      * Handle Conversion Errors.

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=541692&r1=541691&r2=541692
==============================================================================
--- 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 Fri May 25 08:34:19 2007
@@ -298,6 +298,16 @@
     }
 
     /**
+     * Returns the value unchanged.
+     *
+     * @param value The value to convert
+     * @return The value unchanged
+     */
+    protected Object convertArray(Object value) {
+        return value;
+    }
+
+    /**
      * Converts non-array values to a Collection prior
      * to being converted either to an array or a String.
      * </p>

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/NumberConverterTestBase.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/NumberConverterTestBase.java?view=diff&rev=541692&r1=541691&r2=541692
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/NumberConverterTestBase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/converters/NumberConverterTestBase.java Fri May 25 08:34:19 2007
@@ -79,7 +79,8 @@
             "from Float",
             "from Double",
             "from BigDecimal",
-            "from BigInteger"
+            "from BigInteger",
+            "from Integer array",
         };
 
         Object[] number = {
@@ -90,7 +91,8 @@
             new Float(11.1),
             new Double(12.2),
             new BigDecimal("17.2"),
-            new BigInteger("33")
+            new BigInteger("33"),
+            new Integer[] {new Integer(3), new Integer(2), new Integer(1)}
         };
 
         for(int i=0;i<number.length;i++) {
@@ -149,6 +151,21 @@
 
         // Restore the default Locale
         Locale.setDefault(defaultLocale);
+    }
+
+    /**
+     * Convert Array --> Number
+     */
+    public void testStringArrayToInteger() {
+
+        Integer defaultValue = new Integer(-1);
+        NumberConverter converter = makeConverter(defaultValue);
+
+        // Default Locale
+        assertEquals("Valid First",   new Integer(5), converter.convert(Integer.class, new String[] {"5", "4", "3"}));
+        assertEquals("Invalid First", defaultValue,   converter.convert(Integer.class, new String[] {"FOO", "1", "2"}));
+        assertEquals("Null First",    defaultValue,   converter.convert(Integer.class, new String[] {null, "1", "2"}));
+        assertEquals("Long Array",    new Integer(9), converter.convert(Integer.class, new long[] {9, 2, 6}));
     }
 
     /**



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