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.
This commit is contained in:
Thomas Darimont
2013-11-18 09:08:44 +01:00
committed by Oliver Gierke
parent 9f65b57867
commit f2ff84fb63
11 changed files with 701 additions and 127 deletions

View File

@@ -0,0 +1,144 @@
/*
* Copyright 2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.domain;
import java.io.Serializable;
/**
* Abstract Java Bean implementation of {@code Pageable}.
*
* @author Thomas Darimont
*/
public abstract class AbstractPageRequest implements Pageable, Serializable {
private static final long serialVersionUID = 1232825578694716871L;
private final int page;
private final int size;
/**
* Creates a new {@link AbstractPageRequest}. Pages are zero indexed, thus providing 0 for {@code page} will return
* the first page.
*
* @param page
* @param size
*/
public AbstractPageRequest(int page, int size) {
if (page < 0) {
throw new IllegalArgumentException("Page index must not be less than zero!");
}
if (size < 1) {
throw new IllegalArgumentException("Page size must not be less than one!");
}
this.page = page;
this.size = size;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getPageSize()
*/
public int getPageSize() {
return size;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getPageNumber()
*/
public int getPageNumber() {
return page;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getOffset()
*/
public int getOffset() {
return page * size;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#hasPrevious()
*/
public boolean hasPrevious() {
return page > 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#next()
*/
public abstract Pageable next();
/**
* Returns the {@link Pageable} requesting the previous {@link Page}.
*
* @return
*/
public abstract Pageable previous();
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#first()
*/
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;
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AbstractPageRequest other = (AbstractPageRequest) obj;
if (page != other.page)
return false;
if (size != other.size)
return false;
return true;
}
}

View File

@@ -15,21 +15,18 @@
*/
package org.springframework.data.domain;
import java.io.Serializable;
import org.springframework.data.domain.Sort.Direction;
/**
* Basic Java Bean implementation of {@code Pageable}.
*
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class PageRequest implements Pageable, Serializable {
public class PageRequest extends AbstractPageRequest {
private static final long serialVersionUID = 8280485938848398236L;
private static final long serialVersionUID = -4541509938956089562L;
private final int page;
private final int size;
private final Sort sort;
/**
@@ -63,45 +60,10 @@ public class PageRequest implements Pageable, Serializable {
* @param sort can be {@literal null}.
*/
public PageRequest(int page, int size, Sort sort) {
if (page < 0) {
throw new IllegalArgumentException("Page index must not be less than zero!");
}
if (size < 1) {
throw new IllegalArgumentException("Page size must not be less than one!");
}
this.page = page;
this.size = size;
super(page, size);
this.sort = sort;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getPageSize()
*/
public int getPageSize() {
return size;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getPageNumber()
*/
public int getPageNumber() {
return page;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getOffset()
*/
public int getOffset() {
return page * size;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#getSort()
@@ -110,28 +72,20 @@ public class PageRequest implements Pageable, Serializable {
return sort;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#hasPrevious()
*/
public boolean hasPrevious() {
return page > 0;
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#next()
*/
public Pageable next() {
return new PageRequest(page + 1, size, sort);
return new PageRequest(getPageNumber() + 1, getPageSize(), getSort());
}
/*
* (non-Javadoc)
* @see org.springframework.data.domain.Pageable#previousOrFirst()
* @see org.springframework.data.domain.AbstractPageRequest#previous()
*/
public Pageable previousOrFirst() {
return hasPrevious() ? new PageRequest(page - 1, size, sort) : this;
public PageRequest previous() {
return getPageNumber() == 0 ? this : new PageRequest(getPageNumber() - 1, getPageSize(), getSort());
}
/*
@@ -139,7 +93,7 @@ public class PageRequest implements Pageable, Serializable {
* @see org.springframework.data.domain.Pageable#first()
*/
public Pageable first() {
return new PageRequest(0, size, sort);
return new PageRequest(0, getPageSize(), getSort());
}
/*
@@ -159,12 +113,9 @@ public class PageRequest implements Pageable, Serializable {
PageRequest that = (PageRequest) obj;
boolean pageEqual = this.page == that.page;
boolean sizeEqual = this.size == that.size;
boolean sortEqual = this.sort == null ? that.sort == null : this.sort.equals(that.sort);
return pageEqual && sizeEqual && sortEqual;
return super.equals(that) && sortEqual;
}
/*
@@ -173,14 +124,7 @@ public class PageRequest implements Pageable, Serializable {
*/
@Override
public int hashCode() {
int result = 17;
result = 31 * result + page;
result = 31 * result + size;
result = 31 * result + (null == sort ? 0 : sort.hashCode());
return result;
return 31 * super.hashCode() + (null == sort ? 0 : sort.hashCode());
}
/*
@@ -189,7 +133,7 @@ public class PageRequest implements Pageable, Serializable {
*/
@Override
public String toString() {
return String.format("Page request [number: %d, size %d, sort: %s]", page, size,
return String.format("Page request [number: %d, size %d, sort: %s]", getPageNumber(), getPageSize(),
sort == null ? null : sort.toString());
}
}