View Javadoc

1   /***
2    *  The contents of this file are subject to the Mozilla Public
3    *  License Version 1.1 (the "License"); you may not use this file
4    *  except in compliance with the License. You may obtain a copy of
5    *  the License at http://www.mozilla.org/MPL/
6    *
7    *  Software distributed under the License is distributed on an "AS
8    *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9    *  implied. See the License for the specific language governing
10   *  rights and limitations under the License.
11   *
12   *  The Original Code is pow2toolkit library.
13   *
14   *  The Initial Owner of the Original Code is
15   *  Claudio Fratarcangeli
16   *
17   *  Portions created by Power Of Two S.R.L. are
18   *  Copyright (C) Power Of Two S.R.L.
19   *  All Rights Reserved.
20   *
21   * Contributor(s):
22   */
23  
24  package com.pow2.dao.datalist;
25  
26  import java.util.NoSuchElementException;
27  import java.sql.SQLException;
28  
29  import org.apache.log4j.*;
30  
31  
32  
33  /***
34   *  ResultSetDataListIterator class
35   *
36   * @author  Claudio Fratarcangeli
37   * @created  5 dicembre 2002
38   */
39  public class ResultSetDataListIterator  implements DataListIterator
40  {
41    /*** Log4J category. */
42    protected static Category cat = Category.getInstance(ResultSetDataListIterator.class);
43  
44    private ResultSetDataList rsDataList;
45    private int currentIndex = 0;
46  
47  
48    /***
49     *  Constructor
50     *
51     * @param rsDataList a resultSetDataList object
52     * @exception  Exception if any error occurs
53     */
54    public ResultSetDataListIterator(ResultSetDataList rsDataList) throws Exception
55    {
56      this.rsDataList = rsDataList;
57      rsDataList.beforeFirst();
58    }
59  
60  
61    /***
62     *  Check if the data list contains another object.
63     *
64     * @return  true  if the data list contains another object, false otherwise
65     * @exception  Exception if any error occurs
66     */
67    public boolean hasNext() throws Exception
68    {
69      try
70      {
71        return rsDataList.hasNext();
72      }
73      catch (SQLException e)
74      {
75          // log exception here
76          throw e;
77      }
78    }
79  
80  
81    /***
82     *  Get the next object from the data list
83     *
84     * @param  object the object to fill with data
85     * @return the next object from the data list
86     * @exception NoSuchElementException if any error occurs
87     */
88    public Object next(Object obj) throws NoSuchElementException
89    {
90      Object object;
91  
92      try
93      {
94        object = rsDataList.get(currentIndex, obj);
95      }
96      catch (IndexOutOfBoundsException e)
97      {
98         throw new NoSuchElementException(e.getMessage());
99      }
100     catch (Exception e)
101     {
102       throw new NoSuchElementException(e.getMessage());
103     }
104 
105     currentIndex++;
106     return object;
107   }
108 }