KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > db > orm > DbListIterator


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.db.orm;
4
5 import jodd.db.orm.mapper.ResultSetMapper;
6
7 import java.util.Iterator JavaDoc;
8
9 /**
10  * Internal database iterator.
11  */

12 class DbListIterator implements Iterator JavaDoc {
13
14     protected DbOrmQuery query;
15     protected ResultSetMapper resultSetMapper;
16     protected boolean closeOnEnd;
17     protected Class JavaDoc[] types;
18     protected Class JavaDoc type;
19     protected boolean one;
20
21     // ---------------------------------------------------------------- ctors
22

23
24     DbListIterator(DbOrmQuery query, boolean closeOnEnd, boolean one) {
25         this.query = query;
26         this.resultSetMapper = query.buildResultSetMapper();
27         this.closeOnEnd = closeOnEnd;
28         this.one = one;
29     }
30     
31     DbListIterator(DbOrmQuery query, boolean closeOnEnd) {
32         this(query, closeOnEnd, false);
33     }
34
35
36     DbListIterator(DbOrmQuery query, Class JavaDoc type) {
37         this(query, type, true);
38     }
39
40     DbListIterator(DbOrmQuery query, Class JavaDoc type, boolean closeOnEnd) {
41         this.query = query;
42         this.resultSetMapper = query.buildResultSetMapper();
43         this.type = type;
44         this.types = new Class JavaDoc[]{type};
45         this.closeOnEnd = closeOnEnd;
46     }
47
48
49     DbListIterator(DbOrmQuery query, Class JavaDoc[] types) {
50         this(query, types, true);
51     }
52     DbListIterator(DbOrmQuery query, Class JavaDoc[] types, boolean closeOnEnd) {
53         this.query = query;
54         this.resultSetMapper = query.buildResultSetMapper();
55         this.types = types;
56         this.closeOnEnd = closeOnEnd;
57     }
58
59     // ---------------------------------------------------------------- iterate
60

61     public void remove() {
62         throw new UnsupportedOperationException JavaDoc("Removing is not supported.");
63     }
64
65     public boolean hasNext() {
66         if (resultSetMapper.next() == true) {
67             return true;
68         }
69         if (closeOnEnd == true) {
70             query.close();
71         } else {
72             query.close(resultSetMapper.getResultSet());
73         }
74         return false;
75     }
76
77     public Object JavaDoc next() {
78         if (type != null) {
79             return resultSetMapper.parseOneObject(types);
80         }
81         if (one == true) {
82             return resultSetMapper.parseOneObject();
83         }
84         if (types != null) {
85             return resultSetMapper.parseObjects(types);
86         } else {
87             return resultSetMapper.parseObjects();
88         }
89     }
90
91 }
92
Popular Tags