View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2019, 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   * Decorates a dataset to allow decorating the returned {@link ITable}s (and
26   * {@link ITableMetaData}s, by extension). Intended to be used for things like
27   * filtering out columns. Do not use this to filter out entire tables; use
28   * {@link FilteredDataSet} for that.
29   *
30   * @see ColumnFilterTable
31   *
32   * @author rcd (rcd AT users.sourceforge.net)
33   * @since 3.0.0
34   */
35  public class TableDecoratorDataSet extends AbstractDataSet
36  {
37      private final IDataSet _dataSet;
38      private final TableDecoratorFunction _decoratorFunction;
39  
40      public TableDecoratorDataSet(final IDataSet dataSet,
41              final TableDecoratorFunction decoratorFunction)
42      {
43          _dataSet = dataSet;
44          _decoratorFunction = decoratorFunction;
45      }
46  
47      @Override
48      protected ITableIterator createIterator(final boolean reversed)
49              throws DataSetException
50      {
51          return new FilterIterator(
52                  reversed ? _dataSet.reverseIterator() : _dataSet.iterator());
53      }
54  
55      private class FilterIterator implements ITableIterator
56      {
57          private final ITableIterator _iterator;
58  
59          public FilterIterator(final ITableIterator _iterator)
60          {
61              this._iterator = _iterator;
62          }
63  
64          @Override
65          public boolean next() throws DataSetException
66          {
67              return _iterator.next();
68          }
69  
70          @Override
71          public ITableMetaData getTableMetaData() throws DataSetException
72          {
73              return getTable().getTableMetaData();
74          }
75  
76          @Override
77          public ITable getTable() throws DataSetException
78          {
79              return _decoratorFunction.apply(_iterator.getTable());
80          }
81      }
82  
83      @FunctionalInterface
84      public interface TableDecoratorFunction
85      {
86          ITable apply(ITable table) throws DataSetException;
87      }
88  }