KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > filter > AbstractTableFilter


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.filter;
22
23 import org.dbunit.dataset.*;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * This class provides a skeletal implementation of the {@link ITableFilter}
30  * interface to minimize the effort required to implement a filter. Subsclasses
31  * are only required to implement the {@link #isValidName} method.
32  *
33  * @author Manuel Laflamme
34  * @since Mar 8, 2003
35  * @version $Revision: 1.5 $
36  */

37 public abstract class AbstractTableFilter implements ITableFilter
38 {
39
40     /**
41      * Returns <code>true</code> if specified table is allowed by this filter.
42      * This legacy method, now replaced by accept, still exist for compatibily
43      * with older environment
44      */

45     public abstract boolean isValidName(String JavaDoc tableName) throws DataSetException;
46
47     ////////////////////////////////////////////////////////////////////////////
48
// ITableFilter interface
49

50     public boolean accept(String JavaDoc tableName) throws DataSetException
51     {
52         return isValidName(tableName);
53     }
54
55     public String JavaDoc[] getTableNames(IDataSet dataSet) throws DataSetException
56     {
57         String JavaDoc[] tableNames = dataSet.getTableNames();
58         List JavaDoc nameList = new ArrayList JavaDoc();
59         for (int i = 0; i < tableNames.length; i++)
60         {
61             String JavaDoc tableName = tableNames[i];
62             if (accept(tableName))
63             {
64                 nameList.add(tableName);
65             }
66         }
67         return (String JavaDoc[])nameList.toArray(new String JavaDoc[0]);
68     }
69
70     public ITableIterator iterator(IDataSet dataSet, boolean reversed)
71             throws DataSetException
72     {
73         return new FilterIterator(reversed ?
74                 dataSet.reverseIterator() : dataSet.iterator());
75     }
76
77     ////////////////////////////////////////////////////////////////////////////
78
// FilterIterator class
79

80     private class FilterIterator implements ITableIterator
81     {
82         private final ITableIterator _iterator;
83
84         public FilterIterator(ITableIterator iterator)
85         {
86             _iterator = iterator;
87         }
88
89         ////////////////////////////////////////////////////////////////////////////
90
// ITableIterator interface
91

92         public boolean next() throws DataSetException
93         {
94             while(_iterator.next())
95             {
96                 if (accept(_iterator.getTableMetaData().getTableName()))
97                 {
98                     return true;
99                 }
100             }
101             return false;
102         }
103
104         public ITableMetaData getTableMetaData() throws DataSetException
105         {
106             return _iterator.getTableMetaData();
107         }
108
109         public ITable getTable() throws DataSetException
110         {
111             return _iterator.getTable();
112         }
113     }
114 }
115
Popular Tags