From 84f53716b720bd21bb1f1c54f2d916373ed3e84e Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 19 Jul 2023 02:16:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20PagingAndSortingQueryParam?= =?UTF-8?q?s=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/PagingAndSortingQueryParams.java | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java index f0eb1e3..b62b265 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/PagingAndSortingQueryParams.java @@ -17,7 +17,6 @@ package xyz.zhouxy.plusone.commons.util; import java.util.Collections; -import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Set; @@ -27,6 +26,7 @@ import javax.annotation.Nullable; import org.apache.commons.lang3.StringUtils; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; import xyz.zhouxy.plusone.commons.annotation.Overridable; @@ -44,23 +44,22 @@ import xyz.zhouxy.plusone.commons.annotation.Overridable; public class PagingAndSortingQueryParams { private static final int DEFAULT_PAGE_SIZE = 15; - protected final List orderBy = new LinkedList<>(); - protected int size; - protected long pageNum; - private final Set sortableColNames; + private int size; + private long pageNum; + private final List orderBy = new LinkedList<>(); + + private final Set sortableProperties; public PagingAndSortingQueryParams() { - this.sortableColNames = Collections.emptySet(); + this.sortableProperties = Collections.emptySet(); } - public PagingAndSortingQueryParams(String... sortableColNames) { - Set sortableColNameSet = new HashSet<>(sortableColNames.length); - for (String colName : sortableColNames) { - Preconditions.checkArgument(StringUtils.isNotBlank(colName), "Column name must has text."); - sortableColNameSet.add(colName); + public PagingAndSortingQueryParams(String... sortableProperties) { + for (String p : sortableProperties) { + Preconditions.checkArgument(StringUtils.isNotBlank(p), "Property name must not be blank."); } - this.sortableColNames = Collections.unmodifiableSet(sortableColNameSet); + this.sortableProperties = ImmutableSet.builder().add(sortableProperties).build(); } // Getters @@ -85,14 +84,14 @@ public class PagingAndSortingQueryParams { // Setters - public final void setOrderBy(@Nullable List orderBy) { + public final void setOrderBy(@Nullable List properties) { this.orderBy.clear(); - if (orderBy != null && !orderBy.isEmpty()) { - for (String colName : orderBy) { - Preconditions.checkArgument(this.sortableColNames.contains(colName), - "The column name must be in the set of sortable columns."); + if (properties != null && !properties.isEmpty()) { + for (String colName : properties) { + Preconditions.checkArgument(this.sortableProperties.contains(colName), + "The property name must be in the set of sortable properties."); } - this.orderBy.addAll(orderBy); + this.orderBy.addAll(properties); } } @@ -101,7 +100,7 @@ public class PagingAndSortingQueryParams { } public final void setPageNum(@Nullable Long pageNum) { - this.pageNum = pageNum != null ? pageNum : 1; + this.pageNum = pageNum != null ? pageNum : 1L; } // Setters end @@ -110,4 +109,10 @@ public class PagingAndSortingQueryParams { protected int getDefaultSize() { return DEFAULT_PAGE_SIZE; } + + @Override + public String toString() { + return "PagingAndSortingQueryParams [size=" + size + ", pageNum=" + pageNum + ", orderBy=" + orderBy + + ", sortableProperties=" + sortableProperties + "]"; + } }