1 | |
package org.apache.ojb.soda; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
import java.util.Collection; |
19 | |
import java.util.Iterator; |
20 | |
import java.util.Vector; |
21 | |
|
22 | |
import org.apache.ojb.broker.PersistenceBroker; |
23 | |
import org.apache.ojb.broker.accesslayer.RsIterator; |
24 | |
import org.apache.ojb.broker.query.Query; |
25 | |
import org.odbms.ObjectSet; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class ObjectSetImpl implements ObjectSet |
31 | |
{ |
32 | |
protected Iterator ojbIterator; |
33 | |
protected int length; |
34 | |
protected Vector elements; |
35 | |
protected int position; |
36 | |
protected int scrolled; |
37 | |
protected boolean resultSetClosed; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
public ObjectSetImpl(PersistenceBroker broker, Query query, int limit) |
43 | |
{ |
44 | |
super(); |
45 | |
position = 0; |
46 | |
scrolled = 0; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
Collection col = broker.getCollectionByQuery(query); |
61 | |
ojbIterator = col.iterator(); |
62 | |
|
63 | |
length = col.size(); |
64 | |
if (limit >= 0) |
65 | |
{ |
66 | |
length = Math.min(length,limit); |
67 | |
} |
68 | |
elements = new Vector(length); |
69 | |
|
70 | |
setResultSetClosed(false); |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public synchronized boolean hasNext() |
77 | |
{ |
78 | |
if (position < length) |
79 | |
{ |
80 | |
if (position < scrolled) |
81 | |
{ |
82 | |
return true; |
83 | |
} |
84 | |
else |
85 | |
{ |
86 | |
boolean result = ojbIterator.hasNext(); |
87 | |
return result; |
88 | |
} |
89 | |
} |
90 | |
else |
91 | |
{ |
92 | |
releaseJdbcResources(); |
93 | |
return false; |
94 | |
} |
95 | |
|
96 | |
} |
97 | |
|
98 | |
protected void releaseJdbcResources() |
99 | |
{ |
100 | |
if (!isResultSetClosed()) |
101 | |
{ |
102 | |
if (ojbIterator instanceof RsIterator) |
103 | |
{ |
104 | |
((RsIterator) ojbIterator).releaseDbResources(); |
105 | |
} |
106 | |
setResultSetClosed(true); |
107 | |
} |
108 | |
} |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
public synchronized Object next() |
114 | |
{ |
115 | |
if (position < scrolled) |
116 | |
{ |
117 | |
position++; |
118 | |
return elements.get(position - 1); |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | |
Object next = ojbIterator.next(); |
123 | |
elements.add(next); |
124 | |
position++; |
125 | |
scrolled++; |
126 | |
return next; |
127 | |
} |
128 | |
} |
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
public synchronized void reset() |
134 | |
{ |
135 | |
position = 0; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
public int size() |
142 | |
{ |
143 | |
return length; |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public boolean isResultSetClosed() |
151 | |
{ |
152 | |
return resultSetClosed; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public void setResultSetClosed(boolean resultSetClosed) |
160 | |
{ |
161 | |
this.resultSetClosed = resultSetClosed; |
162 | |
} |
163 | |
} |