Skip to content

Commit f2ff84f

Browse files
Thomas Darimontodrotbohm
Thomas Darimont
authored andcommitted
DATACMNS-402 - Add support for sorting by a QueryDSL OrderSpecifier.
Previously we did only support ordering by a string or property path through Sort. In order to be able to express more type safe ordering expressions we introduce QSort as a subclass of Sort that is able to wrap QueryDSL OrderSpecifiers. To be able to separate the concerns of sorting via QueryDSL expressions better we introduce the AbstractPageRequest base class and QPageRequest as a special implementation that accepts a QSort or OrderSpecifiers for ordering. Original pull request: #59.
1 parent 9f65b57 commit f2ff84f

File tree

11 files changed

+701
-127
lines changed

11 files changed

+701
-127
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.domain;
17+
18+
import java.io.Serializable;
19+
20+
/**
21+
* Abstract Java Bean implementation of {@code Pageable}.
22+
*
23+
* @author Thomas Darimont
24+
*/
25+
public abstract class AbstractPageRequest implements Pageable, Serializable {
26+
27+
private static final long serialVersionUID = 1232825578694716871L;
28+
29+
private final int page;
30+
private final int size;
31+
32+
/**
33+
* Creates a new {@link AbstractPageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return
34+
* the first page.
35+
*
36+
* @param page
37+
* @param size
38+
*/
39+
public AbstractPageRequest(int page, int size) {
40+
41+
if (page < 0) {
42+
throw new IllegalArgumentException("Page index must not be less than zero!");
43+
}
44+
45+
if (size < 1) {
46+
throw new IllegalArgumentException("Page size must not be less than one!");
47+
}
48+
49+
this.page = page;
50+
this.size = size;
51+
}
52+
53+
/*
54+
* (non-Javadoc)
55+
* @see org.springframework.data.domain.Pageable#getPageSize()
56+
*/
57+
public int getPageSize() {
58+
return size;
59+
}
60+
61+
/*
62+
* (non-Javadoc)
63+
* @see org.springframework.data.domain.Pageable#getPageNumber()
64+
*/
65+
public int getPageNumber() {
66+
return page;
67+
}
68+
69+
/*
70+
* (non-Javadoc)
71+
* @see org.springframework.data.domain.Pageable#getOffset()
72+
*/
73+
public int getOffset() {
74+
return page * size;
75+
}
76+
77+
/*
78+
* (non-Javadoc)
79+
* @see org.springframework.data.domain.Pageable#hasPrevious()
80+
*/
81+
public boolean hasPrevious() {
82+
return page > 0;
83+
}
84+
85+
/*
86+
* (non-Javadoc)
87+
* @see org.springframework.data.domain.Pageable#next()
88+
*/
89+
public abstract Pageable next();
90+
91+
/**
92+
* Returns the {@link Pageable} requesting the previous {@link Page}.
93+
*
94+
* @return
95+
*/
96+
public abstract Pageable previous();
97+
98+
/*
99+
* (non-Javadoc)
100+
* @see org.springframework.data.domain.Pageable#first()
101+
*/
102+
public abstract Pageable first();
103+
104+
/*
105+
* (non-Javadoc)
106+
* @see org.springframework.data.domain.Pageable#previousOrFirst()
107+
*/
108+
public Pageable previousOrFirst() {
109+
return hasPrevious() ? previous() : first();
110+
}
111+
112+
/*
113+
* (non-Javadoc)
114+
* @see java.lang.Object#hashCode()
115+
*/
116+
@Override
117+
public int hashCode() {
118+
final int prime = 31;
119+
int result = 1;
120+
result = prime * result + page;
121+
result = prime * result + size;
122+
return result;
123+
}
124+
125+
/*
126+
* (non-Javadoc)
127+
* @see java.lang.Object#equals(java.lang.Object)
128+
*/
129+
@Override
130+
public boolean equals(Object obj) {
131+
if (this == obj)
132+
return true;
133+
if (obj == null)
134+
return false;
135+
if (getClass() != obj.getClass())
136+
return false;
137+
AbstractPageRequest other = (AbstractPageRequest) obj;
138+
if (page != other.page)
139+
return false;
140+
if (size != other.size)
141+
return false;
142+
return true;
143+
}
144+
}

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

Lines changed: 12 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,18 @@
1515
*/
1616
package org.springframework.data.domain;
1717

18-
import java.io.Serializable;
19-
2018
import org.springframework.data.domain.Sort.Direction;
2119

