允许根据多个字段进行排序。

feature/net-util
ZhouXY108 2023-04-29 18:41:13 +08:00
parent bf082b56d9
commit c654ffc0c8
1 changed files with 15 additions and 9 deletions

View File

@ -16,11 +16,14 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.List;
import java.util.Set; import java.util.Set;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import xyz.zhouxy.plusone.commons.annotation.Overridable; import xyz.zhouxy.plusone.commons.annotation.Overridable;
@ -37,14 +40,14 @@ import xyz.zhouxy.plusone.commons.annotation.Overridable;
*/ */
public class PagingAndSortingQueryParams { public class PagingAndSortingQueryParams {
protected @Nullable String orderBy; protected final List<String> orderBy = Lists.newLinkedList();
protected int size; protected int size;
protected long pageNum; protected long pageNum;
private final Set<String> sortableColNames; private final Set<String> sortableColNames;
public PagingAndSortingQueryParams() { public PagingAndSortingQueryParams() {
sortableColNames = ImmutableSet.of(); this.sortableColNames = ImmutableSet.of();
} }
public PagingAndSortingQueryParams(String... sortableColNames) { public PagingAndSortingQueryParams(String... sortableColNames) {
@ -57,8 +60,8 @@ public class PagingAndSortingQueryParams {
// Getters // Getters
@Nullable @Nullable
public final String getOrderBy() { public final List<String> getOrderBy() {
return this.orderBy; return ImmutableList.copyOf(this.orderBy);
} }
public final int getSize() { public final int getSize() {
@ -77,12 +80,15 @@ public class PagingAndSortingQueryParams {
// Setters // Setters
public final void setOrderBy(@Nullable String orderBy) { public final void setOrderBy(@Nullable List<String> orderBy) {
if (orderBy != null) { this.orderBy.clear();
Assert.isTrue(this.sortableColNames.contains(orderBy), if (orderBy != null && !orderBy.isEmpty()) {
"The column name must be in the set of sortable columns."); for (String colName : orderBy) {
Assert.isTrue(this.sortableColNames.contains(colName),
"The column name must be in the set of sortable columns.");
}
this.orderBy.addAll(orderBy);
} }
this.orderBy = orderBy;
} }
public final void setSize(@Nullable Integer size) { public final void setSize(@Nullable Integer size) {