Skip to content

Emit items in order when scrolling backwards using WindowIterator. #2858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>3.2.0-SNAPSHOT</version>
<version>3.2.x-2857-SNAPSHOT</version>

<name>Spring Data Core</name>
<description>Core Spring concepts underpinning every Spring Data module.</description>
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/springframework/data/support/WindowIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.support;

import java.util.Collections;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.Function;
Expand Down Expand Up @@ -81,7 +82,9 @@ public boolean hasNext() {

if (currentIterator == null) {
if (currentWindow != null) {
currentIterator = currentWindow.iterator();
currentIterator = isBackwardsScrolling(currentPosition)
? currentWindow.stream().sorted(Collections.reverseOrder()).iterator()
: currentWindow.iterator();
}
}

Expand Down Expand Up @@ -116,15 +119,17 @@ public T next() {

private static ScrollPosition getNextPosition(ScrollPosition currentPosition, Window<?> window) {

if (currentPosition instanceof KeysetScrollPosition ksp) {
if (ksp.scrollsBackward()) {
return window.positionAt(0);
}
if (isBackwardsScrolling(currentPosition)) {
return window.positionAt(0);
}

return window.positionAt(window.size() - 1);
}

private static boolean isBackwardsScrolling(ScrollPosition position) {
return position instanceof KeysetScrollPosition ksp ? ksp.scrollsBackward() : false;
}

/**
* Builder API to construct a {@link WindowIterator}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void allowsToIterateAllWindows() {
assertThat(capturedResult).containsExactly("a", "b", "c", "d");
}

@Test // GH-2151
@Test // GH-2151, GH-2857
void considersBackwardKeysetScrolling() {

Window<String> initial = Window.from(List.of("c", "d"),
Expand All @@ -152,6 +152,6 @@ void considersBackwardKeysetScrolling() {
}).startingAt(ScrollPosition.keyset().backward());

List<String> items = Streamable.of(() -> iterator).toList();
assertThat(items).containsExactly("c", "d", "a", "b");
assertThat(items).containsExactly("d", "c", "b", "a");
}
}