KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > source > DataCursor


1 package jimm.datavision.source;
2 import java.util.List JavaDoc;
3
4 /**
5  * Represents a row of data. Provides the interface needed by
6  * <code>Report</code>, no more. When using JDBC, this is a wrapper around
7  * a <code>ResultSet</code>.
8  * <p>
9  * The only method subclasses <em>must</em> implement is
10  * <code>readRowData()</code>.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 public abstract class DataCursor {
15
16 protected int currRowNumber; // Starts at 1
17
protected List JavaDoc prevRowData;
18 protected List JavaDoc currRowData;
19 protected List JavaDoc nextRowData;
20 protected List JavaDoc lastRowData;
21 protected int lastRowNumber;
22
23 public boolean isFirst() { return currRowNumber == 1; }
24 public boolean isLast() {
25     if (nextRowData != null) // We already have next row cached
26
return false;
27     nextRowData = readRowData(); // Read next row
28
return nextRowData == null; // If it's null, we are at the last row
29
}
30
31 public boolean next() {
32     if (nextRowData == null) // If we have no cached data, read the next row
33
nextRowData = readRowData();
34
35     if (nextRowData == null) { // When no more data, curr row is the last row
36
lastRowData = currRowData;
37     lastRowNumber = currRowNumber;
38     }
39
40     prevRowData = currRowData;
41     currRowData = nextRowData;
42     nextRowData = null;
43
44     ++currRowNumber;
45     return currRowData != null;
46 }
47
48 public boolean previous() {
49     if (currRowNumber <= 1) // Not same as isFirst()
50
return false;
51
52     nextRowData = currRowData;
53     currRowData = prevRowData;
54     prevRowData = null;
55     --currRowNumber;
56     return true;
57 }
58
59 public boolean last() {
60     while (lastRowData == null && next())
61     ;
62     currRowData = lastRowData;
63     currRowNumber = lastRowNumber;
64     return true;
65 }
66
67 public int getRow() { return currRowNumber; }
68
69 public void close() {}
70
71 /**
72  * Returns the object in the specified column. <var>index</var> starts
73  * at 1.
74  * <p>
75  * Even when running a report, <var>currRowData</var> is not always
76  * defined. For example, when a query returns zero rows but a column field
77  * is in some header or footer, this method is called but
78  * <var>currRowData</var> is <code>null</code>. If it is, we return
79  * <code>null</code>.
80  *
81  * @return the object in the <var>index</var>'th column, or <code>null</code>
82  * if no data has yet been read
83  */

84 public Object JavaDoc getObject(int index) {
85     return currRowData == null ? null : currRowData.get(index - 1);
86 }
87
88 protected abstract List JavaDoc readRowData();
89
90 }
91
Popular Tags