commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adri...@apache.org
Subject svn commit: r932838 - in /commons/sandbox/convert/trunk/src: main/java/org/apache/commons/convert/DateTimeConverters.java test/java/org/apache/commons/convert/TestDateTimeConverters.java
Date Sun, 11 Apr 2010 05:46:38 GMT
Author: adrianc
Date: Sun Apr 11 05:46:37 2010
New Revision: 932838

URL: http://svn.apache.org/viewvc?rev=932838&view=rev
Log:
Made the date/time converters reflective, eliminated a number of date/time conversion classes.

Modified:
    commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
    commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java

Modified: commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java?rev=932838&r1=932837&r2=932838&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/main/java/org/apache/commons/convert/DateTimeConverters.java
Sun Apr 11 05:46:37 2010
@@ -18,7 +18,6 @@
  *******************************************************************************/
 package org.apache.commons.convert;
 
-import java.sql.Time;
 import java.sql.Timestamp;
 import java.text.DateFormat;
 import java.text.ParseException;
@@ -31,6 +30,8 @@ import java.util.TimeZone;
 /** Date/time Converter classes. */
 public class DateTimeConverters implements ConverterLoader {
 
+    public static final String CALENDAR_FORMAT = "EEE MMM dd HH:mm:ss.SSS zzz yyyy";
+
     /**
      * Returns an initialized DateFormat object.
      *
@@ -90,7 +91,24 @@ public class DateTimeConverters implemen
 
     public void loadConverters() {
         Converters.loadContainedConverters(DateTimeConverters.class);
-        Converters.registerConverter(new GenericToStringConverter<Calendar>(Calendar.class));
+        Converters.registerConverter(new GenericDateToLong<java.util.Date>(java.util.Date.class));
+        Converters.registerConverter(new GenericDateToLong<java.sql.Date>(java.sql.Date.class));
+        Converters.registerConverter(new GenericDateToLong<java.sql.Time>(java.sql.Time.class));
+        Converters.registerConverter(new GenericDateToLong<java.sql.Timestamp>(java.sql.Timestamp.class));
+        Converters.registerConverter(new GenericSingletonToList<java.util.Calendar>(java.util.Calendar.class));
+        Converters.registerConverter(new GenericSingletonToList<java.util.Date>(java.util.Date.class));
+        Converters.registerConverter(new GenericSingletonToList<java.util.TimeZone>(java.util.TimeZone.class));
+        Converters.registerConverter(new GenericSingletonToList<java.sql.Date>(java.sql.Date.class));
+        Converters.registerConverter(new GenericSingletonToList<java.sql.Time>(java.sql.Time.class));
+        Converters.registerConverter(new GenericSingletonToList<java.sql.Timestamp>(java.sql.Timestamp.class));
+        Converters.registerConverter(new GenericSingletonToSet<java.util.Calendar>(java.util.Calendar.class));
+        Converters.registerConverter(new GenericSingletonToSet<java.util.Date>(java.util.Date.class));
+        Converters.registerConverter(new GenericSingletonToSet<java.util.TimeZone>(java.util.TimeZone.class));
+        Converters.registerConverter(new GenericSingletonToSet<java.sql.Date>(java.sql.Date.class));
+        Converters.registerConverter(new GenericSingletonToSet<java.sql.Time>(java.sql.Time.class));
+        Converters.registerConverter(new GenericSingletonToSet<java.sql.Timestamp>(java.sql.Timestamp.class));
+        Converters.registerConverter(new GenericToStringConverter<java.sql.Date>(java.sql.Date.class));
+        Converters.registerConverter(new GenericToStringConverter<java.sql.Time>(java.sql.Time.class));
     }
 
     public static class CalendarToLong extends AbstractConverter<Calendar, Long> {
@@ -103,13 +121,22 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class DateToLong extends AbstractConverter<Date, Long> {
-        public DateToLong() {
-            super(Date.class, Long.class);
+    public static class CalendarToString extends GenericLocalizedConverter<Calendar, String>
{
+        public CalendarToString() {
+            super(Calendar.class, String.class);
         }
 
-        public Long convert(Date obj) throws ConversionException {
-             return obj.getTime();
+        @Override
+        public String convert(Calendar obj) throws ConversionException {
+            DateFormat df = new SimpleDateFormat(CALENDAR_FORMAT);
+            df.setCalendar(obj);
+            return df.format(obj.getTime());
+        }
+
+        public String convert(Calendar obj, Locale locale, TimeZone timeZone, String formatString)
throws ConversionException {
+            DateFormat df = toDateTimeFormat(formatString, timeZone, locale);
+            df.setCalendar(obj);
+            return df.format(obj.getTime());
         }
     }
 
@@ -135,7 +162,8 @@ public class DateTimeConverters implemen
 
         @Override
         public String convert(Date obj) throws ConversionException {
-            return obj.toString();
+            DateFormat df = new SimpleDateFormat(CALENDAR_FORMAT);
+            return df.format(obj);
         }
 
         public String convert(Date obj, Locale locale, TimeZone timeZone, String formatString)
throws ConversionException {
@@ -159,15 +187,21 @@ public class DateTimeConverters implemen
         }
     }
 
+    public static class GenericDateToLong<S extends Date> extends AbstractConverter<S,
Long> {
+        public GenericDateToLong(Class<S> source) {
+            super(source, Long.class);
+        }
+
+        public Long convert(S obj) throws ConversionException {
+             return obj.getTime();
+        }
+    }
+
     public static abstract class GenericLocalizedConverter<S, T> extends AbstractLocalizedConverter<S,
T> {
         protected GenericLocalizedConverter(Class<S> sourceClass, Class<T> targetClass)
{
             super(sourceClass, targetClass);
         }
 
-        public T convert(S obj) throws ConversionException {
-            return convert(obj, Locale.getDefault(), TimeZone.getDefault(), null);
-        }
-
         public T convert(S obj, Locale locale, TimeZone timeZone) throws ConversionException
{
             return convert(obj, locale, timeZone, null);
         }
@@ -250,44 +284,6 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class SqlDateToList extends GenericSingletonToList<java.sql.Date>
{
-        public SqlDateToList() {
-            super(java.sql.Date.class);
-        }
-    }
-
-    public static class SqlDateToLong extends AbstractConverter<java.sql.Date, Long>
{
-        public SqlDateToLong() {
-            super(java.sql.Date.class, Long.class);
-        }
-
-        public Long convert(java.sql.Date obj) throws ConversionException {
-             return obj.getTime();
-        }
-    }
-
-    public static class SqlDateToSet extends GenericSingletonToSet<java.sql.Date> {
-        public SqlDateToSet() {
-            super(java.sql.Date.class);
-        }
-    }
-
-    public static class SqlDateToString extends GenericLocalizedConverter<java.sql.Date,
String> {
-        public SqlDateToString() {
-            super(java.sql.Date.class, String.class);
-        }
-
-        @Override
-        public String convert(java.sql.Date obj) throws ConversionException {
-            return obj.toString();
-        }
-
-        public String convert(java.sql.Date obj, Locale locale, TimeZone timeZone, String
formatString) throws ConversionException {
-            DateFormat df = toDateFormat(formatString, timeZone, locale);
-            return df.format(obj);
-        }
-    }
-
     public static class SqlDateToTimestamp extends AbstractConverter<java.sql.Date, java.sql.Timestamp>
{
         public SqlDateToTimestamp() {
             super(java.sql.Date.class, java.sql.Timestamp.class);
@@ -303,55 +299,20 @@ public class DateTimeConverters implemen
        }
     }
 
-    public static class SqlTimeToList extends GenericSingletonToList<java.sql.Time>
{
-        public SqlTimeToList() {
-            super(java.sql.Time.class);
-        }
-    }
-
-    public static class SqlTimeToLong extends AbstractConverter<java.sql.Time, Long>
{
-        public SqlTimeToLong() {
-            super(java.sql.Time.class, Long.class);
-        }
-
-        public Long convert(java.sql.Time obj) throws ConversionException {
-             return obj.getTime();
-        }
-    }
-
-    public static class SqlTimeToSet extends GenericSingletonToSet<java.sql.Time> {
-        public SqlTimeToSet() {
-            super(java.sql.Time.class);
-        }
-    }
-
-    public static class SqlTimeToString extends GenericLocalizedConverter<java.sql.Time,
String> {
-        public SqlTimeToString() {
-            super(java.sql.Time.class, String.class);
-        }
-
-        @Override
-        public String convert(Time obj) throws ConversionException {
-            return obj.toString();
-        }
-
-        public String convert(java.sql.Time obj, Locale locale, TimeZone timeZone, String
formatString) throws ConversionException {
-            DateFormat df = toTimeFormat(formatString, timeZone, locale);
-            return df.format(obj);
-        }
-    }
-
-    public static class StringToCalendar extends AbstractLocalizedConverter<String, Calendar>
{
+    public static class StringToCalendar extends GenericLocalizedConverter<String, Calendar>
{
         public StringToCalendar() {
             super(String.class, Calendar.class);
         }
 
         public Calendar convert(String obj) throws ConversionException {
-            return convert(obj, Locale.getDefault(), TimeZone.getDefault(), null);
-        }
-
-        public Calendar convert(String obj, Locale locale, TimeZone timeZone) throws ConversionException
{
-            return convert(obj, Locale.getDefault(), TimeZone.getDefault(), null);
+            try {
+                DateFormat df = new SimpleDateFormat(CALENDAR_FORMAT);
+                Calendar cal = Calendar.getInstance();
+                cal.setTime(df.parse(obj));
+                return cal;
+            } catch (ParseException e) {
+                throw new ConversionException(e);
+            }
         }
 
         public Calendar convert(String obj, Locale locale, TimeZone timeZone, String formatString)
throws ConversionException {
@@ -375,7 +336,7 @@ public class DateTimeConverters implemen
         @Override
         public Date convert(String obj) throws ConversionException {
             try {
-                DateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
+                DateFormat df = new SimpleDateFormat(CALENDAR_FORMAT);
                 return df.parse(obj);
             } catch (ParseException e) {
                 throw new ConversionException(e);
@@ -392,7 +353,7 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class StringToSqlDate extends GenericLocalizedConverter<String, java.sql.Date>
{
+    public static class StringToSqlDate extends AbstractConverter<String, java.sql.Date>
{
         public StringToSqlDate() {
             super(String.class, java.sql.Date.class);
         }
@@ -401,18 +362,9 @@ public class DateTimeConverters implemen
         public java.sql.Date convert(String obj) throws ConversionException {
             return java.sql.Date.valueOf(obj);
         }
-
-        public java.sql.Date convert(String obj, Locale locale, TimeZone timeZone, String
formatString) throws ConversionException {
-            DateFormat df = toDateFormat(formatString, timeZone, locale);
-            try {
-                return new java.sql.Date(df.parse(obj).getTime());
-            } catch (ParseException e) {
-                throw new ConversionException(e);
-            }
-        }
     }
 
-    public static class StringToSqlTime extends GenericLocalizedConverter<String, java.sql.Time>
{
+    public static class StringToSqlTime extends AbstractConverter<String, java.sql.Time>
{
         public StringToSqlTime() {
             super(String.class, java.sql.Time.class);
         }
@@ -421,15 +373,6 @@ public class DateTimeConverters implemen
         public java.sql.Time convert(String obj) throws ConversionException {
             return java.sql.Time.valueOf(obj);
         }
-
-        public java.sql.Time convert(String obj, Locale locale, TimeZone timeZone, String
formatString) throws ConversionException {
-            DateFormat df = toTimeFormat(formatString, timeZone, locale);
-            try {
-                return new java.sql.Time(df.parse(obj).getTime());
-            } catch (ParseException e) {
-                throw new ConversionException(e);
-            }
-        }
     }
 
     public static class StringToTimestamp extends GenericLocalizedConverter<String, java.sql.Timestamp>
{
@@ -477,43 +420,6 @@ public class DateTimeConverters implemen
         }
     }
 
-    public static class TimestampToList extends GenericSingletonToList<Timestamp> {
-        public TimestampToList() {
-            super(Timestamp.class);
-        }
-    }
-
-    public static class TimestampToLong extends AbstractConverter<java.sql.Timestamp,
Long> {
-        public TimestampToLong() {
-            super(java.sql.Timestamp.class, Long.class);
-        }
-
-        public Long convert(java.sql.Timestamp obj) throws ConversionException {
-             return obj.getTime();
-        }
-    }
-
-    public static class TimestampToSet extends GenericSingletonToSet<Timestamp> {
-        public TimestampToSet() {
-            super(Timestamp.class);
-        }
-    }
-
-    public static class TimestampToSqlDate extends AbstractConverter<Timestamp, java.sql.Date>
{
-        public TimestampToSqlDate() {
-            super(Timestamp.class, java.sql.Date.class);
-        }
-
-        @Override
-        public boolean canConvert(Class<?> sourceClass, Class<?> targetClass)
{
-            return java.sql.Timestamp.class.equals(sourceClass) && java.sql.Date.class.equals(targetClass);
-        }
-
-        public java.sql.Date convert(Timestamp obj) throws ConversionException {
-            return new java.sql.Date(obj.getTime());
-        }
-    }
-
     public static class TimestampToString extends GenericLocalizedConverter<java.sql.Timestamp,
String> {
         public TimestampToString() {
             super(java.sql.Timestamp.class, String.class);

Modified: commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java
URL: http://svn.apache.org/viewvc/commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java?rev=932838&r1=932837&r2=932838&view=diff
==============================================================================
--- commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java
(original)
+++ commons/sandbox/convert/trunk/src/test/java/org/apache/commons/convert/TestDateTimeConverters.java
Sun Apr 11 05:46:37 2010
@@ -18,62 +18,91 @@
  *******************************************************************************/
 package org.apache.commons.convert;
 
-import org.apache.commons.convert.Converter;
-import org.apache.commons.convert.DateTimeConverters;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.TimeZone;
 
 import junit.framework.TestCase;
 
 public class TestDateTimeConverters extends TestCase {
 
-    public TestDateTimeConverters(String name) {
-        super(name);
+    public static <S, T> void assertConversion(String label, Converter<S, T>
converter, S source, T target) throws Exception {
+        assertConversion(label, converter, source, target, true);
     }
 
-    public static <S, T> void assertConversion(String label, Converter<S, T>
converter, S source, T target) throws Exception {
+    @SuppressWarnings("unchecked")
+    public static <S, T> void assertConversion(String label, Converter<S, T>
converter, S source, T target, boolean testRegistration) throws Exception {
         assertTrue(label + " can convert", converter.canConvert(source.getClass(), target.getClass()));
+        if (testRegistration) {
+            assertEquals(label + " registered", converter.getClass(), Converters.getConverter(source.getClass(),
target.getClass()).getClass());
+        }
         assertEquals(label + " converted", target, converter.convert(source));
+        try {
+            Converter<T, S> reflectiveConverter = (Converter<T, S>) Converters.getConverter(target.getClass(),
source.getClass());
+            assertEquals(label + " reflection converted", source, reflectiveConverter.convert(target));
+        } catch (ClassNotFoundException e) {
+            System.out.println(converter.getClass() + " not reflective");
+        }
     }
 
-    public static <S, T> void assertStringConversion(String label, Converter<S,
T> converter, S source, java.util.Date target) throws Exception {
-        assertTrue(label + " can convert", converter.canConvert(source.getClass(), target.getClass()));
-        T result = converter.convert(source);
-        assertEquals(label + " converted", source, result.toString());
+    @SuppressWarnings("unchecked")
+    public static <S> void assertToCollection(String label, S source) throws Exception
{
+        Converter<S, ? extends Collection> toList = (Converter<S, ? extends Collection>)
Converters.getConverter(source.getClass(), List.class);
+        Collection<S> listResult = toList.convert(source);
+        assertEquals(label + " converted to List", source, listResult.toArray()[0]);
+        Converter<S, ? extends Collection> toSet = (Converter<S, ? extends Collection>)
Converters.getConverter(source.getClass(), Set.class);
+        Collection<S> setResult = toSet.convert(source);
+        assertEquals(label + " converted to Set", source, setResult.toArray()[0]);
+    }
+
+    public TestDateTimeConverters(String name) {
+        super(name);
     }
 
+    @SuppressWarnings("deprecation")
     public void testDateTimeConverters() throws Exception {
+        ConverterLoader loader = new DateTimeConverters();
+        loader.loadConverters();
         long currentTime = System.currentTimeMillis();
+        java.util.Calendar cal = java.util.Calendar.getInstance();
+        cal.setTimeInMillis(currentTime);
         java.util.Date utilDate = new java.util.Date(currentTime);
-        java.sql.Date sqlDate = new java.sql.Date(currentTime);
-        java.sql.Time sqlTime = new java.sql.Time(currentTime);
+        java.sql.Date sqlDate = new java.sql.Date(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
+        java.sql.Time sqlTime = new java.sql.Time(cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE),
cal.get(Calendar.SECOND));
         java.sql.Timestamp timestamp = new java.sql.Timestamp(currentTime);
+        // Source class = java.util.Calendar
+        DateFormat df = new SimpleDateFormat(DateTimeConverters.CALENDAR_FORMAT);
+        df.setCalendar(cal);
+        assertConversion("CalendarToLong", new DateTimeConverters.CalendarToLong(), cal ,
currentTime);
+        assertConversion("CalendarToString", new DateTimeConverters.CalendarToString(), cal
, df.format(cal.getTime()));
+        assertToCollection("CalendarToCollection", cal);
         // Source class = java.util.Date
-        assertConversion("DateToLong", new DateTimeConverters.DateToLong(), utilDate, currentTime);
-        assertConversion("DateToSqlDate", new DateTimeConverters.DateToSqlDate(), utilDate,
new java.sql.Date(currentTime));
-        assertConversion("DateToString", new DateTimeConverters.DateToString(), utilDate,
utilDate.toString());
-        assertConversion("DateToTimestamp", new DateTimeConverters.DateToTimestamp(), utilDate,
new java.sql.Timestamp(currentTime));
+        assertConversion("DateToLong", new DateTimeConverters.GenericDateToLong<java.util.Date>(java.util.Date.class),
utilDate, currentTime);
+        assertConversion("DateToSqlDate", new DateTimeConverters.DateToSqlDate(), new java.util.Date(sqlDate.getTime()),
sqlDate);
+        assertConversion("DateToSqlDate", new DateTimeConverters.DateToSqlDate(), new java.sql.Timestamp(sqlDate.getTime()),
sqlDate);
+        assertConversion("DateToString", new DateTimeConverters.DateToString(), utilDate,
df.format(cal.getTime()));
+        assertConversion("DateToTimestamp", new DateTimeConverters.DateToTimestamp(), utilDate,
timestamp);
+        assertConversion("DateToTimestamp", new DateTimeConverters.DateToTimestamp(), timestamp,
timestamp, false);
+        assertToCollection("DateToCollection", utilDate);
         // Source class = java.sql.Date
-        assertConversion("SqlDateToDate", new DateTimeConverters.SqlDateToDate(), sqlDate,
utilDate);
-        assertConversion("SqlDateToLong", new DateTimeConverters.SqlDateToLong(), sqlDate,
currentTime);
-        assertConversion("SqlDateToString", new DateTimeConverters.SqlDateToString(), sqlDate,
sqlDate.toString());
-        assertConversion("SqlDateToTimestamp", new DateTimeConverters.SqlDateToTimestamp(),
sqlDate, timestamp);
+        assertConversion("SqlDateToDate", new DateTimeConverters.SqlDateToDate(), sqlDate,
new java.util.Date(sqlDate.getTime()));
+        assertConversion("SqlDateToLong", new DateTimeConverters.GenericDateToLong<java.sql.Date>(java.sql.Date.class),
sqlDate, sqlDate.getTime());
+        assertConversion("SqlDateToTimestamp", new DateTimeConverters.SqlDateToTimestamp(),
sqlDate, new java.sql.Timestamp(sqlDate.getTime()));
+        assertToCollection("SqlDateToCollection", sqlDate);
         // Source class = java.sql.Time
-        // assertConversion("SqlTimeToDate", new DateTimeConverters.SqlTimeToSqlDate(), sqlTime,
sqlDate);
-        assertConversion("SqlTimeToLong", new DateTimeConverters.SqlTimeToLong(), sqlTime,
currentTime);
-        assertConversion("SqlTimeToString", new DateTimeConverters.SqlTimeToString(), sqlTime,
sqlTime.toString());
+        assertConversion("SqlTimeToLong", new DateTimeConverters.GenericDateToLong<java.sql.Time>(java.sql.Time.class),
sqlTime, sqlTime.getTime());
+        assertToCollection("SqlTimeToCollection", sqlTime);
         // Source class = java.sql.Timestamp
-        assertConversion("TimestampToDate", new DateTimeConverters.TimestampToDate(), timestamp,
utilDate);
-        assertConversion("TimestampToLong", new DateTimeConverters.TimestampToLong(), timestamp,
currentTime);
-        assertConversion("TimestampToSqlDate", new DateTimeConverters.TimestampToSqlDate(),
timestamp, sqlDate);
+        assertConversion("TimestampToLong", new DateTimeConverters.GenericDateToLong<java.sql.Timestamp>(java.sql.Timestamp.class),
timestamp, currentTime);
         assertConversion("TimestampToString", new DateTimeConverters.TimestampToString(),
timestamp, timestamp.toString());
-        // Source class = java.lang.String
-        assertStringConversion("StringToDate", new DateTimeConverters.StringToDate(), utilDate.toString(),
utilDate);
-        assertStringConversion("StringToSqlDate", new DateTimeConverters.StringToSqlDate(),
sqlDate.toString(), sqlDate);
-        assertStringConversion("StringToSqlTime", new DateTimeConverters.StringToSqlTime(),
sqlTime.toString(), sqlTime);
-        assertStringConversion("StringToTimestamp", new DateTimeConverters.StringToTimestamp(),
timestamp.toString(), timestamp);
-        // Source class = java.lang.Long
-        assertConversion("LongToDate", new DateTimeConverters.LongToDate(), currentTime,
utilDate);
-        assertConversion("LongToSqlDate", new DateTimeConverters.LongToSqlDate(), currentTime,
sqlDate);
-        assertConversion("LongToSqlTime", new DateTimeConverters.LongToSqlTime(), currentTime,
sqlTime);
-        assertConversion("LongToTimestamp", new DateTimeConverters.LongToTimestamp(), currentTime,
timestamp);
+        assertToCollection("TimestampToCollection", timestamp);
+        // TimeZone tests
+        TimeZone tz = TimeZone.getDefault();
+        assertConversion("TimeZoneToString", new DateTimeConverters.TimeZoneToString(), tz,
tz.getID());
+        assertToCollection("TimeZoneToCollection", tz);
     }
 }



Mime
View raw message