Fix count and offset calculations for backwards pagination

Closes gh-873
This commit is contained in:
rstoyanchev
2024-01-19 21:19:09 +00:00
parent 5fcf803680
commit b7842ff72f
2 changed files with 16 additions and 15 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -81,6 +81,9 @@ public final class ScrollSubrange extends Subrange<ScrollPosition> {
* @since 1.2.4
*/
public static ScrollSubrange create(@Nullable ScrollPosition position, @Nullable Integer count, boolean forward) {
if (count != null && count < 0) {
count = null;
}
if (position instanceof OffsetScrollPosition offsetScrollPosition) {
return initFromOffsetPosition(offsetScrollPosition, count, forward);
}
@@ -96,18 +99,16 @@ public final class ScrollSubrange extends Subrange<ScrollPosition> {
OffsetScrollPosition position, @Nullable Integer count, boolean forward) {
if (!forward) {
if (count != null) {
if (position.getOffset() == 0) {
count = 0;
}
else if (count >= position.getOffset()) {
count = (int) (position.getOffset() - 1);
}
position = position.advanceBy(-count - 1);
int countOrZero = (count != null ? count : 0);
if (countOrZero < position.getOffset()) {
position = position.advanceBy(-countOrZero-1);
}
else {
count = (position.getOffset() > 0 ? (int) (position.getOffset() - 1) : 0);
position = null;
}
forward = true;
}
return new ScrollSubrange(position, count, forward, null);
return new ScrollSubrange(position, count, true, null);
}
private static ScrollSubrange initFromKeysetPosition(

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -91,7 +91,7 @@ public class ScrollSubrangeTests {
ScrollPosition position = ScrollPosition.offset(5);
ScrollSubrange subrange = ScrollSubrange.create(position, 10, false);
assertThat(((OffsetScrollPosition) subrange.position().get()).getOffset()).isEqualTo(0);
assertThat(subrange.position()).isNotPresent();
assertThat(subrange.count().getAsInt()).isEqualTo(4);
assertThat(subrange.forward()).isTrue();
}
@@ -101,7 +101,7 @@ public class ScrollSubrangeTests {
ScrollPosition position = ScrollPosition.offset(0);
ScrollSubrange subrange = ScrollSubrange.create(position, 10, false);
assertThat(((OffsetScrollPosition) subrange.position().get()).getOffset()).isEqualTo(0);
assertThat(subrange.position()).isNotPresent();
assertThat(subrange.count().getAsInt()).isEqualTo(0);
assertThat(subrange.forward()).isTrue();
}
@@ -111,7 +111,7 @@ public class ScrollSubrangeTests {
ScrollPosition position = ScrollPosition.offset(30);
ScrollSubrange subrange = ScrollSubrange.create(position, null, false);
assertThat(((OffsetScrollPosition) subrange.position().get())).isEqualTo(position);
assertThat(subrange.position()).hasValue(ScrollPosition.offset(29));
assertThat(subrange.count()).isNotPresent();
assertThat(subrange.forward()).isTrue();
}