Compare commits

...

17 Commits

Author SHA1 Message Date
e2e5f50162 refactor: AssertTools 类中的方法使用静态导入 2025-06-12 16:11:53 +08:00
8eac9054cd refactor(gson): 重构 JSR310TypeAdapters
- 抽象出 TemporalAccessorTypeAdapter 类,简化了 LocalDate、LocalDateTime、ZonedDateTime 和 Instant 类型适配器的实现
2025-06-09 17:55:25 +08:00
a55c712349 test: 使用 JSR310TypeAdapters 简化测试代码 2025-06-09 17:21:26 +08:00
0eda94a658 refactor(exception): 为异常类添加 serialVersionUID
为以下异常类添加 serialVersionUID 字段:
- ParsingFailureException
- BizException
- InvalidInputException
- RequestParamsException
- DataOperationResultException
- SysException
2025-06-09 17:05:10 +08:00
c816696c55 docs: 改正 ParsingFailureException 文档注释中的错误描述 2025-06-09 16:14:44 +08:00
8828b12c78 release: 1.1.0-RC1 2025-06-08 13:38:03 +08:00
89acbecc5a docs: 修改 javadoc 中的格式错误 2025-06-08 13:08:45 +08:00
336d99d4ba feat(gson): 添加 Gson 适配器以支持 JSR-310 中常用的类
Reviewed-on: plusone/plusone-commons#57
2025-06-07 01:02:15 +08:00
0731bf2c22 refactor(gson): 合并多个 JSR-310 类型适配器到 JSR310TypeAdapters
- 将 InstantTypeAdapter、LocalDateTimeTypeAdapter、LocalDateTypeAdapter 和 ZonedDateTimeTypeAdapter 合并到 JSR310TypeAdapters 类中
- 更新包结构,移动适配器到 adapter 子包
- 新增相关包的 javadoc
2025-06-07 00:56:11 +08:00
2827f69aef feat(gson): 新增 InstantTypeAdapter 用于 Gson 序列化和反序列化 Instant
`InstantTypeAdapter` 用于 Gson 通过 `DateTimeFormatter#ISO_INSTANT` 序列化和反序列化 `Instant` 类型。
2025-06-07 00:22:04 +08:00
2e73ca5f6d feat(gson): 添加 Gson 适配器以支持 Java 8 日期时间 API
- 新增 `LocalDateTimeTypeAdapter`、`LocalDateTypeAdapter` 和 `ZonedDateTimeTypeAdapter`
- 将 Gson 作为可选依赖
2025-06-06 19:13:52 +08:00
1239a11cd7 refactor: 优化 UnifiedResponses 工厂方法的泛型定义
将不指定 data 的工厂方法也改成泛型方法,而不是返回 `UnifiedResponse<Void>`。
2025-06-06 11:40:20 +08:00
f8a2046d2d refactor!: 将 RegexToolsmatchesOne 方法重命名为 matchesAny 2025-06-04 17:12:29 +08:00
fb2036c038 build: 升级 logback 到 1.3.15
logback 1.3.x 是最后一个支持 JDK8 的版本。

