KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.dbunit.dataset.ITable;
25
26 import java.math.BigDecimal JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 /**
32  * @author Manuel Laflamme
33  * @version $Revision: 1.14 $
34  * @since Feb 17, 2002
35  */

36 public class NumberDataType extends AbstractDataType
37 {
38     private static final Number JavaDoc TRUE = new BigDecimal JavaDoc(1);
39     private static final Number JavaDoc FALSE = new BigDecimal JavaDoc(0);
40
41     NumberDataType(String JavaDoc name, int sqlType)
42     {
43         super(name, sqlType, BigDecimal JavaDoc.class, true);
44     }
45
46     ////////////////////////////////////////////////////////////////////////////
47
// DataType class
48

49     public Object JavaDoc typeCast(Object JavaDoc value) throws TypeCastException
50     {
51         if (value == null || value == ITable.NO_VALUE)
52         {
53             return null;
54         }
55
56         if (value instanceof BigDecimal JavaDoc)
57         {
58             return value;
59         }
60
61         if (value instanceof Boolean JavaDoc)
62         {
63             return ((Boolean JavaDoc)value).booleanValue() ? TRUE : FALSE;
64         }
65
66         try
67         {
68             return new BigDecimal JavaDoc(value.toString());
69         }
70         catch (java.lang.NumberFormatException JavaDoc e)
71         {
72             throw new TypeCastException(value, this, e);
73         }
74     }
75
76     public Object JavaDoc getSqlValue(int column, ResultSet JavaDoc resultSet)
77             throws SQLException JavaDoc, TypeCastException
78     {
79         BigDecimal JavaDoc value = resultSet.getBigDecimal(column);
80         if (value == null || resultSet.wasNull())
81         {
82             return null;
83         }
84         return value;
85     }
86
87     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement JavaDoc statement)
88             throws SQLException JavaDoc, TypeCastException
89     {
90         statement.setBigDecimal(column, (BigDecimal JavaDoc)typeCast(value));
91     }
92 }
93
94
95
96
97
98
99
Popular Tags