SortedDataSet.java

  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. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;


  24. /**
  25.  * Decorator that returns the {@link ITable}s of the decorated dataset
  26.  * as {@link SortedTable}s.
  27.  *
  28.  * @author Manuel Laflamme
  29.  * @version $Revision$
  30.  * @since Feb 19, 2003
  31.  */
  32. public class SortedDataSet extends AbstractDataSet
  33. {

  34.     /**
  35.      * Logger for this class
  36.      */
  37.     private static final Logger logger = LoggerFactory.getLogger(SortedDataSet.class);

  38.     private final IDataSet _dataSet;

  39.     public SortedDataSet(IDataSet dataSet) throws DataSetException
  40.     {
  41.         _dataSet = dataSet;
  42.     }

  43.     ////////////////////////////////////////////////////////////////////////////
  44.     // AbstractDataSet class

  45.     protected ITableIterator createIterator(boolean reversed)
  46.             throws DataSetException
  47.     {
  48.         if(logger.isDebugEnabled())
  49.             logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));

  50.         return new SortedIterator(reversed ?
  51.                 _dataSet.reverseIterator() : _dataSet.iterator());
  52.     }

  53.     ////////////////////////////////////////////////////////////////////////////
  54.     // IDataSet interface

  55.     public String[] getTableNames() throws DataSetException
  56.     {
  57.         return _dataSet.getTableNames();
  58.     }

  59.     public ITableMetaData getTableMetaData(String tableName) throws DataSetException
  60.     {
  61.         logger.debug("getTableMetaData(tableName={}) - start", tableName);
  62.         return _dataSet.getTableMetaData(tableName);
  63.     }

  64.     public ITable getTable(String tableName) throws DataSetException
  65.     {
  66.         logger.debug("getTable(tableName={}) - start", tableName);
  67.         return new SortedTable(_dataSet.getTable(tableName));
  68.     }

  69.     ////////////////////////////////////////////////////////////////////////////
  70.     // SortedIterator class

  71.     private class SortedIterator implements ITableIterator
  72.     {
  73.         private final ITableIterator _iterator;

  74.         public SortedIterator(ITableIterator iterator)
  75.         {
  76.             _iterator = iterator;
  77.         }

  78.         ////////////////////////////////////////////////////////////////////////
  79.         // ITableIterator interface

  80.         public boolean next() throws DataSetException
  81.         {
  82.             return _iterator.next();
  83.         }

  84.         public ITableMetaData getTableMetaData() throws DataSetException
  85.         {
  86.             return _iterator.getTableMetaData();
  87.         }

  88.         public ITable getTable() throws DataSetException
  89.         {
  90.             return new SortedTable(_iterator.getTable());
  91.         }
  92.     }

  93. }