Skip to content

Commit 130db32

Browse files
committed
Polishing.
Introduce factory methods on Pageable, PageRequest, and QPageRequest to construct PageRequest objects. Replace builder with with…(…) methods. Closes #322.
1 parent 2db26d2 commit 130db32

File tree

7 files changed

+134
-38
lines changed

7 files changed

+134
-38
lines changed

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

+46-27
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*
2525
* @author Oliver Gierke
2626
* @author Thomas Darimont
27+
* @author Anastasiia Smirnova
28+
* @author Mark Paluch
2729
*/
2830
public class PageRequest extends AbstractPageRequest {
2931

@@ -83,8 +85,15 @@ public static PageRequest of(int page, int size, Direction direction, String...
8385
return of(page, size, Sort.by(direction, properties));
8486
}
8587

86-
public static PageRequestBuilder pageRequest() {
87-
return new PageRequestBuilder();
88+
/**
89+
* Creates a new {@link PageRequest} for the first page (page number {@code 0}) given {@code pageSize} .
90+
*
91+
* @param pageSize the size of the page to be returned, must be greater than 0.
92+
* @return a new {@link PageRequest}.
93+
* @since 2.5
94+
*/
95+
public static PageRequest ofSize(int pageSize) {
96+
return PageRequest.of(0, pageSize);
8897
}
8998

9099
/*
@@ -142,6 +151,41 @@ public boolean equals(@Nullable Object obj) {
142151
return super.equals(that) && this.sort.equals(that.sort);
143152
}
144153

154+
/**
155+
* Creates a new {@link PageRequest} with {@code pageNumber} applied.
156+
*
157+
* @param pageNumber
158+
* @return a new {@link PageRequest}.
159+
* @since 2.5
160+
*/
161+
@Override
162+
public PageRequest withPage(int pageNumber) {
163+
return new PageRequest(pageNumber, getPageSize(), getSort());
164+
}
165+
166+
/**
167+
* Creates a new {@link PageRequest} with {@link Direction} and {@code properties} applied.
168+
*
169+
* @param direction must not be {@literal null}.
170+
* @param properties must not be {@literal null}.
171+
* @return a new {@link PageRequest}.
172+
* @since 2.5
173+
*/
174+
public PageRequest withSort(Direction direction, String... properties) {
175+
return new PageRequest(getPageNumber(), getPageSize(), Sort.by(direction, properties));
176+
}
177+
178+
/**
179+
* Creates a new {@link PageRequest} with {@link Sort} applied.
180+
*
181+
* @param sort must not be {@literal null}.
182+
* @return a new {@link PageRequest}.
183+
* @since 2.5
184+
*/
185+
public PageRequest withSort(Sort sort) {
186+
return new PageRequest(getPageNumber(), getPageSize(), sort);
187+
}
188+
145189
/*
146190
* (non-Javadoc)
147191
* @see java.lang.Object#hashCode()
@@ -160,29 +204,4 @@ public String toString() {
160204
return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(), sort);
161205
}
162206

163-
public static final class PageRequestBuilder {
164-
165-
private Sort sort = Sort.unsorted();
166-
private int page;
167-
private int size;
168-
169-
public PageRequestBuilder sort(Sort sort) {
170-
this.sort = sort;
171-
return this;
172-
}
173-
174-
public PageRequestBuilder page(int page) {
175-
this.page = page;
176-
return this;
177-
}
178-
179-
public PageRequestBuilder size(int size) {
180-
this.size = size;
181-
return this;
182-
}
183-
184-
public PageRequest build() {
185-
return PageRequest.of(page, size, sort);
186-
}
187-
}
188207
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Abstract interface for pagination information.
2424
*
2525
* @author Oliver Gierke
26+
* @author Mark Paluch
2627
*/
2728
public interface Pageable {
2829

@@ -35,6 +36,17 @@ static Pageable unpaged() {
3536
return Unpaged.INSTANCE;
3637
}
3738

39+
/**
40+
* Creates a new {@link Pageable} for the first page (page number {@code 0}) given {@code pageSize} .
41+
*
42+
* @param pageSize the size of the page to be returned, must be greater than 0.
43+
* @return a new {@link Pageable}.
44+
* @since 2.5
45+
*/
46+
static Pageable ofSize(int pageSize) {
47+
return PageRequest.of(0, pageSize);
48+
}
49+
3850
/**
3951
* Returns whether the current {@link Pageable} contains pagination information.
4052
*
@@ -115,6 +127,15 @@ default Sort getSortOr(Sort sort) {
115127
*/
116128
Pageable first();
117129

130+
/**
131+
* Creates a new {@link Pageable} with {@code pageNumber} applied.
132+
*
133+
* @param pageNumber
134+
* @return a new {@link PageRequest}.
135+
* @since 2.5
136+
*/
137+
Pageable withPage(int pageNumber);
138+
118139
/**
119140
* Returns whether there's a previous {@link Pageable} we can access from the current one. Will return
120141
* {@literal false} in case the current {@link Pageable} already refers to the first page.
@@ -131,4 +152,5 @@ default Sort getSortOr(Sort sort) {
131152
default Optional<Pageable> toOptional() {
132153
return isUnpaged() ? Optional.empty() : Optional.of(this);
133154
}
155+
134156
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,19 @@ public long getOffset() {
104104
public Pageable first() {
105105
return this;
106106
}
107+
108+
/*
109+
* (non-Javadoc)
110+
* @see org.springframework.data.domain.Pageable#withPage(int)
111+
*/
112+
@Override
113+
public Pageable withPage(int pageNumber) {
114+
115+
if (pageNumber == 0) {
116+
return this;
117+
}
118+
119+
throw new UnsupportedOperationException();
120+
}
121+
107122
}

src/main/java/org/springframework/data/querydsl/QPageRequest.java

+36
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.querydsl;
1717

1818
import org.springframework.data.domain.AbstractPageRequest;
19+
import org.springframework.data.domain.PageRequest;
1920
import org.springframework.data.domain.Pageable;
2021
import org.springframework.data.domain.Sort;
2122
import org.springframework.util.Assert;
@@ -27,6 +28,7 @@
2728
*
2829
* @author Thomas Darimont
2930
* @author Oliver Drotbohm
31+
* @author Mark Paluch
3032
*/
3133
public class QPageRequest extends AbstractPageRequest {
3234

@@ -114,6 +116,17 @@ public static QPageRequest of(int page, int size, QSort sort) {
114116
return new QPageRequest(page, size, sort);
115117
}
116118

119+
/**
120+
* Creates a new {@link QPageRequest} for the first page (page number {@code 0}) given {@code pageSize} .
121+
*
122+
* @param pageSize the size of the page to be returned, must be greater than 0.
123+
* @return a new {@link QPageRequest}.
124+
* @since 2.5
125+
*/
126+
public static QPageRequest ofSize(int pageSize) {
127+
return QPageRequest.of(0, pageSize);
128+
}
129+
117130
/*
118131
* (non-Javadoc)
119132
* @see org.springframework.data.domain.Pageable#getSort()
@@ -149,4 +162,27 @@ public Pageable previous() {
149162
public Pageable first() {
150163
return QPageRequest.of(0, getPageSize(), sort);
151164
}
165+
166+
/**
167+
* Creates a new {@link QPageRequest} with {@code pageNumber} applied.
168+
*
169+
* @param pageNumber
170+
* @return a new {@link PageRequest}.
171+
* @since 2.5
172+
*/
173+
@Override
174+
public QPageRequest withPage(int pageNumber) {
175+
return new QPageRequest(pageNumber, getPageSize(), sort);
176+
}
177+
178+
/**
179+
* Creates a new {@link QPageRequest} with {@link QSort} applied.
180+
*
181+
* @param sort must not be {@literal null}.
182+
* @return a new {@link PageRequest}.
183+
* @since 2.5
184+
*/
185+
public QPageRequest withSort(QSort sort) {
186+
return new QPageRequest(getPageNumber(), getPageSize(), sort);
187+
}
152188
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void navigatesPageablesCorrectly() {
4949
Pageable first = request.previousOrFirst();
5050

5151
assertThat(first.hasPrevious()).isFalse();
52-
assertThat(first).isEqualTo((Pageable) newPageRequest(0, 10));
52+
assertThat(first).isEqualTo(newPageRequest(0, 10));
5353
assertThat(first).isEqualTo(request.first());
5454
assertThat(first.previousOrFirst()).isEqualTo(first);
5555
}

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,18 @@
1616
package org.springframework.data.domain;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.springframework.data.domain.PageRequest.pageRequest;
2019
import static org.springframework.data.domain.UnitTestUtils.*;
2120

2221
import org.junit.jupiter.api.Test;
22+
2323
import org.springframework.data.domain.Sort.Direction;
2424

2525
/**
2626
* Unit test for {@link PageRequest}.
2727
*
2828
* @author Oliver Gierke
29+
* @author Anastasiia Smirnova
30+
* @author Mark Paluch
2931
*/
3032
class PageRequestUnitTests extends AbstractPageRequestUnitTests {
3133

@@ -38,30 +40,29 @@ public AbstractPageRequest newPageRequest(int page, int size) {
3840
return PageRequest.of(page, size);
3941
}
4042

41-
AbstractPageRequest newPageRequest(int page, int size, Sort sort) {
42-
return PageRequest.of(page, size, sort);
43-
}
44-
4543
@Test
4644
void equalsRegardsSortCorrectly() {
4745

4846
Sort sort = Sort.by(Direction.DESC, "foo");
49-
AbstractPageRequest request = pageRequest().page(0).size(10).sort(sort).build();
47+
AbstractPageRequest request = PageRequest.ofSize(10).withPage(1).withSort(sort);
5048

5149
// Equals itself
5250
assertEqualsAndHashcode(request, request);
5351

5452
// Equals another instance with same setup
55-
assertEqualsAndHashcode(request, PageRequest.of(0, 10, sort));
53+
assertEqualsAndHashcode(request, PageRequest.of(1, 10, sort));
54+
55+
// Equals another instance with same sort by properties
56+
assertEqualsAndHashcode(request, PageRequest.ofSize(10).withPage(1).withSort(Direction.DESC, "foo"));
5657

5758
// Equals without sort entirely
5859
assertEqualsAndHashcode(PageRequest.of(0, 10), PageRequest.of(0, 10));
5960

6061
// Is not equal to instance without sort
61-
assertNotEqualsAndHashcode(request, PageRequest.of(0, 10));
62+
assertNotEqualsAndHashcode(request, PageRequest.of(1, 10));
6263

6364
// Is not equal to instance with another sort
64-
assertNotEqualsAndHashcode(request, PageRequest.of(0, 10, Direction.ASC, "foo"));
65+
assertNotEqualsAndHashcode(request, PageRequest.of(1, 10, Direction.ASC, "foo"));
6566
}
6667

6768
@Test // DATACMNS-1581

src/test/java/org/springframework/data/querydsl/QPageRequestUnitTests.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
import org.springframework.data.domain.AbstractPageRequestUnitTests;
2424

2525
/**
26+
* Unit tests for {@link QPageRequest}.
27+
*
2628
* @author Thomas Darimont
29+
* @author Mark Paluch
2730
*/
2831
public class QPageRequestUnitTests extends AbstractPageRequestUnitTests {
2932

@@ -49,7 +52,7 @@ void constructsQPageRequestWithOrderSpecifiers() {
4952
void constructsQPageRequestWithQSort() {
5053

5154
QUser user = QUser.user;
52-
QPageRequest pageRequest = QPageRequest.of(0, 10, QSort.by(user.firstname.asc()));
55+
QPageRequest pageRequest = QPageRequest.ofSize(10).withSort(QSort.by(user.firstname.asc()));
5356

5457
assertThat(pageRequest.getSort()).isEqualTo(QSort.by(user.firstname.asc()));
5558
}

0 commit comments

Comments
 (0)