31
31
32
32
import org .bson .Document ;
33
33
import org .springframework .data .domain .KeysetScrollPosition ;
34
+ import org .springframework .data .domain .Limit ;
34
35
import org .springframework .data .domain .OffsetScrollPosition ;
35
36
import org .springframework .data .domain .Pageable ;
36
37
import org .springframework .data .domain .ScrollPosition ;
@@ -66,7 +67,7 @@ public class Query implements ReadConcernAware, ReadPreferenceAware {
66
67
private @ Nullable Field fieldSpec = null ;
67
68
private Sort sort = Sort .unsorted ();
68
69
private long skip ;
69
- private int limit ;
70
+ private Limit limit = Limit . unlimited () ;
70
71
71
72
private KeysetScrollPosition keysetScrollPosition ;
72
73
private @ Nullable ReadConcern readConcern ;
@@ -155,10 +156,30 @@ public Query skip(long skip) {
155
156
* @return this.
156
157
*/
157
158
public Query limit (int limit ) {
158
- this .limit = limit ;
159
+ this .limit = limit > 0 ? Limit . of ( limit ) : Limit . unlimited () ;
159
160
return this ;
160
161
}
161
162
163
+ /**
164
+ * Limit the number of returned documents to {@link Limit}.
165
+ *
166
+ * @param limit number of documents to return.
167
+ * @return this.
168
+ * @since 4.2
169
+ */
170
+ public Query limit (Limit limit ) {
171
+
172
+ Assert .notNull (limit , "Limit must not be null" );
173
+
174
+ if (limit .isUnlimited ()) {
175
+ this .limit = limit ;
176
+ return this ;
177
+ }
178
+
179
+ // retain zero/negative semantics for unlimited.
180
+ return limit (limit .max ());
181
+ }
182
+
162
183
/**
163
184
* Configures the query to use the given hint when being executed. The {@code hint} can either be an index name or a
164
185
* json {@link Document} representation.
@@ -254,7 +275,7 @@ public Query with(Pageable pageable) {
254
275
return this ;
255
276
}
256
277
257
- this .limit = pageable .getPageSize ();
278
+ this .limit = pageable .toLimit ();
258
279
this .skip = pageable .getOffset ();
259
280
260
281
return with (pageable .getSort ());
@@ -457,7 +478,7 @@ public long getSkip() {
457
478
* @since 4.1
458
479
*/
459
480
public boolean isLimited () {
460
- return this .limit > 0 ;
481
+ return this .limit . isLimited () ;
461
482
}
462
483
463
484
/**
@@ -468,7 +489,7 @@ public boolean isLimited() {
468
489
* @see #isLimited()
469
490
*/
470
491
public int getLimit () {
471
- return this .limit ;
492
+ return limit . isUnlimited () ? 0 : this .limit . max () ;
472
493
}
473
494
474
495
/**
@@ -683,7 +704,8 @@ public boolean isSorted() {
683
704
};
684
705
685
706
target .skip = source .getSkip ();
686
- target .limit = source .getLimit ();
707
+
708
+ target .limit = source .isLimited () ? Limit .of (source .getLimit ()) : Limit .unlimited ();
687
709
target .hint = source .getHint ();
688
710
target .collation = source .getCollation ();
689
711
target .restrictedTypes = new HashSet <>(source .getRestrictedTypes ());
@@ -746,7 +768,7 @@ public int hashCode() {
746
768
result += 31 * nullSafeHashCode (sort );
747
769
result += 31 * nullSafeHashCode (hint );
748
770
result += 31 * skip ;
749
- result += 31 * limit ;
771
+ result += 31 * limit . hashCode () ;
750
772
result += 31 * nullSafeHashCode (meta );
751
773
result += 31 * nullSafeHashCode (collation .orElse (null ));
752
774
0 commit comments