6 Commits

Author SHA1 Message Date
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: http://zhouxy.xyz:3000/plusone/plusone-commons/pulls/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
48 changed files with 414 additions and 89 deletions

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>
@@ -111,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,13 +24,12 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
*
* <p>
* 异常在不同场景下被抛出,可以用不同的枚举值,表示不同的场景类型。
* </p>
*
* <p>
* 异常实现 {@link MultiTypesException} 的 {@link #getType} 方法,返回对应的场景类型。
* </p>
*
* <p>
* 表示场景类型的枚举实现 {@link ExceptionType},其中的工厂方法用于创建对应类型的异常。
* </p>
*
* <pre>
* public final class LoginException
@@ -61,7 +60,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 +116,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

@@ -33,7 +33,6 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
* <pre>
* throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE));
* </pre>
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@@ -21,11 +21,10 @@ 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
*/

View File

@@ -26,11 +26,10 @@ 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
*/

View File

@@ -21,7 +21,6 @@ package xyz.zhouxy.plusone.commons.exception.business;
*
* <p>
* 用户请求参数错误
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@@ -15,18 +15,17 @@
*/
/**
* <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
@@ -57,7 +56,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 +112,6 @@
* <pre>
* throw LoginException.Type.TOKEN_TIMEOUT.create();
* </pre>
* </p>
*
* <h3>2. 业务异常</h3>
* 预设常见的业务异常。可继承 {@link BizException} 自定义业务异常。

View File

@@ -21,13 +21,12 @@ 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
*/

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,7 +21,6 @@ package xyz.zhouxy.plusone.commons.exception.system;
*
* <p>
* 通常表示应用代码存在问题,或因环境问题,引发异常。
* </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 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,187 @@
/*
* 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 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 com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/**
* 包含 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 TypeAdapter<LocalDate> {
private final DateTimeFormatter dateTimeFormatter;
/**
* 默认构造函数,
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE} 进行 {@link LocalDate} 的序列化与反序列化。
*/
public LocalDateTypeAdapter() {
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
}
/**
* 构造函数,
* 使用传入的 {@link DateTimeFormatter} 进行 {@link LocalDate} 的序列化与反序列化。
*
* @param formatter 用于序列化 {@link LocalDate} 的格式化器,不可为 {@code null}。
*/
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
this.dateTimeFormatter = formatter;
}
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, LocalDate value) throws IOException {
out.value(dateTimeFormatter.format(value));
}
/** {@inheritDoc} */
@Override
public LocalDate read(JsonReader in) throws IOException {
return LocalDate.parse(in.nextString(), dateTimeFormatter);
}
}
/**
* {@code LocalDateTime} 的 {@code TypeAdapter}
* 用于 Gson 对 {@code LocalDateTime} 进行相互转换。
*/
public static final class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
private final DateTimeFormatter dateTimeFormatter;
/**
* 默认构造函数,
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} 进行 {@link LocalDateTime} 的序列化与反序列化。
*/
public LocalDateTimeTypeAdapter() {
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
}
/**
* 构造函数,
* 使用传入的 {@link DateTimeFormatter} 进行 {@link LocalDateTime} 的序列化与反序列化。
*
* @param formatter 用于序列化 {@link LocalDateTime} 的格式化器,不可为 {@code null}。
*/
public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
this.dateTimeFormatter = formatter;
}
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, LocalDateTime value) throws IOException {
out.value(dateTimeFormatter.format(value));
}
/** {@inheritDoc} */
@Override
public LocalDateTime read(JsonReader in) throws IOException {
return LocalDateTime.parse(in.nextString(), dateTimeFormatter);
}
}
/**
* {@code ZonedDateTime} 的 {@code TypeAdapter}
* 用于 Gson 对 {@code ZonedDateTime} 进行相互转换。
*/
public static final class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
private final DateTimeFormatter dateTimeFormatter;
/**
* 默认构造函数,
* 使用 {@link DateTimeFormatter#ISO_ZONED_DATE_TIME} 进行 {@link ZonedDateTime} 的序列化与反序列化。
*/
public ZonedDateTimeTypeAdapter() {
this.dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
}
/**
* 构造函数,
* 使用传入的 {@link DateTimeFormatter} 进行 {@link ZonedDateTime} 的序列化与反序列化。
*
* @param formatter 用于序列化 {@link ZonedDateTime} 的格式化器,不可为 {@code null}。
*/
public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) {
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
this.dateTimeFormatter = formatter;
}
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, ZonedDateTime value) throws IOException {
out.value(dateTimeFormatter.format(value));
}
/** {@inheritDoc} */
@Override
public ZonedDateTime read(JsonReader in) throws IOException {
return ZonedDateTime.parse(in.nextString(), dateTimeFormatter);
}
}
/**
* {@code Instant} 的 {@code TypeAdapter}
* 用于 Gson 对 {@code Instant} 进行相互转换。
*
* <p>
* 使用 {@link DateTimeFormatter#ISO_INSTANT} 进行 {@link Instant} 的序列化与反序列化。
*
*/
public static final class InstantTypeAdapter extends TypeAdapter<Instant> {
/** {@inheritDoc} */
@Override
public void write(JsonWriter out, Instant value) throws IOException {
out.value(DateTimeFormatter.ISO_INSTANT.format(value));
}
/** {@inheritDoc} */
@Override
public Instant read(JsonReader in) throws IOException {
return Instant.parse(in.nextString());
}
}
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

@@ -40,7 +40,6 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
*
* <p>
* 中国第二代居民身份证号
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@@ -37,7 +37,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

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} 对返回给前端的数据进行封装,包含 code、message、data。
* </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

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

View File

@@ -33,7 +33,6 @@ import javax.annotation.Nullable;
*
* <p>
* 数组工具类
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

View File

@@ -29,7 +29,6 @@ import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
*
* <p>
* 本工具类不封装过多判断逻辑,鼓励充分使用项目中的工具类进行逻辑判断。
* </p>
*
* <pre>
* AssertTools.checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");

View File

@@ -28,7 +28,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

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

@@ -34,7 +34,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

View File

@@ -26,7 +26,6 @@ import java.util.concurrent.ConcurrentHashMap;
*
* <p>
* 生成 UUID 和 修改版雪花IDSeata 版本)
* </p>
*
* @see UUID
* @see IdWorker

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

@@ -26,7 +26,7 @@ import java.util.concurrent.ThreadLocalRandom;
* 随机工具类
* <p>
* 建议调用方自行维护 Random 对象
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public final class RandomTools {

View File

@@ -31,7 +31,6 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
*
* <p>
* 字符串工具类。
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0

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

@@ -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

@@ -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>

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>