forked from plusone/plusone-commons
使用不可变的 Set 存放可用于排序的列名。
parent
20549648c5
commit
bf9d2fc45b
|
@ -16,9 +16,13 @@
|
||||||
|
|
||||||
package xyz.zhouxy.plusone.commons.util;
|
package xyz.zhouxy.plusone.commons.util;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Set;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.annotation.Overridable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分页排序查询参数
|
* 分页排序查询参数
|
||||||
|
@ -33,53 +37,71 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class PagingAndSortingQueryParams {
|
public class PagingAndSortingQueryParams {
|
||||||
|
|
||||||
protected String orderBy;
|
protected @Nullable String orderBy;
|
||||||
protected Integer size;
|
protected int size;
|
||||||
protected Long pageNum;
|
protected long pageNum;
|
||||||
|
|
||||||
private final List<String> sortableColNames;
|
private final Set<String> sortableColNames;
|
||||||
|
|
||||||
public PagingAndSortingQueryParams() {
|
public PagingAndSortingQueryParams() {
|
||||||
sortableColNames = Collections.emptyList();
|
sortableColNames = ImmutableSet.of();
|
||||||
}
|
}
|
||||||
|
|
||||||
public PagingAndSortingQueryParams(String... sortableColNames) {
|
public PagingAndSortingQueryParams(String... sortableColNames) {
|
||||||
this.sortableColNames = Arrays.asList(sortableColNames);
|
for (String colName : sortableColNames) {
|
||||||
|
Assert.hasText(colName, "Column name must has text.");
|
||||||
|
}
|
||||||
|
this.sortableColNames = ImmutableSet.copyOf(sortableColNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters
|
// Getters
|
||||||
|
|
||||||
public String getOrderBy() {
|
@Nullable
|
||||||
return orderBy != null && sortableColNames.contains(orderBy) ? orderBy : null;
|
public final String getOrderBy() {
|
||||||
|
return this.orderBy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSize() {
|
public final int getSize() {
|
||||||
return this.size != null ? this.size : 15;
|
return this.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getPageNum() {
|
public final long getPageNum() {
|
||||||
return this.pageNum != null ? this.pageNum : 1;
|
return this.pageNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getOffset() {
|
public final long getOffset() {
|
||||||
return (getPageNum() - 1) * getSize();
|
return (this.pageNum - 1) * this.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Getters end
|
// Getters end
|
||||||
|
|
||||||
// Setters
|
// Setters
|
||||||
|
|
||||||
public void setOrderBy(String orderBy) {
|
public final void setOrderBy(@Nullable String orderBy) {
|
||||||
|
if (orderBy != null) {
|
||||||
|
Assert.isTrue(this.sortableColNames.contains(orderBy),
|
||||||
|
"The column name must be in the set of sortable columns.");
|
||||||
|
}
|
||||||
this.orderBy = orderBy;
|
this.orderBy = orderBy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSize(Integer size) {
|
public final void setSize(@Nullable Integer size) {
|
||||||
this.size = size;
|
this.size = size != null ? size : getDefaultSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPageNum(Long pageNum) {
|
public final void setPageNum(@Nullable Long pageNum) {
|
||||||
this.pageNum = pageNum;
|
this.pageNum = pageNum != null ? pageNum : getDefaultPageNum();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setters end
|
// Setters end
|
||||||
|
|
||||||
|
@Overridable
|
||||||
|
protected int getDefaultSize() {
|
||||||
|
return 15;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Overridable
|
||||||
|
protected int getDefaultPageNum() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue