KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > AbstractDataType


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.dataset.datatype;
23
24 import java.sql.PreparedStatement JavaDoc;
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27
28 /**
29  * @author Manuel Laflamme
30  * @version $Revision: 1.11 $
31  * @since Mar 19, 2002
32  */

33 public abstract class AbstractDataType extends DataType
34 {
35     private final String JavaDoc _name;
36     private final int _sqlType;
37     private final Class JavaDoc _classType;
38     private final boolean _isNumber;
39
40     public AbstractDataType(String JavaDoc name, int sqlType, Class JavaDoc classType,
41             boolean isNumber)
42     {
43         _sqlType = sqlType;
44         _name = name;
45         _classType = classType;
46         _isNumber = isNumber;
47     }
48
49     ////////////////////////////////////////////////////////////////////////////
50
// DataType class
51

52     public int compare(Object JavaDoc o1, Object JavaDoc o2) throws TypeCastException
53     {
54         try
55         {
56             Comparable JavaDoc value1 = (Comparable JavaDoc)typeCast(o1);
57             Comparable JavaDoc value2 = (Comparable JavaDoc)typeCast(o2);
58
59             if (value1 == null && value2 == null)
60             {
61                 return 0;
62             }
63
64             if (value1 == null && value2 != null)
65             {
66                 return -1;
67             }
68
69             if (value1 != null && value2 == null)
70             {
71                 return 1;
72             }
73
74             return value1.compareTo(value2);
75         }
76         catch (ClassCastException JavaDoc e)
77         {
78             throw new TypeCastException(e);
79         }
80     }
81
82     public int getSqlType()
83     {
84         return _sqlType;
85     }
86
87     public Class JavaDoc getTypeClass()
88     {
89         return _classType;
90     }
91
92     public boolean isNumber()
93     {
94         return _isNumber;
95     }
96
97     public boolean isDateTime()
98     {
99         return false;
100     }
101
102     public Object JavaDoc getSqlValue(int column, ResultSet JavaDoc resultSet)
103             throws SQLException JavaDoc, TypeCastException
104     {
105         Object JavaDoc value = resultSet.getObject(column);
106         if (value == null || resultSet.wasNull())
107         {
108             return null;
109         }
110         return value;
111     }
112
113     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement JavaDoc statement)
114             throws SQLException JavaDoc, TypeCastException
115     {
116         statement.setObject(column, typeCast(value), getSqlType());
117     }
118
119     ////////////////////////////////////////////////////////////////////////////
120
// Object class
121

122     public String JavaDoc toString()
123     {
124         return _name;
125     }
126 }
127
128
129
130
Popular Tags