KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > TimestampType


1 //$Id: TimestampType.java,v 1.10 2005/05/03 16:01:03 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.sql.Timestamp JavaDoc;
8 import java.sql.Types JavaDoc;
9 import java.text.ParseException JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
11 import java.util.Comparator JavaDoc;
12
13 import org.hibernate.EntityMode;
14 import org.hibernate.HibernateException;
15 import org.hibernate.cfg.Environment;
16 import org.hibernate.util.ComparableComparator;
17
18 /**
19  * <tt>timestamp</tt>: A type that maps an SQL TIMESTAMP to a Java
20  * java.util.Date or java.sql.Timestamp.
21  * @author Gavin King
22  */

23 public class TimestampType extends MutableType implements VersionType, LiteralType {
24
25     private static final String JavaDoc TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss";
26
27     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws SQLException JavaDoc {
28         return rs.getTimestamp(name);
29     }
30     
31     public Class JavaDoc getReturnedClass() {
32         return java.util.Date JavaDoc.class;
33     }
34     
35     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws SQLException JavaDoc {
36         Timestamp JavaDoc ts;
37         if (value instanceof Timestamp JavaDoc) {
38             ts = (Timestamp JavaDoc) value;
39         }
40         else {
41             ts = new Timestamp JavaDoc( ( (java.util.Date JavaDoc) value ).getTime() );
42         }
43         st.setTimestamp(index, ts);
44     }
45
46     public int sqlType() {
47         return Types.TIMESTAMP;
48     }
49     
50     public String JavaDoc getName() { return "timestamp"; }
51
52     public String JavaDoc toString(Object JavaDoc val) {
53         return new SimpleDateFormat JavaDoc(TIMESTAMP_FORMAT).format( (java.util.Date JavaDoc) val );
54     }
55
56     public Object JavaDoc deepCopyNotNull(Object JavaDoc value) {
57         if ( value instanceof Timestamp JavaDoc ) {
58             Timestamp JavaDoc orig = (Timestamp JavaDoc) value;
59             Timestamp JavaDoc ts = new Timestamp JavaDoc( orig.getTime() );
60             ts.setNanos( orig.getNanos() );
61             return ts;
62         }
63         else {
64             java.util.Date JavaDoc orig = (java.util.Date JavaDoc) value;
65             return new java.util.Date JavaDoc( orig.getTime() );
66         }
67     }
68
69     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) {
70
71         if (x==y) return true;
72         if (x==null || y==null) return false;
73
74         long xTime = ( (java.util.Date JavaDoc) x ).getTime();
75         long yTime = ( (java.util.Date JavaDoc) y ).getTime();
76         boolean xts = x instanceof Timestamp JavaDoc;
77         boolean yts = y instanceof Timestamp JavaDoc;
78         int xNanos = xts ? ( (Timestamp JavaDoc) x ).getNanos() : 0;
79         int yNanos = yts ? ( (Timestamp JavaDoc) y ).getNanos() : 0;
80         if ( !Environment.jvmHasJDK14Timestamp() ) {
81             xTime += xNanos / 1000000;
82             yTime += yNanos / 1000000;
83         }
84         if ( xTime!=yTime ) return false;
85         if (xts && yts) {
86             // both are Timestamps
87
int xn = xNanos % 1000000;
88             int yn = yNanos % 1000000;
89             return xn==yn;
90         }
91         else {
92             // at least one is a plain old Date
93
return true;
94         }
95
96     }
97
98     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
99         java.util.Date JavaDoc ts = (java.util.Date JavaDoc) x;
100         return new Long JavaDoc( ts.getTime() / 1000 ).hashCode();
101     }
102
103     public Object JavaDoc next(Object JavaDoc current) {
104         return seed();
105     }
106
107     public Object JavaDoc seed() {
108         return new Timestamp JavaDoc( System.currentTimeMillis() );
109     }
110
111     public Comparator JavaDoc getComparator() {
112         return ComparableComparator.INSTANCE;
113     }
114
115     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
116         return '\'' + value.toString() + '\'';
117     }
118
119     public Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
120         try {
121             return new Timestamp JavaDoc( new SimpleDateFormat JavaDoc(TIMESTAMP_FORMAT).parse(xml).getTime() );
122         }
123         catch (ParseException JavaDoc pe) {
124             throw new HibernateException("could not parse XML", pe);
125         }
126     }
127
128 }
129
130
131
132
133
134
Popular Tags