3131
3232import org .bson .Document ;
3333import org .springframework .data .domain .KeysetScrollPosition ;
34+ import org .springframework .data .domain .Limit ;
3435import org .springframework .data .domain .OffsetScrollPosition ;
3536import org .springframework .data .domain .Pageable ;
3637import org .springframework .data .domain .ScrollPosition ;
@@ -66,7 +67,7 @@ public class Query implements ReadConcernAware, ReadPreferenceAware {
6667 private @ Nullable Field fieldSpec = null ;
6768 private Sort sort = Sort .unsorted ();
6869 private long skip ;
69- private int limit ;
70+ private Limit limit = Limit . unlimited () ;
7071
7172 private KeysetScrollPosition keysetScrollPosition ;
7273 private @ Nullable ReadConcern readConcern ;
@@ -155,10 +156,30 @@ public Query skip(long skip) {
155156 * @return this.
156157 */
157158 public Query limit (int limit ) {
158- this .limit = limit ;
159+ this .limit = limit > 0 ? Limit . of ( limit ) : Limit . unlimited () ;
159160 return this ;
160161 }
161162
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+
162183 /**
163184 * Configures the query to use the given hint when being executed. The {@code hint} can either be an index name or a
164185 * json {@link Document} representation.
@@ -254,7 +275,7 @@ public Query with(Pageable pageable) {
254275 return this ;
255276 }
256277
257- this .limit = pageable .getPageSize ();
278+ this .limit = pageable .toLimit ();
258279 this .skip = pageable .getOffset ();
259280
260281 return with (pageable .getSort ());
@@ -457,7 +478,7 @@ public long getSkip() {
457478 * @since 4.1
458479 */
459480 public boolean isLimited () {
460- return this .limit > 0 ;
481+ return this .limit . isLimited () ;
461482 }
462483
463484 /**
@@ -468,7 +489,7 @@ public boolean isLimited() {
468489 * @see #isLimited()
469490 */
470491 public int getLimit () {
471- return this .limit ;
492+ return limit . isUnlimited () ? 0 : this .limit . max () ;
472493 }
473494
474495 /**
@@ -683,7 +704,8 @@ public boolean isSorted() {
683704 };
684705
685706 target .skip = source .getSkip ();
686- target .limit = source .getLimit ();
707+
708+ target .limit = source .isLimited () ? Limit .of (source .getLimit ()) : Limit .unlimited ();
687709 target .hint = source .getHint ();
688710 target .collation = source .getCollation ();
689711 target .restrictedTypes = new HashSet <>(source .getRestrictedTypes ());
@@ -746,7 +768,7 @@ public int hashCode() {
746768 result += 31 * nullSafeHashCode (sort );
747769 result += 31 * nullSafeHashCode (hint );
748770 result += 31 * skip ;
749- result += 31 * limit ;
771+ result += 31 * limit . hashCode () ;
750772 result += 31 * nullSafeHashCode (meta );
751773 result += 31 * nullSafeHashCode (collation .orElse (null ));
752774
0 commit comments