DateConverter at revision 31848

While the idea to provide a converter from date to other types was found in GeoTools, the code has been totally rewritten and share nothing in common. The Geotoolkit.org code handles only the conversions to java.sql.Date, Timestamp and Long. Conversions from/to String are excluded.

The table below compares the GeoTools code with the Geotoolkit.org one.

Command line:

svn cat -r31848 https://svn.osgeo.org/geotools/trunk/modules/library/main/src/main/java/org/geotools/util/TemporalConverterFactory.java
Revision 31848Geotoolkit.org
/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 *
 *    (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.util;

import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.geotools.factory.Hints;
/**
 * Converter factory which created converting between the various temporal types.
 * <p>
 * Supported converstions:
 * <ul>
 *  <li>{@link java.util.Date} to {@link Calendar}
 *  <li>{@link java.sql.Timestamp} to {@link java.util.Calendar}
 *  <li>{@link java.sql.Time} to {@link java.util.Calendar}
 *  <li>{@link java.util.Date} to {@link java.sql.Timestamp}
 *  <li>{@link java.util.Date} to {@link java.sql.Time}
 *  <li>{@link java.util.Date} to {@link java.sql.Date}
 *  <li>{@link java.util.Calendar} to {@link java.util.Date}
 *  <li>{@link java.util.Calendar} to {@link java.sql.Timestamp}
 *  <li>{@link java.util.Calendar} to {@link java.sql.Time}
 *  <li>{@link XMLGregorianCalendar} to {@link Calendar}
 *  <li>{@link Calendar} to {@link XMLGregorianCalendar}
 *  <li>{@link XMLGregorianCalendar} to {@link Date}
 *  <li>{@link Date} to {@link XMLGregorianCalendar}
 * </ul>
 * </p>
 * <p>
 * The hint {@link #DATE_FORMAT} can be used to control the format of converting a temporal value
 * to a String.
 * </p>
 * @author Justin Deoliveira, The Open Planning Project
 * @since 2.4
 */
public class TemporalConverterFactory implements ConverterFactory {

    public Converter createConverter(Class source, Class target, Hints hints) {

        if ( Date.class.isAssignableFrom( source ) ) {
            // handle all of (java.util.Date,java.sql.Timestamp,and java.sql.Time) -> java.util.Calendar
            if ( Calendar.class.isAssignableFrom( target ) ) {
                return new Converter() {
                    public Object convert(Object source, Class target) throws Exception {
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTime( (Date) source );

                            return calendar;
                    }
                };
            }

            //handle all of (java.util.Date) -> (java.sql.Timestamp,java.sql.Time)
            if ( Timestamp.class.isAssignableFrom( target ) || Time.class.isAssignableFrom( target ) ||
                java.sql.Date.class.isAssignableFrom( target ) ) {

                return new Converter() {

                    public Object convert(Object source, Class target) throws Exception {
                        Date date = (Date) source;
                        return target.getConstructor( new Class[] { Long.TYPE } )
                                .newInstance( new Object[]{ new Long( date.getTime() ) } );
                    }

                };
            }

            if ( XMLGregorianCalendar.class.isAssignableFrom( target ) ) {
                return new Converter() {
                    public <T> T convert(Object source, Class<T> target)
                            throws Exception {
                        Date date = (Date) source;
                        Calendar calendar = createConverter(Date.class, Calendar.class,null)
                            .convert( date, Calendar.class);

                        return (T) createConverter( Calendar.class, XMLGregorianCalendar.class, null )
                            .convert( calendar, XMLGregorianCalendar.class );

                    }
                };
            }
//          if ( String.class.equals( target ) ) {
//              final DateFormat fFormat;
//              if ( dateFormat != null ) {
//                  fFormat = dateFormat;
//              }
//              else {
//                  //create a default
//                  fFormat = DateFormat.getDateInstance();
//              }
//
//              return new Converter() {
//                  public Object convert(Object source, Class target) throws Exception {
//                      return fFormat.format( (Date)source );
//                  }
//              };
//          }
        }

        //this should handle java.util.Calendar to (java.util.Date,java.sql.Timestamp,java.util.Time}
        if ( Calendar.class.isAssignableFrom( source ) ) {
            if ( Date.class.isAssignableFrom( target ) ) {
                final Class fTarget = target;
                return new Converter() {
                    public Object convert(Object source, Class target) throws Exception {
                        Calendar calendar = (Calendar) source;

                        return target.getConstructor( new Class[] { Long.TYPE } ).newInstance(
                                new Object[]{ new Long( calendar.getTimeInMillis() ) }
                        );
                    }
                };
            }
            if ( XMLGregorianCalendar.class.isAssignableFrom( target ) ) {
                return new Converter() {
                    public <T> T convert(Object source, Class<T> target)
                            throws Exception {
                        if( source instanceof GregorianCalendar ) {
                            return (T) DatatypeFactory.newInstance().newXMLGregorianCalendar( (GregorianCalendar) source );
                        }

                        return null;
                    }
                };
            }
//          if ( String.class.equals( target ) ) {
//              final DateFormat fFormat;
//              if ( dateTimeFormat != null ) {
//                  fFormat = dateTimeFormat;
//              }
//              else {
//                  //create a default
//                  fFormat = DateFormat.getDateTimeInstance();
//              }
//
//              return new Converter() {
//                  public Object convert(Object source, Class target) throws Exception {
//                      Date date = ((Calendar)source).getTime();
//                      return fFormat.format( date );
//                  }
//              };
//          }
        }

        if ( XMLGregorianCalendar.class.isAssignableFrom( source ) ) {
            if ( Calendar.class.isAssignableFrom( target ) ) {
                return new Converter() {
                    public <T> T convert(Object source, Class<T> target)
                            throws Exception {
                        XMLGregorianCalendar calendar = (XMLGregorianCalendar) source;
                        return (T) calendar.toGregorianCalendar();
                    }
                };
            }
            if ( Date.class.isAssignableFrom( target ) ) {
                return new Converter() {
                    public <T> T convert(Object source, Class<T> target)
                            throws Exception {
                        Calendar calendar = createConverter(XMLGregorianCalendar.class, Calendar.class, null)
                            .convert( source, Calendar.class );
                        if ( calendar != null ) {
                            return (T) createConverter( Calendar.class, Date.class, null )
                                .convert( calendar, Date.class );
                        }
                        return null;
                    }
                };
            }
        }
        return null;
    }

}
/*
 *    Geotoolkit.org - An Open Source Java GIS Toolkit
 *    http://www.geotoolkit.org
 *
 *    (C) 2008-2012, Open Source Geospatial Foundation (OSGeo)
 *    (C) 2009-2012, Geomatys
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotoolkit.util.converter;

import java.util.Date;
import java.io.Serializable;
import java.io.ObjectStreamException;
import net.jcip.annotations.Immutable;


/**
 * Handles conversions from {@link java.util.Date} to various objects.
 *
 * {@section String representation}
 * There is currently no converter between {@link String} and {@link java.util.Date} because the
 * date format is not yet defined (we are considering the ISO format for a future Geotk version).
 *
 * @author Martin Desruisseaux (Geomatys)
 * @version 3.00
 *
 * @since 2.4
 * @module
 */
@Immutable
abstract class DateConverter<T> extends SimpleConverter<Date,T> implements Serializable {
    /**
     * For cross-version compatibility.
     */
    private static final long serialVersionUID = -7770401534710581917L;

    /**
     * Returns the source class, which is always {@link Date}.
     */
    @Override
    public final Class<Date> getSourceClass() {
        return Date.class;
    }

    /**
     * Converter from dates to long integers.
     *
     * @author Martin Desruisseaux (Geomatys)
     * @version 3.00
     *
     * @since 2.4
     */
    @Immutable
    static final class Long extends DateConverter<java.lang.Long> {
        private static final long serialVersionUID = 3163928356094316134L;
        public static final Long INSTANCE = new Long();
        private Long() {
        }

        @Override
        public Class<java.lang.Long> getTargetClass() {
            return java.lang.Long.class;
        }

        @Override
        public java.lang.Long convert(final Date source) {
            if (source == null) {
                return null;
            }
            return source.getTime();
        }

        /** Returns the singleton instance on deserialization. */
        protected Object readResolve() throws ObjectStreamException {
            return INSTANCE;
        }
    }

    /**
     * Converter from dates to SQL dates.
     *
     * @author Martin Desruisseaux (Geomatys)
     * @version 3.17
     *
     * @since 3.17
     */
    @Immutable
    static final class SQL extends DateConverter<java.sql.Date> {
        private static final long serialVersionUID = -3644605344718636345L;
        public static final SQL INSTANCE = new SQL();
        private SQL() {
        }

        @Override
        public Class<java.sql.Date> getTargetClass() {
            return java.sql.Date.class;
        }

        @Override
        public java.sql.Date convert(final Date source) {
            if (source == null) {
                return null;
            }
            return new java.sql.Date(source.getTime());
        }

        /** Returns the singleton instance on deserialization. */
        protected Object readResolve() throws ObjectStreamException {
            return INSTANCE;
        }
    }

    /**
     * Converter from dates to timestamps.
     *
     * @author Martin Desruisseaux (Geomatys)
     * @version 3.00
     *
     * @since 3.00
     */
    @Immutable
    static final class Timestamp extends DateConverter<java.sql.Timestamp> {
        private static final long serialVersionUID = 3798633184562706892L;
        public static final Timestamp INSTANCE = new Timestamp();
        private Timestamp() {
        }

        @Override
        public Class<java.sql.Timestamp> getTargetClass() {
            return java.sql.Timestamp.class;
        }

        @Override
        public java.sql.Timestamp convert(final Date source) {
            if (source == null) {
                return null;
            }
            return new java.sql.Timestamp(source.getTime());
        }

        /** Returns the singleton instance on deserialization. */
        protected Object readResolve() throws ObjectStreamException {
            return INSTANCE;
        }
    }
}