01: /*
02: * Created on Oct 27, 2004
03: */
04: package net.sourceforge.orbroker;
05:
06: import java.util.Iterator;
07: import java.util.NoSuchElementException;
08:
09: /**
10: * Iterator returned from
11: * {@link net.sourceforge.orbroker.QueryableConnection#selectIterator(String, int)
12: * selectIterator(String, int)}.
13: * @author Nils Kilden-Pedersen
14: */
15: final class ResultSetIterator implements Iterator {
16:
17: private Boolean hasNext = null;
18: private QueryResult queryResult;
19: private final ResultRow row;
20: private final Statement statement;
21:
22: /**
23: * Constructor.
24: * @param queryResult
25: * @param statement
26: */
27: ResultSetIterator(QueryResult queryResult, Statement statement) {
28: this .queryResult = queryResult;
29: this .statement = statement;
30: this .row = this .queryResult.getResultRow();
31: }
32:
33: synchronized void closeIterator() {
34: if (this .queryResult != null) {
35: this .queryResult.close();
36: this .queryResult = null;
37: }
38: }
39:
40: /**
41: * Make sure query result is closed.
42: * @inheritDoc
43: * @see java.lang.Object#finalize()
44: */
45: protected void finalize() throws Throwable {
46: closeIterator();
47: super .finalize();
48: }
49:
50: /**
51: * @inheritDoc
52: * @see java.util.Iterator#hasNext()
53: */
54: public boolean hasNext() {
55: if (this .queryResult == null) {
56: return false;
57: }
58: if (this .hasNext == null) {
59: this .hasNext = Boolean.valueOf(this .queryResult.nextRow());
60: }
61: if (!this .hasNext.booleanValue()) {
62: closeIterator();
63: }
64: return this .hasNext.booleanValue();
65: }
66:
67: /**
68: * @inheritDoc
69: * @see java.util.Iterator#next()
70: */
71: public Object next() {
72: if (this .hasNext == null || !this .hasNext.booleanValue()) {
73: throw new NoSuchElementException(
74: "Always check hasNext() before calling next().");
75: }
76: Object result = this .statement.buildResultObject(this .row);
77: this .hasNext = null;
78: return result;
79: }
80:
81: /**
82: * @inheritDoc
83: * @see java.util.Iterator#remove()
84: */
85: public void remove() {
86: throw new UnsupportedOperationException("remove()");
87: }
88:
89: }
|