TableDecoratorDataSet.java

  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. package org.dbunit.dataset;

  22. /**
  23.  * Decorates a dataset to allow decorating the returned {@link ITable}s (and
  24.  * {@link ITableMetaData}s, by extension). Intended to be used for things like
  25.  * filtering out columns. Do not use this to filter out entire tables; use
  26.  * {@link FilteredDataSet} for that.
  27.  *
  28.  * @see ColumnFilterTable
  29.  *
  30.  * @author rcd (rcd AT users.sourceforge.net)
  31.  * @since 3.0.0
  32.  */
  33. public class TableDecoratorDataSet extends AbstractDataSet
  34. {
  35.     private final IDataSet _dataSet;
  36.     private final TableDecoratorFunction _decoratorFunction;

  37.     public TableDecoratorDataSet(final IDataSet dataSet,
  38.             final TableDecoratorFunction decoratorFunction)
  39.     {
  40.         _dataSet = dataSet;
  41.         _decoratorFunction = decoratorFunction;
  42.     }

  43.     @Override
  44.     protected ITableIterator createIterator(final boolean reversed)
  45.             throws DataSetException
  46.     {
  47.         return new FilterIterator(
  48.                 reversed ? _dataSet.reverseIterator() : _dataSet.iterator());
  49.     }

  50.     private class FilterIterator implements ITableIterator
  51.     {
  52.         private final ITableIterator _iterator;

  53.         public FilterIterator(final ITableIterator _iterator)
  54.         {
  55.             this._iterator = _iterator;
  56.         }

  57.         @Override
  58.         public boolean next() throws DataSetException
  59.         {
  60.             return _iterator.next();
  61.         }

  62.         @Override
  63.         public ITableMetaData getTableMetaData() throws DataSetException
  64.         {
  65.             return getTable().getTableMetaData();
  66.         }

  67.         @Override
  68.         public ITable getTable() throws DataSetException
  69.         {
  70.             return _decoratorFunction.apply(_iterator.getTable());
  71.         }
  72.     }

  73.     @FunctionalInterface
  74.     public interface TableDecoratorFunction
  75.     {
  76.         ITable apply(ITable table) throws DataSetException;
  77.     }
  78. }