KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: TimeType.java,v 1.6 2005/06/22 03:52:32 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.Time JavaDoc;
8 import java.sql.Types JavaDoc;
9 import java.text.ParseException JavaDoc;
10 import java.text.SimpleDateFormat JavaDoc;
11 import java.util.Calendar JavaDoc;
12 import java.util.Date JavaDoc;
13
14 import org.hibernate.EntityMode;
15 import org.hibernate.HibernateException;
16
17 /**
18  * <tt>time</tt>: A type that maps an SQL TIME to a Java
19  * java.util.Date or java.sql.Time.
20  * @author Gavin King
21  */

22 public class TimeType extends MutableType implements LiteralType {
23
24     private static final String JavaDoc TIME_FORMAT = "HH:mm:ss";
25
26     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws SQLException JavaDoc {
27         return rs.getTime(name);
28     }
29     public Class JavaDoc getReturnedClass() {
30         return java.util.Date JavaDoc.class;
31     }
32     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws SQLException JavaDoc {
33
34         Time JavaDoc time;
35         if (value instanceof Time JavaDoc) {
36             time = (Time JavaDoc) value;
37         }
38         else {
39             time = new Time JavaDoc( ( (java.util.Date JavaDoc) value ).getTime() );
40         }
41         st.setTime(index, time);
42     }
43
44     public int sqlType() {
45         return Types.TIME;
46     }
47     public String JavaDoc getName() { return "time"; }
48
49     public String JavaDoc toString(Object JavaDoc val) {
50         return new SimpleDateFormat JavaDoc(TIME_FORMAT).format( (java.util.Date JavaDoc) val );
51     }
52     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) {
53
54         if (x==y) return true;
55         if (x==null || y==null) return false;
56
57         Date JavaDoc xdate = (Date JavaDoc) x;
58         Date JavaDoc ydate = (Date JavaDoc) y;
59         
60         if ( xdate.getTime()==ydate.getTime() ) return true;
61         
62         Calendar JavaDoc calendar1 = java.util.Calendar.getInstance();
63         Calendar JavaDoc calendar2 = java.util.Calendar.getInstance();
64         calendar1.setTime( xdate );
65         calendar2.setTime( ydate );
66
67         return calendar1.get(Calendar.HOUR_OF_DAY) == calendar2.get(Calendar.HOUR_OF_DAY)
68             && calendar1.get(Calendar.MINUTE) == calendar2.get(Calendar.MINUTE)
69             && calendar1.get(Calendar.SECOND) == calendar2.get(Calendar.SECOND)
70             && calendar1.get(Calendar.MILLISECOND) == calendar2.get(Calendar.MILLISECOND);
71     }
72
73     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
74         Calendar JavaDoc calendar = java.util.Calendar.getInstance();
75         calendar.setTime( (java.util.Date JavaDoc) x );
76         int hashCode = 1;
77         hashCode = 31 * hashCode + calendar.get(Calendar.HOUR_OF_DAY);
78         hashCode = 31 * hashCode + calendar.get(Calendar.MINUTE);
79         hashCode = 31 * hashCode + calendar.get(Calendar.SECOND);
80         hashCode = 31 * hashCode + calendar.get(Calendar.MILLISECOND);
81         return hashCode;
82     }
83
84     public Object JavaDoc deepCopyNotNull(Object JavaDoc value) {
85         return new Time JavaDoc( ( (java.util.Date JavaDoc) value ).getTime() );
86     }
87
88     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
89         return '\'' + value.toString() + '\'';
90     }
91
92     public Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
93         try {
94             return new SimpleDateFormat JavaDoc(TIME_FORMAT).parse(xml);
95         }
96         catch (ParseException JavaDoc pe) {
97             throw new HibernateException("could not parse XML", pe);
98         }
99     }
100
101 }
102
103
104
105
106
107
Popular Tags