You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/10/23 22:15:56 UTC

svn commit: r1535151 - in /commons/proper/beanutils/branches/java5/src: main/java/org/apache/commons/beanutils/converters/ test/java/org/apache/commons/beanutils/converters/

Author: oheger
Date: Wed Oct 23 20:15:55 2013
New Revision: 1535151

URL: http://svn.apache.org/r1535151
Log:
Generified all date/time converters.

Modified:
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CalendarConverter.java
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateConverter.java
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateTimeConverter.java
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlDateConverter.java
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CalendarConverterTestCase.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestBase.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestCase.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CalendarConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CalendarConverter.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CalendarConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/CalendarConverter.java Wed Oct 23 20:15:55 2013
@@ -61,7 +61,7 @@ public final class CalendarConverter ext
      * @return The default type this <code>Converter</code> handles.
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return Calendar.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateConverter.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateConverter.java Wed Oct 23 20:15:55 2013
@@ -61,7 +61,7 @@ public final class DateConverter extends
      * @return The default type this <code>Converter</code> handles.
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return Date.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateTimeConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateTimeConverter.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateTimeConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/DateTimeConverter.java Wed Oct 23 20:15:55 2013
@@ -16,13 +16,14 @@
  */
 package org.apache.commons.beanutils.converters;
 
+import java.text.DateFormat;
+import java.text.ParsePosition;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
 import java.util.Date;
 import java.util.Locale;
-import java.util.Calendar;
 import java.util.TimeZone;
-import java.text.DateFormat;
-import java.text.SimpleDateFormat;
-import java.text.ParsePosition;
+
 import org.apache.commons.beanutils.ConversionException;
 
 /**
@@ -277,15 +278,16 @@ public abstract class DateTimeConverter 
      * Otherwise the default <code>DateFormat</code> for the default locale
      * (and <i>style</i> if configured) will be used.
      *
+     * @param <T> The desired target type of the conversion.
      * @param targetType Data type to which this value should be converted.
      * @param value The input value to be converted.
      * @return The converted value.
      * @throws Exception if conversion cannot be performed successfully
      */
     @Override
