DATACMNS-323 - PageImpl returns correct number of pages for use with content only.

So far, the PageImpl class returned 0 as total number of pages if set up from a plain List. This returns 1 now (as it should be). We've also adapted the hasNextPage() implementation to correctly calculate whether a next page is available.
This commit is contained in:
Oliver Gierke
2013-05-14 16:40:45 +02:00
parent 602834f86a
commit 70ce9a0908
2 changed files with 16 additions and 3 deletions

View File

@@ -84,7 +84,7 @@ public class PageImpl<T> implements Page<T>, Serializable {
* @see org.springframework.data.domain.Page#getTotalPages()
*/
public int getTotalPages() {
return getSize() == 0 ? 0 : (int) Math.ceil((double) total / (double) getSize());
return getSize() == 0 ? 1 : (int) Math.ceil((double) total / (double) getSize());
}
/*
@@ -124,7 +124,7 @@ public class PageImpl<T> implements Page<T>, Serializable {
* @see org.springframework.data.domain.Page#hasNextPage()
*/
public boolean hasNextPage() {
return (getNumber() + 1) * getSize() < total;
return getNumber() + 1 < getTotalPages();
}
/*

View File

@@ -106,11 +106,24 @@ public class PageImplUnitTests {
assertThat(page.getSize(), is(0));
assertThat(page.getSort(), is((Sort) null));
assertThat(page.getTotalElements(), is(0L));
assertThat(page.getTotalPages(), is(0));
assertThat(page.getTotalPages(), is(1));
assertThat(page.hasNextPage(), is(false));
assertThat(page.hasPreviousPage(), is(false));
assertThat(page.isFirstPage(), is(true));
assertThat(page.isLastPage(), is(true));
assertThat(page.hasContent(), is(false));
}
/**
* @see DATACMNS-323
*/
@Test
public void returnsCorrectTotalPages() {
Page<String> page = new PageImpl<String>(Arrays.asList("a"));
assertThat(page.getTotalPages(), is(1));
assertThat(page.hasNextPage(), is(false));
assertThat(page.hasPreviousPage(), is(false));
}
}