Title: [waffle-scm] [644] trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters: Better implementation of number handling in list value converter.

Diff

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/AbstractValueConverter.java (643 => 644)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/AbstractValueConverter.java	2008-04-21 14:19:13 UTC (rev 643)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/AbstractValueConverter.java	2008-04-21 15:46:46 UTC (rev 644)
@@ -27,6 +27,12 @@
         this.messageResources = messageResources;
     }
 
+    /**
+     * Determines if the value is missing.  
+     * 
+     * @param value the String value
+     * @return A boolean, <code>true</code> if value is <code>null</code> or trimmed length is 0.
+     */
     protected boolean missingValue(String value) {
         return value == null || value.trim().length() == 0;
     }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java (643 => 644)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java	2008-04-21 14:19:13 UTC (rev 643)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/DateValueConverter.java	2008-04-21 15:46:46 UTC (rev 644)
@@ -17,13 +17,17 @@
 import org.codehaus.waffle.i18n.MessageResources;
 
 /**
- * <code>ValueConverter</code> that converts Date values.  The date format is configurable via the message resources bundle.
- * A <code>null</code>, empty or invalid value will cause a BindException to be thrown.
- * The message keys and default values used are:
+ * <code>ValueConverter</code> that converts Date values. The date format is configurable via the message resources
+ * bundle. A <code>null</code> or empty value (once trimmed) will be returned as <code>null</code> (behaviour which
+ * can be overridden via the [EMAIL PROTECTED] convertMissingValue()} method), while an invalid value will cause a BindException to
+ * be thrown. The message keys and default values used are:
  * <ul>
- *  <li>"bind.error.date" ([EMAIL PROTECTED] #BIND_ERROR_DATE_KEY}): bind error in date parsing (message defaults to [EMAIL PROTECTED] #DEFAULT_DATE_MESSAGE})</li>
- *  <li>"bind.error.date.missing" ([EMAIL PROTECTED] #BIND_ERROR_DATE_MISSING_KEY}): date is <code>null</code> or empty (message defaults to [EMAIL PROTECTED] #DEFAULT_DATE_MISSING_MESSAGE})</li>
- *  <li>"date.format" ([EMAIL PROTECTED] #DATE_FORMAT_KEY}): date format used in parsing (defaults to [EMAIL PROTECTED] #DEFAULT_DATE_FORMAT})</li>
+ * <li>"bind.error.date" ([EMAIL PROTECTED] #BIND_ERROR_DATE_KEY}): bind error in date parsing (message defaults to
+ * [EMAIL PROTECTED] #DEFAULT_DATE_MESSAGE})</li>
+ * <li>"bind.error.date.missing" ([EMAIL PROTECTED] #BIND_ERROR_DATE_MISSING_KEY}): date is <code>null</code> or empty
+ * (message defaults to [EMAIL PROTECTED] #DEFAULT_DATE_MISSING_MESSAGE})</li>
+ * <li>"date.format" ([EMAIL PROTECTED] #DATE_FORMAT_KEY}): date format used in parsing (defaults to
+ * [EMAIL PROTECTED] #DEFAULT_DATE_FORMAT})</li>
  * </ul>
  * 
  * @author Michael Ward

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java (643 => 644)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java	2008-04-21 14:19:13 UTC (rev 643)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/converters/ListValueConverter.java	2008-04-21 15:46:46 UTC (rev 644)
@@ -6,7 +6,7 @@
  * style license a copy of which has been included with this distribution in *
  * the LICENSE.txt file.                                                     *
  *                                                                           *
- * Original code by: Mauro Talevi                                            *
+ * Original code by: Michael Ward                                            *
  *****************************************************************************/
 package org.codehaus.waffle.bind.converters;
 
