Commits

Anonymous committed 5e09baf

resolves XW-87 (added float support to type conversion)

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk/src@45e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

java/com/opensymphony/xwork/XWorkBasicConverter.java

 package com.opensymphony.xwork;
 
 import com.opensymphony.util.TextUtils;
-
 import ognl.DefaultTypeConverter;
 
 import java.lang.reflect.Member;
-
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-
 import java.util.*;
 
 
             result = doConvertToDoubleArray(context, value);
         } else if (toType == Double[].class) {
             result = doConvertToDoubleArray2(context, value);
+        } else if (toType == float[].class) {
+            result = doConvertToFloatArray(context, value);
+        } else if (toType == Float[].class) {
+            result = doConvertToFloatArray2(context, value);
         } else if (toType == Date.class) {
             result = doConvertToDate(context, value);
         } else if (toType == List.class) {
             result = doConvertToClass(context, value);
         } else {
             if ((toType == String[].class) && (value instanceof String)) {
-                result = new String[] {(String) value};
+                result = new String[]{(String) value};
             }
         }
 
         return result;
     }
 
+    private float[] doConvertToFloatArray(Map context, Object value) {
+        float[] result = null;
+
+        if (value instanceof String[]) {
+            String[] sa = (String[]) value;
+            float[] da = new float[sa.length];
+
+            for (int i = 0; i < sa.length; i++) {
+                da[i] = ((Float) super.convertValue(context, sa[i], Float.class)).floatValue();
+            }
+
+            result = da;
+        }
+
+        return result;
+    }
+
+    private Float[] doConvertToFloatArray2(Map context, Object value) {
+        float[] primitives = doConvertToFloatArray(context, value);
+        Float[] result = null;
+
+        if (primitives != null) {
+            result = new Float[primitives.length];
+
+            for (int i = 0; i < primitives.length; i++) {
+                float primitive = primitives[i];
+                result[i] = new Float(primitive);
+            }
+        }
+
+        return result;
+    }
+
     private int[] doConvertToIntArray(Map context, Object value) {
         int[] result = null;
 

java/xwork-default-conversion.properties

 double               =com.opensymphony.xwork.XWorkBasicConverter
 java.lang.Double     =com.opensymphony.xwork.XWorkBasicConverter
 [Ljava.lang.Double;  =com.opensymphony.xwork.XWorkBasicConverter
+[F                   =com.opensymphony.xwork.XWorkBasicConverter
+float                =com.opensymphony.xwork.XWorkBasicConverter
+java.lang.Float      =com.opensymphony.xwork.XWorkBasicConverter
+[Ljava.lang.Float;   =com.opensymphony.xwork.XWorkBasicConverter
 [I                   =com.opensymphony.xwork.XWorkBasicConverter
 int                  =com.opensymphony.xwork.XWorkBasicConverter
 java.lang.Integer    =com.opensymphony.xwork.XWorkBasicConverter

test/com/opensymphony/xwork/util/XWorkConverterTest.java

 package com.opensymphony.xwork.util;
 
 import junit.framework.TestCase;
-
 import ognl.Ognl;
 import ognl.OgnlException;
 
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
-                    "foo", "bar", "baz"
-                }, Collection.class));
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[]{
+            "foo", "bar", "baz"
+        }, Collection.class));
     }
 
     public void testStringArrayToList() {
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
-                    "foo", "bar", "baz"
-                }, List.class));
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[]{
+            "foo", "bar", "baz"
+        }, List.class));
     }
 
     public void testStringArrayToPrimitiveWrappers() {
-        Long[] longs = (Long[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, Long[].class);
+        Long[] longs = (Long[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, Long[].class);
         assertNotNull(longs);
-        assertTrue(Arrays.equals(new Long[] {new Long(123), new Long(456)}, longs));
+        assertTrue(Arrays.equals(new Long[]{new Long(123), new Long(456)}, longs));
 
-        Integer[] ints = (Integer[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, Integer[].class);
+        Integer[] ints = (Integer[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, Integer[].class);
         assertNotNull(ints);
-        assertTrue(Arrays.equals(new Integer[] {
-                    new Integer(123), new Integer(456)
-                }, ints));
+        assertTrue(Arrays.equals(new Integer[]{
+            new Integer(123), new Integer(456)
+        }, ints));
 
-        Double[] doubles = (Double[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, Double[].class);
+        Double[] doubles = (Double[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, Double[].class);
         assertNotNull(doubles);
-        assertTrue(Arrays.equals(new Double[] {new Double(123), new Double(456)}, doubles));
+        assertTrue(Arrays.equals(new Double[]{new Double(123), new Double(456)}, doubles));
+
+        Float[] floats = (Float[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, Float[].class);
+        assertNotNull(floats);
+        assertTrue(Arrays.equals(new Float[]{new Float(123), new Float(456)}, floats));
 
-        Boolean[] booleans = (Boolean[]) converter.convertValue(context, null, null, null, new String[] {
-                "true", "false"
-            }, Boolean[].class);
+        Boolean[] booleans = (Boolean[]) converter.convertValue(context, null, null, null, new String[]{
+            "true", "false"
+        }, Boolean[].class);
         assertNotNull(booleans);
-        assertTrue(Arrays.equals(new Boolean[] {Boolean.TRUE, Boolean.FALSE}, booleans));
+        assertTrue(Arrays.equals(new Boolean[]{Boolean.TRUE, Boolean.FALSE}, booleans));
     }
 
     public void testStringArrayToPrimitives() throws OgnlException {
-        long[] longs = (long[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, long[].class);
+        long[] longs = (long[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, long[].class);
         assertNotNull(longs);
-        assertTrue(Arrays.equals(new long[] {123, 456}, longs));
+        assertTrue(Arrays.equals(new long[]{123, 456}, longs));
 
-        int[] ints = (int[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, int[].class);
+        int[] ints = (int[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, int[].class);
         assertNotNull(ints);
-        assertTrue(Arrays.equals(new int[] {123, 456}, ints));
+        assertTrue(Arrays.equals(new int[]{123, 456}, ints));
 
-        double[] doubles = (double[]) converter.convertValue(context, null, null, null, new String[] {
-                "123", "456"
-            }, double[].class);
+        double[] doubles = (double[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, double[].class);
         assertNotNull(doubles);
-        assertTrue(Arrays.equals(new double[] {123, 456}, doubles));
+        assertTrue(Arrays.equals(new double[]{123, 456}, doubles));
+
+        float[] floats = (float[]) converter.convertValue(context, null, null, null, new String[]{
+            "123", "456"
+        }, float[].class);
+        assertNotNull(floats);
+        assertTrue(Arrays.equals(new float[]{123, 456}, floats));
 
-        boolean[] booleans = (boolean[]) converter.convertValue(context, null, null, null, new String[] {
-                "true", "false"
-            }, boolean[].class);
+        boolean[] booleans = (boolean[]) converter.convertValue(context, null, null, null, new String[]{
+            "true", "false"
+        }, boolean[].class);
         assertNotNull(booleans);
-        assertTrue(Arrays.equals(new boolean[] {true, false}, booleans));
+        assertTrue(Arrays.equals(new boolean[]{true, false}, booleans));
     }
 
     public void testStringArrayToSet() {
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
-                    "foo", "bar", "bar", "baz"
-                }, Set.class));
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[]{
+            "foo", "bar", "bar", "baz"
+        }, Set.class));
     }
 
     public void testStringToPrimitiveWrappers() {
         assertEquals(new Long(123), converter.convertValue(context, null, null, null, "123", Long.class));
         assertEquals(new Integer(123), converter.convertValue(context, null, null, null, "123", Integer.class));
         assertEquals(new Double(123.5), converter.convertValue(context, null, null, null, "123.5", Double.class));
+        assertEquals(new Float(123.5), converter.convertValue(context, null, null, null, "123.5", float.class));
         assertEquals(new Boolean(false), converter.convertValue(context, null, null, null, "false", Boolean.class));
         assertEquals(new Boolean(true), converter.convertValue(context, null, null, null, "true", Boolean.class));
     }
         assertEquals(new Long(123), converter.convertValue(context, null, null, null, "123", long.class));
         assertEquals(new Integer(123), converter.convertValue(context, null, null, null, "123", int.class));
         assertEquals(new Double(123.5), converter.convertValue(context, null, null, null, "123.5", double.class));
+        assertEquals(new Float(123.5), converter.convertValue(context, null, null, null, "123.5", float.class));
         assertEquals(new Boolean(false), converter.convertValue(context, null, null, null, "false", boolean.class));
         assertEquals(new Boolean(true), converter.convertValue(context, null, null, null, "true", boolean.class));
     }