Polishing.
Introduce factory methods on Pageable, PageRequest, and QPageRequest to construct PageRequest objects. Replace builder with with…(…) methods. Closes #322.
This commit is contained in:
@@ -24,6 +24,8 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Anastasiia Smirnova
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class PageRequest extends AbstractPageRequest {
|
||||
|
||||
@@ -83,8 +85,15 @@ public class PageRequest extends AbstractPageRequest {
|
||||
return of(page, size, Sort.by(direction, properties));
|
||||
}
|
||||
|
||||
public static PageRequestBuilder pageRequest() {
|
||||
return new PageRequestBuilder();
|
||||
/**
|
||||
* Creates a new {@link PageRequest} for the first page (page number {@code 0}) given {@code pageSize} .
|
||||
*
|
||||
* @param pageSize the size of the page to be returned, must be greater than 0.
|
||||
* @return a new {@link PageRequest}.
|
||||
* @since 2.5
|
||||
*/
|
||||
public static PageRequest ofSize(int pageSize) {
|
||||
return PageRequest.of(0, pageSize);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -142,6 +151,41 @@ public class PageRequest extends AbstractPageRequest {
|
||||
return super.equals(that) && this.sort.equals(that.sort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link PageRequest} with {@code pageNumber} applied.
|
||||
*
|
||||
* @param pageNumber
|
||||
* @return a new {@link PageRequest}.
|
||||
* @since 2.5
|
||||
*/
|
||||
@Override
|
||||
public PageRequest withPage(int pageNumber) {
|
||||
return new PageRequest(pageNumber, getPageSize(), getSort());
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link PageRequest} with {@link Direction} and {@code properties} applied.
|
||||
*
|
||||
* @param direction must not be {@literal null}.
|
||||
* @param properties must not be {@literal null}.
|
||||
* @return a new {@link PageRequest}.
|
||||
* @since 2.5
|
||||
*/
|
||||
public PageRequest withSort(Direction direction, String... properties) {
|
||||
return new PageRequest(getPageNumber(), getPageSize(), Sort.by(direction, properties));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link PageRequest} with {@link Sort} applied.
|
||||
*
|
||||
* @param sort must not be {@literal null}.
|
||||
* @return a new {@link PageRequest}.
|
||||
* @since 2.5
|
||||
*/
|
||||
public PageRequest withSort(Sort sort) {
|
||||
return new PageRequest(getPageNumber(), getPageSize(), sort);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Object#hashCode()
|
||||
@@ -160,29 +204,4 @@ public class PageRequest extends AbstractPageRequest {
|
||||
return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(), sort);
|
||||
}
|
||||
|
||||
public static final class PageRequestBuilder {
|
||||
|
||||
private Sort sort = Sort.unsorted();
|
||||
private int page;
|
||||
private int size;
|
||||
|
||||
public PageRequestBuilder sort(Sort sort) {
|
||||
this.sort = sort;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PageRequestBuilder page(int page) {
|
||||
this.page = page;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PageRequestBuilder size(int size) {
|
||||
this.size = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public PageRequest build() {
|
||||
return PageRequest.of(page, size, sort);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ import org.springframework.util.Assert;
|
||||
* Abstract interface for pagination information.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public interface Pageable {
|
||||
|
||||
@@ -35,6 +36,17 @@ public interface Pageable {
|
||||
return Unpaged.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link Pageable} for the first page (page number {@code 0}) given {@code pageSize} .
|
||||
*
|
||||
* @param pageSize the size of the page to be returned, must be greater than 0.
|
||||
* @return a new {@link Pageable}.
|
||||
* @since 2.5
|
||||
*/
|
||||
static Pageable ofSize(int pageSize) {
|
||||
return PageRequest.of(0, pageSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the current {@link Pageable} contains pagination information.
|
||||
*
|
||||
@@ -115,6 +127,15 @@ public interface Pageable {
|
||||
*/
|
||||
Pageable first();
|
||||
|
||||
/**
|
||||
* Creates a new {@link Pageable} with {@code pageNumber} applied.
|
||||
*
|
||||
* @param pageNumber
|
||||
* @return a new {@link PageRequest}.
|
||||
* @since 2.5
|
||||
*/
|
||||
Pageable withPage(int pageNumber);
|
||||
|
||||
/**
|
||||
* Returns whether there's a previous {@link Pageable} we can access from the current one. Will return
|
||||
* {@literal false} in case the current {@link Pageable} already refers to the first page.
|
||||
@@ -131,4 +152,5 @@ public interface Pageable {
|
||||
default Optional<Pageable> toOptional() {
|
||||
return isUnpaged() ? Optional.empty() : Optional.of(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -104,4 +104,19 @@ enum Unpaged implements Pageable {
|
||||
public Pageable first() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#withPage(int)
|
||||
*/
|
||||
@Override
|
||||
public Pageable withPage(int pageNumber) {
|
||||
|
||||
if (pageNumber == 0) {
|
||||
return this;
|
||||
}
|
||||
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.querydsl;
|
||||
|
||||
import org.springframework.data.domain.AbstractPageRequest;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -27,6 +28,7 @@ import com.querydsl.core.types.OrderSpecifier;
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @author Oliver Drotbohm
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class QPageRequest extends AbstractPageRequest {
|
||||
|
||||
@@ -114,6 +116,17 @@ public class QPageRequest extends AbstractPageRequest {
|
||||
return new QPageRequest(page, size, sort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link QPageRequest} for the first page (page number {@code 0}) given {@code pageSize} .
|
||||
*
|
||||
* @param pageSize the size of the page to be returned, must be greater than 0.
|
||||
* @return a new {@link QPageRequest}.
|
||||
* @since 2.5
|
||||
*/
|
||||
public static QPageRequest ofSize(int pageSize) {
|
||||
return QPageRequest.of(0, pageSize);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.domain.Pageable#getSort()
|
||||
@@ -149,4 +162,27 @@ public class QPageRequest extends AbstractPageRequest {
|
||||
public Pageable first() {
|
||||
return QPageRequest.of(0, getPageSize(), sort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link QPageRequest} with {@code pageNumber} applied.
|
||||
*
|
||||
* @param pageNumber
|
||||
* @return a new {@link PageRequest}.
|
||||
* @since 2.5
|
||||
*/
|
||||
@Override
|
||||
public QPageRequest withPage(int pageNumber) {
|
||||
return new QPageRequest(pageNumber, getPageSize(), sort);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link QPageRequest} with {@link QSort} applied.
|
||||
*
|
||||
* @param sort must not be {@literal null}.
|
||||
* @return a new {@link PageRequest}.
|
||||
* @since 2.5
|
||||
*/
|
||||
public QPageRequest withSort(QSort sort) {
|
||||
return new QPageRequest(getPageNumber(), getPageSize(), sort);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public abstract class AbstractPageRequestUnitTests {
|
||||
Pageable first = request.previousOrFirst();
|
||||
|
||||
assertThat(first.hasPrevious()).isFalse();
|
||||
assertThat(first).isEqualTo((Pageable) newPageRequest(0, 10));
|
||||
assertThat(first).isEqualTo(newPageRequest(0, 10));
|
||||
assertThat(first).isEqualTo(request.first());
|
||||
assertThat(first.previousOrFirst()).isEqualTo(first);
|
||||
}
|
||||
|
||||
@@ -16,16 +16,18 @@
|
||||
package org.springframework.data.domain;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.domain.PageRequest.pageRequest;
|
||||
import static org.springframework.data.domain.UnitTestUtils.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.data.domain.Sort.Direction;
|
||||
|
||||
/**
|
||||
* Unit test for {@link PageRequest}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Anastasiia Smirnova
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class PageRequestUnitTests extends AbstractPageRequestUnitTests {
|
||||
|
||||
@@ -38,30 +40,29 @@ class PageRequestUnitTests extends AbstractPageRequestUnitTests {
|
||||
return PageRequest.of(page, size);
|
||||
}
|
||||
|
||||
AbstractPageRequest newPageRequest(int page, int size, Sort sort) {
|
||||
return PageRequest.of(page, size, sort);
|
||||
}
|
||||
|
||||
@Test
|
||||
void equalsRegardsSortCorrectly() {
|
||||
|
||||
Sort sort = Sort.by(Direction.DESC, "foo");
|
||||
AbstractPageRequest request = pageRequest().page(0).size(10).sort(sort).build();
|
||||
AbstractPageRequest request = PageRequest.ofSize(10).withPage(1).withSort(sort);
|
||||
|
||||
// Equals itself
|
||||
assertEqualsAndHashcode(request, request);
|
||||
|
||||
// Equals another instance with same setup
|
||||
assertEqualsAndHashcode(request, PageRequest.of(0, 10, sort));
|
||||
assertEqualsAndHashcode(request, PageRequest.of(1, 10, sort));
|
||||
|
||||
// Equals another instance with same sort by properties
|
||||
assertEqualsAndHashcode(request, PageRequest.ofSize(10).withPage(1).withSort(Direction.DESC, "foo"));
|
||||
|
||||
// Equals without sort entirely
|
||||
assertEqualsAndHashcode(PageRequest.of(0, 10), PageRequest.of(0, 10));
|
||||
|
||||
// Is not equal to instance without sort
|
||||
assertNotEqualsAndHashcode(request, PageRequest.of(0, 10));
|
||||
assertNotEqualsAndHashcode(request, PageRequest.of(1, 10));
|
||||
|
||||
// Is not equal to instance with another sort
|
||||
assertNotEqualsAndHashcode(request, PageRequest.of(0, 10, Direction.ASC, "foo"));
|
||||
assertNotEqualsAndHashcode(request, PageRequest.of(1, 10, Direction.ASC, "foo"));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1581
|
||||
|
||||
@@ -23,7 +23,10 @@ import org.springframework.data.domain.AbstractPageRequest;
|
||||
import org.springframework.data.domain.AbstractPageRequestUnitTests;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link QPageRequest}.
|
||||
*
|
||||
* @author Thomas Darimont
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
public class QPageRequestUnitTests extends AbstractPageRequestUnitTests {
|
||||
|
||||
@@ -49,7 +52,7 @@ public class QPageRequestUnitTests extends AbstractPageRequestUnitTests {
|
||||
void constructsQPageRequestWithQSort() {
|
||||
|
||||
QUser user = QUser.user;
|
||||
QPageRequest pageRequest = QPageRequest.of(0, 10, QSort.by(user.firstname.asc()));
|
||||
QPageRequest pageRequest = QPageRequest.ofSize(10).withSort(QSort.by(user.firstname.asc()));
|
||||
|
||||
assertThat(pageRequest.getSort()).isEqualTo(QSort.by(user.firstname.asc()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user