001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, GeoTools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.util;
017:
018: import java.sql.Time;
019: import java.sql.Timestamp;
020: import java.util.Calendar;
021: import java.util.Date;
022:
023: import org.geotools.factory.Hints;
024:
025: /**
026: * Converter factory which created converting between the various temporal types.
027: * <p>
028: * Supported converstions:
029: * <ul>
030: * <li>{@link java.util.Date} to {@link Calendar}
031: * <li>{@link java.sql.Timestamp} to {@link java.util.Calendar}
032: * <li>{@link java.sql.Time} to {@link java.util.Calendar}
033: * <li>{@link java.util.Date} to {@link java.sql.Timestamp}
034: * <li>{@link java.util.Date} to {@link java.sql.Time}
035: * <li>{@link java.util.Date} to {@link java.sql.Date}
036: * <li>{@link java.util.Calendar} to {@link java.util.Date}
037: * <li>{@link java.util.Calendar} to {@link java.sql.Timestamp}
038: * <li>{@link java.util.Calendar} to {@link java.sql.Time}
039: * </ul>
040: * </p>
041: * <p>
042: * The hint {@link #DATE_FORMAT} can be used to control the format of converting a temporal value
043: * to a String.
044: * </p>
045: * @author Justin Deoliveira, The Open Planning Project
046: * @since 2.4
047: */
048: public class TemporalConverterFactory implements ConverterFactory {
049:
050: public Converter createConverter(Class source, Class target,
051: Hints hints) {
052:
053: if (Date.class.isAssignableFrom(source)) {
054: // handle all of (java.util.Date,java.sql.Timestamp,and java.sql.Time) -> java.util.Calendar
055: if (Calendar.class.isAssignableFrom(target)) {
056: return new Converter() {
057: public Object convert(Object source, Class target)
058: throws Exception {
059: Calendar calendar = Calendar.getInstance();
060: calendar.setTime((Date) source);
061:
062: return calendar;
063: }
064: };
065: }
066:
067: //handle all of (java.util.Date) -> (java.sql.Timestamp,java.sql.Time)
068: if (Timestamp.class.isAssignableFrom(target)
069: || Time.class.isAssignableFrom(target)
070: || java.sql.Date.class.isAssignableFrom(target)) {
071:
072: return new Converter() {
073:
074: public Object convert(Object source, Class target)
075: throws Exception {
076: Date date = (Date) source;
077: return target.getConstructor(
078: new Class[] { Long.TYPE })
079: .newInstance(
080: new Object[] { new Long(date
081: .getTime()) });
082: }
083:
084: };
085: }
086: // if ( String.class.equals( target ) ) {
087: // final DateFormat fFormat;
088: // if ( dateFormat != null ) {
089: // fFormat = dateFormat;
090: // }
091: // else {
092: // //create a default
093: // fFormat = DateFormat.getDateInstance();
094: // }
095: //
096: // return new Converter() {
097: // public Object convert(Object source, Class target) throws Exception {
098: // return fFormat.format( (Date)source );
099: // }
100: // };
101: // }
102: }
103:
104: //this should handle java.util.Calendar to (java.util.Date,java.sql.Timestamp,java.util.Time}
105: if (Calendar.class.isAssignableFrom(source)) {
106: if (Date.class.isAssignableFrom(target)) {
107: final Class fTarget = target;
108: return new Converter() {
109: public Object convert(Object source, Class target)
110: throws Exception {
111: Calendar calendar = (Calendar) source;
112:
113: return target.getConstructor(
114: new Class[] { Long.TYPE }).newInstance(
115: new Object[] { new Long(calendar
116: .getTimeInMillis()) });
117: }
118: };
119: }
120: // if ( String.class.equals( target ) ) {
121: // final DateFormat fFormat;
122: // if ( dateTimeFormat != null ) {
123: // fFormat = dateTimeFormat;
124: // }
125: // else {
126: // //create a default
127: // fFormat = DateFormat.getDateTimeInstance();
128: // }
129: //
130: // return new Converter() {
131: // public Object convert(Object source, Class target) throws Exception {
132: // Date date = ((Calendar)source).getTime();
133: // return fFormat.format( date );
134: // }
135: // };
136: // }
137: }
138:
139: return null;
140: }
141:
142: }
|