2220
/**
2321
* Basic Java Bean implementation of {@code Pageable}.
2422
*
2523
* @author Oliver Gierke
24+
* @author Thomas Darimont
2625
*/
27-
public class PageRequest implements Pageable, Serializable {
26+
public class PageRequest extends AbstractPageRequest {
2827

29-
private static final long serialVersionUID = 8280485938848398236L;
28+
private static final long serialVersionUID = -4541509938956089562L;
3029

31-
private final int page;
32-
private final int size;
3330
private final Sort sort;
3431

3532
/**
@@ -63,45 +60,10 @@ public PageRequest(int page, int size, Direction direction, String... properties
6360
* @param sort can be {@literal null}.
6461
*/
6562
public PageRequest(int page, int size, Sort sort) {
66-
67-
if (page < 0) {
68-
throw new IllegalArgumentException("Page index must not be less than zero!");
69-
}
70-
71-
if (size < 1) {
72-
throw new IllegalArgumentException("Page size must not be less than one!");
73-
}
74-
75-
this.page = page;
76-
this.size = size;
63+
super(page, size);
7764
this.sort = sort;
7865
}
7966

80-
/*
81-
* (non-Javadoc)
82-
* @see org.springframework.data.domain.Pageable#getPageSize()
83-
*/
84-
public int getPageSize() {
85-
86-
return size;
87-
}
88-
89-
/*
90-
* (non-Javadoc)
91-
* @see org.springframework.data.domain.Pageable#getPageNumber()
92-
*/
93-
public int getPageNumber() {
94-
return page;
95-
}
96-
97-
/*
98-
* (non-Javadoc)
99-
* @see org.springframework.data.domain.Pageable#getOffset()
100-
*/
101-
public int getOffset() {
102-
return page * size;
103-
}
104-
10567
/*
10668
* (non-Javadoc)
10769
* @see org.springframework.data.domain.Pageable#getSort()
@@ -110,36 +72,28 @@ public Sort getSort() {
11072
return sort;
11173
}
11274

113-
/*
114-
* (non-Javadoc)
115-
* @see org.springframework.data.domain.Pageable#hasPrevious()
116-
*/
117-
public boolean hasPrevious() {
118-
return page > 0;
119-
}
120-
12175
/*
12276
* (non-Javadoc)
12377
* @see org.springframework.data.domain.Pageable#next()
12478
*/
12579
public Pageable next() {
126-
return new PageRequest(page + 1, size, sort);
80+
return new PageRequest(getPageNumber() + 1, getPageSize(), getSort());
12781
}
12882

12983
/*
13084
* (non-Javadoc)
131-
* @see org.springframework.data.domain.Pageable#previousOrFirst()
85+
* @see org.springframework.data.domain.AbstractPageRequest#previous()
13286
*/
133-
public Pageable previousOrFirst() {
134-
return hasPrevious() ? new PageRequest(page - 1, size, sort) : this;
87+
public PageRequest previous() {
88+
return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());
13589
}
13690

13791
/*
13892
* (non-Javadoc)
13993
* @see org.springframework.data.domain.Pageable#first()
14094
*/
14195
public Pageable first() {
142-
return new PageRequest(0, size, sort);
96+
return new PageRequest(0, getPageSize(), getSort());
14397
}
14498

14599
/*
@@ -159,12 +113,9 @@ public boolean equals(final Object obj) {
159113

160114
PageRequest that = (PageRequest) obj;
161115

162-
boolean pageEqual = this.page == that.page;
163-
boolean sizeEqual = this.size == that.size;
164-
165116
boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort);
166117

167-
return pageEqual && sizeEqual && sortEqual;
118+
return super.equals(that) && sortEqual;
168119
}
169120

170121
/*
@@ -173,14 +124,7 @@ public boolean equals(final Object obj) {
173124
*/
174125
@Override
175126
public int hashCode() {
176-
177-
int result = 17;
178-
179-
result = 31 * result + page;
180-
result = 31 * result + size;
181-
result = 31 * result + (null == sort ? 0 : sort.hashCode());
182-
183-
return result;
127+
return 31 * super.hashCode() + (null == sort ? 0 : sort.hashCode());
184128
}
185129

186130
/*
@@ -189,7 +133,7 @@ public int hashCode() {
189133
*/
190134
@Override
191135
public String toString() {
192-
return String.format("Page request [number: %d, size %d, sort: %s]", page, size,
136+
return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(),
193137
sort == null ? null : sort.toString());
194138
}
195139
}

0 commit comments

Comments
 (0)