From 1e4306005ee75cacf45d9e90b55aa775bd1f33e6 Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Mon, 16 Dec 2024 09:38:52 +0800
Subject: [PATCH 1/9] =?UTF-8?q?Gender=20=E5=AE=9E=E7=8E=B0=20IWithIntCode?=
=?UTF-8?q?=20=E6=8E=A5=E5=8F=A3=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/xyz/zhouxy/plusone/commons/model/Gender.java | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/Gender.java b/src/main/java/xyz/zhouxy/plusone/commons/model/Gender.java
index 24dbeb9..7922305 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/model/Gender.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/model/Gender.java
@@ -16,6 +16,7 @@
package xyz.zhouxy.plusone.commons.model;
+import xyz.zhouxy.plusone.commons.base.IWithIntCode;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
@@ -23,7 +24,7 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
*
* @author ZhouXY
*/
-public enum Gender {
+public enum Gender implements IWithIntCode {
UNKNOWN(0, "Unknown", "未知"),
MALE(1, "Male", "男"),
FEMALE(2, "Female", "女"),
@@ -60,4 +61,9 @@ public enum Gender {
return displayNameZh;
}
+ @Override
+ public int getCode() {
+ return getValue();
+ }
+
}
--
2.40.1
From 488aaad452f5f955e1f0eadfd8e302546e88e1f6 Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Mon, 16 Dec 2024 09:40:18 +0800
Subject: [PATCH 2/9] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20AssertTools=20?=
=?UTF-8?q?=E6=9B=BF=E6=8D=A2=20Preconditions=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../plusone/commons/model/dto/PageResult.java | 5 ++---
.../model/dto/PagingAndSortingQueryParams.java | 17 +++++++++--------
.../zhouxy/plusone/commons/util/EnumTools.java | 10 ++++------
.../plusone/commons/util/Enumeration.java | 6 ++----
4 files changed, 17 insertions(+), 21 deletions(-)
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PageResult.java b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PageResult.java
index 3467182..7f913a8 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PageResult.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PageResult.java
@@ -18,9 +18,8 @@ package xyz.zhouxy.plusone.commons.model.dto;
import java.util.List;
-import com.google.common.base.Preconditions;
-
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
+import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
* 返回分页查询的结果
@@ -37,7 +36,7 @@ public class PageResult {
private final List content;
private PageResult(List content, long total) {
- Preconditions.checkNotNull(content, "Content must not be null.");
+ AssertTools.checkNotNull(content, "Content must not be null.");
this.content = content;
this.total = total;
}
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java
index 64f8db2..9c04fd3 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java
@@ -21,12 +21,13 @@ import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import xyz.zhouxy.plusone.commons.annotation.Virtual;
+import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.StringTools;
/**
@@ -52,11 +53,11 @@ public class PagingAndSortingQueryParams {
private final Map sortableProperties;
- public PagingAndSortingQueryParams(Map sortableProperties) {
- Preconditions.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(),
+ public PagingAndSortingQueryParams(@Nonnull Map sortableProperties) {
+ AssertTools.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(),
"Sortable properties can not be empty.");
sortableProperties.forEach((k, v) ->
- Preconditions.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
+ AssertTools.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
"Property name must not be blank."));
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
}
@@ -101,12 +102,12 @@ public class PagingAndSortingQueryParams {
}
private SortableProperty generateSortableProperty(String orderByStr) {
- Preconditions.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches());
+ AssertTools.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches());
String[] propertyNameAndOrderType = orderByStr.split("-");
- Preconditions.checkArgument(propertyNameAndOrderType.length == 2);
+ AssertTools.checkArgument(propertyNameAndOrderType.length == 2);
String propertyName = propertyNameAndOrderType[0];
- Preconditions.checkArgument(sortableProperties.containsKey(propertyName),
+ AssertTools.checkArgument(sortableProperties.containsKey(propertyName),
"The property name must be in the set of sortable properties.");
String columnName = sortableProperties.get(propertyName);
String orderType = propertyNameAndOrderType[1];
@@ -123,7 +124,7 @@ public class PagingAndSortingQueryParams {
SortableProperty(String propertyName, String columnName, String orderType) {
this.propertyName = propertyName;
this.columnName = columnName;
- Preconditions.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
+ AssertTools.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
this.orderType = orderType.toUpperCase();
this.sqlSnippet = this.propertyName + " " + this.orderType;
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java
index 428b29c..8f2e4d6 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java
@@ -20,8 +20,6 @@ import java.util.function.Supplier;
import javax.annotation.Nullable;
-import com.google.common.base.Preconditions;
-
/**
* 枚举工具类
*
@@ -44,7 +42,7 @@ public final class EnumTools {
*/
@Deprecated
public static > E valueOf(Class clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉
- Preconditions.checkNotNull(clazz, "Clazz must not be null.");
+ AssertTools.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants();
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
() -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
@@ -102,7 +100,7 @@ public final class EnumTools {
@Deprecated
public static > E getValueOrDefault(Class clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
return getValueOrDefault(clazz, ordinal, () -> {
- Preconditions.checkNotNull(clazz, "Clazz must not be null.");
+ AssertTools.checkNotNull(clazz, "Clazz must not be null.");
E[] values = clazz.getEnumConstants();
return values[0];
});
@@ -123,8 +121,8 @@ public final class EnumTools {
}
public static > Integer checkOrdinal(Class clazz, Integer ordinal) {
- Preconditions.checkNotNull(clazz, "Clazz must not be null.");
- Preconditions.checkNotNull(ordinal, "Ordinal must not be null.");
+ AssertTools.checkNotNull(clazz, "Clazz must not be null.");
+ AssertTools.checkNotNull(ordinal, "Ordinal must not be null.");
E[] values = clazz.getEnumConstants();
if (ordinal >= 0 && ordinal < values.length) {
return ordinal;
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java
index a6bfa8f..aee9e0b 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Enumeration.java
@@ -24,8 +24,6 @@ import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
-import com.google.common.base.Preconditions;
-
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
/**
@@ -43,7 +41,7 @@ public abstract class Enumeration> // NOSONAR 暂不移
protected final String name;
protected Enumeration(final int id, final String name) {
- Preconditions.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
+ AssertTools.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
this.id = id;
this.name = name;
}
@@ -98,7 +96,7 @@ public abstract class Enumeration> // NOSONAR 暂不移
}
public T get(int id) {
- Preconditions.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
+ AssertTools.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
return this.valueMap.get(id);
}
--
2.40.1
From 672e180f43453d654409160fa65631ee66b13356 Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Mon, 16 Dec 2024 10:21:20 +0800
Subject: [PATCH 3/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F?=
=?UTF-8?q?=E5=90=8D=EF=BC=8C=E4=B8=8D=E4=BD=BF=E7=94=A8=E7=BC=A9=E5=86=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../xyz/zhouxy/plusone/commons/time/YearQuarterTests.java | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/src/test/java/xyz/zhouxy/plusone/commons/time/YearQuarterTests.java b/src/test/java/xyz/zhouxy/plusone/commons/time/YearQuarterTests.java
index 0583839..0e63de8 100644
--- a/src/test/java/xyz/zhouxy/plusone/commons/time/YearQuarterTests.java
+++ b/src/test/java/xyz/zhouxy/plusone/commons/time/YearQuarterTests.java
@@ -59,8 +59,8 @@ public class YearQuarterTests {
void of_ValidYearQuarter_GetsCorrectStartAndEndDate() {
for (int year = 1990; year <= 2024; year++) {
- for (int qrtr = 1; qrtr <= 4; qrtr++) {
- Quarter quarter = Quarter.of(qrtr);
+ for (int quarterValue = 1; quarterValue <= 4; quarterValue++) {
+ Quarter quarter = Quarter.of(quarterValue);
YearQuarter yearQuarter = YearQuarter.of(year, quarter);
LocalDate expectedStartDate = quarter.firstMonthDay().atYear(year);
--
2.40.1
From 102ce5185a77361439e0298df40c130974a64846 Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Mon, 16 Dec 2024 10:22:07 +0800
Subject: [PATCH 4/9] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E7=9A=84=20Type=20?=
=?UTF-8?q?=E5=AE=9E=E7=8E=B0=20IWithCode=20=E6=8E=A5=E5=8F=A3?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../exception/ParsingFailureException.java | 48 ++++++++++++++++---
.../business/InvalidInputException.java | 24 +++++++++-
2 files changed, 64 insertions(+), 8 deletions(-)
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/ParsingFailureException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/ParsingFailureException.java
index 25543ed..343bc2b 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/exception/ParsingFailureException.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/ParsingFailureException.java
@@ -18,6 +18,9 @@ package xyz.zhouxy.plusone.commons.exception;
import java.time.format.DateTimeParseException;
+import javax.annotation.Nonnull;
+
+import xyz.zhouxy.plusone.commons.base.IWithCode;
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
/**
@@ -26,7 +29,10 @@ import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
*
* 解析失败的不一定是客户传的参数,也可能是其它来源的数据解析失败。
* 如果表示用户传参造成的解析失败,可使用 {@link RequestParamsException#RequestParamsException(Throwable)},
- * 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出
+ * 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出。
+ *
+ * throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE));
+ *
*
*
* @author ZhouXY
@@ -56,6 +62,22 @@ public final class ParsingFailureException extends RuntimeException {
this.type = type;
}
+ public ParsingFailureException() {
+ this(Type.DEFAULT);
+ }
+
+ public ParsingFailureException(String msg) {
+ this(Type.DEFAULT, msg);
+ }
+
+ public ParsingFailureException(Throwable e) {
+ this(Type.DEFAULT, e);
+ }
+
+ public ParsingFailureException(String msg, Throwable e) {
+ this(Type.DEFAULT, msg, e);
+ }
+
public static ParsingFailureException of(Type type) {
return new ParsingFailureException(type);
}
@@ -80,19 +102,29 @@ public final class ParsingFailureException extends RuntimeException {
return new ParsingFailureException(Type.DATE_TIME_PARSING_FAILURE, msg, e);
}
+ public static ParsingFailureException of(NumberFormatException e) {
+ return new ParsingFailureException(Type.NUMBER_PARSING_FAILURE, e.getMessage(), e);
+ }
+
+ public static ParsingFailureException of(String msg, NumberFormatException e) {
+ return new ParsingFailureException(Type.NUMBER_PARSING_FAILURE, msg, e);
+ }
+
public Type getType() {
return type;
}
- public enum Type {
- DEFAULT("4010500", "解析失败"),
- NUMBER_PARSING_FAILURE("4010501", "数字转换失败"),
- DATE_TIME_PARSING_FAILURE("4010502", "时间解析失败"),
- JSON_PARSING_FAILURE("4010503", "JSON 解析失败"),
- XML_PARSING_FAILURE("4010504", "XML 解析失败"),
+ public enum Type implements IWithCode {
+ DEFAULT("00", "解析失败"),
+ NUMBER_PARSING_FAILURE("10", "数字转换失败"),
+ DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),
+ JSON_PARSING_FAILURE("30", "JSON 解析失败"),
+ XML_PARSING_FAILURE("40", "XML 解析失败"),
;
+ @Nonnull
final String code;
+ @Nonnull
final String defaultMsg;
Type(String code, String defaultMsg) {
@@ -100,6 +132,8 @@ public final class ParsingFailureException extends RuntimeException {
this.defaultMsg = defaultMsg;
}
+ @Override
+ @Nonnull
public String getCode() {
return code;
}
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/business/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/business/InvalidInputException.java
index 8b8495c..53c26ed 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/exception/business/InvalidInputException.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/business/InvalidInputException.java
@@ -16,6 +16,10 @@
package xyz.zhouxy.plusone.commons.exception.business;
+import javax.annotation.Nonnull;
+
+import xyz.zhouxy.plusone.commons.base.IWithCode;
+
/**
* InvalidInputException
*
@@ -53,6 +57,22 @@ public final class InvalidInputException extends RequestParamsException {
this.type = type;
}
+ public InvalidInputException() {
+ this(Type.DEFAULT);
+ }
+
+ public InvalidInputException(String msg) {
+ this(Type.DEFAULT, msg);
+ }
+
+ public InvalidInputException(Throwable e) {
+ this(Type.DEFAULT, e);
+ }
+
+ public InvalidInputException(String msg, Throwable e) {
+ this(Type.DEFAULT, msg, e);
+ }
+
public static InvalidInputException of(Type type) {
return new InvalidInputException(type);
}
@@ -81,7 +101,7 @@ public final class InvalidInputException extends RequestParamsException {
return type;
}
- public enum Type {
+ public enum Type implements IWithCode {
DEFAULT("00", "用户输入内容非法"),
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
@@ -97,6 +117,8 @@ public final class InvalidInputException extends RequestParamsException {
this.defaultMsg = defaultMsg;
}
+ @Override
+ @Nonnull
public String getCode() {
return code;
}
--
2.40.1
From ef35833dc0469137d1f8e23df7665902de115135 Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Mon, 16 Dec 2024 10:41:02 +0800
Subject: [PATCH 5/9] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=B5=8B=E8=AF=95?=
=?UTF-8?q?=E7=B1=BB=E6=89=80=E5=9C=A8=E5=8C=85?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../dto}/test/PagingAndSortingQueryParamsTests.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
rename src/test/java/xyz/zhouxy/plusone/commons/{queryparams => model/dto}/test/PagingAndSortingQueryParamsTests.java (99%)
diff --git a/src/test/java/xyz/zhouxy/plusone/commons/queryparams/test/PagingAndSortingQueryParamsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/model/dto/test/PagingAndSortingQueryParamsTests.java
similarity index 99%
rename from src/test/java/xyz/zhouxy/plusone/commons/queryparams/test/PagingAndSortingQueryParamsTests.java
rename to src/test/java/xyz/zhouxy/plusone/commons/model/dto/test/PagingAndSortingQueryParamsTests.java
index d6241bc..e94c7d5 100644
--- a/src/test/java/xyz/zhouxy/plusone/commons/queryparams/test/PagingAndSortingQueryParamsTests.java
+++ b/src/test/java/xyz/zhouxy/plusone/commons/model/dto/test/PagingAndSortingQueryParamsTests.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package xyz.zhouxy.plusone.commons.queryparams.test;
+package xyz.zhouxy.plusone.commons.model.dto.test;
import java.io.IOException;
import java.time.LocalDate;
--
2.40.1
From 4b9b38eeb98df7c0a80b015b5f92b30eb5270fef Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Mon, 16 Dec 2024 20:02:28 +0800
Subject: [PATCH 6/9] =?UTF-8?q?Quarter=20=E5=AE=9E=E7=8E=B0=20IWithIntCode?=
=?UTF-8?q?=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../zhouxy/plusone/commons/time/Quarter.java | 19 ++++++++++++++-----
1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java b/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java
index 4af922a..cbc38e1 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/time/Quarter.java
@@ -23,13 +23,15 @@ import java.time.temporal.ChronoField;
import com.google.common.collect.Range;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
+import xyz.zhouxy.plusone.commons.base.IWithIntCode;
+import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
* 季度
*
* @author ZhouXY
*/
-public enum Quarter {
+public enum Quarter implements IWithIntCode {
/** 第一季度 */
Q1(1),
/** 第二季度 */
@@ -107,10 +109,7 @@ public enum Quarter {
*/
@StaticFactoryMethod(Quarter.class)
public static Quarter of(int value) {
- if (value < 1 || value > 4) {
- throw new IllegalArgumentException("Invalid value for Quarter: " + value);
- }
- return ENUMS[value - 1];
+ return ENUMS[checkValidIntValue(value) - 1];
}
// StaticFactoryMethods end
@@ -134,6 +133,11 @@ public enum Quarter {
return value;
}
+ @Override
+ public int getCode() {
+ return getValue();
+ }
+
public Month firstMonth() {
return Month.of(firstMonthValue());
}
@@ -166,6 +170,11 @@ public enum Quarter {
// Getters end
+ public static int checkValidIntValue(int value) {
+ AssertTools.checkArgument(value >= 1 && value <= 4, () -> "Invalid value for Quarter: " + value);
+ return value;
+ }
+
// Internal
/**
--
2.40.1
From c9db3828a30d4076a8ed1fb3bb5fcaa170626bcb Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Tue, 24 Dec 2024 17:38:08 +0800
Subject: [PATCH 7/9] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=B7=A5=E5=85=B7?=
=?UTF-8?q?=E7=B1=BB=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../commons/model/dto/PagingAndSortingQueryParams.java | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java
index 9c04fd3..6eb0106 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/PagingAndSortingQueryParams.java
@@ -27,7 +27,9 @@ import javax.annotation.Nullable;
import com.google.common.collect.ImmutableMap;
import xyz.zhouxy.plusone.commons.annotation.Virtual;
+import xyz.zhouxy.plusone.commons.collection.CollectionTools;
import xyz.zhouxy.plusone.commons.util.AssertTools;
+import xyz.zhouxy.plusone.commons.util.RegexTools;
import xyz.zhouxy.plusone.commons.util.StringTools;
/**
@@ -49,12 +51,12 @@ public class PagingAndSortingQueryParams {
private Long pageNum;
private List orderBy;
- private static final Pattern sortStrPattern = Pattern.compile("^[a-zA-Z]\\w+-(desc|asc|DESC|ASC)$");
+ private static final Pattern SORT_STR_PATTERN = Pattern.compile("^[a-zA-Z]\\w+-(desc|asc|DESC|ASC)$");
private final Map sortableProperties;
public PagingAndSortingQueryParams(@Nonnull Map sortableProperties) {
- AssertTools.checkArgument(sortableProperties != null && !sortableProperties.isEmpty(),
+ AssertTools.checkArgument(CollectionTools.isNotEmpty(sortableProperties),
"Sortable properties can not be empty.");
sortableProperties.forEach((k, v) ->
AssertTools.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
@@ -102,7 +104,7 @@ public class PagingAndSortingQueryParams {
}
private SortableProperty generateSortableProperty(String orderByStr) {
- AssertTools.checkArgument(PagingAndSortingQueryParams.sortStrPattern.matcher(orderByStr).matches());
+ AssertTools.checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
String[] propertyNameAndOrderType = orderByStr.split("-");
AssertTools.checkArgument(propertyNameAndOrderType.length == 2);
--
2.40.1
From 8ac446e2287eb16dd0a65532a7f657a23c1ac971 Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Tue, 24 Dec 2024 17:39:42 +0800
Subject: [PATCH 8/9] =?UTF-8?q?YearQuarter=20=E6=96=B0=E5=A2=9E=E6=96=B9?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../plusone/commons/time/YearQuarter.java | 32 ++++++++++++++++---
1 file changed, 28 insertions(+), 4 deletions(-)
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java b/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java
index 966c7b9..7fd4885 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java
@@ -59,7 +59,7 @@ public final class YearQuarter implements Comparable, Serializable
this.lastDate = quarter.lastMonthDay().atYear(year);
}
- // #region - StaticFactoryMethod
+ // #region - StaticFactory
/**
* 根据指定年份与季度,创建 {@link YearQuarter} 实例
@@ -186,7 +186,7 @@ public final class YearQuarter implements Comparable, Serializable
// #region - computes
public YearQuarter plusQuarters(long quartersToAdd) { // TODO 单元测试
- if (quartersToAdd == 0) {
+ if (quartersToAdd == 0L) {
return this;
}
long quarterCount = this.year * 4L + (this.quarter.getValue() - 1);
@@ -200,8 +200,16 @@ public final class YearQuarter implements Comparable, Serializable
return plusQuarters(-quartersToAdd);
}
+ public YearQuarter nextQuarter() { // TODO 单元测试
+ return plusQuarters(1L);
+ }
+
+ public YearQuarter lastQuarter() { // TODO 单元测试
+ return minusQuarters(1L);
+ }
+
public YearQuarter plusYears(long yearsToAdd) { // TODO 单元测试
- if (yearsToAdd == 0) {
+ if (yearsToAdd == 0L) {
return this;
}
int newYear = YEAR.checkValidIntValue(this.year + yearsToAdd); // safe overflow
@@ -212,6 +220,14 @@ public final class YearQuarter implements Comparable, Serializable
return plusYears(-yearsToAdd);
}
+ public YearQuarter nextYear() {
+ return plusYears(1L);
+ }
+
+ public YearQuarter lastYear() {
+ return minusYears(1L);
+ }
+
// #endregion
// #region - hashCode & equals
@@ -235,7 +251,7 @@ public final class YearQuarter implements Comparable, Serializable
// #endregion
- // #region - compareTo
+ // #region - compare
@Override
public int compareTo(YearQuarter other) {
@@ -254,6 +270,14 @@ public final class YearQuarter implements Comparable, Serializable
return this.compareTo(other) > 0;
}
+ public static YearQuarter min(YearQuarter yearQuarter1, YearQuarter yearQuarter2) { // TODO 单元测试
+ return yearQuarter1.compareTo(yearQuarter2) <= 0 ? yearQuarter1 : yearQuarter2;
+ }
+
+ public static YearQuarter max(YearQuarter yearQuarter1, YearQuarter yearQuarter2) { // TODO 单元测试
+ return yearQuarter1.compareTo(yearQuarter2) >= 0 ? yearQuarter1 : yearQuarter2;
+ }
+
// #endregion
// #region - toString
--
2.40.1
From 9f7eda47feca4e515bd3c291d9b20ed09cdc020d Mon Sep 17 00:00:00 2001
From: ZhouXY108
Date: Tue, 24 Dec 2024 17:41:04 +0800
Subject: [PATCH 9/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20EnumTools=20=E4=B8=AD?=
=?UTF-8?q?=E6=96=B9=E6=B3=95=E7=9A=84=E5=8F=82=E6=95=B0=E5=90=8D=E7=A7=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../plusone/commons/util/EnumTools.java | 103 ++++++++----------
1 file changed, 47 insertions(+), 56 deletions(-)
diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java
index 8f2e4d6..37b53a1 100644
--- a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java
+++ b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java
@@ -34,18 +34,18 @@ public final class EnumTools {
/**
* 通过 ordinal 获取枚举实例
*
- * @param 枚举的类型
- * @param clazz 枚举的类型信息
- * @param ordinal 序号
+ * @param 枚举的类型
+ * @param enumType 枚举的类型信息
+ * @param ordinal 序号
* @return 枚举对象
* @deprecated 不推荐使用枚举的 ordinal。
*/
@Deprecated
- public static > E valueOf(Class clazz, int ordinal) { // NOSONAR 该方法弃用,但不删掉
- AssertTools.checkNotNull(clazz, "Clazz must not be null.");
- E[] values = clazz.getEnumConstants();
+ public static > E valueOf(Class enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
+ AssertTools.checkNotNull(enumType, "Enum type must not be null.");
+ E[] values = enumType.getEnumConstants();
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
- () -> new EnumConstantNotPresentException(clazz, Integer.toString(ordinal)));
+ () -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
return values[ordinal];
}
@@ -53,25 +53,23 @@ public final class EnumTools {
* 通过 ordinal 获取枚举实例
*
* @param 枚举的类型
- * @param clazz 枚举的类型信息
+ * @param enumType 枚举的类型信息
* @param ordinal 序号
* @param defaultValue 默认值
* @return 枚举对象
* @deprecated 不推荐使用枚举的 ordinal。
*/
@Deprecated
- public static > E valueOf(Class clazz, @Nullable Integer ordinal, E defaultValue) { // NOSONAR 该方法弃用,但不删掉
- if (null == ordinal) {
- return defaultValue;
- }
- return valueOf(clazz, ordinal);
+ public static > E valueOf(Class enumType, // NOSONAR 该方法弃用,但不删掉
+ @Nullable Integer ordinal, E defaultValue) {
+ return null == ordinal ? defaultValue : valueOf(enumType, ordinal);
}
/**
* 通过 ordinal 获取枚举实例
*
* @param 枚举的类型
- * @param clazz 枚举的类型信息
+ * @param enumType 枚举的类型信息
* @param ordinal 序号
* @param defaultValue 默认值
* @return 枚举对象
@@ -79,29 +77,26 @@ public final class EnumTools {
*/
@Deprecated
public static > E getValueOrDefault( // NOSONAR 该方法弃用,但不删掉
- Class clazz,
+ Class enumType,
@Nullable Integer ordinal,
Supplier defaultValue) {
- if (null == ordinal) {
- return defaultValue.get();
- }
- return valueOf(clazz, ordinal);
+ return null == ordinal ? defaultValue.get() : valueOf(enumType, ordinal);
}
/**
* 通过 ordinal 获取枚举实例
*
- * @param 枚举的类型
- * @param clazz 枚举的类型信息
- * @param ordinal 序号
+ * @param 枚举的类型
+ * @param enumType 枚举的类型信息
+ * @param ordinal 序号
* @return 枚举对象
* @deprecated 不推荐使用枚举的 ordinal。
*/
@Deprecated
- public static > E getValueOrDefault(Class clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
- return getValueOrDefault(clazz, ordinal, () -> {
- AssertTools.checkNotNull(clazz, "Clazz must not be null.");
- E[] values = clazz.getEnumConstants();
+ public static > E getValueOrDefault(Class enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
+ return getValueOrDefault(enumType, ordinal, () -> {
+ AssertTools.checkNotNull(enumType, "Enum type must not be null.");
+ E[] values = enumType.getEnumConstants();
return values[0];
});
}
@@ -109,69 +104,65 @@ public final class EnumTools {
/**
* 通过 ordinal 获取枚举实例
*
- * @param 枚举的类型
- * @param clazz 枚举的类型信息
- * @param ordinal 序号
+ * @param 枚举的类型
+ * @param enumType 枚举的类型信息
+ * @param ordinal 序号
* @return 枚举对象
* @deprecated 不推荐使用枚举的 ordinal。
*/
@Deprecated
- public static > E getValueNullable(Class clazz, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
- return valueOf(clazz, ordinal, null);
+ public static > E getValueNullable(Class enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
+ return valueOf(enumType, ordinal, null);
}
- public static > Integer checkOrdinal(Class clazz, Integer ordinal) {
- AssertTools.checkNotNull(clazz, "Clazz must not be null.");
+ public static > Integer checkOrdinal(Class enumType, Integer ordinal) {
+ AssertTools.checkNotNull(enumType, "Enum type must not be null.");
AssertTools.checkNotNull(ordinal, "Ordinal must not be null.");
- E[] values = clazz.getEnumConstants();
- if (ordinal >= 0 && ordinal < values.length) {
- return ordinal;
- }
- throw new EnumConstantNotPresentException(clazz, Integer.toString(ordinal));
+ E[] values = enumType.getEnumConstants();
+ AssertTools.checkCondition(ordinal >= 0 && ordinal < values.length,
+ () -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
+ return ordinal;
}
/**
* 校验枚举的 ordinal。
*
- * @param 枚举类型
- * @param clazz 枚举类型
- * @param ordinal The ordinal
+ * @param 枚举类型
+ * @param enumType 枚举类型
+ * @param ordinal The ordinal
* @return The ordinal
*/
@Nullable
- public static > Integer checkOrdinalNullable(Class clazz, @Nullable Integer ordinal) {
- return checkOrdinalOrDefault(clazz, ordinal, null);
+ public static > Integer checkOrdinalNullable(Class enumType, @Nullable Integer ordinal) {
+ return checkOrdinalOrDefault(enumType, ordinal, null);
}
/**
* 校验枚举的 ordinal,如果 ordinal 为 {@code null},则返回 {@code 0}。
*
- * @param 枚举类型
- * @param clazz 枚举类型
- * @param ordinal The ordinal
+ * @param 枚举类型
+ * @param enumType 枚举类型
+ * @param ordinal The ordinal
* @return The ordinal
*/
@Nullable
- public static > Integer checkOrdinalOrDefault(Class clazz, @Nullable Integer ordinal) {
- return checkOrdinalOrDefault(clazz, ordinal, 0);
+ public static > Integer checkOrdinalOrDefault(Class enumType, @Nullable Integer ordinal) {
+ return checkOrdinalOrDefault(enumType, ordinal, 0);
}
/**
* 校验枚举的 ordinal,如果 ordinal 为 {@code null},则返回 {@code defaultValue}。
*
- * @param 枚举类型
- * @param clazz 枚举类型
- * @param ordinal The ordinal
+ * @param 枚举类型
+ * @param enumType 枚举类型
+ * @param ordinal The ordinal
* @return The ordinal
*/
@Nullable
public static > Integer checkOrdinalOrDefault(
- Class clazz,
+ Class enumType,
@Nullable Integer ordinal,
@Nullable Integer defaultValue) {
- if (ordinal != null) {
- return checkOrdinal(clazz, ordinal);
- }
- return defaultValue;
+ return ordinal != null ? checkOrdinal(enumType, ordinal) : defaultValue;
}
}
--
2.40.1