Skip to content

Commit 62e14d2

Browse files
committed
DATACMNS-402 - Slight refinements in QSort implementation.
Re-enable concatenation with plain Sort via ….and(…) method. Some general polishing and JavaDoc cleanups. Original pull request: #59.
1 parent f2ff84f commit 62e14d2

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* Abstract Java Bean implementation of {@code Pageable}.
2222
*
2323
* @author Thomas Darimont
24+
* @author Oliver Gierke
2425
*/
2526
public abstract class AbstractPageRequest implements Pageable, Serializable {
2627

@@ -33,8 +34,8 @@ public abstract class AbstractPageRequest implements Pageable, Serializable {
3334
* Creates a new {@link AbstractPageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return
3435
* the first page.
3536
*
36-
* @param page
37-
* @param size
37+
* @param page must not be less than zero.
38+
* @param size must not be less than one.
3839
*/
3940
public AbstractPageRequest(int page, int size) {
4041

@@ -82,6 +83,14 @@ public boolean hasPrevious() {
8283
return page > 0;
8384
}
8485

86+
/*
87+
* (non-Javadoc)
88+
* @see org.springframework.data.domain.Pageable#previousOrFirst()
89+
*/
90+
public Pageable previousOrFirst() {
91+
return hasPrevious() ? previous() : first();
92+
}
93+
8594
/*
8695
* (non-Javadoc)
8796
* @see org.springframework.data.domain.Pageable#next()
@@ -101,24 +110,19 @@ public boolean hasPrevious() {
101110
*/
102111
public abstract Pageable first();
103112

104-
/*
105-
* (non-Javadoc)
106-
* @see org.springframework.data.domain.Pageable#previousOrFirst()
107-
*/
108-
public Pageable previousOrFirst() {
109-
return hasPrevious() ? previous() : first();
110-
}
111-
112113
/*
113114
* (non-Javadoc)
114115
* @see java.lang.Object#hashCode()
115116
*/
116117
@Override
117118
public int hashCode() {
119+
118120
final int prime = 31;
119121
int result = 1;
122+
120123
result = prime * result + page;
121124
result = prime * result + size;
125+
122126
return result;
123127
}
124128

@@ -128,17 +132,16 @@ public int hashCode() {
128132
*/
129133
@Override
130134
public boolean equals(Object obj) {
131-
if (this == obj)
135+
136+
if (this == obj) {
132137
return true;
133-
if (obj == null)
134-
return false;
135-
if (getClass() != obj.getClass())
138+
}
139+
140+
if (obj == null || getClass() != obj.getClass()) {
136141
return false;
142+
}
143+
137144
AbstractPageRequest other = (AbstractPageRequest) obj;
138-
if (page != other.page)
139-
return false;
140-
if (size != other.size)
141-
return false;
142-
return true;
145+
return this.page == other.page && this.size == other.size;
143146
}
144147
}

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static Order toOrder(OrderSpecifier<?> orderSpecifier) {
8686
Assert.notNull(orderSpecifier, "Order specifier must not be null!");
8787

8888
Expression<?> target = orderSpecifier.getTarget();
89-
Object targetElement = ((com.mysema.query.types.Path) target).getMetadata().getElement();
89+
Object targetElement = ((com.mysema.query.types.Path<?>) target).getMetadata().getElement();
9090

9191
Assert.notNull(targetElement, "Target element must not be null!");
9292

@@ -100,15 +100,6 @@ public List<OrderSpecifier<?>> getOrderSpecifiers() {
100100
return orderSpecifiers;
101101
}
102102

103-
/*
104-
* (non-Javadoc)
105-
* @see org.springframework.data.domain.Sort#and(org.springframework.data.domain.Sort)
106-
*/
107-
@Override
108-
public Sort and(Sort sort) {
109-
throw new UnsupportedOperationException("You cannot combine a querydsl based QSort with an arbitrary Sort!");
110-
}
111-
112103
/**
113104
* Returns a new {@link QSort} consisting of the {@link OrderSpecifier}s of the current {@code QSort} combined with
114105
* the ones from the given {@code QSort}.
@@ -147,7 +138,6 @@ public QSort and(List<OrderSpecifier<?>> orderSpecifiers) {
147138
public QSort and(OrderSpecifier<?>... orderSpecifiers) {
148139

149140
Assert.notEmpty(orderSpecifiers, "OrderSpecifiers must not be null or empty!");
150-
151141
return and(Arrays.asList(orderSpecifiers));
152142
}
153143
}

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515
*/
1616
package org.springframework.data.querydsl;
1717

18-
import static org.hamcrest.CoreMatchers.*;
18+
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020

2121
import java.util.List;
2222

23+
import org.hamcrest.Matchers;
2324
import org.junit.Test;
2425
import org.springframework.data.domain.Sort;
26+
import org.springframework.data.domain.Sort.Direction;
27+
import org.springframework.data.domain.Sort.Order;
2528

2629
import com.mysema.query.types.OrderSpecifier;
2730

2831
/**
32+
* Unit tests for {@link QSort}.
33+
*
2934
* @author Thomas Darimont
35+
* @author Oliver Gierke
3036
*/
3137
public class QSortUnitTests {
3238

@@ -126,4 +132,18 @@ public void ensureInteroperabilityWithSort() {
126132
assertThat(sort.getOrderFor("firstname"), is(new Sort.Order(Sort.Direction.ASC, "firstname")));
127133
assertThat(sort.getOrderFor("lastname"), is(new Sort.Order(Sort.Direction.DESC, "lastname")));
128134
}
135+
136+
/**
137+
* @see DATACMNS-402
138+
*/
139+
@Test
140+
public void concatenatesPlainSortCorrectly() {
141+
142+
QUser user = QUser.user;
143+
QSort sort = new QSort(user.firstname.asc());
144+
145+
Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
146+
assertThat(result, is(Matchers.<Order> iterableWithSize(2)));
147+
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, "firstname")));
148+
}
129149
}

0 commit comments

Comments
 (0)