Browse Source

DATACMNS-23 - Made PageImpl safe to take an empty collection.

Added hasContent() method to Page interface.
pull/2/head
Oliver Gierke 15 years ago
parent
commit
4b51c5fd1f
  1. 8
      spring-data-commons-core/src/main/java/org/springframework/data/domain/Page.java
  2. 28
      spring-data-commons-core/src/main/java/org/springframework/data/domain/PageImpl.java
  3. 21
      spring-data-commons-core/src/test/java/org/springframework/data/domain/PageImplUnitTests.java

8
spring-data-commons-core/src/main/java/org/springframework/data/domain/Page.java

@ -115,6 +115,14 @@ public interface Page<T> extends Iterable<T> {
* @return * @return
*/ */
List<T> getContent(); List<T> getContent();
/**
* Returns whether the {@link Page} has content at all.
*
* @return
*/
boolean hasContent();
/** /**

28
spring-data-commons-core/src/main/java/org/springframework/data/domain/PageImpl.java

@ -49,10 +49,7 @@ public class PageImpl<T> implements Page<T> {
this.content.addAll(content); this.content.addAll(content);
this.total = total; this.total = total;
this.pageable = pageable;
this.pageable =
null == pageable ? new PageRequest(0, content.size())
: pageable;
} }
@ -75,7 +72,7 @@ public class PageImpl<T> implements Page<T> {
*/ */
public int getNumber() { 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() { 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() { 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); 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() { 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 totalEqual = this.total == that.total;
boolean contentEqual = this.content.equals(that.content); 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; return totalEqual && contentEqual && pageableEqual;
} }
@ -256,7 +264,7 @@ public class PageImpl<T> implements Page<T> {
int result = 17; int result = 17;
result = 31 * result + (int) (total ^ total >>> 32); 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(); result = 31 * result + content.hashCode();
return result; return result;

21
spring-data-commons-core/src/test/java/org/springframework/data/domain/PageImplUnitTests.java

@ -16,9 +16,12 @@
package org.springframework.data.domain; package org.springframework.data.domain;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.UnitTestUtils.*; import static org.springframework.data.domain.UnitTestUtils.*;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
@ -78,4 +81,22 @@ public class PageImplUnitTests {
new PageImpl<Object>(null, null, 0); 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));
}
} }

Loading…
Cancel
Save