Skip to content

Commit 70ce9a0

Browse files
committed
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.
1 parent 602834f commit 70ce9a0

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/main/java/org/springframework/data/domain/PageImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public int getSize() {
8484
* @see org.springframework.data.domain.Page#getTotalPages()
8585
*/
8686
public int getTotalPages() {
87-
return getSize() == 0 ? 0 : (int) Math.ceil((double) total / (double) getSize());
87+
return getSize() == 0 ? 1 : (int) Math.ceil((double) total / (double) getSize());
8888
}
8989

9090
/*
@@ -124,7 +124,7 @@ public boolean isFirstPage() {
124124
* @see org.springframework.data.domain.Page#hasNextPage()
125125
*/
126126
public boolean hasNextPage() {
127-
return (getNumber() + 1) * getSize() < total;
127+
return getNumber() + 1 < getTotalPages();
128128
}
129129

130130
/*

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,24 @@ public void createsPageForEmptyContentCorrectly() {
106106
assertThat(page.getSize(), is(0));
107107
assertThat(page.getSort(), is((Sort) null));
108108
assertThat(page.getTotalElements(), is(0L));
109-
assertThat(page.getTotalPages(), is(0));
109+
assertThat(page.getTotalPages(), is(1));
110110
assertThat(page.hasNextPage(), is(false));
111111
assertThat(page.hasPreviousPage(), is(false));
112112
assertThat(page.isFirstPage(), is(true));
113113
assertThat(page.isLastPage(), is(true));
114114
assertThat(page.hasContent(), is(false));
115115
}
116+
117+
/**
118+
* @see DATACMNS-323
119+
*/
120+
@Test
121+
public void returnsCorrectTotalPages() {
122+
123+
Page<String> page = new PageImpl<String>(Arrays.asList("a"));
124+
125+
assertThat(page.getTotalPages(), is(1));
126+
assertThat(page.hasNextPage(), is(false));
127+
assertThat(page.hasPreviousPage(), is(false));
128+
}
116129
}

0 commit comments

Comments
 (0)