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 package org.dbunit.dataset;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26
27 /**
28 * Decorator that returns the {@link ITable}s of the decorated dataset
29 * as {@link SortedTable}s.
30 *
31 * @author Manuel Laflamme
32 * @version $Revision: 815 $
33 * @since Feb 19, 2003
34 */
35 public class SortedDataSet extends AbstractDataSet
36 {
37
38 /**
39 * Logger for this class
40 */
41 private static final Logger logger = LoggerFactory.getLogger(SortedDataSet.class);
42
43 private final IDataSet _dataSet;
44
45 public SortedDataSet(IDataSet dataSet) throws DataSetException
46 {
47 _dataSet = dataSet;
48 }
49
50 ////////////////////////////////////////////////////////////////////////////
51 // AbstractDataSet class
52
53 protected ITableIterator createIterator(boolean reversed)
54 throws DataSetException
55 {
56 if(logger.isDebugEnabled())
57 logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
58
59 return new SortedIterator(reversed ?
60 _dataSet.reverseIterator() : _dataSet.iterator());
61 }
62
63 ////////////////////////////////////////////////////////////////////////////
64 // IDataSet interface
65
66 public String[] getTableNames() throws DataSetException
67 {
68 return _dataSet.getTableNames();
69 }
70
71 public ITableMetaData getTableMetaData(String tableName) throws DataSetException
72 {
73 logger.debug("getTableMetaData(tableName={}) - start", tableName);
74 return _dataSet.getTableMetaData(tableName);
75 }
76
77 public ITable getTable(String tableName) throws DataSetException
78 {
79 logger.debug("getTable(tableName={}) - start", tableName);
80 return new SortedTable(_dataSet.getTable(tableName));
81 }
82
83 ////////////////////////////////////////////////////////////////////////////
84 // SortedIterator class
85
86 private class SortedIterator implements ITableIterator
87 {
88 private final ITableIterator _iterator;
89
90 public SortedIterator(ITableIterator iterator)
91 {
92 _iterator = iterator;
93 }
94
95 ////////////////////////////////////////////////////////////////////////
96 // ITableIterator interface
97
98 public boolean next() throws DataSetException
99 {
100 return _iterator.next();
101 }
102
103 public ITableMetaData getTableMetaData() throws DataSetException
104 {
105 return _iterator.getTableMetaData();
106 }
107
108 public ITable getTable() throws DataSetException
109 {
110 return new SortedTable(_iterator.getTable());
111 }
112 }
113
114 }