@@ -63,18 +63,18 @@ static <T> Window<T> createWindow(Query query, List<T> result, Class<?> sourceTy
63
63
KeysetScrollPosition keyset = query .getKeyset ();
64
64
KeysetScrollDirector director = KeysetScrollDirector .of (keyset .getDirection ());
65
65
66
- director .postPostProcessResults (result );
66
+ List < T > resultsToUse = director .postPostProcessResults (result , query . getLimit () );
67
67
68
68
IntFunction <KeysetScrollPosition > positionFunction = value -> {
69
69
70
- T last = result .get (value );
70
+ T last = resultsToUse .get (value );
71
71
Entity <T > entity = operations .forEntity (last );
72
72
73
73
Map <String , Object > keys = entity .extractKeys (sortObject , sourceType );
74
74
return KeysetScrollPosition .of (keys );
75
75
};
76
76
77
- return createWindow ( result , query .getLimit (), positionFunction );
77
+ return Window . from ( resultsToUse , positionFunction , hasMoreElements ( result , query .getLimit ()) );
78
78
}
79
79
80
80
static <T > Window <T > createWindow (List <T > result , int limit , IntFunction <? extends ScrollPosition > positionFunction ) {
@@ -187,13 +187,14 @@ public Document createQuery(KeysetScrollPosition keyset, Document queryObject, D
187
187
return queryObject ;
188
188
}
189
189
190
- public <T > void postPostProcessResults (List <T > result ) {
191
-
192
- }
193
-
194
190
protected String getComparator (int sortOrder ) {
195
191
return sortOrder == 1 ? "$gt" : "$lt" ;
196
192
}
193
+
194
+ protected <T > List <T > postPostProcessResults (List <T > list , int limit ) {
195
+ return getFirst (limit , list );
196
+ }
197
+
197
198
}
198
199
199
200
/**
@@ -219,20 +220,48 @@ public Document getSortObject(String idPropertyName, Query query) {
219
220
}
220
221
221
222
@ Override
222
- protected String getComparator ( int sortOrder ) {
223
+ public < T > List < T > postPostProcessResults ( List < T > list , int limit ) {
223
224
224
- // use gte/lte to include the object at the cursor/keyset so that
225
- // we can include it in the result to check whether there is a next object.
226
- // It needs to be filtered out later on.
227
- return sortOrder == 1 ? "$gte" : "$lte" ;
228
- }
229
-
230
- @ Override
231
- public <T > void postPostProcessResults (List <T > result ) {
232
225
// flip direction of the result list as we need to accomodate for the flipped sort order for proper offset
233
226
// querying.
234
- Collections .reverse (result );
227
+ Collections .reverse (list );
228
+
229
+ return getLast (limit , list );
235
230
}
231
+
236
232
}
237
233
234
+ /**
235
+ * Return the first {@code count} items from the list.
236
+ *
237
+ * @param count
238
+ * @param list
239
+ * @return
240
+ * @param <T>
241
+ */
242
+ static <T > List <T > getFirst (int count , List <T > list ) {
243
+
244
+ if (count > 0 && list .size () > count ) {
245
+ return list .subList (0 , count );
246
+ }
247
+
248
+ return list ;
249
+ }
250
+
251
+ /**
252
+ * Return the last {@code count} items from the list.
253
+ *
254
+ * @param count
255
+ * @param list
256
+ * @return
257
+ * @param <T>
258
+ */
259
+ static <T > List <T > getLast (int count , List <T > list ) {
260
+
261
+ if (count > 0 && list .size () > count ) {
262
+ return list .subList (list .size () - (count ), list .size ());
263
+ }
264
+
265
+ return list ;
266
+ }
238
267
}
0 commit comments