如果使用 Spring Boot,建议使用 Spring Boot 绑定的版本,但是 Spring Boot 2.7.x 最高只支持 logback 1.2.x,所以不可避免使用的有漏洞的版本。
2025-05-28 21:14:27 +08:00
f9b4c3c58c feat: 新增 StringTools#toQuotedString 方法 2025-05-18 15:18:52 +08:00
3ca2ec3be0 build: 简化依赖声明 2025-05-14 10:42:06 +08:00
f83bb55fd6 refactor!: 重构 DataOperationResultException (#56)
删除 `DataOperationResultException` 多余的构造方法,仅提供两个构造方法。
创建 `DataOperationResultException` 实例时,必须将预计影响的行数
和实际影响的行数作为入参。(不兼容)

重构 `AssertTools` 中相关的断言方法。

Reviewed-on: plusone/plusone-commons#56
Co-authored-by: ZhouXY108 <luquanlion@outlook.com>
Co-committed-by: ZhouXY108 <luquanlion@outlook.com>
2025-05-09 21:45:30 +08:00
63 changed files with 727 additions and 359 deletions

View File

@ -78,6 +78,7 @@ System.out.println(result); // Output: Return string
public final class LoginException
extends RuntimeException
implements MultiTypesException<LoginException, LoginException.Type> {
private static final long serialVersionUID = 881293090625085616L;
private final Type type;
private LoginException(@Nonnull Type type, @Nonnull String message) {
super(message);

View File

@ -7,7 +7,7 @@
<parent>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.0-RC1</version>
</parent>
<artifactId>plusone-commons</artifactId>
@ -28,7 +28,7 @@
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-dependencies</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.0-RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@ -55,6 +55,13 @@
<optional>true</optional>
</dependency>
<!-- Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<optional>true</optional>
</dependency>
<!-- ========== Test Dependencies ========== -->
<dependency>
@ -71,17 +78,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
@ -121,12 +118,6 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<scope>test</scope>
</dependency>
<!-- Gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -24,17 +24,15 @@
* 标识<b>静态工厂方法</b>
* Effective Java Item1 建议考虑用静态工厂方法替换构造器
* 因而考虑有一个注解可以标记一下静态工厂方法以和其它方法进行区分
* </p>
*
* <h3>
* 2. {@link ReaderMethod} {@link WriterMethod}
* </h3>
* <p>
* 分别标识<b>读方法</b> getter<b>写方法</b> setter
* </p>
*
* <p>
* 最早是写了一个集合类为了方便判断使用读写锁时哪些情况下使用读锁哪些情况下使用写锁
* </p>
*
* <h3>
* 3. {@link UnsupportedOperation}
@ -42,22 +40,19 @@
* <p>
* 标识该方法不被支持或没有实现将抛出 {@link UnsupportedOperationException}
* 为了方便在使用时不需要点进源码就能知道该方法没有实现
* </p>
*
* <h3>
* 4. {@link Virtual}
* </h3>
* <p>
* Java final 的实例方法对应 C++/C# 中的虚方法允许被子类覆写
* Java final 的实例方法对应 C++/C&num; 中的虚方法允许被子类覆写
* {@link Virtual} 注解旨在设计父类时强调该方法父类虽然有默认实现但子类可以根据自己的需要覆写
* </p>
*
* <h3>
* 5. {@link ValueObject}
* </h3>
* <p>
* 标记一个类表示其作为值对象区别于 Entity
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/

View File

@ -27,7 +27,7 @@ import javax.annotation.Nullable;
/**
* {@link Ref} 包装了一个值表示对该值的应用
*
* <p>灵感来自于 C# {@value ref} 参数修饰符C# 允许通过以下方式将值返回给调用端</p>
* <p>灵感来自于 C&num; {@code ref} 参数修饰符C&num; 允许通过以下方式将值返回给调用端</p>
* <pre>
* void Method(ref int refArgument)
* {
@ -51,7 +51,7 @@ import javax.annotation.Nullable;
* <p>
* 当一个方法需要产生多个结果时无法有多个返回值可以使用 {@link Ref} 作为参数传入方法内部修改 {@link Ref} 的值
* 调用方在调用方法之后使用 {@code getValue()} 获取结果
* </p>
*
* <pre>
* String method(final Ref&lt;Integer&gt; intRefArgument, final Ref&lt;String&gt; strRefArgument) {
* intRefArgument.transformValue(i -&gt; i + 44);

View File

@ -20,8 +20,8 @@
* <h3>1. Ref</h3>
* <p>
* {@link Ref} 包装了一个值表示对该值的应用
* </p>
* <p>灵感来自于 C# {@value ref} 参数修饰符C# 允许通过以下方式将值返回给调用端</p>
*
* <p>灵感来自于 C&num; {@code ref} 参数修饰符C&num; 允许通过以下方式将值返回给调用端</p>
* <pre>
* void Method(ref int refArgument)
* {
@ -45,7 +45,7 @@
* <p>
* 当一个方法需要产生多个结果时无法有多个返回值可以使用 {@link Ref} 作为参数传入方法内部修改 {@link Ref} 的值
* 调用方在调用方法之后使用 {@code getValue()} 获取结果
* </p>
*
* <pre>
* String method(Ref&lt;Integer&gt; intRefArgument, Ref&lt;String&gt; strRefArgument) {
* intRefArgument.transformValue(i -&gt; i + 44);
@ -65,7 +65,6 @@
* <p>
* 类似于枚举这样的类型通常需要设置固定的码值表示对应的含义
* 可实现 {@link IWithCode}{@link IWithIntCode}{@link IWithLongCode}便于在需要的地方对这些接口的实现进行处理
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/

View File

@ -15,7 +15,7 @@
*/
/**
* <h2>集合<h2>
* <h2>集合</h2>
*
* <h3>
* 1. {@link CollectionTools}

View File

@ -31,7 +31,7 @@ public final class PatternConsts {
* yyyyMMdd
*
* @see RegexConsts#BASIC_ISO_DATE
* </p>
*
*/
public static final Pattern BASIC_ISO_DATE = Pattern.compile(RegexConsts.BASIC_ISO_DATE);

View File

@ -15,7 +15,7 @@
*/
/**
* <h2>常量<h2>
* <h2>常量</h2>
*
* <h3>
* 1. 正则常量

View File

@ -24,18 +24,18 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
*
* <p>
* 异常在不同场景下被抛出可以用不同的枚举值表示不同的场景类型
* </p>
*
* <p>
* 异常实现 {@link MultiTypesException} {@link #getType} 方法返回对应的场景类型
* </p>
*
* <p>
* 表示场景类型的枚举实现 {@link ExceptionType}其中的工厂方法用于创建对应类型的异常
* </p>
*
* <pre>
* public final class LoginException
* extends RuntimeException
* implements MultiTypesException&lt;LoginException, LoginException.Type&gt; {
* private static final long serialVersionUID = 881293090625085616L;
* private final Type type;
* private LoginException(&#64;Nonnull Type type, &#64;Nonnull String message) {
* super(message);
@ -61,7 +61,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
*
* // ...
*
* public enum Type implements ExceptionType<LoginException> {
* public enum Type implements ExceptionType&lt;LoginException&gt; {
* DEFAULT("00", "当前会话未登录"),
* NOT_TOKEN("10", "未提供token"),
* INVALID_TOKEN("20", "token无效"),
@ -117,7 +117,6 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* <pre>
* throw LoginException.Type.TOKEN_TIMEOUT.create();
* </pre>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@ -31,9 +31,8 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
* 如果表示用户传参造成的解析失败可使用 {@link RequestParamsException#RequestParamsException(Throwable)}
* ParsingFailureException 包装成 {@link RequestParamsException} 再抛出
* <pre>
* throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE));
* throw new RequestParamsException(ParsingFailureException.Type.NUMBER_PARSING_FAILURE.create());
* </pre>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
@ -41,6 +40,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
public final class ParsingFailureException
extends RuntimeException
implements MultiTypesException<ParsingFailureException, ParsingFailureException.Type> {
private static final long serialVersionUID = 795996090625132616L;
private final Type type;

View File

@ -21,15 +21,15 @@ package xyz.zhouxy.plusone.commons.exception.business;
*
* <p>
* 业务异常
* </p>
*
* <p>
* <b>NOTE: 通常表示业务中的意外情况用户错误输入缺失必填字段用户余额不足等</b>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class BizException extends RuntimeException {
private static final long serialVersionUID = 982585090625482416L;
private static final String DEFAULT_MSG = "业务异常";

View File

@ -26,17 +26,17 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
*
* <p>
* 用户输入内容非法
* </p>
*
* <p>
* <b>NOTE: 属业务异常</b>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public final class InvalidInputException
extends RequestParamsException
implements MultiTypesException<InvalidInputException, InvalidInputException.Type> {
private static final long serialVersionUID = -28994090625082516L;
private final Type type;

View File

@ -21,12 +21,12 @@ package xyz.zhouxy.plusone.commons.exception.business;
*
* <p>
* 用户请求参数错误
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class RequestParamsException extends BizException {
private static final long serialVersionUID = 448337090625192516L;
private static final String DEFAULT_MSG = "用户请求参数错误";

View File

@ -15,23 +15,23 @@
*/
/**
* <h2>异常<h2>
* <h2>异常</h2>
*
* <h3>1. {@link MultiTypesException} - 多类型异常</h3>
* <p>
* 异常在不同场景下被抛出可以用不同的枚举值表示不同的场景类型
* </p>
*
* <p>
* 异常实现 {@link MultiTypesException} {@link MultiTypesException#getType} 方法返回对应的场景类型
* </p>
*
* <p>
* 表示场景类型的枚举实现 {@link MultiTypesException.ExceptionType}其中的工厂方法用于创建对应类型的异常
* </p>
*
* <pre>
* public final class LoginException
* extends RuntimeException
* implements MultiTypesException&lt;LoginException, LoginException.Type&gt; {
* private static final long serialVersionUID = 881293090625085616L;
* private final Type type;
* private LoginException(&#64;Nonnull Type type, &#64;Nonnull String message) {
* super(message);
@ -57,7 +57,7 @@
*
* // ...
*
* public enum Type implements ExceptionType<LoginException> {
* public enum Type implements ExceptionType&lt;LoginException&gt; {
* DEFAULT("00", "当前会话未登录"),
* NOT_TOKEN("10", "未提供token"),
* INVALID_TOKEN("20", "token无效"),
@ -113,7 +113,6 @@
* <pre>
* throw LoginException.Type.TOKEN_TIMEOUT.create();
* </pre>
* </p>
*
* <h3>2. 业务异常</h3>
* 预设常见的业务异常可继承 {@link BizException} 自定义业务异常

View File

@ -21,55 +21,61 @@ package xyz.zhouxy.plusone.commons.exception.system;
*
* <p>
* 当数据操作的结果不符合预期时抛出
* </p>
*
* <p>
* 比如当一个 insert update 操作时预计影响数据库中的一行数据但结果却影响了零条数据或多条数据
* 当出现这种始料未及的诡异情况时抛出 {@link DataOperationResultException} 并回滚事务
* 后续需要排查原因
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public final class DataOperationResultException extends SysException {
private static final long serialVersionUID = 992754090625352516L;
private static final String DEFAULT_MSG = "数据操作的结果不符合预期";
private final long expected;
private final long actual;
/**
* 使用默认 message 构造新的 {@code DataOperationResultException}
* {@code cause} 未初始化后面可能会通过调用 {@link #initCause} 进行初始化
* 创建一个 {@code DataOperationResultException} 对象
*
* @param expected 预期影响的行数
* @param actual 实际影响的行数
*/
public DataOperationResultException() {
super(DEFAULT_MSG);
public DataOperationResultException(long expected, long actual) {
super(String.format("The number of rows affected is expected to be %d, but is: %d", expected, actual));
this.expected = expected;
this.actual = actual;
}
/**
* 使用指定的 {@code message} 构造新的 {@code DataOperationResultException}
* {@code cause} 未初始化后面可能会通过调用 {@link #initCause} 进行初始化
* 创建一个 {@code DataOperationResultException} 对象
*
* @param message 异常信息
* @param expected 预期影响的行数
* @param actual 实际影响的行数
* @param message 错误信息
*/
public DataOperationResultException(String message) {
public DataOperationResultException(long expected, long actual, String message) {
super(message);
this.expected = expected;
this.actual = actual;
}
/**
* 使用指定的 {@code cause} 构造新的 {@code DataOperationResultException}
* {@code message} (cause==null ? null : cause.toString())
* 预期影响的行数
*
* @param cause 包装的异常
* @return the expected
*/
public DataOperationResultException(Throwable cause) {
super(cause);
public long getExpected() {
return expected;
}
/**
* 使用指定的 {@code message} {@code cause} 构造新的 {@code DataOperationResultException}
* 实际影响的行数
*
* @param message 异常信息
* @param cause 包装的异常
* @return the actual
*/
public DataOperationResultException(String message, Throwable cause) {
super(message, cause);
public long getActual() {
return actual;
}
}

View File

@ -21,7 +21,6 @@ package xyz.zhouxy.plusone.commons.exception.system;
*
* <p>
* 在无法找到可访问的 Mac 地址时抛出
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@ -21,12 +21,12 @@ package xyz.zhouxy.plusone.commons.exception.system;
*
* <p>
* 通常表示应用代码存在问题或因环境问题引发异常
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class SysException extends RuntimeException {
private static final long serialVersionUID = -936435090625482516L;
private static final String DEFAULT_MSG = "系统异常";

View File

@ -24,7 +24,6 @@ import com.google.common.annotations.Beta;
* <p>
* 一个特殊的 {@link java.util.function.UnaryOperator}
* 表示对 {@code boolean} 值的一元操作
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@ -24,7 +24,6 @@ import com.google.common.annotations.Beta;
* <p>
* 一个特殊的 {@link java.util.function.UnaryOperator}
* 表示对 {@code char} 的一元操作
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@ -21,7 +21,6 @@ package xyz.zhouxy.plusone.commons.function;
*
* <p>
* 表示一个无入参无返回值的操作可抛出异常
* </p>
*
* @param <E> 可抛出的异常类型
*

View File

@ -24,7 +24,6 @@ import java.util.function.Supplier;
*
* <p>
* 返回 {@code Optional&lt;T&gt;} 对象
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@ -23,7 +23,6 @@ import java.util.function.Predicate;
*
* <p>
* {@link Predicate} 相关操作
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@ -21,7 +21,6 @@ package xyz.zhouxy.plusone.commons.function;
*
* <p>
* 允许抛出异常的消费操作是一个特殊的 {@link java.util.function.Consumer}
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@ -20,7 +20,6 @@ package xyz.zhouxy.plusone.commons.function;
*
* <p>
* 接收一个参数并返回一个结果可以抛出异常
* </p>
*
* @param <T> 入参类型
* @param <R> 返回结果类型

View File

@ -21,7 +21,6 @@ package xyz.zhouxy.plusone.commons.function;
*
* <p>
* 接收一个参数返回一个布尔值可抛出异常
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@ -21,7 +21,6 @@ package xyz.zhouxy.plusone.commons.function;
*
* <p>
* 允许抛出异常的 Supplier 接口
* </p>
*
* @param <T> 结果类型
* @param <E> 异常类型

View File

@ -20,7 +20,6 @@
* <h3>1. PredicateTools</h3>
* <p>
* {@link PredicateTools} 用于 {@link java.util.function.Predicate} 的相关操作
* </p>
*
* <h3>2. Functional interfaces</h3>
* <p>
@ -39,7 +38,6 @@
* | Optional | ToOptionalBiFunction | Optional&lt;R&gt; apply(T,U) |
* | Optional | ToOptionalFunction | Optional&lt;R&gt; apply(T) |
* </pre>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/

View File

@ -0,0 +1,169 @@
/*
* Copyright 2025 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
*
* https://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 xyz.zhouxy.plusone.commons.gson.adapter;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalQuery;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
/**
* 包含 JSR-310 相关数据类型的 {@code TypeAdapter}
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.1.0
* @see TypeAdapter
* @see com.google.gson.GsonBuilder
*/
public class JSR310TypeAdapters {
/**
* {@code LocalDate} {@code TypeAdapter}
* 用于 Gson {@code LocalDate} 进行相互转换
*/
public static final class LocalDateTypeAdapter
extends TemporalAccessorTypeAdapter<LocalDate, LocalDateTypeAdapter> {
/**
* 默认构造函数
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE} 进行 {@link LocalDate} 的序列化与反序列化
*/
public LocalDateTypeAdapter() {
this(DateTimeFormatter.ISO_LOCAL_DATE);
}
/**
* 构造函数
* 使用传入的 {@link DateTimeFormatter} 进行 {@link LocalDate} 的序列化与反序列化
*
* @param formatter 用于序列化 {@link LocalDate} 的格式化器不可为 {@code null}
*/
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
super(LocalDate::from, formatter);
}
}
/**
* {@code LocalDateTime} {@code TypeAdapter}
* 用于 Gson {@code LocalDateTime} 进行相互转换
*/
public static final class LocalDateTimeTypeAdapter
extends TemporalAccessorTypeAdapter<LocalDateTime, LocalDateTimeTypeAdapter> {
/**
* 默认构造函数
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} 进行 {@link LocalDateTime} 的序列化与反序列化
*/
public LocalDateTimeTypeAdapter() {
this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
/**
* 构造函数
* 使用传入的 {@link DateTimeFormatter} 进行 {@link LocalDateTime} 的序列化与反序列化
*
* @param formatter 用于序列化 {@link LocalDateTime} 的格式化器不可为 {@code null}
*/
public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
super(LocalDateTime::from, formatter);
}
}
/**
* {@code ZonedDateTime} {@code TypeAdapter}
* 用于 Gson {@code ZonedDateTime} 进行相互转换
*/
public static final class ZonedDateTimeTypeAdapter
extends TemporalAccessorTypeAdapter<ZonedDateTime, ZonedDateTimeTypeAdapter> {
/**
* 默认构造函数
* 使用 {@link DateTimeFormatter#ISO_ZONED_DATE_TIME} 进行 {@link ZonedDateTime} 的序列化与反序列化
*/
public ZonedDateTimeTypeAdapter() {
this(DateTimeFormatter.ISO_ZONED_DATE_TIME);
}
/**
* 构造函数
* 使用传入的 {@link DateTimeFormatter} 进行 {@link ZonedDateTime} 的序列化与反序列化
*
* @param formatter 用于序列化 {@link ZonedDateTime} 的格式化器不可为 {@code null}
*/
public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) {
super(ZonedDateTime::from, formatter);
}
}
/**
* {@code Instant} {@code TypeAdapter}
* 用于 Gson {@code Instant} 进行相互转换
*
* <p>
* 使用 {@link DateTimeFormatter#ISO_INSTANT} 进行 {@link Instant} 的序列化与反序列化
*
*/
public static final class InstantTypeAdapter
extends TemporalAccessorTypeAdapter<Instant, InstantTypeAdapter> {
public InstantTypeAdapter() {
super(Instant::from, DateTimeFormatter.ISO_INSTANT);
}
}
private abstract static class TemporalAccessorTypeAdapter<
T extends TemporalAccessor,
TTypeAdapter extends TemporalAccessorTypeAdapter<T, TTypeAdapter>>
extends TypeAdapter<T> {
private final TemporalQuery<T> temporalQuery;
private final DateTimeFormatter dateTimeFormatter;
protected TemporalAccessorTypeAdapter(
TemporalQuery<T> temporalQuery, DateTimeFormatter dateTimeFormatter) {
checkArgumentNotNull(dateTimeFormatter, "formatter must not be null.");
this.temporalQuery = temporalQuery;
this.dateTimeFormatter = dateTimeFormatter;
}
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, T value) throws IOException {
out.value(dateTimeFormatter.format(value));
}
/** {@inheritDoc} */
@Override
public T read(JsonReader in) throws IOException {
return dateTimeFormatter.parse(in.nextString(), temporalQuery);
}
}
private JSR310TypeAdapters() {
throw new IllegalStateException("Utility class");
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright 2025 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
*
* https://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.
*/
/**
* Gson 相关类型适配器
*/
package xyz.zhouxy.plusone.commons.gson.adapter;

View File

@ -0,0 +1,20 @@
/*
* Copyright 2025 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
*
* https://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.
*/
/**
* Gson 相关辅助工具
*/
package xyz.zhouxy.plusone.commons.gson;

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.model;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@ -32,7 +34,6 @@ import com.google.errorprone.annotations.Immutable;
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.StringTools;
/**
@ -40,7 +41,6 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
*
* <p>
* 中国第二代居民身份证号
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
@ -87,13 +87,13 @@ public class Chinese2ndGenIDCardNumber
*/
public static Chinese2ndGenIDCardNumber of(final String idCardNumber) {
try {
AssertTools.checkArgument(StringTools.isNotBlank(idCardNumber), "二代居民身份证校验失败:号码为空");
checkArgument(StringTools.isNotBlank(idCardNumber), "二代居民身份证校验失败:号码为空");
final String value = idCardNumber.toUpperCase();
final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(value);
AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
final String provinceCode = matcher.group("province");
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
final String cityCode = matcher.group("city");
final String countyCode = matcher.group("county");

View File

@ -16,8 +16,9 @@
package xyz.zhouxy.plusone.commons.model;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
import xyz.zhouxy.plusone.commons.base.IWithIntCode;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
* 性别
@ -50,7 +51,7 @@ public enum Gender implements IWithIntCode {
* @return 枚举值
*/
public static Gender of(int value) {
AssertTools.checkCondition(0 <= value && value < VALUES.length,
checkCondition(0 <= value && value < VALUES.length,
() -> new EnumConstantNotPresentException(Gender.class, String.valueOf(value)));
return VALUES[value];
}

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.model;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.regex.Matcher;
@ -25,7 +27,6 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
* 带校验的字符串值对象
@ -74,10 +75,10 @@ public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<
* @param errorMessage 正则不匹配时的错误信息
*/
protected ValidatableStringRecord(String value, Pattern pattern, String errorMessage) {
AssertTools.checkArgument(Objects.nonNull(value), "The value cannot be null.");
AssertTools.checkArgument(Objects.nonNull(pattern), "The pattern cannot be null.");
checkArgument(Objects.nonNull(value), "The value cannot be null.");
checkArgument(Objects.nonNull(pattern), "The pattern cannot be null.");
this.matcher = pattern.matcher(value);
AssertTools.checkArgument(this.matcher.matches(), errorMessage);
checkArgument(this.matcher.matches(), errorMessage);
this.value = value;
}

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.model.dto;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
@ -27,7 +29,6 @@ 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;
@ -37,7 +38,6 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
* <p>
* 根据传入的 {@code size} {@code pageNum}
* 提供 {@code getOffset} 方法计算 SQL 语句中 {@code offset} 的值
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @see PagingParams
@ -61,10 +61,10 @@ public class PagingAndSortingQueryParams {
* @param sortableProperties 可排序的属性不可为空
*/
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) {
AssertTools.checkArgument(CollectionTools.isNotEmpty(sortableProperties),
checkArgument(CollectionTools.isNotEmpty(sortableProperties),
"Sortable properties can not be empty.");
sortableProperties.forEach((k, v) ->
AssertTools.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
"Property name must not be blank."));
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
}
@ -108,7 +108,7 @@ public class PagingAndSortingQueryParams {
public final PagingParams buildPagingParams() {
final int sizeValue = this.size != null ? this.size : defaultSizeInternal();
final long pageNumValue = this.pageNum != null ? this.pageNum : 1L;
AssertTools.checkArgument(CollectionTools.isNotEmpty(this.orderBy),
checkArgument(CollectionTools.isNotEmpty(this.orderBy),
"The 'orderBy' cannot be empty");
final List<SortableProperty> propertiesToSort = this.orderBy.stream()
.map(this::generateSortableProperty)
@ -139,13 +139,13 @@ public class PagingAndSortingQueryParams {
}
private SortableProperty generateSortableProperty(String orderByStr) {
AssertTools.checkArgument(StringTools.isNotBlank(orderByStr));
AssertTools.checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
checkArgument(StringTools.isNotBlank(orderByStr));
checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
String[] propertyNameAndOrderType = orderByStr.split("-");
AssertTools.checkArgument(propertyNameAndOrderType.length == 2);
checkArgument(propertyNameAndOrderType.length == 2);
String propertyName = propertyNameAndOrderType[0];
AssertTools.checkArgument(sortableProperties.containsKey(propertyName),
checkArgument(sortableProperties.containsKey(propertyName),
"The property name must be in the set of sortable properties.");
String columnName = sortableProperties.get(propertyName);
String orderType = propertyNameAndOrderType[1];
@ -165,7 +165,7 @@ public class PagingAndSortingQueryParams {
SortableProperty(String propertyName, String columnName, String orderType) {
this.propertyName = propertyName;
this.columnName = columnName;
AssertTools.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
this.orderType = orderType.toUpperCase();
this.sqlSnippet = this.propertyName + " " + this.orderType;

View File

@ -40,7 +40,7 @@ public class UnifiedResponses {
* @return {@code UnifiedResponse} 对象
* {@code code} = "2000000", {@code message} = "SUCCESS", {@code data} = null
*/
public static UnifiedResponse<Void> success() {
public static <T> UnifiedResponse<T> success() {
return new UnifiedResponse<>(SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
}
@ -51,7 +51,7 @@ public class UnifiedResponses {
* @return {@code UnifiedResponse} 对象
* {@code code} = "2000000", {@code data} = null
*/
public static UnifiedResponse<Void> success(@Nullable String message) {
public static <T> UnifiedResponse<T> success(@Nullable String message) {
return new UnifiedResponse<>(SUCCESS_CODE, message);
}
@ -83,7 +83,7 @@ public class UnifiedResponses {
* @param message 错误信息
* @return {@code UnifiedResponse} 对象{@code data} {@code null}
*/
public static UnifiedResponse<Void> error(String code, @Nullable String message) {
public static <T> UnifiedResponse<T> error(String code, @Nullable String message) {
return new UnifiedResponse<>(code, message);
}
@ -109,7 +109,7 @@ public class UnifiedResponses {
* {@code message} 为异常的 {@code message}
* {@code data} {@code null}
*/
public static UnifiedResponse<Void> error(String code, Throwable e) {
public static <T> UnifiedResponse<T> error(String code, Throwable e) {
return new UnifiedResponse<>(code, e.getMessage());
}
@ -128,7 +128,7 @@ public class UnifiedResponses {
* @param message 响应信息
* @return {@code UnifiedResponse} 对象{@code data} {@code null}
*/
public static UnifiedResponse<Void> of(String code, @Nullable String message) {
public static <T> UnifiedResponse<T> of(String code, @Nullable String message) {
return new UnifiedResponse<>(code, message);
}

View File

@ -22,12 +22,12 @@
* 分页组件由 {@link PagingAndSortingQueryParams} 作为入参
* 因为分页必须伴随着排序不然可能出现同一个对象重复出现在不同页有的对象不被查询到的情况
* 所以分页查询的入参必须包含排序条件
* </p>
*
* <p>
* 用户可继承 {@link PagingAndSortingQueryParams}
* 构建自己的分页查询入参需在构造器中调用 {@link PagingAndSortingQueryParams} 的构造器传入一个 Map 作为白名单
* key 是供前端指定用于排序的属性名value 是对应数据库中的字段名只有在白名单中指定的属性名才允许作为排序条件
* </p>
*
* <p>
* {@link PagingAndSortingQueryParams} 包含三个主要的属性
* <ul>
@ -37,21 +37,20 @@
* </ul>
* 其中 orderBy 是一个 List可以指定多个排序条件每个排序条件是一个字符串
* 格式为属性名-ASC属性名-DESC分别表示升序和降序
* </p>
*
* <p>
* 比如前端传入的 orderBy ["name-ASC","age-DESC"]意味着要按 name 进行升序name 相同的情况下则按 age 进行降序
* </p>
*
* <p>
* 使用时调用 {@link PagingAndSortingQueryParams#buildPagingParams()} 方法获取分页参数 {@link PagingParams}
* </p>
*
* <p>
* 分页结果可以存放到 {@link PageResult} 作为出参
* </p>
*
* <h3>2. {@link UnifiedResponse}</h3>
* <p>
* {@link UnifiedResponse} 对返回给前端的数据进行封装包含 codemessagedata
* </p>
*
* <p>
* 可使用 {@link UnifiedResponses} 快速构建 {@link UnifiedResponse} 对象
* {@link UnifiedResponses} 默认的成功代码为 "2000000"
@ -60,7 +59,6 @@
* 中所示范的继承 {@link UnifiedResponses} 实现自己的工厂类
* 自定义 SUCCESS_CODE DEFAULT_SUCCESS_MSG 和工厂方法
* <a href="http://zhouxy.xyz:3000/plusone/plusone-commons/issues/22">issue#22</a>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/

View File

@ -18,7 +18,6 @@
* <h2>业务建模组件</h2>
* <p>
* 包含业务建模可能用到的性别身份证等元素也包含 DTO 相关类如分页查询参数响应结果分页结果等
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/

View File

@ -16,6 +16,9 @@
package xyz.zhouxy.plusone.commons.time;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
import java.time.DateTimeException;
import java.time.Month;
import java.time.MonthDay;
@ -25,7 +28,6 @@ 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;
/**
* 季度
@ -89,7 +91,7 @@ public enum Quarter implements IWithIntCode {
*/
@StaticFactoryMethod(Quarter.class)
public static Quarter fromMonth(Month month) {
AssertTools.checkNotNull(month);
checkNotNull(month);
final int monthValue = month.getValue();
return of(computeQuarterValueInternal(monthValue));
}
@ -246,7 +248,7 @@ public enum Quarter implements IWithIntCode {
* @throws DateTimeException 如果给定的季度值不在有效范围内1到4将抛出异常
*/
public static int checkValidIntValue(int value) {
AssertTools.checkCondition(value >= 1 && value <= 4,
checkCondition(value >= 1 && value <= 4,
() -> new DateTimeException("Invalid value for Quarter: " + value));
return value;
}

View File

@ -17,6 +17,7 @@
package xyz.zhouxy.plusone.commons.time;
import static java.time.temporal.ChronoField.YEAR;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.io.Serializable;
import java.time.LocalDate;
@ -32,7 +33,6 @@ import javax.annotation.Nullable;
import com.google.errorprone.annotations.Immutable;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
* 表示年份与季度
@ -93,7 +93,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(LocalDate date) {
AssertTools.checkNotNull(date);
checkNotNull(date);
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
}
@ -105,7 +105,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(Date date) {
AssertTools.checkNotNull(date);
checkNotNull(date);
@SuppressWarnings("deprecation")
final int yearValue = YEAR.checkValidIntValue(date.getYear() + 1900L);
@SuppressWarnings("deprecation")
@ -121,7 +121,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(Calendar date) {
AssertTools.checkNotNull(date);
checkNotNull(date);
final int yearValue = ChronoField.YEAR.checkValidIntValue(date.get(Calendar.YEAR));
final int monthValue = date.get(Calendar.MONTH) + 1;
return new YearQuarter(yearValue, Quarter.fromMonth(monthValue));
@ -135,7 +135,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/
@StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(YearMonth yearMonth) {
AssertTools.checkNotNull(yearMonth);
checkNotNull(yearMonth);
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
}

View File

@ -15,7 +15,7 @@
*/
/**
* <h2>时间 API<h2>
* <h2>时间 API</h2>
*
* <h3>1. 季度 API</h3>
*

View File

@ -16,6 +16,9 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
@ -33,7 +36,6 @@ import javax.annotation.Nullable;
*
* <p>
* 数组工具类
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
@ -259,7 +261,7 @@ public class ArrayTools {
* @throws IllegalArgumentException 当参数为空时抛出
*/
public static <T> boolean isAllElementsNotNull(final T[] arr) {
AssertTools.checkArgument(arr != null, "The array cannot be null.");
checkArgument(arr != null, "The array cannot be null.");
return Arrays.stream(arr).allMatch(Objects::nonNull);
}
@ -489,10 +491,10 @@ public class ArrayTools {
* @return 重复后的数组
*/
public static char[] repeat(char[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0,
checkArgument(Objects.nonNull(arr));
checkArgument(times >= 0,
"The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0,
checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero");
if (times == 0) {
return EMPTY_CHAR_ARRAY;
@ -524,10 +526,10 @@ public class ArrayTools {
* @return 重复后的数组
*/
public static byte[] repeat(byte[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0,
checkArgument(Objects.nonNull(arr));
checkArgument(times >= 0,
"The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0,
checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero");
if (times == 0) {
return EMPTY_BYTE_ARRAY;
@ -559,10 +561,10 @@ public class ArrayTools {
* @return 重复后的数组
*/
public static short[] repeat(short[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0,
checkArgument(Objects.nonNull(arr));
checkArgument(times >= 0,
"The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0,
checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero");
if (times == 0) {
return EMPTY_SHORT_ARRAY;
@ -594,10 +596,10 @@ public class ArrayTools {
* @return 重复后的数组
*/
public static int[] repeat(int[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0,
checkArgument(Objects.nonNull(arr));
checkArgument(times >= 0,
"The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0,
checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero");
if (times == 0) {
return EMPTY_INT_ARRAY;
@ -629,10 +631,10 @@ public class ArrayTools {
* @return 重复后的数组
*/
public static long[] repeat(long[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0,
checkArgument(Objects.nonNull(arr));
checkArgument(times >= 0,
"The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0,
checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero");
if (times == 0) {
return EMPTY_LONG_ARRAY;
@ -664,10 +666,10 @@ public class ArrayTools {
* @return 重复后的数组
*/
public static float[] repeat(float[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0,
checkArgument(Objects.nonNull(arr));
checkArgument(times >= 0,
"The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0,
checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero");
if (times == 0) {
return EMPTY_FLOAT_ARRAY;
@ -699,10 +701,10 @@ public class ArrayTools {
* @return 重复后的数组
*/
public static double[] repeat(double[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0,
checkArgument(Objects.nonNull(arr));
checkArgument(times >= 0,
"The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0,
checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero");
if (times == 0) {
return EMPTY_DOUBLE_ARRAY;
@ -748,7 +750,7 @@ public class ArrayTools {
* @param values 填充内容
*/
public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) {
AssertTools.checkArgument(Objects.nonNull(a));
checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) {
return;
}
@ -791,7 +793,7 @@ public class ArrayTools {
* @param values 填充内容
*/
public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) {
AssertTools.checkArgument(Objects.nonNull(a));
checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) {
return;
}
@ -834,7 +836,7 @@ public class ArrayTools {
* @param values 填充内容
*/
public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) {
AssertTools.checkArgument(Objects.nonNull(a));
checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) {
return;
}
@ -877,7 +879,7 @@ public class ArrayTools {
* @param values 填充内容
*/
public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) {
AssertTools.checkArgument(Objects.nonNull(a));
checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) {
return;
}
@ -920,7 +922,7 @@ public class ArrayTools {
* @param values 填充内容
*/
public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) {
AssertTools.checkArgument(Objects.nonNull(a));
checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) {
return;
}
@ -963,7 +965,7 @@ public class ArrayTools {
* @param values 填充内容
*/
public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) {
AssertTools.checkArgument(Objects.nonNull(a));
checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) {
return;
}
@ -1006,7 +1008,7 @@ public class ArrayTools {
* @param values 填充内容
*/
public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) {
AssertTools.checkArgument(Objects.nonNull(a));
checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) {
return;
}
@ -1061,7 +1063,7 @@ public class ArrayTools {
* @param values 填充内容
*/
private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
AssertTools.checkArgument(Objects.nonNull(a));
checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) {
return;
}
@ -1088,7 +1090,7 @@ public class ArrayTools {
// #region - indexOf
public static <T> int indexOf(@Nullable T[] arr, Predicate<? super T> predicate) {
AssertTools.checkNotNull(predicate);
checkNotNull(predicate);
if (arr == null || arr.length == 0) {
return NOT_FOUND_INDEX;
}
@ -1193,7 +1195,7 @@ public class ArrayTools {
// #region - lastIndexOf
public static <T> int lastIndexOf(@Nullable T[] arr, Predicate<? super T> predicate) {
AssertTools.checkNotNull(predicate);
checkNotNull(predicate);
if (arr == null || arr.length == 0) {
return NOT_FOUND_INDEX;
}

View File

@ -29,14 +29,13 @@ import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
*
* <p>
* 本工具类不封装过多判断逻辑鼓励充分使用项目中的工具类进行逻辑判断
* </p>
*
* <pre>
* AssertTools.checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
* AssertTools.checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
* AssertTools.checkCondition(!CollectionUtils.isEmpty(roles),
* checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
* checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
* checkCondition(!CollectionUtils.isEmpty(roles),
* () -> new InvalidInputException("The roles cannot be empty."));
* AssertTools.checkCondition(RegexTools.matches(email, PatternConsts.EMAIL),
* checkCondition(RegexTools.matches(email, PatternConsts.EMAIL),
* "must be a well-formed email address");
* </pre>
*
@ -411,141 +410,154 @@ public class AssertTools {
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param expected 预期影响的行数
* @param actualRowCount 实际影响的行数
*/
public static void checkAffectedRows(int expectedValue, int result) {
checkAffectedRows(expectedValue, result,
"The number of rows affected is expected to be %d, but is: %d", expectedValue, result);
public static void checkAffectedRows(int expected, int actualRowCount) {
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount);
}
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param expected 预期影响的行数
* @param actualRowCount 实际影响的行数
* @param errorMessage 异常信息
*/
public static void checkAffectedRows(int expectedValue, int result, @Nullable String errorMessage) {
checkCondition(expectedValue == result, () -> new DataOperationResultException(errorMessage));
public static void checkAffectedRows(int expected, int actualRowCount,
@Nullable String errorMessage) {
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount, errorMessage);
}
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param expected 预期影响的行数
* @param actualRowCount 实际影响的行数
* @param errorMessageSupplier 异常信息
*/
public static void checkAffectedRows(int expectedValue, int result,
public static void checkAffectedRows(int expected, int actualRowCount,
Supplier<String> errorMessageSupplier) {
checkCondition(expectedValue == result,
() -> new DataOperationResultException(errorMessageSupplier.get()));
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount, errorMessageSupplier.get());
}
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param expected 预期影响的行数
* @param actualRowCount 实际影响的行数
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
*/
public static void checkAffectedRows(int expectedValue, int result,
public static void checkAffectedRows(int expected, int actualRowCount,
String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(expectedValue == result,
() -> new DataOperationResultException(String.format(errorMessageTemplate, errorMessageArgs)));
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount,
String.format(errorMessageTemplate, errorMessageArgs));
}
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param expected 预期影响的行数
* @param actualRowCount 实际影响的行数
*/
public static void checkAffectedRows(long expectedValue, long result) {
checkAffectedRows(expectedValue, result,
"The number of rows affected is expected to be %d, but is: %d", expectedValue, result);
public static void checkAffectedRows(long expected, long actualRowCount) {
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount);
}
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param expected 预期影响的行数
* @param actualRowCount 实际影响的行数
* @param errorMessage 异常信息
*/
public static void checkAffectedRows(long expectedValue, long result, @Nullable String errorMessage) {
checkCondition(expectedValue == result, () -> new DataOperationResultException(errorMessage));
public static void checkAffectedRows(long expected, long actualRowCount,
@Nullable String errorMessage) {
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount, errorMessage);
}
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param expected 预期影响的行数
* @param actualRowCount 实际影响的行数
* @param errorMessageSupplier 异常信息
*/
public static void checkAffectedRows(long expectedValue, long result,
public static void checkAffectedRows(long expected, long actualRowCount,
Supplier<String> errorMessageSupplier) {
checkCondition(expectedValue == result,
() -> new DataOperationResultException(errorMessageSupplier.get()));
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount, errorMessageSupplier.get());
}
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param expected 预期影响的行数
* @param actualRowCount 实际影响的行数
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
*/
public static void checkAffectedRows(long expectedValue, long result,
public static void checkAffectedRows(long expected, long actualRowCount,
String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(expectedValue == result,
() -> new DataOperationResultException(String.format(errorMessageTemplate, errorMessageArgs)));
if (expected != actualRowCount) {
throw new DataOperationResultException(expected, actualRowCount,
String.format(errorMessageTemplate, errorMessageArgs));
}
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
*
* @param result 实际影响的数据量
* @param actualRowCount 实际影响的行数
*/
public static void checkAffectedOneRow(int result) {
checkAffectedRows(1, result,
() -> "The number of rows affected is expected to be 1, but is: " + result);
public static void checkAffectedOneRow(int actualRowCount) {
checkAffectedRows(1, actualRowCount);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
*
* @param result 实际影响的数据量
* @param actualRowCount 实际影响的行数
* @param errorMessage 异常信息
*/
public static void checkAffectedOneRow(int result, String errorMessage) {
checkAffectedRows(1, result, errorMessage);
public static void checkAffectedOneRow(int actualRowCount, String errorMessage) {
checkAffectedRows(1, actualRowCount, errorMessage);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
*
* @param result 实际影响的数据量
* @param actualRowCount 实际影响的行数
* @param errorMessageSupplier 异常信息
*/
public static void checkAffectedOneRow(int result, Supplier<String> errorMessageSupplier) {
checkAffectedRows(1, result, errorMessageSupplier);
public static void checkAffectedOneRow(int actualRowCount, Supplier<String> errorMessageSupplier) {
checkAffectedRows(1, actualRowCount, errorMessageSupplier);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
*
* @param result 实际影响的数据量
* @param actualRowCount 实际影响的行数
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
*/
public static void checkAffectedOneRow(int result,
public static void checkAffectedOneRow(int actualRowCount,
String errorMessageTemplate, Object... errorMessageArgs) {
checkAffectedRows(1, result, errorMessageTemplate, errorMessageArgs);
checkAffectedRows(1, actualRowCount, errorMessageTemplate, errorMessageArgs);
}
/**
@ -554,40 +566,39 @@ public class AssertTools {
* @param result 实际影响的数据量
*/
public static void checkAffectedOneRow(long result) {
checkAffectedRows(1L, result,
() -> "The number of rows affected is expected to be 1, but is: " + result);
checkAffectedRows(1L, result);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
*
* @param result 实际影响的数据量
* @param actualRowCount 实际影响的行数
* @param errorMessage 异常信息
*/
public static void checkAffectedOneRow(long result, String errorMessage) {
checkAffectedRows(1L, result, errorMessage);
public static void checkAffectedOneRow(long actualRowCount, String errorMessage) {
checkAffectedRows(1L, actualRowCount, errorMessage);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
*
* @param result 实际影响的数据量
* @param actualRowCount 实际影响的行数
* @param errorMessageSupplier 异常信息
*/
public static void checkAffectedOneRow(long result, Supplier<String> errorMessageSupplier) {
checkAffectedRows(1L, result, errorMessageSupplier);
public static void checkAffectedOneRow(long actualRowCount, Supplier<String> errorMessageSupplier) {
checkAffectedRows(1L, actualRowCount, errorMessageSupplier);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}
*
* @param result 实际影响的数据量
* @param actualRowCount 实际影响的行数
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
*/
public static void checkAffectedOneRow(long result,
public static void checkAffectedOneRow(long actualRowCount,
String errorMessageTemplate, Object... errorMessageArgs) {
checkAffectedRows(1L, result, errorMessageTemplate, errorMessageArgs);
checkAffectedRows(1L, actualRowCount, errorMessageTemplate, errorMessageArgs);
}
// ================================

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.math.BigDecimal;
import javax.annotation.Nonnull;
@ -28,7 +30,6 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
*
* <p>
* BigDecimal 工具类
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
@ -54,8 +55,8 @@ public class BigDecimals {
* @return {@code a} 大于 {@code b} 时返回 {@code true}
*/
public static boolean gt(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null.");
checkNotNull(a, "Parameter could not be null.");
checkNotNull(b, "Parameter could not be null.");
return (a != b) && (a.compareTo(b) > 0);
}
@ -67,8 +68,8 @@ public class BigDecimals {
* @return {@code a} 大于等于 {@code b} 时返回 {@code true}
*/
public static boolean ge(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null.");
checkNotNull(a, "Parameter could not be null.");
checkNotNull(b, "Parameter could not be null.");
return (a == b) || (a.compareTo(b) >= 0);
}
@ -80,8 +81,8 @@ public class BigDecimals {
* @return {@code a} 小于 {@code b} 时返回 {@code true}
*/
public static boolean lt(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null.");
checkNotNull(a, "Parameter could not be null.");
checkNotNull(b, "Parameter could not be null.");
return (a != b) && (a.compareTo(b) < 0);
}
@ -93,8 +94,8 @@ public class BigDecimals {
* @return {@code a} 小于等于 {@code b} 时返回 {@code true}
*/
public static boolean le(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null.");
checkNotNull(a, "Parameter could not be null.");
checkNotNull(b, "Parameter could not be null.");
return (a == b) || (a.compareTo(b) <= 0);
}

View File

@ -215,7 +215,6 @@ public class DateTimeTools {
* <p>
* 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上还是同一时间戳
* 只是不同时区的表示
* </p>
*
* @param timeMillis 时间戳
* @param zone 时区
@ -230,7 +229,6 @@ public class DateTimeTools {
* <p>
* 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上还是同一时间戳
* 只是不同时区的表示
* </p>
*
* @param dateTime {@link Date} 对象
* @param zone 时区
@ -245,7 +243,6 @@ public class DateTimeTools {
* <p>
* 传入不同 {@link ZoneId}获取到的 {@link ZonedDateTime} 对象实际上表示的还是还是同一时间戳的时间
* 只是不同时区的表示
* </p>
*
* @param dateTime {@link Date} 对象
* @param timeZone 时区

View File

@ -16,6 +16,9 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.util.function.Supplier;
import javax.annotation.Nullable;
@ -43,7 +46,7 @@ public final class EnumTools {
@Deprecated
private static <E extends Enum<?>> E valueOfInternal(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用但不删掉
E[] values = enumType.getEnumConstants();
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
checkCondition((ordinal >= 0 && ordinal < values.length),
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
return values[ordinal];
}
@ -59,7 +62,7 @@ public final class EnumTools {
*/
@Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用但不删掉
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
checkNotNull(enumType, "Enum type must not be null.");
return valueOfInternal(enumType, ordinal);
}
@ -76,7 +79,7 @@ public final class EnumTools {
@Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用但不删掉
@Nullable Integer ordinal, @Nullable E defaultValue) {
AssertTools.checkNotNull(enumType);
checkNotNull(enumType);
return null == ordinal ? defaultValue : valueOfInternal(enumType, ordinal);
}
@ -95,8 +98,8 @@ public final class EnumTools {
Class<E> enumType,
@Nullable Integer ordinal,
Supplier<E> defaultValue) {
AssertTools.checkNotNull(enumType);
AssertTools.checkNotNull(defaultValue);
checkNotNull(enumType);
checkNotNull(defaultValue);
return null == ordinal ? defaultValue.get() : valueOfInternal(enumType, ordinal);
}
@ -112,7 +115,7 @@ public final class EnumTools {
@Deprecated
public static <E extends Enum<?>> E getValueOrDefault(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用但不删掉
return getValueOrDefault(enumType, ordinal, () -> {
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
checkNotNull(enumType, "Enum type must not be null.");
E[] values = enumType.getEnumConstants();
return values[0];
});
@ -133,10 +136,10 @@ public final class EnumTools {
}
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> enumType, Integer ordinal) {
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
AssertTools.checkNotNull(ordinal, "Ordinal must not be null.");
checkNotNull(enumType, "Enum type must not be null.");
checkNotNull(ordinal, "Ordinal must not be null.");
E[] values = enumType.getEnumConstants();
AssertTools.checkCondition(ordinal >= 0 && ordinal < values.length,
checkCondition(ordinal >= 0 && ordinal < values.length,
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
return ordinal;
}
@ -180,7 +183,7 @@ public final class EnumTools {
Class<E> enumType,
@Nullable Integer ordinal,
@Nullable Integer defaultValue) {
AssertTools.checkNotNull(enumType);
checkNotNull(enumType);
return checkOrdinalOrGetInternal(enumType, ordinal, () -> checkOrdinalOrDefaultInternal(enumType, defaultValue, null));
}

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -34,7 +36,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
* 参考 <a href="https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/">Enumeration classes</a>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @deprecated 设计 Enumeration 的灵感来自于 .net 社区因为 C# 的枚举不带行为
* @deprecated 设计 Enumeration 的灵感来自于 .net 社区因为 C&num; 的枚举不带行为
* Java 的枚举可以带行为故大多数情况下不需要这种设计
*/
@Deprecated
@ -44,7 +46,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
protected final String name;
protected Enumeration(final int id, final String name) {
AssertTools.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
this.id = id;
this.name = name;
}
@ -126,7 +128,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
* @return 枚举对象
*/
public T get(int id) {
AssertTools.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
return this.valueMap.get(id);
}

View File

@ -16,8 +16,9 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
@ -26,7 +27,6 @@ import java.util.concurrent.ConcurrentHashMap;
*
* <p>
* 生成 UUID 修改版雪花IDSeata 版本
* </p>
*
* @see UUID
* @see IdWorker
@ -70,7 +70,7 @@ public class IdGenerator {
* @return UUID 字符串
*/
public static String toSimpleString(UUID uuid) {
AssertTools.checkArgument(Objects.nonNull(uuid));
checkArgumentNotNull(uuid);
return (uuidDigits(uuid.getMostSignificantBits() >> 32, 8) +
uuidDigits(uuid.getMostSignificantBits() >> 16, 4) +
uuidDigits(uuid.getMostSignificantBits(), 4) +

View File

@ -34,7 +34,7 @@ import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
* <li>每个机器线程安全地生成序列前面加上机器的id这样就不会与其它机器的id相冲突</li>
* <li>时间戳作为序列的预留位它更像是应用启动时最开始的序列的一部分在一个时间戳里生成 4096 id 之后直接生成下一个时间戳的 id</li>
* </ol>
* </p>
*
* <p>
* 详情见以下介绍
* <ul>
@ -43,7 +43,7 @@ import xyz.zhouxy.plusone.commons.exception.system.NoAvailableMacFoundException;
* <li><a href="https://juejin.cn/post/7264387737276203065">在开源项目中看到一个改良版的雪花算法现在它是你的了</a></li>
* <li><a href="https://juejin.cn/post/7265516484029743138">关于若干读者阅读改良版雪花算法后提出的几个共性问题的回复</a></li>
* </ul>
* </p>
*
*/
public class IdWorker {

View File

@ -45,7 +45,6 @@ public class OptionalTools {
* <p>
* 包装类为 {@code null} 表示值的缺失转为 {@link OptionalInt}
* {@link OptionalInt#empty()} 表示值的缺失
* </p>
*
* @param value 包装对象
* @return {@link OptionalInt} 实例
@ -58,7 +57,6 @@ public class OptionalTools {
* {@code Optional<Integer>} 对象转为 {@link OptionalInt} 对象
* <p>
* {@code Optional<Integer>} 将整数包装了两次改为使用 {@link OptionalInt} 包装其中的整数数据
* </p>
*
* @param optionalObj {@code Optional<Integer>} 对象
* @return {@link OptionalInt} 实例
@ -72,7 +70,6 @@ public class OptionalTools {
* <p>
* 包装类为 {@code null} 表示值的缺失转为 {@link OptionalLong}
* {@link OptionalLong#empty()} 表示值的缺失
* </p>
*
* @param value 包装对象
* @return {@link OptionalLong} 实例
@ -85,7 +82,6 @@ public class OptionalTools {
* {@code Optional<Long>} 转为 {@link OptionalLong}
* <p>
* {@code Optional<Long>} 将整数包装了两次改为使用 {@link OptionalLong} 包装其中的整数数据
* </p>
*
* @param optionalObj 包装对象
* @return {@link OptionalLong} 实例
@ -99,7 +95,6 @@ public class OptionalTools {
* <p>
* 包装类为 {@code null} 表示值的缺失转为 {@link OptionalDouble}
* {@link OptionalDouble#empty()} 表示值的缺失
* </p>
*
* @param value 包装对象
* @return {@link OptionalDouble} 实例
@ -112,7 +107,6 @@ public class OptionalTools {
* {@code Optional<Double>} 转为 {@link OptionalDouble}
* <p>
* {@code Optional<Double>} 将整数包装了两次改为使用 {@link OptionalDouble} 包装其中的整数数据
* </p>
*
* @param optionalObj 包装对象
* @return {@link OptionalDouble} 实例

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Objects;
@ -26,7 +28,7 @@ import java.util.concurrent.ThreadLocalRandom;
* 随机工具类
* <p>
* 建议调用方自行维护 Random 对象
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public final class RandomTools {
@ -67,21 +69,21 @@ public final class RandomTools {
* @return 随机字符串
*/
public static String randomStr(Random random, char[] sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null.");
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
checkArgument(Objects.nonNull(random), "Random cannot be null.");
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(random, sourceCharacters, length);
}
public static String randomStr(char[] sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
}
public static String secureRandomStr(char[] sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
}
@ -96,21 +98,21 @@ public final class RandomTools {
* @return 随机字符串
*/
public static String randomStr(Random random, String sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null.");
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
checkArgument(Objects.nonNull(random), "Random cannot be null.");
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(random, sourceCharacters, length);
}
public static String randomStr(String sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
}
public static String secureRandomStr(String sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
}

View File

@ -16,6 +16,9 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
@ -72,7 +75,7 @@ public final class RegexTools {
* @return {@link Pattern} 实例
*/
public static Pattern getPattern(final String pattern, final int flags, final boolean cachePattern) {
AssertTools.checkNotNull(pattern);
checkNotNull(pattern);
return cachePattern ? cacheAndGetPatternInternal(pattern, flags) : getPatternInternal(pattern, flags);
}
@ -95,7 +98,7 @@ public final class RegexTools {
*/
@Nonnull
public static Pattern getPattern(final String pattern, final int flags) {
AssertTools.checkNotNull(pattern);
checkNotNull(pattern);
return getPatternInternal(pattern, flags);
}
@ -115,7 +118,7 @@ public final class RegexTools {
* @return 判断结果
*/
public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) {
AssertTools.checkNotNull(pattern);
checkNotNull(pattern);
return matchesInternal(input, pattern);
}
@ -126,9 +129,9 @@ public final class RegexTools {
* @param patterns 正则
* @return 判断结果
*/
public static boolean matchesOne(@Nullable final CharSequence input, final Pattern[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return matchesOneInternal(input, patterns);
public static boolean matchesAny(@Nullable final CharSequence input, final Pattern[] patterns) {
checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return matchesAnyInternal(input, patterns);
}
/**
@ -139,7 +142,7 @@ public final class RegexTools {
* @return 判断结果
*/
public static boolean matchesAll(@Nullable final CharSequence input, final Pattern[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return matchesAllInternal(input, patterns);
}
@ -210,8 +213,8 @@ public final class RegexTools {
* @return 结果
*/
public static Matcher getMatcher(final CharSequence input, final Pattern pattern) {
AssertTools.checkNotNull(input);
AssertTools.checkNotNull(pattern);
checkNotNull(input);
checkNotNull(pattern);
return pattern.matcher(input);
}
@ -261,8 +264,8 @@ public final class RegexTools {
* @return 结果
*/
public static Matcher getMatcher(final CharSequence input, final String pattern, final int flags) {
AssertTools.checkNotNull(input);
AssertTools.checkNotNull(pattern);
checkNotNull(input);
checkNotNull(pattern);
return getPatternInternal(pattern, flags).matcher(input);
}
@ -319,7 +322,7 @@ public final class RegexTools {
* @param patterns 正则表达式
* @return 判断结果
*/
private static boolean matchesOneInternal(@Nullable final CharSequence input, final Pattern[] patterns) {
private static boolean matchesAnyInternal(@Nullable final CharSequence input, final Pattern[] patterns) {
return input != null
&& Arrays.stream(patterns)
.anyMatch(pattern -> pattern.matcher(input).matches());

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.util.concurrent.TimeUnit;
/**
@ -73,9 +75,9 @@ public class SnowflakeIdGenerator {
* @param datacenterId 数据中心ID (0~31)
*/
public SnowflakeIdGenerator(final long workerId, final long datacenterId) {
AssertTools.checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0),
checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0),
"WorkerId can't be greater than %s or less than 0.", MAX_WORKER_ID);
AssertTools.checkArgument((datacenterId <= MAX_DATACENTER_ID && datacenterId >= 0),
checkArgument((datacenterId <= MAX_DATACENTER_ID && datacenterId >= 0),
"DatacenterId can't be greater than %s or less than 0.", MAX_DATACENTER_ID);
this.datacenterIdAndWorkerId
= (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT);

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;
@ -31,7 +33,6 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
*
* <p>
* 字符串工具类
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
@ -111,7 +112,7 @@ public class StringTools {
* @return 结果
*/
public static String repeat(final String str, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(str));
checkArgument(Objects.nonNull(str));
return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength));
}
@ -211,8 +212,8 @@ public class StringTools {
if (src == null || src.isEmpty()) {
return EMPTY_STRING;
}
AssertTools.checkArgument(front >= 0 && end >= 0);
AssertTools.checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度");
checkArgument(front >= 0 && end >= 0);
checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度");
final char[] charArray = src.toCharArray();
for (int i = front; i < charArray.length - end; i++) {
charArray[i] = replacedChar;
@ -220,6 +221,23 @@ public class StringTools {
return String.valueOf(charArray);
}
/**
* 转换为带引号的字符串
*
* @param value
* @return 带引号的字符串
* @since 1.1.0
*/
public static String toQuotedString(@Nullable String value) {
if (value == null) {
return "null";
}
if (value.isEmpty()) {
return "\"\"";
}
return "\"" + value + "\"";
}
private StringTools() {
throw new IllegalStateException("Utility class");
}

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
@ -76,7 +78,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
* @param nodes 平铺的节点列表
*/
public List<T> buildTree(Collection<T> nodes) {
AssertTools.checkNotNull(nodes);
checkNotNull(nodes);
return buildTreeInternal(nodes, this.defaultComparator);
}
@ -93,7 +95,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
* <b>仅影响调用 addChild 的顺序如果操作对象本身对应的控制了子节点的顺序无法影响其相关逻辑</b>
*/
public List<T> buildTree(Collection<T> nodes, @Nullable Comparator<? super T> comparator) {
AssertTools.checkNotNull(nodes);
checkNotNull(nodes);
final Comparator<? super T> c = (comparator != null) ? comparator : this.defaultComparator;
return buildTreeInternal(nodes, c);
}

View File

@ -18,7 +18,6 @@
* <h2>工具类</h2>
* <p>
* 包含树构建器{@link TreeBuilder}断言工具{@link AssertTools}ID 生成器{@link IdGenerator}及其它实用工具类
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/

View File

@ -17,12 +17,12 @@
package xyz.zhouxy.plusone.commons.base;
import static org.junit.jupiter.api.Assertions.*;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import javax.annotation.Nonnull;
import org.junit.jupiter.api.Test;
import xyz.zhouxy.plusone.commons.util.AssertTools;
class IWithCodeTests {
@ -91,7 +91,7 @@ class IWithCodeTests {
private final String code;
WithCode(String code) {
AssertTools.checkNotNull(code);
checkNotNull(code);
this.code = code;
}

View File

@ -0,0 +1,145 @@
/*
* Copyright 2025 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
*
* https://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 xyz.zhouxy.plusone.commons.gson.adapter;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.*;
@Slf4j
public final class JSR310TypeAdaptersTests {
final Gson gsonWithDefaultFormatter = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter().nullSafe())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter().nullSafe())
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeTypeAdapter().nullSafe())
.registerTypeAdapter(Instant.class, new InstantTypeAdapter().nullSafe())
.create();
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
final DateTimeFormatter localDateTimeFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy/MM/dd HH:mm:ss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.toFormatter();
final DateTimeFormatter zonedDateTimeFormatter = new DateTimeFormatterBuilder()
.appendPattern("yyyy/MM/dd HH:mm:ss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3)
.appendZoneId()
.toFormatter();
final Gson gsonWithSpecifiedFormatter = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter(dateFormatter).nullSafe())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter(localDateTimeFormatter).nullSafe())
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeTypeAdapter(zonedDateTimeFormatter).nullSafe())
.create();
final LocalDate date = LocalDate.of(2025, 6, 6);
final LocalDateTime localDateTime = date.atTime(6, 6, 6, 666000000);
final ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("+08:00"));
final Instant instant = zonedDateTime.toInstant();
@DisplayName("测试使用 TypeAdapter 中默认的 formatter 进行序列化")
@Test
void test_serialize_defaultFormatter() {
Foo foo = new Foo();
foo.localDate = date;
foo.localDateTime = localDateTime;
foo.zonedDateTime = zonedDateTime;
foo.instant = instant;
String json = String.format(
"{\"localDate\":\"%s\",\"localDateTime\":\"%s\",\"zonedDateTime\":\"%s\",\"instant\":\"%s\"}",
DateTimeFormatter.ISO_LOCAL_DATE.format(date),
DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime),
DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zonedDateTime),
DateTimeFormatter.ISO_INSTANT.format(instant)
);
assertEquals(json, gsonWithDefaultFormatter.toJson(foo));
}
@DisplayName("测试指定 formatter 进行序列化")
@Test
void test_serialize_specifiedFormatter() {
Foo foo = new Foo();
foo.localDate = date;
foo.localDateTime = localDateTime;
foo.zonedDateTime = zonedDateTime;
String json = String.format(
"{\"localDate\":\"%s\",\"localDateTime\":\"%s\",\"zonedDateTime\":\"%s\"}",
dateFormatter.format(date),
localDateTimeFormatter.format(localDateTime),
zonedDateTimeFormatter.format(zonedDateTime)
);
assertEquals(json, gsonWithSpecifiedFormatter.toJson(foo));
}
@DisplayName("测试使用 TypeAdapter 中默认的 formatter 进行反序列化")
@Test
void test_deserialize_defaultFormatter() {
String json = String.format(
"{\"localDate\":\"%s\",\"localDateTime\":\"%s\",\"zonedDateTime\":\"%s\",\"instant\":\"%s\"}",
DateTimeFormatter.ISO_LOCAL_DATE.format(date),
DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime),
DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zonedDateTime),
DateTimeFormatter.ISO_INSTANT.format(instant)
);
Foo foo = gsonWithDefaultFormatter.fromJson(json, Foo.class);
assertEquals(date, foo.localDate);
assertEquals(localDateTime, foo.localDateTime);
assertEquals(zonedDateTime, foo.zonedDateTime);
assertEquals(instant, foo.instant);
}
@DisplayName("测试指定 formatter 进行反序列化")
@Test
void test_deserialize_specifiedFormatter() {
String json = String.format(
"{\"localDate\":\"%s\",\"localDateTime\":\"%s\",\"zonedDateTime\":\"%s\"}",
dateFormatter.format(date),
localDateTimeFormatter.format(localDateTime),
zonedDateTimeFormatter.format(zonedDateTime)
);
Foo foo = gsonWithSpecifiedFormatter.fromJson(json, Foo.class);
assertEquals(date, foo.localDate);
assertEquals(localDateTime, foo.localDateTime);
assertEquals(zonedDateTime, foo.zonedDateTime);
}
static class Foo {
LocalDate localDate;
LocalDateTime localDateTime;
ZonedDateTime zonedDateTime;
Instant instant;
}
}

View File

@ -558,11 +558,11 @@ class CustomUnifiedResponseFactoryTests {
public static final String SUCCESS_CODE = "0000000";
public static final String DEFAULT_SUCCESS_MSG = "成功";
public static UnifiedResponse<Void> success() {
public static <T> UnifiedResponse<T> success() {
return of(SUCCESS_CODE, DEFAULT_SUCCESS_MSG);
}
public static UnifiedResponse<Void> success(@Nullable String message) {
public static <T> UnifiedResponse<T> success(@Nullable String message) {
return of(SUCCESS_CODE, message);
}

View File

@ -19,14 +19,12 @@ package xyz.zhouxy.plusone.commons.model.dto.test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
@ -45,9 +43,6 @@ import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -57,6 +52,9 @@ import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.LocalDateTimeTypeAdapter;
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.LocalDateTypeAdapter;
import xyz.zhouxy.plusone.commons.model.dto.PageResult;
import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams;
import xyz.zhouxy.plusone.commons.model.dto.PagingParams;
@ -187,31 +185,8 @@ public class PagingAndSortingQueryParamsTests {
@Test
void testGson() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(LocalDate.class, new TypeAdapter<LocalDate>() {
@Override
public void write(JsonWriter out, LocalDate value) throws IOException {
out.value(DateTimeFormatter.ISO_DATE.format(value));
}
@Override
public LocalDate read(JsonReader in) throws IOException {
return LocalDate.parse(in.nextString(), DateTimeFormatter.ISO_DATE);
}
})
.registerTypeAdapter(LocalDateTime.class, new TypeAdapter<LocalDateTime>() {
@Override
public void write(JsonWriter out, LocalDateTime value) throws IOException {
out.value(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value));
}
@Override
public LocalDateTime read(JsonReader in) throws IOException {
return LocalDateTime.parse(in.nextString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
}
})
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter().nullSafe())
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter().nullSafe())
.create();
try (SqlSession session = sqlSessionFactory.openSession()) {
AccountQueryParams params = gson.fromJson(JSON_STR, AccountQueryParams.class);

View File

@ -111,24 +111,24 @@ class RegexToolsTests {
}
@Test
void matchesOne_InputMatchesOnePattern_ReturnsTrue() {
void matchesAny_InputMatchesAnyPattern_ReturnsTrue() {
String[] patterns = {"abc", "def"};
Pattern[] compiledPatterns = new Pattern[patterns.length];
for (int i = 0; i < patterns.length; i++) {
compiledPatterns[i] = Pattern.compile(patterns[i]);
}
assertTrue(RegexTools.matchesOne("abc", compiledPatterns), "Input should match one pattern");
assertTrue(RegexTools.matchesAny("abc", compiledPatterns), "Input should match one pattern");
}
@Test
void matchesOne_InputDoesNotMatchAnyPattern_ReturnsFalse() {
void matchesAny_InputDoesNotMatchAnyPattern_ReturnsFalse() {
String[] patterns = {"abc", "def"};
Pattern[] compiledPatterns = new Pattern[patterns.length];
for (int i = 0; i < patterns.length; i++) {
compiledPatterns[i] = Pattern.compile(patterns[i]);
}
assertFalse(RegexTools.matchesOne("xyz", compiledPatterns), "Input should not match any pattern");
assertFalse(RegexTools.matchesOne(null, compiledPatterns), "Input should not match any pattern");
assertFalse(RegexTools.matchesAny("xyz", compiledPatterns), "Input should not match any pattern");
assertFalse(RegexTools.matchesAny(null, compiledPatterns), "Input should not match any pattern");
}
@Test

View File

@ -370,6 +370,38 @@ class StringToolsTests {
// #endregion - desensitize
// ================================
// ================================
// #region - toQuotedString
// ================================
@Test
void toQuotedString_NullInput_ReturnsNullStr() {
String result = StringTools.toQuotedString(null);
assertEquals("null", result);
assertEquals("The value is null.", String.format("The value is %s.", result));
}
@Test
void toQuotedString_EmptyString_ReturnsEmptyString() {
String result = StringTools.toQuotedString("");
assertEquals("\"\"", result);
assertEquals("The value is \"\".", String.format("The value is %s.", result));
}
@Test
void toQuotedString_ValidInput_ReturnsQuotedString() {
String result = StringTools.toQuotedString("Hello World");
assertEquals("\"Hello World\"", result);
assertEquals("The value is \"Hello World\".", String.format("The value is %s.", result));
}
// ================================
// #endregion - toQuotedString
// ================================
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors = StringTools.class.getDeclaredConstructors();

View File

@ -7,7 +7,7 @@
<parent>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.0-RC1</version>
</parent>
<artifactId>plusone-dependencies</artifactId>
@ -35,7 +35,7 @@
<commons-dbutils.version>1.8.1</commons-dbutils.version>
<commons-crypto.version>1.2.0</commons-crypto.version>
<logback.version>1.2.13</logback.version>
<logback.version>1.3.15</logback.version>
<jackson.version>2.18.3</jackson.version>
<gson.version>2.13.1</gson.version>
@ -209,19 +209,11 @@
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

View File

@ -6,7 +6,7 @@
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.0-RC1</version>
<packaging>pom</packaging>
<modules>