KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: CalendarType.java,v 1.11 2005/02/19 12:58:24 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.util.Calendar JavaDoc;
10 import java.util.Comparator JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.GregorianCalendar JavaDoc;
13
14 import org.hibernate.EntityMode;
15 import org.hibernate.Hibernate;
16 import org.hibernate.HibernateException;
17 import org.hibernate.cfg.Environment;
18 import org.hibernate.util.CalendarComparator;
19
20 /**
21  * <tt>calendar</tt>: A type mapping for a <tt>Calendar</tt> object that
22  * represents a datetime.
23  * @author Gavin King
24  */

25 public class CalendarType extends MutableType implements VersionType {
26
27     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws HibernateException, SQLException JavaDoc {
28
29         Timestamp JavaDoc ts = rs.getTimestamp(name);
30         if (ts!=null) {
31             Calendar JavaDoc cal = new GregorianCalendar JavaDoc();
32             if ( Environment.jvmHasTimestampBug() ) {
33                 cal.setTime( new Date JavaDoc( ts.getTime() + ts.getNanos() / 1000000 ) );
34             }
35             else {
36                 cal.setTime(ts);
37             }
38             return cal;
39         }
40         else {
41             return null;
42         }
43
44     }
45
46     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws HibernateException, SQLException JavaDoc {
47         final Calendar JavaDoc cal = (Calendar JavaDoc) value;
48         //st.setTimestamp( index, new Timestamp( cal.getTimeInMillis() ), cal ); //JDK 1.5 only
49
st.setTimestamp( index, new Timestamp JavaDoc( cal.getTime().getTime() ), cal );
50     }
51
52     public int sqlType() {
53         return Types.TIMESTAMP;
54     }
55
56     public String JavaDoc toString(Object JavaDoc value) throws HibernateException {
57         return Hibernate.TIMESTAMP.toString( ( (Calendar JavaDoc) value ).getTime() );
58     }
59
60     public Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
61         Calendar JavaDoc result = new GregorianCalendar JavaDoc();
62         result.setTime( ( (Date JavaDoc) Hibernate.TIMESTAMP.fromStringValue(xml) ) );
63         return result;
64     }
65
66     public Object JavaDoc deepCopyNotNull(Object JavaDoc value) throws HibernateException {
67         return ( (Calendar JavaDoc) value ).clone();
68     }
69
70     public Class JavaDoc getReturnedClass() {
71         return Calendar JavaDoc.class;
72     }
73     
74     public int compare(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode) {
75         return CalendarComparator.INSTANCE.compare(x, y);
76     }
77
78     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) {
79         if (x==y) return true;
80         if (x==null || y==null) return false;
81
82         Calendar JavaDoc calendar1 = (Calendar JavaDoc) x;
83         Calendar JavaDoc calendar2 = (Calendar JavaDoc) y;
84
85         return calendar1.get(Calendar.MILLISECOND) == calendar2.get(Calendar.MILLISECOND)
86             && calendar1.get(Calendar.SECOND) == calendar2.get(Calendar.SECOND)
87             && calendar1.get(Calendar.MINUTE) == calendar2.get(Calendar.MINUTE)
88             && calendar1.get(Calendar.HOUR_OF_DAY) == calendar2.get(Calendar.HOUR_OF_DAY)
89             && calendar1.get(Calendar.DAY_OF_MONTH) == calendar2.get(Calendar.DAY_OF_MONTH)
90             && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)
91             && calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR);
92     }
93
94     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
95         Calendar JavaDoc calendar = (Calendar JavaDoc) x;
96         int hashCode = 1;
97         hashCode = 31 * hashCode + calendar.get(Calendar.MILLISECOND);
98         hashCode = 31 * hashCode + calendar.get(Calendar.SECOND);
99         hashCode = 31 * hashCode + calendar.get(Calendar.MINUTE);
100         hashCode = 31 * hashCode + calendar.get(Calendar.HOUR_OF_DAY);
101         hashCode = 31 * hashCode + calendar.get(Calendar.DAY_OF_MONTH);
102         hashCode = 31 * hashCode + calendar.get(Calendar.MONTH);
103         hashCode = 31 * hashCode + calendar.get(Calendar.YEAR);
104         return hashCode;
105     }
106
107     public String JavaDoc getName() {
108         return "calendar";
109     }
110
111     public Object JavaDoc next(Object JavaDoc current) {
112         return seed();
113     }
114
115     public Object JavaDoc seed() {
116         return Calendar.getInstance();
117     }
118
119     public Comparator JavaDoc getComparator() {
120         return CalendarComparator.INSTANCE;
121     }
122
123 }
124
Popular Tags