-    protected Object convertToType(Class targetType, Object value) throws Exception {
+    protected <T> T convertToType(Class<T> targetType, Object value) throws Exception {
 
-        Class sourceType = value.getClass();
+        Class<?> sourceType = value.getClass();
 
         // Handle java.sql.Timestamp
         if (value instanceof java.sql.Timestamp) {
@@ -335,7 +337,7 @@ public abstract class DateTimeConverter 
                 calendar = parse(sourceType, targetType, stringValue, format);
             }
             if (Calendar.class.isAssignableFrom(targetType)) {
-                return calendar;
+                return targetType.cast(calendar);
             } else {
                 return toDate(targetType, calendar.getTime().getTime());
             }
@@ -360,30 +362,31 @@ public abstract class DateTimeConverter 
      *     <li><code>java.sql.Timestamp</code></li>
      * </ul>
      *
+     * @param <T> The target type
      * @param type The Date type to convert to
      * @param value The long value to convert.
      * @return The converted date value.
      */
-    private Object toDate(Class type, long value) {
+    private <T> T toDate(Class<T> type, long value) {
 
         // java.util.Date
         if (type.equals(Date.class)) {
-            return new Date(value);
+            return type.cast(new Date(value));
         }
 
         // java.sql.Date
         if (type.equals(java.sql.Date.class)) {
-            return new java.sql.Date(value);
+            return type.cast(new java.sql.Date(value));
         }
 
         // java.sql.Time
         if (type.equals(java.sql.Time.class)) {
-            return new java.sql.Time(value);
+            return type.cast(new java.sql.Time(value));
         }
 
         // java.sql.Timestamp
         if (type.equals(java.sql.Timestamp.class)) {
-            return new java.sql.Timestamp(value);
+            return type.cast(new java.sql.Timestamp(value));
         }
 
         // java.util.Calendar
@@ -400,7 +403,7 @@ public abstract class DateTimeConverter 
             }
             calendar.setTime(new Date(value));
             calendar.setLenient(false);
-            return calendar;
+            return type.cast(calendar);
         }
 
         String msg = toString(getClass()) + " cannot handle conversion to '"
@@ -425,15 +428,16 @@ public abstract class DateTimeConverter 
      * mechanism is provided for <code>java.util.Date</code>
      * and <code>java.util.Calendar</code> type.
      *
-     * @param type The Number type to convert to
+     * @param <T> The target type
+     * @param type The date type to convert to
      * @param value The String value to convert.
      * @return The converted Number value.
      */
-    private Object toDate(Class type, String value) {
+    private <T> T toDate(Class<T> type, String value) {
         // java.sql.Date
         if (type.equals(java.sql.Date.class)) {
             try {
-                return java.sql.Date.valueOf(value);
+                return type.cast(java.sql.Date.valueOf(value));
             } catch (IllegalArgumentException e) {
                 throw new ConversionException(
                         "String must be in JDBC format [yyyy-MM-dd] to create a java.sql.Date");
@@ -443,7 +447,7 @@ public abstract class DateTimeConverter 
         // java.sql.Time
         if (type.equals(java.sql.Time.class)) {
             try {
-                return java.sql.Time.valueOf(value);
+                return type.cast(java.sql.Time.valueOf(value));
             } catch (IllegalArgumentException e) {
                 throw new ConversionException(
                         "String must be in JDBC format [HH:mm:ss] to create a java.sql.Time");
@@ -453,7 +457,7 @@ public abstract class DateTimeConverter 
         // java.sql.Timestamp
         if (type.equals(java.sql.Timestamp.class)) {
             try {
-                return java.sql.Timestamp.valueOf(value);
+                return type.cast(java.sql.Timestamp.valueOf(value));
             } catch (IllegalArgumentException e) {
                 throw new ConversionException(
                         "String must be in JDBC format [yyyy-MM-dd HH:mm:ss.fffffffff] " +
@@ -514,7 +518,7 @@ public abstract class DateTimeConverter 
      * @return The converted Date object.
      * @throws Exception if an error occurs parsing the date.
      */
-    private Calendar parse(Class sourceType, Class targetType, String value) throws Exception {
+    private Calendar parse(Class<?> sourceType, Class<?> targetType, String value) throws Exception {
         Exception firstEx = null;
         for (int i = 0; i < patterns.length; i++) {
             try {
@@ -547,7 +551,7 @@ public abstract class DateTimeConverter 
      * @return The converted Calendar object.
      * @throws ConversionException if the String cannot be converted.
      */
-    private Calendar parse(Class sourceType, Class targetType, String value, DateFormat format) {
+    private Calendar parse(Class<?> sourceType, Class<?> targetType, String value, DateFormat format) {
         logFormat("Parsing", format);
         format.setLenient(false);
         ParsePosition pos = new ParsePosition(0);

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlDateConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlDateConverter.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlDateConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlDateConverter.java Wed Oct 23 20:15:55 2013
@@ -62,7 +62,7 @@ public final class SqlDateConverter exte
      * @since 1.8.0
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return Date.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimeConverter.java Wed Oct 23 20:15:55 2013
@@ -65,7 +65,7 @@ public final class SqlTimeConverter exte
      * @since 1.8.0
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return Time.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/SqlTimestampConverter.java Wed Oct 23 20:15:55 2013
@@ -65,7 +65,7 @@ public final class SqlTimestampConverter
      * @since 1.8.0
      */
     @Override
-    protected Class getDefaultType() {
+    protected Class<?> getDefaultType() {
         return Timestamp.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CalendarConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CalendarConverterTestCase.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CalendarConverterTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/CalendarConverterTestCase.java Wed Oct 23 20:15:55 2013
@@ -17,6 +17,7 @@
 package org.apache.commons.beanutils.converters;
 
 import java.util.Calendar;
+
 import junit.framework.TestSuite;
 
 /**
@@ -69,7 +70,7 @@ public class CalendarConverterTestCase e
      * @return The expected type
      */
     @Override
-    protected Class getExpectedType() {
+    protected Class<?> getExpectedType() {
         return Calendar.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestBase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestBase.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestBase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestBase.java Wed Oct 23 20:15:55 2013
@@ -25,8 +25,9 @@ import java.util.GregorianCalendar;
 import java.util.Locale;
 
 import junit.framework.TestCase;
-import org.apache.commons.beanutils.Converter;
+
 import org.apache.commons.beanutils.ConversionException;
+import org.apache.commons.beanutils.Converter;
 
 /**
  * Abstract base for &lt;Date&gt;Converter classes.
@@ -39,7 +40,7 @@ public abstract class DateConverterTestB
     // ------------------------------------------------------------------------
 
     /**
-     * Construtc a new test case.
+     * Construct a new test case.
      * @param name Name of the test
      */
     public DateConverterTestBase(String name) {
@@ -65,7 +66,7 @@ public abstract class DateConverterTestB
      * Return the expected type
      * @return The expected type
      */
-    protected abstract Class getExpectedType();
+    protected abstract Class<?> getExpectedType();
 
     /**
      * Convert from a Calendar to the appropriate Date type
@@ -152,7 +153,7 @@ public abstract class DateConverterTestB
     /**
      * Test default String to type conversion
      *
-     * N.B. This method is overriden by test case
+     * N.B. This method is overridden by test case
      * implementations for java.sql.Date/Time/Timestamp
      */
     public void testDefaultStringToTypeConvert() {
@@ -394,8 +395,8 @@ public abstract class DateConverterTestB
         String msg = "Converting '" + valueType + "' value '" + value + "'";
         try {
             Object result = converter.convert(getExpectedType(), value);
-            Class resultType = (result   == null ? null : result.getClass());
-            Class expectType = (expected == null ? null : expected.getClass());
+            Class<?> resultType = (result   == null ? null : result.getClass());
+            Class<?> expectType = (expected == null ? null : expected.getClass());
             assertEquals("TYPE "  + msg, expectType, resultType);
             assertEquals("VALUE " + msg, expected, result);
         } catch (Exception ex) {
@@ -414,8 +415,8 @@ public abstract class DateConverterTestB
         String msg = "Converting '" + valueType + "' value '" + value + "' to String";
         try {
             Object result = converter.convert(String.class, value);
-            Class resultType = (result   == null ? null : result.getClass());
-            Class expectType = (expected == null ? null : expected.getClass());
+            Class<?> resultType = (result   == null ? null : result.getClass());
+            Class<?> expectType = (expected == null ? null : expected.getClass());
             assertEquals("TYPE "  + msg, expectType, resultType);
             assertEquals("VALUE " + msg, expected, result);
         } catch (Exception ex) {

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestCase.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/DateConverterTestCase.java Wed Oct 23 20:15:55 2013
@@ -82,7 +82,7 @@ public class DateConverterTestCase exten
      * @return The expected type
      */
     @Override
-    protected Class getExpectedType() {
+    protected Class<?> getExpectedType() {
         return Date.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlDateConverterTestCase.java Wed Oct 23 20:15:55 2013
@@ -111,7 +111,7 @@ public class SqlDateConverterTestCase ex
      * @return The expected type
      */
     @Override
-    protected Class getExpectedType() {
+    protected Class<?> getExpectedType() {
         return Date.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimeConverterTestCase.java Wed Oct 23 20:15:55 2013
@@ -134,7 +134,7 @@ public class SqlTimeConverterTestCase ex
      * @return The expected type
      */
     @Override
-    protected Class getExpectedType() {
+    protected Class<?> getExpectedType() {
         return Time.class;
     }
 

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java?rev=1535151&r1=1535150&r2=1535151&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/converters/SqlTimestampConverterTestCase.java Wed Oct 23 20:15:55 2013
@@ -131,7 +131,7 @@ public class SqlTimestampConverterTestCa
      * @return The expected type
      */
     @Override
-    protected Class getExpectedType() {
+    protected Class<?> getExpectedType() {
         return Timestamp.class;
     }