KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > controls > system > jdbc > ResultSetIterator


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.controls.system.jdbc;
19
20 import org.apache.beehive.controls.api.ControlException;
21 import org.apache.beehive.controls.api.context.ControlBeanContext;
22
23 import java.lang.reflect.Method JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Calendar JavaDoc;
27 import java.util.NoSuchElementException JavaDoc;
28
29 /**
30  * Used by DefaultIteratorResultSetMapper for mapping a ResultSet to an Iterator type.
31  */

32 public class ResultSetIterator implements java.util.Iterator JavaDoc {
33
34     private final Class JavaDoc _returnClass;
35     private final ResultSet JavaDoc _rs;
36     private final RowMapper _rowMapper;
37
38     private boolean _primed = false;
39
40     /**
41      * Create a new ResultSetIterator.
42      * @param context A ControlBeanContext.
43      * @param method The annotated method.
44      * @param rs The ResultSet to map.
45      * @param cal A Calendar instance for mapping date/time values.
46      */

47     ResultSetIterator(ControlBeanContext context, Method JavaDoc method, ResultSet JavaDoc rs, Calendar JavaDoc cal) {
48         _rs = rs;
49
50         JdbcControl.SQL methodSQL = (JdbcControl.SQL) context.getMethodPropertySet(method, JdbcControl.SQL.class);
51         _returnClass = methodSQL.iteratorElementType();
52
53         if (_returnClass == null) {
54             throw new ControlException("Invalid return class declared for Iterator:" + _returnClass.getName());
55         }
56
57         _rowMapper = RowMapperFactory.getRowMapper(rs, _returnClass, cal);
58     }
59
60     /**
61      * Does this iterater have more elements?
62      * @return true if there is another element
63      */

64     public boolean hasNext() {
65         if (_primed) {
66             return true;
67         }
68
69         try {
70             _primed = _rs.next();
71         } catch (SQLException JavaDoc sqle) {
72             return false;
73         }
74         return _primed;
75     }
76
77     /**
78      * Get the next element in the iteration.
79      * @return The next element in the iteration.
80      */

81     public Object JavaDoc next() {
82         try {
83             if (!_primed) {
84                 _primed = _rs.next();
85                 if (!_primed) {
86                     throw new NoSuchElementException JavaDoc();
87                 }
88             }
89             // reset upon consumption
90
_primed = false;
91             return _rowMapper.mapRowToReturnType();
92         } catch (SQLException JavaDoc e) {
93             // Since Iterator interface is locked, all we can do
94
// is put the real exception inside an expected one.
95
NoSuchElementException JavaDoc xNoSuch = new NoSuchElementException JavaDoc("ResultSet exception: " + e);
96             xNoSuch.initCause(e);
97             throw xNoSuch;
98         }
99     }
100
101     /**
102      * Remove is currently not supported.
103      */

104     public void remove() {
105         throw new UnsupportedOperationException JavaDoc("remove not supported");
106     }
107 }
108
109
Popular Tags