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:
Mark Paluch
2021-01-27 15:27:16 +01:00
parent 2db26d2765
commit 130db32194
7 changed files with 134 additions and 38 deletions

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}