Consider KeysetScrollPosition direction in WindowIterator.

We now consider the scroll direction in the iterator to properly continue Keyset backward scrolling.

Closes #2851
This commit is contained in:
Mark Paluch
2023-06-13 11:50:28 +02:00
parent 487795dcee
commit 05d68a0eab
2 changed files with 40 additions and 1 deletions

View File

@@ -19,6 +19,7 @@ import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Function;
import org.springframework.data.domain.KeysetScrollPosition;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Window;
import org.springframework.lang.Nullable;
@@ -92,7 +93,7 @@ public class WindowIterator<T> implements Iterator<T> {
if (currentWindow != null && currentWindow.hasNext()) {
currentPosition = currentWindow.positionAt(currentWindow.size() - 1);
currentPosition = getNextPosition(currentPosition, currentWindow);
currentIterator = null;
currentWindow = null;
continue;
@@ -113,6 +114,17 @@ public class WindowIterator<T> implements Iterator<T> {
return currentIterator.next();
}
private static ScrollPosition getNextPosition(ScrollPosition currentPosition, Window<?> window) {
if (currentPosition instanceof KeysetScrollPosition ksp) {
if (ksp.scrollsBackward()) {
return window.positionAt(0);
}
}
return window.positionAt(window.size() - 1);
}
/**
* Builder API to construct a {@link WindowIterator}.
*