1 package org.hibernate.type; 3 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.sql.Timestamp ; 8 import java.sql.Types ; 9 import java.util.Calendar ; 10 import java.util.Comparator ; 11 import java.util.Date ; 12 import java.util.GregorianCalendar ; 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 25 public class CalendarType extends MutableType implements VersionType { 26 27 public Object get(ResultSet rs, String name) throws HibernateException, SQLException { 28 29 Timestamp ts = rs.getTimestamp(name); 30 if (ts!=null) { 31 Calendar cal = new GregorianCalendar (); 32 if ( Environment.jvmHasTimestampBug() ) { 33 cal.setTime( new Date ( 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 st, Object value, int index) throws HibernateException, SQLException { 47 final Calendar cal = (Calendar ) value; 48 st.setTimestamp( index, new Timestamp ( cal.getTime().getTime() ), cal ); 50 } 51 52 public int sqlType() { 53 return Types.TIMESTAMP; 54 } 55 56 public String toString(Object value) throws HibernateException { 57 return Hibernate.TIMESTAMP.toString( ( (Calendar ) value ).getTime() ); 58 } 59 60 public Object fromStringValue(String xml) throws HibernateException { 61 Calendar result = new GregorianCalendar (); 62 result.setTime( ( (Date ) Hibernate.TIMESTAMP.fromStringValue(xml) ) ); 63 return result; 64 } 65 66 public Object deepCopyNotNull(Object value) throws HibernateException { 67 return ( (Calendar ) value ).clone(); 68 } 69 70 public Class getReturnedClass() { 71 return Calendar .class; 72 } 73 74 public int compare(Object x, Object y, EntityMode entityMode) { 75 return CalendarComparator.INSTANCE.compare(x, y); 76 } 77 78 public boolean isEqual(Object x, Object y) { 79 if (x==y) return true; 80 if (x==null || y==null) return false; 81 82 Calendar calendar1 = (Calendar ) x; 83 Calendar calendar2 = (Calendar ) 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 x, EntityMode entityMode) { 95 Calendar calendar = (Calendar ) 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 getName() { 108 return "calendar"; 109 } 110 111 public Object next(Object current) { 112 return seed(); 113 } 114 115 public Object seed() { 116 return Calendar.getInstance(); 117 } 118 119 public Comparator getComparator() { 120 return CalendarComparator.INSTANCE; 121 } 122 123 } 124 | Popular Tags |