From 855194a13da1477f5152b8b117fdc77db60db087 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 10 Apr 2024 10:41:04 +0200 Subject: [PATCH] Polishing. Introduce method to obtain a position function from OffsetScrollPosition. Tweak documentation wording. See #3070 Original pull request: #3072 --- .../ROOT/pages/repositories/scrolling.adoc | 10 +++++----- .../data/domain/OffsetScrollPosition.java | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/main/antora/modules/ROOT/pages/repositories/scrolling.adoc b/src/main/antora/modules/ROOT/pages/repositories/scrolling.adoc index 8071dc91e8..0e79413e37 100644 --- a/src/main/antora/modules/ROOT/pages/repositories/scrolling.adoc +++ b/src/main/antora/modules/ROOT/pages/repositories/scrolling.adoc @@ -6,7 +6,7 @@ Scrolling consists of a stable sort, a scroll type (Offset- or Keyset-based scro You can define simple sorting expressions by using property names and define static result limiting using the xref:repositories/query-methods-details.adoc#repositories.limit-query-result[`Top` or `First` keyword] through query derivation. You can concatenate expressions to collect multiple criteria into one expression. -Scroll queries return a `Window` that allows obtaining the elements scroll position which can be used to fetch the next `Window` until your application has consumed the entire query result. +Scroll queries return a `Window` that allows obtaining the element's scroll position to fetch the next `Window` until your application has consumed the entire query result. Similar to consuming a Java `Iterator>` by obtaining the next batch of results, query result scrolling lets you access the a `ScrollPosition` through `Window.positionAt(...)`. [source,java] @@ -26,8 +26,8 @@ do { [NOTE] ==== The `ScrollPosition` identifies the exact position of an element with the entire query result. -Query execution treats the position parameter as _exclusive_, which means results will start _after_ the given position. -`ScrollPosition#offset` and `ScrollPosition#keyset()` as special incarnations of a `ScrollPosition` indicating the start of a scroll operation. +Query execution treats the position parameter _exclusive_, results will start _after_ the given position. +`ScrollPosition#offset()` and `ScrollPosition#keyset()` as special incarnations of a `ScrollPosition` indicating the start of a scroll operation. ==== `WindowIterator` provides a utility to simplify scrolling across ``Window``s by removing the need to check for the presence of a next `Window` and applying the `ScrollPosition`. @@ -69,8 +69,8 @@ WindowIterator users = WindowIterator.of(position -> repository.findFirst1 [CAUTION] ==== There is a difference between `ScollPosition.offset()` and `ScollPosition.offset(0L)`. -The former indicates the start of scroll operation, pointing to no specific offset where as the latter identifies the first element (at position `0`) of the result. -Given the _exclusive_ nature of scrolling using `ScollPosition.offset(0)` will skip the first element and translate to an offset of 1. +The former indicates the start of scroll operation, pointing to no specific offset whereas the latter identifies the first element (at position `0`) of the result. +Given the _exclusive_ nature of scrolling, using `ScollPosition.offset(0)` skips the first element and translate to an offset of `1`. ==== [[repositories.scrolling.keyset]] diff --git a/src/main/java/org/springframework/data/domain/OffsetScrollPosition.java b/src/main/java/org/springframework/data/domain/OffsetScrollPosition.java index 926afc7d39..ff71fc01ea 100644 --- a/src/main/java/org/springframework/data/domain/OffsetScrollPosition.java +++ b/src/main/java/org/springframework/data/domain/OffsetScrollPosition.java @@ -24,7 +24,8 @@ /** * A {@link ScrollPosition} based on the offsets within query results. *

- * An initial {@link OffsetScrollPosition} does not point to a specific element and is different to a the Po + * An initial {@link OffsetScrollPosition} does not point to a specific element and is different to a position + * {{@link ScrollPosition#offset(long)}. * * @author Mark Paluch * @author Oliver Drotbohm @@ -69,7 +70,7 @@ static OffsetScrollPosition of(long offset) { } /** - * Returns the {@link IntFunction position function} to calculate. + * Returns a {@link IntFunction position function} starting at {@code startOffset}. * * @param startOffset the start offset to be used. Must not be negative. * @return the offset-based position function. @@ -81,6 +82,16 @@ public static IntFunction positionFunction(long startOffse return startOffset == 0 ? OffsetPositionFunction.ZERO : new OffsetPositionFunction(startOffset); } + /** + * Returns the {@link IntFunction position function} starting after the current {@code offset}. + * + * @return the offset-based position function. + * @since 3.3 + */ + public IntFunction positionFunction() { + return positionFunction(isInitial() ? 0 : getOffset() + 1); + } + /** * The zero or positive offset. *