1 Commits

Author SHA1 Message Date
ba3266aaea refactor!: 重构正则表达式相关代码
- 将正则相关根据移至 regex 包下
- 新增 PatternInfos 记录不同 pattern 的信息
- 新增 Chinese2ndIdCardNumberMatcher 和 LocalDateMatcher 作为日期和二代居民身份证的匹配结果,方便从中获取对应的 group
2025-07-22 14:58:29 +08:00
85 changed files with 710 additions and 212 deletions

View File

@@ -68,16 +68,16 @@ System.out.println(result); // Output: Return string
`RegexConsts` 包含常见正则表达式;`PatternConsts` 包含对应的 `Pattern` 对象
## 五、exception - 异常
### 1. IMultiTypesException - 多类型异常
### 1. MultiTypesException - 多类型异常
异常在不同场景下被抛出,可以用不同的枚举值,表示不同的场景类型。
异常实现 `IMultiTypesException` 的 `IMultiTypesException#getType` 方法,返回对应的场景类型。
异常实现 `MultiTypesException` 的 `MultiTypesException#getType` 方法,返回对应的场景类型。
表示场景类型的枚举实现 `IMultiTypesException.IExceptionType`,其中的工厂方法用于创建对应类型的异常。
表示场景类型的枚举实现 `MultiTypesException.ExceptionType`,其中的工厂方法用于创建对应类型的异常。
```java
public final class LoginException
extends RuntimeException
implements IMultiTypesException<LoginException, LoginException.Type, String> {
implements MultiTypesException<LoginException, LoginException.Type> {
private static final long serialVersionUID = 881293090625085616L;
private final Type type;
private LoginException(@Nonnull Type type, @Nonnull String message) {
@@ -104,7 +104,7 @@ public final class LoginException
// ...
public enum Type implements IExceptionType<LoginException, String> {
public enum Type implements ExceptionType {
DEFAULT("00", "当前会话未登录"),
NOT_TOKEN("10", "未提供token"),
INVALID_TOKEN("20", "token无效"),
@@ -211,7 +211,7 @@ throw LoginException.Type.TOKEN_TIMEOUT.create();
#### 2. UnifiedResponse
UnifiedResponse 对返回给前端的数据进行封装,包含 `code`、`message`、`data。`
可使用 `UnifiedResponses` 快速构建 `UnifiedResponse` 对象。 `UnifiedResponses` 默认的成功代码为 "2000000" 用户按测试类 `CustomUnifiedResponseFactoryTests` 中所示范的,继承 `UnifiedResponses` 实现自己的工厂类, 自定义 `SUCCESS_CODE` 和 `DEFAULT_SUCCESS_MSG` 和工厂方法。 见 [issue#22](http://gitea.zhouxy.xyz/plusone/plusone-commons/issues/22)。
可使用 `UnifiedResponses` 快速构建 `UnifiedResponse` 对象。 `UnifiedResponses` 默认的成功代码为 "2000000" 用户按测试类 `CustomUnifiedResponseFactoryTests` 中所示范的,继承 `UnifiedResponses` 实现自己的工厂类, 自定义 `SUCCESS_CODE` 和 `DEFAULT_SUCCESS_MSG` 和工厂方法。 见 [issue#22](http://zhouxy.xyz:3000/plusone/plusone-commons/issues/22)。
## 八、time - 时间 API
### 1. 季度

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>
@@ -17,6 +17,7 @@
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>

View File

@@ -27,7 +27,7 @@ import java.lang.annotation.Target;
* <p>
* 标识方法是读方法,如 getter。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see WriterMethod
*/

View File

@@ -26,7 +26,7 @@ import java.lang.annotation.Target;
*
* <p>标识方法为静态工厂方法
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
@Target(ElementType.METHOD)

View File

@@ -27,7 +27,7 @@ import java.lang.annotation.Documented;
*
* <p>标识方法为不支持的操作。该方法将抛出 {@link UnsupportedOperationException}。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @version 1.0
* @since 1.0.0
* @see UnsupportedOperationException

View File

@@ -25,7 +25,7 @@ import java.lang.annotation.Target;
/**
* ValueObject - 值对象
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
@Inherited

View File

@@ -25,7 +25,7 @@ import java.lang.annotation.Target;
* 标识该方法是虚方法。
* <p>该注解用于提醒、强调父类虽然有默认实现,但子类可以根据自己的需要覆写。</p>
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
@Target(ElementType.METHOD)

View File

@@ -27,7 +27,7 @@ import java.lang.annotation.Target;
* <p>
* 标识方法是写方法,如 setter。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see ReaderMethod
*/

View File

@@ -54,6 +54,6 @@
* <p>
* 标记一个类,表示其作为值对象,区别于 Entity。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
package xyz.zhouxy.plusone.commons.annotation;

View File

@@ -26,7 +26,7 @@ import javax.annotation.Nullable;
* 用于像自定义异常等需要带有 {@code code} 字段的类,
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public interface IWithCode<T> {

View File

@@ -25,7 +25,7 @@ import javax.annotation.Nullable;
* 用于像自定义异常等需要带有 {@code code} 字段的类,
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public interface IWithIntCode {

View File

@@ -25,7 +25,7 @@ import javax.annotation.Nullable;
* 用于像自定义异常等需要带有 {@code code} 字段的类,
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public interface IWithLongCode {

View File

@@ -67,7 +67,7 @@ import javax.annotation.Nullable;
* System.out.println(result); // Output: Return string
* </pre>
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public final class Ref<T> {

View File

@@ -66,7 +66,7 @@
* 类似于枚举这样的类型,通常需要设置固定的码值表示对应的含义。
* 可实现 {@link IWithCode}、{@link IWithIntCode}、{@link IWithLongCode},便于在需要的地方对这些接口的实现进行处理。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@CheckReturnValue
@ParametersAreNonnullByDefault

View File

@@ -33,7 +33,7 @@ import com.google.common.collect.Table;
/**
* 集合工具类
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class CollectionTools {

View File

@@ -22,7 +22,7 @@
* </h3>
* 集合工具类
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.collection;

View File

@@ -19,7 +19,7 @@ package xyz.zhouxy.plusone.commons.exception;
/**
* 数据不存在异常
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public final class DataNotExistsException extends Exception {

View File

@@ -1,61 +0,0 @@
/*
* 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.exception;
import javax.annotation.Nonnull;
/**
* 异常工厂
*
* @author ZhouXY
*/
public interface IExceptionFactory<X extends Exception> {
/**
* 创建异常
*
* @return 异常对象
*/
@Nonnull
X create();
/**
* 使用指定 {@code message} 创建异常
*
* @param message 异常信息
* @return 异常对象
*/
@Nonnull
X create(String message);
/**
* 使用指定 {@code cause} 创建异常
*
* @param cause 包装的异常
* @return 异常对象
*/
@Nonnull
X create(Throwable cause);
/**
* 使用指定 {@code message} 和 {@code cause} 创建异常
*
* @param message 异常信息
* @param cause 包装的异常
* @return 异常对象
*/
@Nonnull
X create(String message, Throwable cause);
}

View File

@@ -15,30 +15,26 @@
*/
package xyz.zhouxy.plusone.commons.exception;
import java.io.Serializable;
import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.annotation.Virtual;
import xyz.zhouxy.plusone.commons.base.IWithCode;
/**
* IMultiTypesException
* MultiTypesException
*
* <p>
* 异常在不同场景下被抛出可以用不同的枚举值表示不同的场景类型
*
* <p>
* 异常实现 {@link IMultiTypesException} {@link #getType} 方法返回对应的场景类型
* 异常实现 {@link MultiTypesException} {@link #getType} 方法返回对应的场景类型
*
* <p>
* 表示场景类型的枚举实现 {@link IExceptionType}各个枚举值本身就是该场景的异常的工厂实例
* 使用其中的工厂方法用于创建对应类型的异常
* 表示场景类型的枚举实现 {@link ExceptionType}其中的工厂方法用于创建对应类型的异常
*
* <pre>
* public final class LoginException
* extends RuntimeException
* implements IMultiTypesException&lt;LoginException, LoginException.Type, String&gt; {
* 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) {
@@ -65,7 +61,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
*
* // ...
*
* public enum Type implements IExceptionType&lt;LoginException, String&gt; {
* public enum Type implements ExceptionType&lt;LoginException&gt; {
* DEFAULT("00", "当前会话未登录"),
* NOT_TOKEN("10", "未提供token"),
* INVALID_TOKEN("20", "token无效"),
@@ -122,20 +118,15 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
* throw LoginException.Type.TOKEN_TIMEOUT.create();
* </pre>
*
* @param <X> 具体异常类
* @param <T> 异常场景
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public interface IMultiTypesException<
X extends Exception,
T extends IMultiTypesException.IExceptionType<X, TCode>,
TCode extends Serializable> {
public interface MultiTypesException<E extends Exception, T extends MultiTypesException.ExceptionType<E>> {
/**
* 异常类型
*
* @return 异常类型通常是实现了 {@link IExceptionType} 的枚举
* @return 异常类型通常是实现了 {@link ExceptionType} 的枚举
*/
@Nonnull
T getType();
@@ -145,25 +136,55 @@ public interface IMultiTypesException<
*
* @return 异常类型编码
*/
default @Nonnull TCode getTypeCode() {
default @Nonnull String getTypeCode() {
return getType().getCode();
}
/**
* 异常类型
*/
public static interface IExceptionType<X extends Exception, TCode extends Serializable>
extends IWithCode<TCode>, IExceptionFactory<X> {
public static interface ExceptionType<E extends Exception> extends IWithCode<String> {
/**
* 默认异常信息
*/
String getDefaultMessage();
@Virtual
default String getDescription() {
return getDefaultMessage();
}
/**
* 创建异常
*
* @return 异常对象
*/
@Nonnull
E create();
/**
* 使用指定 {@code message} 创建异常
*
* @param message 异常信息
* @return 异常对象
*/
@Nonnull
E create(String message);
/**
* 使用指定 {@code cause} 创建异常
*
* @param cause 包装的异常
* @return 异常对象
*/
@Nonnull
E create(Throwable cause);
/**
* 使用指定 {@code message} {@code cause} 创建异常
*
* @param message 异常信息
* @param cause 包装的异常
* @return 异常对象
*/
@Nonnull
E create(String message, Throwable cause);
}
}

View File

@@ -21,7 +21,7 @@ import java.time.format.DateTimeParseException;
import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException.IExceptionType;
import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
/**
* 解析失败异常
@@ -34,12 +34,12 @@ import xyz.zhouxy.plusone.commons.exception.IMultiTypesException.IExceptionType;
* throw new RequestParamsException(ParsingFailureException.Type.NUMBER_PARSING_FAILURE.create());
* </pre>
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public final class ParsingFailureException
extends Exception
implements IMultiTypesException<ParsingFailureException, ParsingFailureException.Type, String> {
implements MultiTypesException<ParsingFailureException, ParsingFailureException.Type> {
private static final long serialVersionUID = 795996090625132616L;
private final Type type;
@@ -171,7 +171,7 @@ public final class ParsingFailureException
/** XML 解析失败 */
public static final Type XML_PARSING_FAILURE = Type.XML_PARSING_FAILURE;
public enum Type implements IExceptionType<ParsingFailureException, String> {
public enum Type implements ExceptionType<ParsingFailureException> {
DEFAULT("00", "解析失败"),
NUMBER_PARSING_FAILURE("10", "数字转换失败"),
DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),

View File

@@ -25,7 +25,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
* <p>
* <b>NOTE: 通常表示业务中的意外情况。如:用户错误输入、缺失必填字段、用户余额不足等。</b>
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class BizException extends RuntimeException {

View File

@@ -18,8 +18,8 @@ package xyz.zhouxy.plusone.commons.exception.business;
import javax.annotation.Nonnull;
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException.IExceptionType;
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException;
import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
/**
* InvalidInputException
@@ -30,12 +30,12 @@ import xyz.zhouxy.plusone.commons.exception.IMultiTypesException;
* <p>
* <b>NOTE: 属业务异常</b>
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public final class InvalidInputException
extends RequestParamsException
implements IMultiTypesException<InvalidInputException, InvalidInputException.Type, String> {
implements MultiTypesException<InvalidInputException, InvalidInputException.Type> {
private static final long serialVersionUID = -28994090625082516L;
private final Type type;
@@ -109,7 +109,7 @@ public final class InvalidInputException
return this.type;
}
public enum Type implements IExceptionType<InvalidInputException, String> {
public enum Type implements ExceptionType<InvalidInputException> {
DEFAULT("00", "用户输入内容非法"),
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),

View File

@@ -22,7 +22,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
* <p>
* 用户请求参数错误
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class RequestParamsException extends BizException {

View File

@@ -17,6 +17,6 @@
/**
* 业务异常
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
package xyz.zhouxy.plusone.commons.exception.business;

View File

@@ -17,21 +17,20 @@
/**
* <h2>异常</h2>
*
* <h3>1. {@link IMultiTypesException} - 多类型异常</h3>
* <h3>1. {@link MultiTypesException} - 多类型异常</h3>
* <p>
* 异常在不同场景下被抛出,可以用不同的枚举值,表示不同的场景类型。
*
* <p>
* 异常实现 {@link IMultiTypesException} 的 {@link IMultiTypesException#getType} 方法,返回对应的场景类型。
* 异常实现 {@link MultiTypesException} 的 {@link MultiTypesException#getType} 方法,返回对应的场景类型。
*
* <p>
* 表示场景类型的枚举实现 {@link IMultiTypesException.IExceptionType}各个枚举值本身就是该场景的异常的工厂实例,
* 使用其中的工厂方法用于创建对应类型的异常。
* 表示场景类型的枚举实现 {@link MultiTypesException.ExceptionType}其中的工厂方法用于创建对应类型的异常。
*
* <pre>
* public final class LoginException
* extends RuntimeException
* implements IMultiTypesException&lt;LoginException, LoginException.Type, String&gt; {
* 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) {
@@ -58,7 +57,7 @@
*
* // ...
*
* public enum Type implements IExceptionType&lt;LoginException, String&gt; {
* public enum Type implements ExceptionType&lt;LoginException&gt; {
* DEFAULT("00", "当前会话未登录"),
* NOT_TOKEN("10", "未提供token"),
* INVALID_TOKEN("20", "token无效"),
@@ -121,7 +120,7 @@
* <h3>3. 系统异常</h3>
* 预设常见的系统异常。可继承 {@link SysException} 自定义系统异常。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
package xyz.zhouxy.plusone.commons.exception;

View File

@@ -27,7 +27,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
* 当出现这种始料未及的诡异情况时,抛出 {@link DataOperationResultException} 并回滚事务。
* 后续需要排查原因。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public final class DataOperationResultException extends SysException {

View File

@@ -22,7 +22,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
* <p>
* 在无法找到可访问的 Mac 地址时抛出
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class NoAvailableMacFoundException extends SysException {

View File

@@ -22,7 +22,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
* <p>
* 通常表示应用代码存在问题,或因环境问题,引发异常。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class SysException extends RuntimeException {

View File

@@ -17,6 +17,6 @@
/**
* 系统异常
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
package xyz.zhouxy.plusone.commons.exception.system;

View File

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

View File

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

View File

@@ -24,7 +24,7 @@ package xyz.zhouxy.plusone.commons.function;
*
* @param <E> 可抛出的异常类型
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
@FunctionalInterface

View File

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

View File

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

View File

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

View File

@@ -25,7 +25,7 @@ package xyz.zhouxy.plusone.commons.function;
* @param <R> 返回结果类型
* @param <E> 异常类型
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0
* @see java.util.function.Function
*/

View File

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

View File

@@ -25,7 +25,7 @@ package xyz.zhouxy.plusone.commons.function;
* @param <T> 结果类型
* @param <E> 异常类型
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see java.util.function.Supplier
*/

View File

@@ -25,7 +25,7 @@ import java.util.function.BiFunction;
* <p>
* 接受类型为 T 和 U 的两个参数,返回 {@code Optional&lt;R&gt;} 对象。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see Optional
* @see BiFunction

View File

@@ -25,7 +25,7 @@ import java.util.function.Function;
* <p>
* 接受类型为 T 的参数,返回 {@code Optional&lt;R&gt;} 对象。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see Optional
* @see Function

View File

@@ -39,6 +39,6 @@
* | Optional | ToOptionalFunction | Optional&lt;R&gt; apply(T) |
* </pre>
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
package xyz.zhouxy.plusone.commons.function;

View File

@@ -33,7 +33,7 @@ import com.google.gson.stream.JsonWriter;
/**
* 包含 JSR-310 相关数据类型的 {@code TypeAdapter}
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.1.0
* @see TypeAdapter
* @see com.google.gson.GsonBuilder

View File

@@ -33,7 +33,7 @@ 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.regex.PatternConsts;
import xyz.zhouxy.plusone.commons.util.StringTools;
/**
@@ -42,9 +42,9 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
* <p>
* 中国第二代居民身份证号
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see xyz.zhouxy.plusone.commons.constant.PatternConsts#CHINESE_2ND_ID_CARD_NUMBER
* @see xyz.zhouxy.plusone.commons.regex.PatternConsts#CHINESE_2ND_ID_CARD_NUMBER
*/
@ValueObject
@Immutable

View File

@@ -23,7 +23,7 @@ import xyz.zhouxy.plusone.commons.base.IWithIntCode;
/**
* 性别
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public enum Gender implements IWithIntCode {
UNKNOWN(0, "Unknown", "未知"),

View File

@@ -24,7 +24,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
/**
* 身份证号
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public interface IDCardNumber {

View File

@@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
/**
* 带校验的字符串值对象
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*
* @deprecated 弃用。使用工厂方法创建对象,并在其中进行校验即可。

View File

@@ -29,7 +29,7 @@ import xyz.zhouxy.plusone.commons.collection.CollectionTools;
*
* @param <T> 内容列表的元素类型
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @see PagingAndSortingQueryParams
*/
public class PageResult<T> {

View File

@@ -39,7 +39,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
* 根据传入的 {@code size} 和 {@code pageNum}
* 提供 {@code getOffset} 方法计算 SQL 语句中 {@code offset} 的值。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @see PagingParams
* @see PageResult
*/

View File

@@ -24,7 +24,7 @@ import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams.Sortable
/**
* 分页参数
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @see PagingAndSortingQueryParams
*/
public class PagingParams {

View File

@@ -22,7 +22,7 @@ import javax.annotation.Nullable;
/**
* 统一结果,对返回给前端的数据进行封装。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class UnifiedResponse<T> {

View File

@@ -21,7 +21,7 @@ import javax.annotation.Nullable;
/**
* UnifiedResponse 工厂
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see UnifiedResponse
*/

View File

@@ -60,7 +60,7 @@
* 自定义 SUCCESS_CODE 和 DEFAULT_SUCCESS_MSG 和工厂方法。
* 见 <a href="http://zhouxy.xyz:3000/plusone/plusone-commons/issues/22">issue#22</a>。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.model.dto;

View File

@@ -19,7 +19,7 @@
* <p>
* 包含业务建模可能用到的性别、身份证等元素,也包含 DTO 相关类,如分页查询参数,响应结果,分页结果等。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.model;

View File

@@ -0,0 +1,38 @@
/*
* 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.regex;
import java.util.regex.Matcher;
public abstract class AbstractMatcher {
private final Matcher matcher;
AbstractMatcher(Matcher matcher) {
this.matcher = matcher;
}
public final Matcher matcher() {
return this.matcher;
}
public final boolean matches() {
return this.matcher.matches();
}
public final String getGroupValue(String groupName) {
return this.matcher.group(groupName);
}
}

View File

@@ -0,0 +1,60 @@
/*
* 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.regex;
import java.util.regex.Matcher;
import xyz.zhouxy.plusone.commons.model.Gender;
public final class Chinese2ndIdCardNumberMatcher extends AbstractMatcher {
Chinese2ndIdCardNumberMatcher(Matcher matcher) {
super(matcher);
}
public final String getProvince() {
return getGroupValue("province");
}
public final String getCity() {
return getGroupValue("city");
}
public final String getCounty() {
return getGroupValue("county");
}
public final String getBirthDate() {
return getGroupValue("birthDate");
}
public final String getOrderCode() {
return getGroupValue("orderCode");
}
public final String getGenderCode() {
return getGroupValue("gender");
}
public final Gender getGender() {
final int genderCode = Integer.parseInt(getGenderCode());
return genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE;
}
public final String getCheckDigit() {
return getGroupValue("checkDigit");
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.regex;
import java.util.regex.Matcher;
public final class LocalDateMatcher extends AbstractMatcher {
LocalDateMatcher(Matcher matcher) {
super(matcher);
}
public String getYear() {
return getGroupValue("yyyy");
}
public String getMonth() {
return getGroupValue("MM");
}
public String getDay() {
return getGroupValue("dd");
}
}

View File

@@ -14,14 +14,14 @@
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.constant;
package xyz.zhouxy.plusone.commons.regex;
import java.util.regex.Pattern;
/**
* 正则表达式常量
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @see RegexConsts
* @see xyz.zhouxy.plusone.commons.util.RegexTools
*/

View File

@@ -0,0 +1,38 @@
/*
* 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.regex;
import java.util.regex.Pattern;
public abstract class PatternInfo<T> {
private final String regex;
private final Pattern pattern;
PatternInfo(String regex, Pattern pattern) {
this.regex = regex;
this.pattern = pattern;
}
public final String regex() {
return this.regex;
}
public final Pattern pattern() {
return this.pattern;
}
public abstract T matcher(String input);
}

View File

@@ -0,0 +1,114 @@
/*
* 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.regex;
import java.util.regex.Matcher;
public final class PatternInfos {
// @see RegexConsts#BASIC_ISO_DATE
public static final PatternInfo<LocalDateMatcher> BASIC_ISO_DATE = new PatternInfo<LocalDateMatcher>(
RegexConsts.BASIC_ISO_DATE,
PatternConsts.BASIC_ISO_DATE) {
@Override
public LocalDateMatcher matcher(String input) {
Matcher matcher = pattern().matcher(input);
return new LocalDateMatcher(matcher);
}
};
// @see RegexConsts#ISO_LOCAL_DATE
public static final PatternInfo<LocalDateMatcher> ISO_LOCAL_DATE = new PatternInfo<LocalDateMatcher>(
RegexConsts.ISO_LOCAL_DATE,
PatternConsts.ISO_LOCAL_DATE) {
@Override
public LocalDateMatcher matcher(String input) {
Matcher matcher = pattern().matcher(input);
return new LocalDateMatcher(matcher);
}
};
// @see RegexConsts#PASSWORD
public static final PatternInfo<Matcher> PASSWORD = new PatternInfo<Matcher>(
RegexConsts.PASSWORD,
PatternConsts.PASSWORD) {
@Override
public Matcher matcher(String input) {
return pattern().matcher(input);
}
};
// @see RegexConsts#CAPTCHA
public static final PatternInfo<Matcher> CAPTCHA = new PatternInfo<Matcher>(
RegexConsts.CAPTCHA,
PatternConsts.CAPTCHA) {
@Override
public Matcher matcher(String input) {
return pattern().matcher(input);
}
};
// @see RegexConsts#EMAIL
public static final PatternInfo<Matcher> EMAIL = new PatternInfo<Matcher>(
RegexConsts.EMAIL,
PatternConsts.EMAIL) {
@Override
public Matcher matcher(String input) {
return pattern().matcher(input);
}
};
public static final PatternInfo<Matcher> MOBILE_PHONE = new PatternInfo<Matcher>(
RegexConsts.MOBILE_PHONE,
PatternConsts.MOBILE_PHONE) {
@Override
public Matcher matcher(String input) {
return pattern().matcher(input);
}
};
public static final PatternInfo<Matcher> USERNAME = new PatternInfo<Matcher>(
RegexConsts.USERNAME,
PatternConsts.USERNAME) {
@Override
public Matcher matcher(String input) {
return pattern().matcher(input);
}
};
public static final PatternInfo<Matcher> NICKNAME = new PatternInfo<Matcher>(
RegexConsts.NICKNAME,
PatternConsts.NICKNAME) {
@Override
public Matcher matcher(String input) {
return pattern().matcher(input);
}
};
public static final PatternInfo<Chinese2ndIdCardNumberMatcher> CHINESE_2ND_ID_CARD_NUMBER = new PatternInfo<Chinese2ndIdCardNumberMatcher>(
RegexConsts.CHINESE_2ND_ID_CARD_NUMBER,
PatternConsts.CHINESE_2ND_ID_CARD_NUMBER) {
@Override
public Chinese2ndIdCardNumberMatcher matcher(String input) {
Matcher matcher = pattern().matcher(input);
return new Chinese2ndIdCardNumberMatcher(matcher);
}
};
private PatternInfos() {
throw new IllegalStateException("Utility class");
}
}

View File

@@ -14,12 +14,12 @@
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.constant;
package xyz.zhouxy.plusone.commons.regex;
/**
* 正则表达式常量
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @see PatternConsts
*/
public final class RegexConsts {
@@ -41,12 +41,15 @@ public final class RegexConsts {
public static final String MOBILE_PHONE = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
public static final String USERNAME = "^[\\w-_.@]{4,36}$";
public static final String USERNAME = "^[\\w-_]{4,36}$";
public static final String NICKNAME = "^[\\w-_.@]{4,36}$";
public static final String CHINESE_2ND_ID_CARD_NUMBER
= "^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})(?<birthDate>\\d{8})\\d{2}(?<gender>\\d)([\\dX])$";
= "^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})"
+ "(?<birthDate>\\d{8})"
+ "(?<orderCode>\\d{2}(?<gender>\\d))"
+ "(?<checkDigit>[\\dX])$";
private RegexConsts() {
throw new IllegalStateException("Utility class");

View File

@@ -14,16 +14,4 @@
* limitations under the License.
*/
/**
* <h2>常量</h2>
*
* <h3>
* 1. 正则常量
* </h3>
* {@link RegexConsts} 包含常见正则表达式{@link PatternConsts} 包含对应的 {@link Pattern} 对象
*
* @author ZhouXY108 <luquanlion@outlook.com>
*/
package xyz.zhouxy.plusone.commons.constant;
import java.util.regex.Pattern;
package xyz.zhouxy.plusone.commons.regex;

View File

@@ -32,7 +32,7 @@ import xyz.zhouxy.plusone.commons.base.IWithIntCode;
/**
* 季度
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public enum Quarter implements IWithIntCode {
/** 第一季度 */

View File

@@ -37,7 +37,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
/**
* 表示年份与季度
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@Immutable
public final class YearQuarter implements Comparable<YearQuarter>, Serializable {

View File

@@ -22,7 +22,7 @@
* 模仿 JDK 的 {@link java.time.Month} 和 {@link java.time.YearMonth}
* 实现 {@link Quarter}{@link YearQuarter},对季度进行建模。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.time;

View File

@@ -37,7 +37,7 @@ import javax.annotation.Nullable;
* <p>
* 数组工具类
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class ArrayTools {

View File

@@ -39,7 +39,7 @@ import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
* "must be a well-formed email address");
* </pre>
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class AssertTools {

View File

@@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
* <p>
* BigDecimal 工具类
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class BigDecimals {

View File

@@ -40,7 +40,7 @@ import xyz.zhouxy.plusone.commons.time.YearQuarter;
/**
* 日期时间工具类
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class DateTimeTools {

View File

@@ -26,7 +26,7 @@ import javax.annotation.Nullable;
/**
* 枚举工具类
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public final class EnumTools {

View File

@@ -35,7 +35,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
*
* 参考 <a href="https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/">Enumeration classes</a>
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @deprecated 设计 Enumeration 的灵感来自于 .net 社区,因为 C&num; 的枚举不带行为。
* 但 Java 的枚举可以带行为,故大多数情况下不需要这种设计。
*/

View File

@@ -30,7 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
*
* @see UUID
* @see IdWorker
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class IdGenerator {

View File

@@ -19,7 +19,7 @@ package xyz.zhouxy.plusone.commons.util;
/**
* Joda-Time 工具类
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class JodaTimeTools {

View File

@@ -25,7 +25,7 @@ import javax.annotation.Nullable;
/**
* 数字工具类
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class Numbers {

View File

@@ -31,7 +31,7 @@ import com.google.common.annotations.Beta;
* <p>
* 提供一些 Optional 相关的方法
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
* @see Optional
* @see OptionalInt

View File

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

View File

@@ -35,7 +35,7 @@ import com.google.common.cache.LoadingCache;
/**
* 封装一些常用的正则操作,并可以缓存 {@link Pattern} 实例以复用。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public final class RegexTools {

View File

@@ -26,7 +26,7 @@ import javax.annotation.Nullable;
import com.google.common.annotations.Beta;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.regex.PatternConsts;
/**
* StringTools
@@ -34,7 +34,7 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
* <p>
* 字符串工具类。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class StringTools {

View File

@@ -32,7 +32,7 @@ import javax.annotation.Nullable;
/**
* TreeBuilder
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 1.0.0
*/
public class TreeBuilder<T, TSubTree extends T, TIdentity> {

View File

@@ -19,7 +19,7 @@
* <p>
* 包含树构建器({@link TreeBuilder})、断言工具({@link AssertTools}、ID 生成器({@link IdGenerator})及其它实用工具类。
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@ParametersAreNonnullByDefault
package xyz.zhouxy.plusone.commons.util;

View File

@@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.regex.PatternConsts;
import java.util.Arrays;
import java.util.Collections;

View File

@@ -220,7 +220,7 @@ public class PagingAndSortingQueryParamsTests {
/**
* 账号信息查询参数
*
* @author ZhouXY108 <luquanlion@outlook.com>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
@ToString(callSuper = true)
class AccountQueryParams extends PagingAndSortingQueryParams {

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package xyz.zhouxy.plusone.commons.constant;
package xyz.zhouxy.plusone.commons.regex;
import static org.junit.jupiter.api.Assertions.*;

View File

@@ -0,0 +1,259 @@
/*
* Copyright 2024-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.regex;
import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.regex.Matcher;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.commons.model.Gender;
@Slf4j
public //
class PatternInfosTests {
// ================================
// #region - BASIC_ISO_DATE
// ================================
@Test
void testBasicIsoDate_ValidDate() {
LocalDateMatcher localDateMatcher = PatternInfos.BASIC_ISO_DATE.matcher("20241229");
Matcher matcher = localDateMatcher.matcher();
assertTrue(matcher.matches());
assertTrue(localDateMatcher.matches());
assertEquals("2024", localDateMatcher.getYear());
assertEquals("12", localDateMatcher.getMonth());
assertEquals("29", localDateMatcher.getDay());
}
@ParameterizedTest
@ValueSource(strings = {
"20231301", // InvalidMonth
"20230230", // InvalidDay
"20210229", // NonLeapYearFeb29
})
void testBasicIsoDate_InvalidDate_butMatches(String date) {
// 虽然日期有误,但这个正则无法判断。实际工作中,应使用日期时间 API。
LocalDateMatcher matcher = PatternInfos.BASIC_ISO_DATE.matcher(date);
assertTrue(matcher.matches());
}
@ParameterizedTest
@ValueSource(strings = {
"2023041", // TooShort
"99999999990415", // TooLong
"2023-04-15", // NonNumeric
})
void testBasicIsoDate_InvalidDate_Mismatches(String date) {
LocalDateMatcher matcher = PatternInfos.BASIC_ISO_DATE.matcher(date);
assertFalse(matcher.matches());
}
// ================================
// #endregion - BASIC_ISO_DATE
// ================================
// ================================
// #region - ISO_LOCAL_DATE
// ================================
@Test
void testIsoLocalDate_ValidDate() {
LocalDateMatcher matcher = PatternInfos.ISO_LOCAL_DATE.matcher("2024-12-29");
assertTrue(matcher.matches());
assertEquals("2024", matcher.getYear());
assertEquals("12", matcher.getMonth());
assertEquals("29", matcher.getDay());
// LeapYearFeb29()
assertTrue(PatternInfos.ISO_LOCAL_DATE.matcher("2020-02-29").matches());
// BoundaryMin()
assertTrue(PatternInfos.ISO_LOCAL_DATE.matcher("0000-01-01").matches());
// BoundaryMax()
assertTrue(PatternInfos.ISO_LOCAL_DATE.matcher("999999999-12-31").matches());
}
@ParameterizedTest
@ValueSource(strings = {
"2023-13-01", // InvalidMonth
"2023-02-30", // InvalidDay
"2021-02-29", // NonLeapYearFeb29
})
void testIsoLocalDate_InvalidDate_butMatches(String date) {
// 虽然日期有误,但这个正则无法判断。实际工作中,应使用日期时间 API。
LocalDateMatcher matcher = PatternInfos.ISO_LOCAL_DATE.matcher(date);
assertTrue(matcher.matches());
}
@ParameterizedTest
@ValueSource(strings = {
"2023-04-1", // TooShort
"9999999999-04-15", // TooLong
"20230415",
})
void testIsoLocalDate_InvalidDate_Mismatches(String date) {
LocalDateMatcher matcher = PatternInfos.ISO_LOCAL_DATE.matcher(date);
assertFalse(matcher.matches());
}
// ================================
// #endregion - ISO_LOCAL_DATE
// ================================
// ================================
// #region - PASSWORD
// ================================
@Test
void testPassword_ValidPassword_Matches() {
assertTrue(PatternInfos.PASSWORD.matcher("Abc123!@#").matches());
}
@Test
void testPassword_InvalidPassword_Mismatches() {
assertFalse(PatternInfos.PASSWORD.matcher("Abc123 !@#").matches()); // 带空格
assertFalse(PatternInfos.PASSWORD.matcher("Abc123!@# ").matches()); // 带空格
assertFalse(PatternInfos.PASSWORD.matcher(" Abc123!@#").matches()); // 带空格
assertFalse(PatternInfos.PASSWORD.matcher(" Abc123!@# ").matches()); // 带空格
assertFalse(PatternInfos.PASSWORD.matcher("77553366998844113322").matches()); // 纯数字
assertFalse(PatternInfos.PASSWORD.matcher("poiujhgbfdsazxcfvghj").matches()); // 纯小写字母
assertFalse(PatternInfos.PASSWORD.matcher("POIUJHGBFDSAZXCFVGHJ").matches()); // 纯大写字母
assertFalse(PatternInfos.PASSWORD.matcher("!#$%&'*\\+-/=?^`{|}~@()[]\",.;':").matches()); // 纯特殊字符
assertFalse(PatternInfos.PASSWORD.matcher("sdfrghbv525842582752").matches()); // 没有小写字母
assertFalse(PatternInfos.PASSWORD.matcher("SDFRGHBV525842582752").matches()); // 没有小写字母
assertFalse(PatternInfos.PASSWORD.matcher("sdfrghbvSDFRGHBV").matches()); // 没有数字
assertFalse(PatternInfos.PASSWORD.matcher("Abc1!").matches()); // 太短
assertFalse(PatternInfos.PASSWORD.matcher("Abc1!Abc1!Abc1!Abc1!Abc1!Abc1!Abc1!").matches()); // 太长
assertFalse(PatternInfos.PASSWORD.matcher("").matches());
assertFalse(PatternInfos.PASSWORD.matcher(" ").matches());
}
// ================================
// #endregion - PASSWORD
// ================================
// ================================
// #region - EMAIL
// ================================
@Test
public void testValidEmails() {
assertTrue(PatternInfos.EMAIL.matcher("test@example.com").matches());
assertTrue(PatternInfos.EMAIL.matcher("user.name+tag+sorting@example.com").matches());
assertTrue(PatternInfos.EMAIL.matcher("user@sub.example.com").matches());
assertTrue(PatternInfos.EMAIL.matcher("user@123.123.123.123").matches());
}
@Test
public void testInvalidEmails() {
assertFalse(PatternInfos.EMAIL.matcher(".username@example.com").matches());
assertFalse(PatternInfos.EMAIL.matcher("@missingusername.com").matches());
assertFalse(PatternInfos.EMAIL.matcher("plainaddress").matches());
assertFalse(PatternInfos.EMAIL.matcher("username..username@example.com").matches());
assertFalse(PatternInfos.EMAIL.matcher("username.@example.com").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@-example.com").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@-example.com").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@.com.com").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@.com.my").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@.com").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@com.").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@com").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@example..com").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@example.com-").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@example.com.").matches());
assertFalse(PatternInfos.EMAIL.matcher("username@example").matches());
}
// ================================
// #endregion - EMAIL
// ================================
// ================================
// #region - Chinese2ndIdCardNumber
// ================================
@ParameterizedTest
@ValueSource(strings = {
"44520019900101456X",
"44520019900101456x",
"445200199001014566",
})
void testChinese2ndIdCardNumber_ValidChinese2ndIdCardNumber(String value) {
Chinese2ndIdCardNumberMatcher chinese2ndIdCardNumberMatcher = PatternInfos.CHINESE_2ND_ID_CARD_NUMBER.matcher(value);
Matcher matcher = chinese2ndIdCardNumberMatcher.matcher();
assertTrue(matcher.matches());
assertEquals("44", chinese2ndIdCardNumberMatcher.getProvince());
assertEquals("4452", chinese2ndIdCardNumberMatcher.getCity());
assertEquals("445200", chinese2ndIdCardNumberMatcher.getCounty());
assertEquals("19900101", chinese2ndIdCardNumberMatcher.getBirthDate());
assertEquals("456", chinese2ndIdCardNumberMatcher.getOrderCode());
assertEquals("6", chinese2ndIdCardNumberMatcher.getGenderCode());
assertEquals(Gender.FEMALE, chinese2ndIdCardNumberMatcher.getGender());
String checkDigit = value.substring(value.length() - 1);
assertEquals(checkDigit, chinese2ndIdCardNumberMatcher.getCheckDigit());
}
@ParameterizedTest
@ValueSource(strings = {
"4452200199001014566",
"44520199001014566",
" ",
"",
})
void testChinese2ndIdCardNumber_InvalidChinese2ndIdCardNumber(String value) {
assertFalse(PatternInfos.CHINESE_2ND_ID_CARD_NUMBER.matcher(value).matches());
}
// ================================
// #endregion - Chinese2ndIdCardNumber
// ================================
// ================================
// #region - invoke constructor
// ================================
@Test
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
Constructor<?>[] constructors;
constructors = PatternInfos.class.getDeclaredConstructors();
Arrays.stream(constructors)
.forEach(constructor -> {
assertFalse(constructor.isAccessible());
constructor.setAccessible(true);
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
.getCause();
assertInstanceOf(IllegalStateException.class, cause);
assertEquals("Utility class", cause.getMessage());
});
}
// ================================
// #endregion - invoke constructor
// ================================
}

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>
@@ -18,6 +18,11 @@
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<guava.version>33.4.8-jre</guava.version>
<joda-time.version>2.14.0</joda-time.version>
@@ -209,7 +214,7 @@
<version>${junit.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependency>
</dependencies>
</dependencyManagement>
@@ -218,7 +223,7 @@
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
@@ -226,31 +231,31 @@
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<version>3.11.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.0</version>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.4.2</version>
<version>3.3.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.1.2</version>
<version>3.1.1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
<version>3.1.1</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
<version>4.0.0-M7</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.6.1</version>
<version>3.4.2</version>
</plugin>
</plugins>
</pluginManagement>

View File

@@ -6,13 +6,9 @@
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-parent</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.1.0-RC1</version>
<packaging>pom</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>plusone-commons</module>
<module>plusone-dependencies</module>