001: //$Id: TimeType.java 8891 2005-12-21 05:13:29Z oneovthafew $
002: package org.hibernate.type;
003:
004: import java.sql.PreparedStatement;
005: import java.sql.ResultSet;
006: import java.sql.SQLException;
007: import java.sql.Time;
008: import java.sql.Types;
009: import java.text.ParseException;
010: import java.text.SimpleDateFormat;
011: import java.util.Calendar;
012: import java.util.Date;
013:
014: import org.hibernate.EntityMode;
015: import org.hibernate.HibernateException;
016: import org.hibernate.dialect.Dialect;
017:
018: /**
019: * <tt>time</tt>: A type that maps an SQL TIME to a Java
020: * java.util.Date or java.sql.Time.
021: * @author Gavin King
022: */
023: public class TimeType extends MutableType implements LiteralType {
024:
025: private static final String TIME_FORMAT = "HH:mm:ss";
026:
027: public Object get(ResultSet rs, String name) throws SQLException {
028: return rs.getTime(name);
029: }
030:
031: public Class getReturnedClass() {
032: return java.util.Date.class;
033: }
034:
035: public void set(PreparedStatement st, Object value, int index)
036: throws SQLException {
037:
038: Time time;
039: if (value instanceof Time) {
040: time = (Time) value;
041: } else {
042: time = new Time(((java.util.Date) value).getTime());
043: }
044: st.setTime(index, time);
045: }
046:
047: public int sqlType() {
048: return Types.TIME;
049: }
050:
051: public String getName() {
052: return "time";
053: }
054:
055: public String toString(Object val) {
056: return new SimpleDateFormat(TIME_FORMAT)
057: .format((java.util.Date) val);
058: }
059:
060: public boolean isEqual(Object x, Object y) {
061:
062: if (x == y)
063: return true;
064: if (x == null || y == null)
065: return false;
066:
067: Date xdate = (Date) x;
068: Date ydate = (Date) y;
069:
070: if (xdate.getTime() == ydate.getTime())
071: return true;
072:
073: Calendar calendar1 = java.util.Calendar.getInstance();
074: Calendar calendar2 = java.util.Calendar.getInstance();
075: calendar1.setTime(xdate);
076: calendar2.setTime(ydate);
077:
078: return calendar1.get(Calendar.HOUR_OF_DAY) == calendar2
079: .get(Calendar.HOUR_OF_DAY)
080: && calendar1.get(Calendar.MINUTE) == calendar2
081: .get(Calendar.MINUTE)
082: && calendar1.get(Calendar.SECOND) == calendar2
083: .get(Calendar.SECOND)
084: && calendar1.get(Calendar.MILLISECOND) == calendar2
085: .get(Calendar.MILLISECOND);
086: }
087:
088: public int getHashCode(Object x, EntityMode entityMode) {
089: Calendar calendar = java.util.Calendar.getInstance();
090: calendar.setTime((java.util.Date) x);
091: int hashCode = 1;
092: hashCode = 31 * hashCode + calendar.get(Calendar.HOUR_OF_DAY);
093: hashCode = 31 * hashCode + calendar.get(Calendar.MINUTE);
094: hashCode = 31 * hashCode + calendar.get(Calendar.SECOND);
095: hashCode = 31 * hashCode + calendar.get(Calendar.MILLISECOND);
096: return hashCode;
097: }
098:
099: public Object deepCopyNotNull(Object value) {
100: return new Time(((java.util.Date) value).getTime());
101: }
102:
103: public String objectToSQLString(Object value, Dialect dialect)
104: throws Exception {
105: return '\'' + new Time(((java.util.Date) value).getTime())
106: .toString() + '\'';
107: }
108:
109: public Object fromStringValue(String xml) throws HibernateException {
110: try {
111: return new SimpleDateFormat(TIME_FORMAT).parse(xml);
112: } catch (ParseException pe) {
113: throw new HibernateException("could not parse XML", pe);
114: }
115: }
116:
117: }
|