DATACMNS-23 - Made PageImpl safe to take an empty collection.
Added hasContent() method to Page interface.
This commit is contained in:
@@ -115,6 +115,14 @@ public interface Page<T> extends Iterable<T> {
|
||||
* @return
|
||||
*/
|
||||
List<T> getContent();
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the {@link Page} has content at all.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
boolean hasContent();
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,10 +49,7 @@ public class PageImpl<T> implements Page<T> {
|
||||
|
||||
this.content.addAll(content);
|
||||
this.total = total;
|
||||
|
||||
this.pageable =
|
||||
null == pageable ? new PageRequest(0, content.size())
|
||||
: pageable;
|
||||
this.pageable = pageable;
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +72,7 @@ public class PageImpl<T> implements Page<T> {
|
||||
*/
|
||||
public int getNumber() {
|
||||
|
||||
return pageable.getPageNumber();
|
||||
return pageable == null ? 0 : pageable.getPageNumber();
|
||||
}
|
||||
|
||||
|
||||
@@ -86,7 +83,7 @@ public class PageImpl<T> implements Page<T> {
|
||||
*/
|
||||
public int getSize() {
|
||||
|
||||
return pageable.getPageSize();
|
||||
return pageable == null ? 0 : pageable.getPageSize();
|
||||
}
|
||||
|
||||
|
||||
@@ -97,7 +94,7 @@ public class PageImpl<T> implements Page<T> {
|
||||
*/
|
||||
public int getTotalPages() {
|
||||
|
||||
return (int) Math.ceil((double) total / (double) getSize());
|
||||
return getSize() == 0 ? 0 : (int) Math.ceil((double) total / (double) getSize());
|
||||
}
|
||||
|
||||
|
||||
@@ -187,6 +184,17 @@ public class PageImpl<T> implements Page<T> {
|
||||
|
||||
return Collections.unmodifiableList(content);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.domain.Page#hasContent()
|
||||
*/
|
||||
@Override
|
||||
public boolean hasContent() {
|
||||
|
||||
return !content.isEmpty();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
@@ -196,7 +204,7 @@ public class PageImpl<T> implements Page<T> {
|
||||
*/
|
||||
public Sort getSort() {
|
||||
|
||||
return pageable.getSort();
|
||||
return pageable == null ? null : pageable.getSort();
|
||||
}
|
||||
|
||||
|
||||
@@ -239,7 +247,7 @@ public class PageImpl<T> implements Page<T> {
|
||||
|
||||
boolean totalEqual = this.total == that.total;
|
||||
boolean contentEqual = this.content.equals(that.content);
|
||||
boolean pageableEqual = this.pageable.equals(that.pageable);
|
||||
boolean pageableEqual = this.pageable == null ? that.pageable == null : this.pageable.equals(that.pageable);
|
||||
|
||||
return totalEqual && contentEqual && pageableEqual;
|
||||
}
|
||||
@@ -256,7 +264,7 @@ public class PageImpl<T> implements Page<T> {
|
||||
int result = 17;
|
||||
|
||||
result = 31 * result + (int) (total ^ total >>> 32);
|
||||
result = 31 * result + pageable.hashCode();
|
||||
result = 31 * result + (pageable == null ? 0 : pageable.hashCode());
|
||||
result = 31 * result + content.hashCode();
|
||||
|
||||
return result;
|
||||
|
||||
@@ -16,9 +16,12 @@
|
||||
|
||||
package org.springframework.data.domain;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.data.domain.UnitTestUtils.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -78,4 +81,22 @@ public class PageImplUnitTests {
|
||||
|
||||
new PageImpl<Object>(null, null, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void createsPageForEmptyContentCorrectly() {
|
||||
List<String> list = Collections.emptyList();
|
||||
Page<String> page = new PageImpl<String>(list);
|
||||
assertThat(page.getContent(), is(list));
|
||||
assertThat(page.getNumber(), is(0));
|
||||
assertThat(page.getNumberOfElements(), is(0));
|
||||
assertThat(page.getSize(), is(0));
|
||||
assertThat(page.getSort(), is((Sort) null));
|
||||
assertThat(page.getTotalElements(), is(0L));
|
||||
assertThat(page.getTotalPages(), is(0));
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user