KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.ListIterator JavaDoc;
27
28 /**
29  * Combines multiple datasets into a single logical dataset.
30  *
31  * @author Manuel Laflamme
32  * @version $Revision: 1.15 $
33  * @since Feb 19, 2002
34  */

35 public class CompositeDataSet extends AbstractDataSet
36 {
37     private ITable[] _tables;
38
39     /**
40      * Creates a composite dataset that combines specified datasets.
41      * Tables having the same name are merged into one table.
42      */

43     public CompositeDataSet(IDataSet[] dataSets) throws DataSetException
44     {
45         this(dataSets, true);
46     }
47
48     /**
49      * Creates a composite dataset that combines specified datasets.
50      *
51      * @param dataSets
52      * list of datasets
53      * @param combine
54      * if <code>true</code>, tables having the same name are merged into
55      * one table.
56      */

57     public CompositeDataSet(IDataSet[] dataSets, boolean combine)
58             throws DataSetException
59     {
60         List JavaDoc tableList = new ArrayList JavaDoc();
61         for (int i = 0; i < dataSets.length; i++)
62         {
63             IDataSet dataSet = dataSets[i];
64             ITableIterator iterator = dataSet.iterator();
65             while(iterator.next())
66             {
67                 addTable(iterator.getTable(), tableList, combine);
68             }
69         }
70
71         _tables = (ITable[])tableList.toArray(new ITable[0]);
72     }
73
74     /**
75      * Creates a composite dataset that combines the two specified datasets.
76      * Tables having the same name are merged into one table.
77      */

78     public CompositeDataSet(IDataSet dataSet1, IDataSet dataSet2)
79             throws DataSetException
80     {
81         this(new IDataSet[]{dataSet1, dataSet2});
82     }
83
84     /**
85      * Creates a composite dataset that combines the two specified datasets.
86      *
87      * @param dataSet1
88      * first dataset
89      * @param dataSet2
90      * second dataset
91      * @param combine
92      * if <code>true</code>, tables having the same name are merged into
93      * one table.
94      */

95     public CompositeDataSet(IDataSet dataSet1, IDataSet dataSet2, boolean combine)
96             throws DataSetException
97     {
98         this(new IDataSet[]{dataSet1, dataSet2}, combine);
99     }
100
101     /**
102      * Creates a composite dataset that combines duplicate tables of the specified dataset.
103      *
104      * @param dataSet
105      * the dataset
106      * @param combine
107      * if <code>true</code>, tables having the same name are merged into
108      * one table.
109      * @deprecated This constructor is useless when the combine parameter is
110      * <code>false</code>. Use overload that doesn't have the combine argument.
111      */

112     public CompositeDataSet(IDataSet dataSet, boolean combine)
113             throws DataSetException
114     {
115         this(new IDataSet[]{dataSet}, combine);
116     }
117
118     /**
119      * Creates a composite dataset that combines duplicate tables of the specified dataset.
120      *
121      * @param dataSet
122      * the dataset
123      */

124     public CompositeDataSet(IDataSet dataSet) throws DataSetException
125     {
126         this(new IDataSet[]{dataSet}, true);
127     }
128
129     /**
130      * Creates a composite dataset that combines tables having identical name.
131      * Tables having the same name are merged into one table.
132      */

133     public CompositeDataSet(ITable[] tables) throws DataSetException
134     {
135         List JavaDoc tableList = new ArrayList JavaDoc();
136         for (int i = 0; i < tables.length; i++)
137         {
138             addTable(tables[i], tableList, true);
139         }
140
141         _tables = (ITable[])tableList.toArray(new ITable[0]);
142     }
143
144     private void addTable(ITable newTable, List JavaDoc tableList, boolean combine)
145     {
146         // No merge required, simply add new table at then end of the list
147
if (!combine)
148         {
149             tableList.add(newTable);
150             return;
151         }
152
153         // Merge required, search for existing table with the same name
154
String JavaDoc tableName = newTable.getTableMetaData().getTableName();
155         for (ListIterator JavaDoc it = tableList.listIterator(); it.hasNext();)
156         {
157             ITable table = (ITable)it.next();
158             if (tableName.equalsIgnoreCase(table.getTableMetaData().getTableName()))
159             {
160                 // Found existing table, merge existing and new tables together
161
it.set(new CompositeTable(table, newTable));
162                 return;
163             }
164         }
165
166         // No existing table found, add new table at the end of the list
167
tableList.add(newTable);
168     }
169
170     ////////////////////////////////////////////////////////////////////////////
171
// AbstractDataSet class
172

173     protected ITableIterator createIterator(boolean reversed)
174             throws DataSetException
175     {
176         return new DefaultTableIterator(_tables, reversed);
177     }
178 }
179
180
181
182
183
184
185
186
Popular Tags