KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
25  * @author Manuel Laflamme
26  * @version $Revision: 1.8 $
27  * @since Feb 17, 2002
28  */

29 public class CompositeTable extends AbstractTable
30 {
31     private final ITableMetaData _metaData;
32     private final ITable[] _tables;
33
34     /**
35      * Creates a composite table that combines the specified metadata with the
36      * specified table.
37      */

38     public CompositeTable(ITableMetaData metaData, ITable table)
39     {
40         _metaData = metaData;
41         _tables = new ITable[]{table};
42     }
43
44     /**
45      * Creates a composite table that combines the specified metadata with the
46      * specified tables.
47      */

48     public CompositeTable(ITableMetaData metaData, ITable[] tables)
49     {
50         _metaData = metaData;
51         _tables = tables;
52     }
53
54     /**
55      * Creates a composite table that combines the specified specified tables.
56      * The metadata from the first table is used as metadata for the new table.
57      */

58     public CompositeTable(ITable table1, ITable table2)
59     {
60         _metaData = table1.getTableMetaData();
61         _tables = new ITable[]{table1, table2};
62     }
63
64     /**
65      * Creates a composite dataset that encapsulate the specified table with
66      * a new name.
67      */

68     public CompositeTable(String JavaDoc newName, ITable table)
69             throws DataSetException
70     {
71         ITableMetaData metaData = table.getTableMetaData();
72         _metaData = new DefaultTableMetaData(newName,
73                 metaData.getColumns(), metaData.getPrimaryKeys());
74         _tables = new ITable[]{table};
75     }
76
77     ////////////////////////////////////////////////////////////////////////////
78
// ITable interface
79

80     public ITableMetaData getTableMetaData()
81     {
82         return _metaData;
83     }
84
85     public int getRowCount()
86     {
87         int totalCount = 0;
88         for (int i = 0; i < _tables.length; i++)
89         {
90             ITable table = _tables[i];
91             totalCount += table.getRowCount();
92         }
93
94         return totalCount;
95     }
96
97     public Object JavaDoc getValue(int row, String JavaDoc column) throws DataSetException
98     {
99         if (row < 0)
100         {
101             throw new RowOutOfBoundsException(row + " < 0 ");
102         }
103
104         int totalCount = 0;
105         for (int i = 0; i < _tables.length; i++)
106         {
107             ITable table = _tables[i];
108
109             int count = table.getRowCount();
110             if (totalCount + count > row)
111             {
112                 return table.getValue(row - totalCount, column);
113             }
114             totalCount += count;
115         }
116
117         throw new RowOutOfBoundsException(row + " > " + totalCount);
118     }
119 }
120
121
122
123
124
125
Popular Tags