ForwardOnlyDataSet.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.dbunit.database.QueryTableIterator;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;

  25. /**
  26.  * Decorator that allows forward only access to decorated dataset.
  27.  *
  28.  * @author Manuel Laflamme
  29.  * @since Apr 9, 2003
  30.  * @version $Revision$
  31.  */
  32. public class ForwardOnlyDataSet extends AbstractDataSet
  33. {

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

  38.     private final IDataSet _dataSet;
  39.     private int _iteratorCount;

  40.     public ForwardOnlyDataSet(IDataSet dataSet)
  41.     {
  42.         _dataSet = dataSet;
  43.     }

  44.     ////////////////////////////////////////////////////////////////////////////
  45.     // AbstractDataSet class

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

  50.         if (reversed)
  51.         {
  52.             throw new UnsupportedOperationException("Reverse iterator not supported!");
  53.         }

  54.         if (_iteratorCount > 0)
  55.         {
  56.             throw new UnsupportedOperationException("Only one iterator allowed!");
  57.         }

  58.         return new ForwardOnlyIterator(_dataSet.iterator());
  59.     }

  60.     ////////////////////////////////////////////////////////////////////////////
  61.     // IDataSet interface

  62.     public String[] getTableNames() throws DataSetException
  63.     {
  64.         throw new UnsupportedOperationException();
  65.     }

  66.     public ITableMetaData getTableMetaData(String tableName) throws DataSetException
  67.     {
  68.         throw new UnsupportedOperationException();
  69.     }

  70.     public ITable getTable(String tableName) throws DataSetException
  71.     {
  72.         throw new UnsupportedOperationException();
  73.     }

  74.     ////////////////////////////////////////////////////////////////////////////
  75.     // ForwardOnlyIterator class

  76.     private class ForwardOnlyIterator implements ITableIterator
  77.     {
  78.         private final ITableIterator _iterator;

  79.         public ForwardOnlyIterator(ITableIterator iterator)
  80.         {
  81.             _iterator = iterator;
  82.             _iteratorCount++;
  83.         }

  84.         ////////////////////////////////////////////////////////////////////////////
  85.         // ITableIterator interface

  86.         public boolean next() throws DataSetException
  87.         {
  88.             if(_iterator instanceof QueryTableIterator) {
  89.                 return ((QueryTableIterator) _iterator).nextWithoutClosing();
  90.             } else {
  91.                 return _iterator.next();
  92.             }
  93.         }

  94.         public ITableMetaData getTableMetaData() throws DataSetException
  95.         {
  96.             return _iterator.getTableMetaData();
  97.         }

  98.         public ITable getTable() throws DataSetException
  99.         {
  100.             return new ForwardOnlyTable(_iterator.getTable());
  101.         }
  102.     }
  103. }