KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * @author Manuel Laflamme
29  * @version $Revision: 1.10 $
30  * @since Feb 17, 2002
31  */

32 public class DefaultTable extends AbstractTable
33 {
34     private final ITableMetaData _metaData;
35     private final List JavaDoc _rowList;
36
37     /**
38      * Creates a new empty table with specified metadata and values.
39      * @deprecated Use public mutators to initialize table values instead
40      */

41     public DefaultTable(ITableMetaData metaData, List JavaDoc list)
42     {
43         _metaData = metaData;
44         _rowList = list;
45     }
46
47     /**
48      * Creates a new empty table having the specified name.
49      */

50     public DefaultTable(String JavaDoc tableName)
51     {
52         _metaData = new DefaultTableMetaData(tableName, new Column[0]);
53         _rowList = new ArrayList JavaDoc();
54     }
55
56     /**
57      * Creates a new empty table with specified metadata and values.
58      * @deprecated Use public mutators to initialize table values instead
59      */

60     public DefaultTable(String JavaDoc tableName, Column[] columns, List JavaDoc list)
61     {
62         _metaData = new DefaultTableMetaData(tableName, columns);
63         _rowList = list;
64     }
65
66     /**
67      * Creates a new empty table with specified metadata.
68      */

69     public DefaultTable(String JavaDoc tableName, Column[] columns)
70     {
71         _metaData = new DefaultTableMetaData(tableName, columns);
72         _rowList = new ArrayList JavaDoc();
73     }
74
75     public DefaultTable(ITableMetaData metaData)
76     {
77         _metaData = metaData;
78         _rowList = new ArrayList JavaDoc();
79     }
80
81     /**
82      * Inserts a new empty row. You can add values with {@link #setValue}.
83      */

84     public void addRow() throws DataSetException
85     {
86         int columnCount = _metaData.getColumns().length;
87         _rowList.add(new Object JavaDoc[columnCount]);
88     }
89
90     /**
91      * Inserts a new row initialized with specified array of values.
92      * @param values The array of values. Each value correspond to the column at the
93      * same index from {@link ITableMetaData#getColumns}.
94      * @see #getTableMetaData
95      */

96     public void addRow(Object JavaDoc[] values) throws DataSetException
97     {
98         _rowList.add(values);
99     }
100
101     /**
102      * Inserts all rows from the specified table.
103      * @param table The source table.
104      */

105     public void addTableRows(ITable table) throws DataSetException
106     {
107         try
108         {
109             Column[] columns = _metaData.getColumns();
110             if (columns.length > 0)
111             {
112                 for (int i = 0; ; i++)
113                 {
114                     Object JavaDoc[] rowValues = new Object JavaDoc[columns.length];
115                     for (int j = 0; j < columns.length; j++)
116                     {
117                         Column column = columns[j];
118                         rowValues[j] = table.getValue(i, column.getColumnName());
119                     }
120                     _rowList.add(rowValues);
121                 }
122             }
123         }
124         catch(RowOutOfBoundsException e)
125         {
126             // end of table
127
}
128     }
129
130     /**
131      * Replaces the value at the specified position in this table with the specified value.
132      * @param row The row index
133      * @param column The column name
134      * @param value The value to store at the specified location
135      * @return the value previously at the specified location
136      * @throws RowOutOfBoundsException if the row index is out of range
137      * @throws NoSuchColumnException if the column does not exist
138      * @throws DataSetException if an unexpected error occurs
139      */

140     public Object JavaDoc setValue(int row, String JavaDoc column, Object JavaDoc value)
141             throws RowOutOfBoundsException, NoSuchColumnException, DataSetException
142     {
143         assertValidRowIndex(row);
144
145         Object JavaDoc[] rowValues = (Object JavaDoc[])_rowList.get(row);
146         int columnIndex = getColumnIndex(column);
147         Object JavaDoc oldValue = rowValues[columnIndex];
148         rowValues[columnIndex] = value;
149         return oldValue;
150     }
151
152     ////////////////////////////////////////////////////////////////////////////
153
// ITable interface
154

155     public ITableMetaData getTableMetaData()
156     {
157         return _metaData;
158     }
159
160     public int getRowCount()
161     {
162         return _rowList.size();
163     }
164
165     public Object JavaDoc getValue(int row, String JavaDoc column) throws DataSetException
166     {
167         assertValidRowIndex(row);
168
169         Object JavaDoc[] rowValues = (Object JavaDoc[])_rowList.get(row);
170         return rowValues[getColumnIndex(column)];
171     }
172
173 }
174
175
176
177
178
179
Popular Tags