001: /*
002: * $Id: TimeType.java,v 1.10 2005/04/13 06:26:46 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/TimeType.java,v $
004: * SQLUnit - a test harness for unit testing database stored procedures.
005: * Copyright (C) 2003 The SQLUnit Team
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: package net.sourceforge.sqlunit.types;
022:
023: import net.sourceforge.sqlunit.IErrorCodes;
024: import net.sourceforge.sqlunit.SQLUnitException;
025: import net.sourceforge.sqlunit.SymbolTable;
026: import net.sourceforge.sqlunit.utils.TypeUtils;
027:
028: import java.sql.Time;
029: import java.text.ParseException;
030: import java.text.SimpleDateFormat;
031: import java.util.Calendar;
032: import java.util.Date;
033: import java.util.GregorianCalendar;
034:
035: /**
036: * Models a TIME type.
037: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
038: * @author Sujit Pal (spal@users.sourceforge.net)
039: * @version $Revision: 1.10 $
040: * @sqlunit.type name="TimeType" input="Yes" output="Yes" sortable="Yes"
041: * wraps="java.sql.Time"
042: * @sqlunit.typename name="TIME" server="Any"
043: */
044: public class TimeType extends UnsupportedType {
045:
046: private static final int NUM_SECS_PER_MIN = 60;
047: private static final int NUM_SECS_PER_HR = 3600;
048:
049: /**
050: * Specifies the pattern for the DATE object. This can be overriden
051: * to work with other database formats for time in subclasses.
052: */
053: protected static String PATTERN = "HH:mm:ss";
054:
055: private SimpleDateFormat formatter = new SimpleDateFormat(PATTERN);
056:
057: /**
058: * Formats a TIME into its String representation.
059: * @param obj an Object to be converted to the IType interface.
060: * @return the String representation of the object.
061: * @exception SQLUnitException if the formatting failed.
062: */
063: protected String format(final Object obj) throws SQLUnitException {
064: if (!(obj instanceof Time)) {
065: throw new SQLUnitException(
066: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
067: new String[] {
068: SymbolTable.getCurrentResultKey(),
069: (obj == null ? "NULL" : obj.getClass()
070: .getName()), getName(),
071: (new Integer(getId())).toString() });
072: }
073: Time time = (Time) obj;
074: long millisSinceEpoch = time.getTime();
075: Date d = new Date(millisSinceEpoch);
076: return formatter.format(d);
077: }
078:
079: /**
080: * Parses a String representing a TIME to a Time object.
081: * @param str the String representation of the DATE.
082: * @return a Time object.
083: * @exception SQLUnitException if the parsing failed.
084: */
085: protected Object parse(final String str) throws SQLUnitException {
086: try {
087: Date d = formatter.parse(str);
088: Time time = new Time(d.getTime());
089: return time;
090: } catch (ParseException e) {
091: throw new SQLUnitException(
092: IErrorCodes.UNSUPPORTED_DATATYPE_PARSE,
093: new String[] { SymbolTable.getCurrentResultKey(),
094: str, "java.sql.Time", getName(),
095: (new Integer(getId())).toString() });
096: }
097: }
098:
099: /**
100: * Returns true if the two objects are equal, false otherwise.
101: * @param obj the Object to compare against.
102: * @return true or false.
103: */
104: public final boolean equals(final Object obj) {
105: if (!(obj instanceof TimeType)) {
106: return false;
107: } else {
108: return (compareTo(obj) == 0);
109: }
110: }
111:
112: /**
113: * Returns the hashcode for the underlying wrapped datatype.
114: * @return the hashcode for the underlying wrapped datatype.
115: */
116: public final int hashCode() {
117: return super .hashCode();
118: }
119:
120: /**
121: * Returns a negative, zero or positive number according to whether this
122: * object is smaller, equal or larger than the passed in object.
123: * @param obj an Object of type TimeType.
124: * @return a negative, zero or positive number.
125: */
126: public final int compareTo(final Object obj) {
127: if (!(obj instanceof TimeType)) {
128: return 0;
129: }
130: TimeType that = (TimeType) obj;
131: boolean isEitherNull = TypeUtils.checkIfNull(this , that);
132: if (isEitherNull) {
133: return TypeUtils.compareNulls(this , that);
134: } else {
135: // compare only the time portion
136: Calendar cal = new GregorianCalendar();
137: cal.setTime(new Date(((Time) this .getValue()).getTime()));
138: int this SecondsSinceToday = ((cal.get(Calendar.HOUR_OF_DAY) * NUM_SECS_PER_HR)
139: + (cal.get(Calendar.MINUTE) * NUM_SECS_PER_MIN) + (cal
140: .get(Calendar.SECOND)));
141: cal.setTime(new Date(((Time) that.getValue()).getTime()));
142: int thatSecondsSinceToday = ((cal.get(Calendar.HOUR_OF_DAY) * NUM_SECS_PER_HR)
143: + (cal.get(Calendar.MINUTE) * NUM_SECS_PER_MIN) + (cal
144: .get(Calendar.SECOND)));
145: return (thisSecondsSinceToday - thatSecondsSinceToday);
146: }
147: }
148: }
|