View Javadoc

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  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * Decorator that allows forward only access to decorated dataset.
28   *
29   * @author Manuel Laflamme
30   * @since Apr 9, 2003
31   * @version $Revision: 677 $
32   */
33  public class ForwardOnlyDataSet extends AbstractDataSet
34  {
35  
36      /**
37       * Logger for this class
38       */
39      private static final Logger logger = LoggerFactory.getLogger(ForwardOnlyDataSet.class);
40  
41      private final IDataSet _dataSet;
42      private int _iteratorCount;
43  
44      public ForwardOnlyDataSet(IDataSet dataSet)
45      {
46          _dataSet = dataSet;
47      }
48  
49      ////////////////////////////////////////////////////////////////////////////
50      // AbstractDataSet class
51  
52      protected ITableIterator createIterator(boolean reversed)
53              throws DataSetException
54      {
55          logger.debug("createIterator(reversed={}) - start", String.valueOf(reversed));
56  
57          if (reversed)
58          {
59              throw new UnsupportedOperationException("Reverse iterator not supported!");
60          }
61  
62          if (_iteratorCount > 0)
63          {
64              throw new UnsupportedOperationException("Only one iterator allowed!");
65          }
66  
67          return new ForwardOnlyIterator(_dataSet.iterator());
68      }
69  
70      ////////////////////////////////////////////////////////////////////////////
71      // IDataSet interface
72  
73      public String[] getTableNames() throws DataSetException
74      {
75          throw new UnsupportedOperationException();
76      }
77  
78      public ITableMetaData getTableMetaData(String tableName) throws DataSetException
79      {
80          throw new UnsupportedOperationException();
81      }
82  
83      public ITable getTable(String tableName) throws DataSetException
84      {
85          throw new UnsupportedOperationException();
86      }
87  
88      ////////////////////////////////////////////////////////////////////////////
89      // ForwardOnlyIterator class
90  
91      private class ForwardOnlyIterator implements ITableIterator
92      {
93          private final ITableIterator _iterator;
94  
95          public ForwardOnlyIterator(ITableIterator iterator)
96          {
97              _iterator = iterator;
98              _iteratorCount++;
99          }
100 
101         ////////////////////////////////////////////////////////////////////////////
102         // ITableIterator interface
103 
104         public boolean next() throws DataSetException
105         {
106             return _iterator.next();
107         }
108 
109         public ITableMetaData getTableMetaData() throws DataSetException
110         {
111             return _iterator.getTableMetaData();
112         }
113 
114         public ITable getTable() throws DataSetException
115         {
116             return new ForwardOnlyTable(_iterator.getTable());
117         }
118     }
119 }