Title: [waffle-scm] [511] trunk/waffle-core/src/main/java/org/codehaus/waffle/bind: generified ValueConverter.convertValue()'s return type

Diff

Modified: trunk/examples/paranamer-example/src/main/java/org/codehaus/waffle/example/paranamer/DateValueConverter.java (510 => 511)

--- trunk/examples/paranamer-example/src/main/java/org/codehaus/waffle/example/paranamer/DateValueConverter.java	2007-12-19 04:12:36 UTC (rev 510)
+++ trunk/examples/paranamer-example/src/main/java/org/codehaus/waffle/example/paranamer/DateValueConverter.java	2007-12-19 05:04:01 UTC (rev 511)
@@ -28,7 +28,8 @@
         return Date.class.isAssignableFrom(type);
     }
 
-    public Object convertValue(String propertyName, String value, Class<?> toType) {
+    @SuppressWarnings({"unchecked"})
+    public <T> T  convertValue(String propertyName, String value, Class<T> toType) {
         if (value == null || value.equals("")) {
             return null;
         }
@@ -36,7 +37,7 @@
         String datePattern = messageResources.getMessageWithDefault("date.format", "dd-MM-yyyy");
 
         try {
-            return new SimpleDateFormat(datePattern).parse(value);
+            return (T) new SimpleDateFormat(datePattern).parse(value);
         } catch (ParseException e) {
             String fieldName = messageResources.getMessageWithDefault(propertyName, propertyName);
             String message = messageResources.getMessage("date.bind.error", fieldName, value, datePattern);

Modified: trunk/examples/paranamer-example/src/test/java/org/codehaus/waffle/example/paranamer/DateValueConverterTest.java (510 => 511)

--- trunk/examples/paranamer-example/src/test/java/org/codehaus/waffle/example/paranamer/DateValueConverterTest.java	2007-12-19 04:12:36 UTC (rev 510)
+++ trunk/examples/paranamer-example/src/test/java/org/codehaus/waffle/example/paranamer/DateValueConverterTest.java	2007-12-19 05:04:01 UTC (rev 511)
@@ -1,18 +1,16 @@
 package org.codehaus.waffle.example.paranamer;
 
-import static org.junit.Assert.assertNotNull;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
 import ognl.OgnlException;
-
 import org.codehaus.waffle.bind.BindException;
 import org.codehaus.waffle.i18n.DefaultMessageResources;
 import org.junit.Assert;
+import static org.junit.Assert.assertNotNull;
 import org.junit.Test;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
+
 public class DateValueConverterTest {
 
     @Test
@@ -26,7 +24,7 @@
     @Test
     public void canConvert() throws OgnlException {
         DateValueConverter converter = new DateValueConverter(new DefaultMessageResources());
-        Date date = (Date) converter.convertValue("property-name", "19-09-2004", Date.class);
+        Date date = converter.convertValue("property-name", "19-09-2004", Date.class);
 
         Assert.assertEquals("09-19-2004", new SimpleDateFormat("MM-dd-yyyy").format(date));
     }
@@ -40,7 +38,7 @@
     @Test
     public void canUseDefaultFormatWhenNoAlternativeProvided() {
         DateValueConverter converter = new DateValueConverter(new DefaultMessageResources());
-        Date date = (Date) converter.convertValue("property-name", "00-21-1986", Date.class);
+        Date date = converter.convertValue("property-name", "00-21-1986", Date.class);
 
         assertNotNull(date);
     }

Modified: trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/DateValueConverter.java (510 => 511)

--- trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/DateValueConverter.java	2007-12-19 04:12:36 UTC (rev 510)
+++ trunk/examples/simple-example/src/main/java/org/codehaus/waffle/example/simple/DateValueConverter.java	2007-12-19 05:04:01 UTC (rev 511)
@@ -28,7 +28,8 @@
         return Date.class.isAssignableFrom(type);
     }
 
-    public Object convertValue(String propertyName, String value, Class<?> toType) {
+    @SuppressWarnings({"unchecked"})
+    public <T> T convertValue(String propertyName, String value, Class<T> toType) {
         if (value == null || value.equals("")) {
             return null;
         }
@@ -36,7 +37,7 @@
         String datePattern = messageResources.getMessageWithDefault("date.format", "dd-MM-yyyy");
 
         try {
-            return new SimpleDateFormat(datePattern).parse(value);
+            return (T) new SimpleDateFormat(datePattern).parse(value);
         } catch (ParseException e) {
             String fieldName = messageResources.getMessageWithDefault(propertyName, propertyName);
             String message = messageResources.getMessage("date.bind.error", fieldName, value, datePattern);

Modified: trunk/examples/simple-example/src/test/java/org/codehaus/waffle/example/simple/DateValueConverterTest.java (510 => 511)

--- trunk/examples/simple-example/src/test/java/org/codehaus/waffle/example/simple/DateValueConverterTest.java	2007-12-19 04:12:36 UTC (rev 510)
+++ trunk/examples/simple-example/src/test/java/org/codehaus/waffle/example/simple/DateValueConverterTest.java	2007-12-19 05:04:01 UTC (rev 511)
@@ -1,17 +1,15 @@
 package org.codehaus.waffle.example.simple;
 
-import static org.junit.Assert.assertNotNull;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
 import ognl.OgnlException;
-
 import org.codehaus.waffle.bind.BindException;
 import org.codehaus.waffle.i18n.DefaultMessageResources;
 import org.junit.Assert;
+import static org.junit.Assert.assertNotNull;
 import org.junit.Test;
 
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
 public class DateValueConverterTest {
 
     @Test
@@ -25,7 +23,7 @@
     @Test
     public void canConvert() throws OgnlException {
         DateValueConverter converter = new DateValueConverter(new DefaultMessageResources());
-        Date date = (Date) converter.convertValue("property-name", "19-09-2004", Date.class);
+        Date date = converter.convertValue("property-name", "19-09-2004", Date.class);
 
         Assert.assertEquals("09-19-2004", new SimpleDateFormat("MM-dd-yyyy").format(date));
     }
@@ -39,7 +37,7 @@
     @Test
     public void canUseDefaultFormatWhenNoAlternativeProvided() {
         DateValueConverter converter = new DateValueConverter(new DefaultMessageResources());
-        Date date = (Date) converter.convertValue("property-name", "00-21-1986", Date.class);
+        Date date = converter.convertValue("property-name", "00-21-1986", Date.class);
 
         assertNotNull(date);
     }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/action/AbstractMethodDefinitionFinder.java (510 => 511)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/action/AbstractMethodDefinitionFinder.java	2007-12-19 04:12:36 UTC (rev 510)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/action/AbstractMethodDefinitionFinder.java	2007-12-19 05:04:01 UTC (rev 511)
@@ -274,7 +274,7 @@
         return true;
     }
 
-    private Object convertValue(String value, Class<?> type) {
+    private <T> T convertValue(String value, Class<T> type) {
         if (isEmpty(value) && type.isPrimitive()) {
             value = null; // this allows Ognl to use that primitives default value
         }

Added: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/StringTransmuter.java (0 => 511)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/StringTransmuter.java	                        (rev 0)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/StringTransmuter.java	2007-12-19 05:04:01 UTC (rev 511)
@@ -0,0 +1,19 @@
+/*****************************************************************************
+ * Copyright (C) 2005,2006 Michael Ward                                      *
+ * All rights reserved.                                                      *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by: Michael Ward                                            *
+ *****************************************************************************/
+package org.codehaus.waffle.bind;
+
+/**
+ * This interface is used to simplify converting (transmuting) a String value into a given type.
+ */
+public interface StringTransmuter {
+
+    
+}

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverter.java (510 => 511)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverter.java	2007-12-19 04:12:36 UTC (rev 510)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ValueConverter.java	2007-12-19 05:04:01 UTC (rev 511)
@@ -35,6 +35,6 @@
      * @return The converted Object
      * @throws BindException if conversion fails
      */
-    Object convertValue(String propertyName, String value, Class<?> toType);
+    <T> T convertValue(String propertyName, String value, Class<T> toType);
 
 }

Modified: trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverter.java (510 => 511)

--- trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverter.java	2007-12-19 04:12:36 UTC (rev 510)
+++ trunk/waffle-core/src/main/java/org/codehaus/waffle/bind/ognl/OgnlValueConverter.java	2007-12-19 05:04:01 UTC (rev 511)
@@ -36,8 +36,8 @@
         return true;
     }
 
-    public Object convertValue(String propertyName, String value, Class<?> toType) {
-        return converter.convertValue(null, null, null, propertyName, value, toType);
+    public <T> T convertValue(String propertyName, String value, Class<T> toType) {
+        return (T) converter.convertValue(null, null, null, propertyName, value, toType);
     }
 
 }


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to