KickJava   Java API By Example, From Geeks To Geeks.

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


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.dataset.stream.IDataSetConsumer;
25 import org.dbunit.dataset.stream.IDataSetProducer;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * Hold copy of another dataset or a consumed provider content.
32  *
33  * @author Manuel Laflamme
34  * @since Apr 18, 2003
35  * @version $Revision: 1.6 $
36  */

37 public class CachedDataSet extends AbstractDataSet implements IDataSetConsumer
38 {
39     private ITable[] _tables;
40
41     private List JavaDoc _tableList;
42     private DefaultTable _activeTable;
43
44     /**
45      * Default constructor.
46      */

47     public CachedDataSet()
48     {
49     }
50
51     /**
52      * Creates a copy of the specified dataset.
53      */

54     public CachedDataSet(IDataSet dataSet) throws DataSetException
55     {
56         List JavaDoc tableList = new ArrayList JavaDoc();
57         ITableIterator iterator = dataSet.iterator();
58         while (iterator.next())
59         {
60             tableList.add(new CachedTable(iterator.getTable()));
61         }
62         _tables = (ITable[])tableList.toArray(new ITable[0]);
63     }
64
65     /**
66      * Creates a CachedDataSet that syncronously consume the specified producer.
67      */

68     public CachedDataSet(IDataSetProducer producer) throws DataSetException
69     {
70         producer.setConsumer(this);
71         producer.produce();
72     }
73
74     ////////////////////////////////////////////////////////////////////////////
75
// AbstractDataSet class
76

77     protected ITableIterator createIterator(boolean reversed)
78             throws DataSetException
79     {
80         return new DefaultTableIterator(_tables, reversed);
81     }
82
83     ////////////////////////////////////////////////////////////////////////
84
// IDataSetConsumer interface
85

86     public void startDataSet() throws DataSetException
87     {
88         _tableList = new ArrayList JavaDoc();
89         _tables = null;
90     }
91
92     public void endDataSet() throws DataSetException
93     {
94         _tables = (ITable[])_tableList.toArray(new ITable[0]);
95         _tableList = null;
96     }
97
98     public void startTable(ITableMetaData metaData) throws DataSetException
99     {
100         _activeTable = new DefaultTable(metaData);
101 // System.out.println("START " + _activeMetaData.getTableName());
102
}
103
104     public void endTable() throws DataSetException
105     {
106 // System.out.println("END " + _activeMetaData.getTableName());
107
_tableList.add(_activeTable);
108         _activeTable = null;
109     }
110
111     public void row(Object JavaDoc[] values) throws DataSetException
112     {
113         _activeTable.addRow(values);
114     }
115 }
116
Popular Tags