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.
This commit is contained in:
Oliver Drotbohm
2019-01-28 11:03:05 +01:00
parent b5972257af
commit 8c75a92b24
2 changed files with 37 additions and 0 deletions

View File

@@ -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> {
* method.
*
* @return
* @see #previousPageable()
*/
Pageable previousPageable();
@@ -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();
}
}

View File

@@ -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);
}
}