From 8c75a92b246dfe001caae71ed8263901ac448152 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 28 Jan 2019 11:03:05 +0100 Subject: [PATCH] =?UTF-8?q?DATACMNS-1476=20-=20Added=20Slice.nextOrLastPag?= =?UTF-8?q?eable()=20and=20=E2=80=A6.previousOrFirstPageable().?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now expose methods that allow to traverse the Pageables but make sure we stay within the result bounds. Related ticket: DATACMNS-1475. --- .../springframework/data/domain/Slice.java | 24 +++++++++++++++++++ .../data/domain/PageImplUnitTests.java | 13 ++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/main/java/org/springframework/data/domain/Slice.java b/src/main/java/org/springframework/data/domain/Slice.java index e4f73bbd7..9052d029e 100644 --- a/src/main/java/org/springframework/data/domain/Slice.java +++ b/src/main/java/org/springframework/data/domain/Slice.java @@ -115,6 +115,7 @@ public interface Slice extends Streamable { * 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 extends Streamable { * method. * * @return + * @see #previousPageable() */ Pageable previousPageable(); @@ -135,4 +137,26 @@ public interface Slice extends Streamable { * @since 1.10 */ Slice map(Function 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(); + } } diff --git a/src/test/java/org/springframework/data/domain/PageImplUnitTests.java b/src/test/java/org/springframework/data/domain/PageImplUnitTests.java index 5416b3ba3..27e37cc3b 100755 --- a/src/test/java/org/springframework/data/domain/PageImplUnitTests.java +++ b/src/test/java/org/springframework/data/domain/PageImplUnitTests.java @@ -161,4 +161,17 @@ public class PageImplUnitTests { assertThat(new PageImpl<>(Collections. emptyList(), PageRequest.of(1, 10), 0).getTotalElements()) .isEqualTo(0L); } + + @Test // DATACMNS-1476 + public void returnsSelfPagablesIfThePageIsAlreadyTheFirstOrLastOne() { + + Pageable pageable = PageRequest.of(0, 2); + Slice 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); + } }