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.
This commit is contained in:
Oliver Gierke
2013-12-04 13:30:19 +01:00
parent f2ff84fb63
commit 62e14d2b12
3 changed files with 44 additions and 31 deletions

View File

@@ -21,6 +21,7 @@ import java.io.Serializable;
* Abstract Java Bean implementation of {@code Pageable}.
*
* @author Thomas Darimont
* @author Oliver Gierke
*/
public abstract class AbstractPageRequest implements Pageable, Serializable {
@@ -33,8 +34,8 @@ public abstract class AbstractPageRequest implements Pageable, Serializable {
* Creates a new {@link AbstractPageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return
* the first page.
*
* @param page
* @param size
* @param page must not be less than zero.
* @param size must not be less than one.
*/
public AbstractPageRequest(int page, int size) {
@@ -82,6 +83,14 @@ public abstract class AbstractPageRequest implements Pageable, Serializable {
return page > 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#previousOrFirst()
*/
public Pageable previousOrFirst() {
return hasPrevious() ? previous() : first();
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#next()
@@ -101,24 +110,19 @@ public abstract class AbstractPageRequest implements Pageable, Serializable {
*/
public abstract Pageable first();
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#previousOrFirst()
*/
public Pageable previousOrFirst() {
return hasPrevious() ? previous() : first();
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + page;
result = prime * result + size;
return result;
}
@@ -128,17 +132,16 @@ public abstract class AbstractPageRequest implements Pageable, Serializable {
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
AbstractPageRequest other = (AbstractPageRequest) obj;
if (page != other.page)
return false;
if (size != other.size)
return false;
return true;
return this.page == other.page && this.size == other.size;
}
}

View File

@@ -86,7 +86,7 @@ public class QSort extends Sort implements Serializable {
Assert.notNull(orderSpecifier, "Order specifier must not be null!");
Expression<?> target = orderSpecifier.getTarget();
Object targetElement = ((com.mysema.query.types.Path) target).getMetadata().getElement();
Object targetElement = ((com.mysema.query.types.Path<?>) target).getMetadata().getElement();
Assert.notNull(targetElement, "Target element must not be null!");
@@ -100,15 +100,6 @@ public class QSort extends Sort implements Serializable {
return orderSpecifiers;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Sort#and(org.springframework.data.domain.Sort)
*/
@Override
public Sort and(Sort sort) {
throw new UnsupportedOperationException("You cannot combine a querydsl based QSort with an arbitrary Sort!");
}
/**
* Returns a new {@link QSort} consisting of the {@link OrderSpecifier}s of the current {@code QSort} combined with
* the ones from the given {@code QSort}.
@@ -147,7 +138,6 @@ public class QSort extends Sort implements Serializable {
public QSort and(OrderSpecifier<?>... orderSpecifiers) {
Assert.notEmpty(orderSpecifiers, "OrderSpecifiers must not be null or empty!");
return and(Arrays.asList(orderSpecifiers));
}
}

View File

@@ -15,18 +15,24 @@
*/
package org.springframework.data.querydsl;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.List;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import com.mysema.query.types.OrderSpecifier;
/**
* Unit tests for {@link QSort}.
*
* @author Thomas Darimont
* @author Oliver Gierke
*/
public class QSortUnitTests {
@@ -126,4 +132,18 @@ public class QSortUnitTests {
assertThat(sort.getOrderFor("firstname"), is(new Sort.Order(Sort.Direction.ASC, "firstname")));
assertThat(sort.getOrderFor("lastname"), is(new Sort.Order(Sort.Direction.DESC, "lastname")));
}
/**
* @see DATACMNS-402
*/
@Test
public void concatenatesPlainSortCorrectly() {
QUser user = QUser.user;
QSort sort = new QSort(user.firstname.asc());
Sort result = sort.and(new Sort(Direction.ASC, "lastname"));
assertThat(result, is(Matchers.<Order> iterableWithSize(2)));
assertThat(result, hasItems(new Order(Direction.ASC, "lastname"), new Order(Direction.ASC, "firstname")));
}
}