@@ -20,15 +20,15 @@
 import org.codehaus.waffle.i18n.MessageResources;
 
 /**
- * <code>ValueConverter</code> that converts a CSV value to a List. A <code>null</code> value will cause a
- * BindException to thrown. 
- * 
- * The message keys and default values used are:
+ * <code>ValueConverter</code> that converts a CSV value to a List. A <code>null</code> or empty value (once
+ * trimmed) will be returned as <code>null</code> (behaviour which can be overridden via the
+ * [EMAIL PROTECTED] convertMissingValue()} method). The message keys and default values used are:
  * <ul>
- *  <li>"bind.error.list" ([EMAIL PROTECTED] #BIND_ERROR_LIST_KEY}): list is <code>null</code> or empty (message defaults to [EMAIL PROTECTED] #DEFAULT_LIST_MESSAGE})</li>
+ * <li>"bind.error.list" ([EMAIL PROTECTED] #BIND_ERROR_LIST_KEY}): list is <code>null</code> or empty (message defaults to
+ * [EMAIL PROTECTED] #DEFAULT_LIST_MESSAGE})</li>
  * </ul>
- *  
- * The converter also looks to see if the values in the list are numbers and if so parses them using the default <code>NumberFormat</code> instance.
+ * The converter first attempts to parse the values as numbers (using the default <code>NumberFormat</code> instance)
+ * and if not successful returns the string values.
  * 
  * @author Mauro Talevi
  */
@@ -49,39 +49,27 @@
     @SuppressWarnings( { "unchecked" })
     public <T> T convertValue(String propertyName, String value, Class<T> toType) {
 
-        if ( missingValue(value)){
+        if (missingValue(value)) {
             String fieldName = messageFor(propertyName, propertyName);
-            return (T)convertMissingValue(BIND_ERROR_LIST_KEY, DEFAULT_LIST_MESSAGE, fieldName);
+            return (T) convertMissingValue(BIND_ERROR_LIST_KEY, DEFAULT_LIST_MESSAGE, fieldName);
         }
 
-        List<String> values = asList(value.split(COMMA));        
-        if ( values.size() == 0 ){
-            return (T) values;
+        List<String> values = asList(value.split(COMMA));
+        if (values.size() != 0) {
+            try {
+                return (T) toNumbers(values);
+            } catch (ParseException e) {
+                // failed to parse as numbers, return string values
+            }
         }
-        if ( areNumbers(values) ){
-            return (T) toNumbers(values);            
-        }
         return (T) values;
     }
-    
-    private boolean areNumbers(List<String> values) {
-        try {
-            NumberFormat.getInstance().parse(values.get(0));
-            return true;
-        } catch ( ParseException e) {
-            return false;
-        }
-    }
-    
-    private List<Number> toNumbers(List<String> values) {
+
+    private List<Number> toNumbers(List<String> values) throws ParseException {
         NumberFormat format = NumberFormat.getInstance();
         List<Number> list = new ArrayList<Number>();
-        for ( String value : values ){
-            try {
-                list.add(format.parse(value));                
-            } catch (ParseException e) {
-                // skip unparseable
-            }
+        for (String value : values) {
+            list.add(format.parse(value));
         }
         return list;
     }

Modified: trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java (643 => 644)

--- trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java	2008-04-21 14:19:13 UTC (rev 643)
+++ trunk/waffle-core/src/test/java/org/codehaus/waffle/bind/converters/ListValueConverterTest.java	2008-04-21 15:46:46 UTC (rev 644)
@@ -27,6 +27,7 @@
     private static final List<Long> LONGS = asList(1L,2L,3L);
     private static final List<Double> DOUBLES = asList(0.1d,0.2d,0.3d);
     private static final List<Float> FLOATS = asList(0.1f,0.2f,0.3f);
+    private static final List<String> STRINGS = asList("one","two","three");
     
     private MessageResourcesConfiguration configuration = new MessageResourcesConfiguration(){
 
@@ -53,9 +54,10 @@
         assertCanConvertValueToList(converter, LONGS, "1,2,3", Long.class);
         assertCanConvertValueToList(converter, DOUBLES, "0.1,0.2,0.3", Double.class);
         assertCanConvertValueToList(converter, FLOATS, "0.1,0.2,0.3", Float.class);
+        assertCanConvertValueToList(converter, STRINGS, "one,two,three", String.class);
     }
 
-    private void assertCanConvertValueToList(ListValueConverter converter, List<?> list, String value, Class<? extends Number> type) {
+    private void assertCanConvertValueToList(ListValueConverter converter, List<?> list, String value, Class<?> type) {
         assertEquals(list.toString(), converter.convertValue("property-name", value, List.class).toString());
         assertTrue(list.get(0).getClass().isAssignableFrom(type));
     }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to