@@ -151,7 +151,32 @@ public void close() {
151
151
152
152
@ Override
153
153
public void next (final SingleResultCallback <List <T >> callback ) {
154
- internalNext (callback );
154
+ if (isClosed ()) {
155
+ callback .onResult (null , new MongoException ("next() called after the cursor was closed." ));
156
+ } else if (firstBatch != null && (!firstBatch .getResults ().isEmpty ())) {
157
+ // May be empty for a tailable cursor
158
+ List <T > results = firstBatch .getResults ();
159
+ firstBatch = null ;
160
+ if (getServerCursor () == null ) {
161
+ close ();
162
+ }
163
+ callback .onResult (results , null );
164
+ } else {
165
+ ServerCursor localCursor = getServerCursor ();
166
+ if (localCursor == null ) {
167
+ close ();
168
+ callback .onResult (null , null );
169
+ } else {
170
+ synchronized (this ) {
171
+ if (isClosed ()) {
172
+ callback .onResult (null , new MongoException ("next() called after the cursor was closed." ));
173
+ return ;
174
+ }
175
+ isOperationInProgress = true ;
176
+ }
177
+ getMore (localCursor , callback );
178
+ }
179
+ }
155
180
}
156
181
157
182
@ Override
@@ -193,35 +218,6 @@ public int getMaxWireVersion() {
193
218
return maxWireVersion ;
194
219
}
195
220
196
- private void internalNext (final SingleResultCallback <List <T >> callback ) {
197
- if (isClosed ()) {
198
- callback .onResult (null , new MongoException ("next() called after the cursor was closed." ));
199
- } else if (firstBatch != null && (!firstBatch .getResults ().isEmpty ())) {
200
- // May be empty for a tailable cursor
201
- List <T > results = firstBatch .getResults ();
202
- firstBatch = null ;
203
- if (getServerCursor () == null ) {
204
- close ();
205
- }
206
- callback .onResult (results , null );
207
- } else {
208
- ServerCursor localCursor = getServerCursor ();
209
- if (localCursor == null ) {
210
- close ();
211
- callback .onResult (null , null );
212
- } else {
213
- synchronized (this ) {
214
- if (isClosed ()) {
215
- callback .onResult (null , new MongoException ("next() called after the cursor was closed." ));
216
- return ;
217
- }
218
- isOperationInProgress = true ;
219
- }
220
- getMore (localCursor , callback );
221
- }
222
- }
223
- }
224
-
225
221
private boolean limitReached () {
226
222
return Math .abs (limit ) != 0 && count .get () >= Math .abs (limit );
227
223
}
0 commit comments