KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > SortedTable


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;
23
24 import org.dbunit.DatabaseUnitRuntimeException;
25 import org.dbunit.dataset.datatype.DataType;
26
27 import java.util.Arrays JavaDoc;
28 import java.util.Comparator JavaDoc;
29
30 /**
31  * This is a ITable decorator that provide a sorted view of the decorated table.
32  * This implementation does not keep a separate copy of the decorated table data.
33  *
34  * @author Manuel Laflamme
35  * @version $Revision: 1.5 $
36  * @since Feb 19, 2003
37  */

38 public class SortedTable extends AbstractTable
39 {
40     private final ITable _table;
41     private final Column[] _columns;
42     private Integer JavaDoc[] _indexes;
43
44     /**
45      * Sort the decorated table by specified columns order.
46      */

47     public SortedTable(ITable table, Column[] columns)
48     {
49         _table = table;
50         _columns = columns;
51     }
52
53     /**
54      * Sort the decorated table by specified columns order.
55      */

56     public SortedTable(ITable table, String JavaDoc[] columnNames) throws DataSetException
57     {
58         _table = table;
59         _columns = new Column[columnNames.length];
60
61         Column[] columns = table.getTableMetaData().getColumns();
62         for (int i = 0; i < columnNames.length; i++)
63         {
64             String JavaDoc columnName = columnNames[i];
65             _columns[i] = DataSetUtils.getColumn(columnName, columns);
66         }
67     }
68
69     /**
70      * Sort the decorated table by specified metadata columns order. All
71      * metadata columns will be used.
72      */

73     public SortedTable(ITable table, ITableMetaData metaData) throws DataSetException
74     {
75         this(table, metaData.getColumns());
76     }
77
78     /**
79      * Sort the decorated table by its own columns order. All
80      * table columns will be used.
81      */

82     public SortedTable(ITable table) throws DataSetException
83     {
84         this(table, table.getTableMetaData());
85     }
86
87     private int getOriginalRowIndex(int row) throws DataSetException
88     {
89         if (_indexes == null)
90         {
91             Integer JavaDoc[] indexes = new Integer JavaDoc[getRowCount()];
92             for (int i = 0; i < indexes.length; i++)
93             {
94                 indexes[i] = new Integer JavaDoc(i);
95             }
96
97             try
98             {
99                 Arrays.sort(indexes, new RowComparator());
100             }
101             catch (DatabaseUnitRuntimeException e)
102             {
103                 throw (DataSetException)e.getException();
104             }
105
106             _indexes = indexes;
107         }
108
109         return _indexes[row].intValue();
110     }
111
112     ////////////////////////////////////////////////////////////////////////////
113
// ITable interface
114

115     public ITableMetaData getTableMetaData()
116     {
117         return _table.getTableMetaData();
118     }
119
120     public int getRowCount()
121     {
122         return _table.getRowCount();
123     }
124
125     public Object JavaDoc getValue(int row, String JavaDoc column) throws DataSetException
126     {
127         assertValidRowIndex(row);
128
129         return _table.getValue(getOriginalRowIndex(row), column);
130     }
131
132     ////////////////////////////////////////////////////////////////////////////
133
// Comparator interface
134

135     private class RowComparator implements Comparator JavaDoc
136     {
137         public int compare(Object JavaDoc o1, Object JavaDoc o2)
138         {
139             Integer JavaDoc i1 = (Integer JavaDoc)o1;
140             Integer JavaDoc i2 = (Integer JavaDoc)o2;
141
142             try
143             {
144                 for (int i = 0; i < _columns.length; i++)
145                 {
146                     String JavaDoc columnName = _columns[i].getColumnName();
147                     Object JavaDoc value1 = _table.getValue(i1.intValue(), columnName);
148                     Object JavaDoc value2 = _table.getValue(i2.intValue(), columnName);
149
150                     if (value1 == null && value2 == null)
151                     {
152                         continue;
153                     }
154
155                     if (value1 == null && value2 != null)
156                     {
157                         return -1;
158                     }
159
160                     if (value1 != null && value2 == null)
161                     {
162                         return 1;
163                     }
164
165                     String JavaDoc stringValue1 = DataType.asString(value1);
166                     String JavaDoc stringValue2 = DataType.asString(value2);
167                     int result = stringValue1.compareTo(stringValue2);
168                     if (result != 0)
169                     {
170                         return result;
171                     }
172                 }
173             }
174             catch (DataSetException e)
175             {
176                 throw new DatabaseUnitRuntimeException(e);
177             }
178
179             return 0;
180         }
181     }
182 }
183
184
185
186
187
188
Popular Tags