01: /*
02: * $Id: DoubleType.java,v 1.11 2005/04/13 06:26:46 spal Exp $
03: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/DoubleType.java,v $
04: * SQLUnit - a test harness for unit testing database stored procedures.
05: * Copyright (C) 2003 The SQLUnit Team
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21: package net.sourceforge.sqlunit.types;
22:
23: import net.sourceforge.sqlunit.IErrorCodes;
24: import net.sourceforge.sqlunit.SQLUnitException;
25: import net.sourceforge.sqlunit.SymbolTable;
26: import net.sourceforge.sqlunit.utils.TypeUtils;
27:
28: /**
29: * Models a Double type.
30: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
31: * @author Sujit Pal (spal@users.sourceforge.net)
32: * @version $Revision: 1.11 $
33: * @sqlunit.type name="DoubleType" input="Yes" output="Yes" sortable="Yes"
34: * wraps="java.lang.Double"
35: * @sqlunit.typename name="DOUBLE" server="Any"
36: */
37: public class DoubleType extends UnsupportedType {
38:
39: /**
40: * Formats a Double type.
41: * @param obj an Object to be converted to the IType interface.
42: * @return the String representation of the object.
43: * @exception SQLUnitException if the formatting failed.
44: */
45: protected String format(final Object obj) throws SQLUnitException {
46: if (!(obj instanceof Double)) {
47: throw new SQLUnitException(
48: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
49: new String[] {
50: SymbolTable.getCurrentResultKey(),
51: (obj == null ? "NULL" : obj.getClass()
52: .getName()), getName(),
53: (new Integer(getId())).toString() });
54: }
55: Double d = (Double) obj;
56: return d.toString();
57: }
58:
59: /**
60: * Parses a Double type.
61: * @param str the String representation of the object.
62: * @return an object which needs to be cast to a Double.
63: * @exception SQLUnitException if the parsing failed.
64: */
65: protected Object parse(final String str) throws SQLUnitException {
66: try {
67: Double d = new Double(str);
68: return d;
69: } catch (NumberFormatException e) {
70: throw new SQLUnitException(
71: IErrorCodes.UNSUPPORTED_DATATYPE_PARSE,
72: new String[] { SymbolTable.getCurrentResultKey(),
73: str, "java.lang.Double", getName(),
74: (new Integer(getId())).toString() });
75: }
76: }
77:
78: /**
79: * Returns a negative, zero or positive number according to whether this
80: * object is smaller, equal or larger than the passed in object.
81: * @param obj an Object of type DateType.
82: * @return a negative, zero or positive number.
83: */
84: public final int compareTo(final Object obj) {
85: if (!(obj instanceof DoubleType)) {
86: return 0;
87: }
88: DoubleType that = (DoubleType) obj;
89: boolean isEitherNull = TypeUtils.checkIfNull(this , that);
90: if (isEitherNull) {
91: return TypeUtils.compareNulls(this , that);
92: } else {
93: Double this Value = (Double) this .getValue();
94: Double thatValue = (Double) that.getValue();
95: return (thisValue.compareTo(thatValue));
96: }
97: }
98: }
|