Browse Source

DATACMNS-1476 - Added Slice.nextOrLastPageable() and ….previousOrFirstPageable().

We now expose methods that allow to traverse the Pageables but make sure we stay within the result bounds.

Related ticket: DATACMNS-1475.
pull/336/head
Oliver Drotbohm 7 years ago
parent
commit
8c75a92b24
  1. 24
      src/main/java/org/springframework/data/domain/Slice.java
  2. 13
      src/test/java/org/springframework/data/domain/PageImplUnitTests.java

24
src/main/java/org/springframework/data/domain/Slice.java

@ -115,6 +115,7 @@ public interface Slice<T> extends Streamable<T> { @@ -115,6 +115,7 @@ public interface Slice<T> extends Streamable<T> {
* current {@link Slice} is already the last one. Clients should check {@link #hasNext()} before calling this method.
*
* @return
* @see #nextOrLastPageable()
*/
Pageable nextPageable();
@ -124,6 +125,7 @@ public interface Slice<T> extends Streamable<T> { @@ -124,6 +125,7 @@ public interface Slice<T> extends Streamable<T> {
* method.
*
* @return
* @see #previousPageable()
*/
Pageable previousPageable();
@ -135,4 +137,26 @@ public interface Slice<T> extends Streamable<T> { @@ -135,4 +137,26 @@ public interface Slice<T> extends Streamable<T> {
* @since 1.10
*/
<U> Slice<U> map(Function<? super T, ? extends U> converter);
/**
* Returns the {@link Pageable} describing the next slice or the one describing the current slice in case it's the
* last one.
*
* @return
* @since 2.2
*/
default Pageable nextOrLastPageable() {
return hasNext() ? nextPageable() : getPageable();
}
/**
* Returns the {@link Pageable} describing the previous slice or the one describing the current slice in case it's the
* first one.
*
* @return
* @since 2.2
*/
default Pageable previousOrFirstPageable() {
return hasPrevious() ? previousPageable() : getPageable();
}
}

13
src/test/java/org/springframework/data/domain/PageImplUnitTests.java

@ -161,4 +161,17 @@ public class PageImplUnitTests { @@ -161,4 +161,17 @@ public class PageImplUnitTests {
assertThat(new PageImpl<>(Collections.<String> emptyList(), PageRequest.of(1, 10), 0).getTotalElements())
.isEqualTo(0L);
}
@Test // DATACMNS-1476
public void returnsSelfPagablesIfThePageIsAlreadyTheFirstOrLastOne() {
Pageable pageable = PageRequest.of(0, 2);
Slice<String> page = new PageImpl<>(Arrays.asList("foo", "bar"), pageable, 2);
assertThat(page.previousPageable()).isEqualTo(Pageable.unpaged());
assertThat(page.previousOrFirstPageable()).isEqualTo(pageable);
assertThat(page.nextPageable()).isEqualTo(Pageable.unpaged());
assertThat(page.nextOrLastPageable()).isEqualTo(pageable);
}
}

Loading…
Cancel
Save