forked from plusone/plusone-commons
Compare commits
16 Commits
refactor/p
...
3980001654
| Author | SHA1 | Date | |
|---|---|---|---|
| 3980001654 | |||
| 27e5650482 | |||
| 8106126e79 | |||
| 99d8f210c5 | |||
| 08432353fe | |||
| a7b1067ebb | |||
| 1dbfb36146 | |||
| 1fc0b198c9 | |||
| 15e07901e6 | |||
| 8f451e7eb9 | |||
| 2b6f946759 | |||
| ce9f3edfbc | |||
| 0f90756f44 | |||
| 34a49d30ca | |||
| f4c3793aab | |||
| 6556a53163 |
12
README.md
12
README.md
@@ -68,16 +68,16 @@ System.out.println(result); // Output: Return string
|
||||
`RegexConsts` 包含常见正则表达式;`PatternConsts` 包含对应的 `Pattern` 对象
|
||||
|
||||
## 五、exception - 异常
|
||||
### 1. MultiTypesException - 多类型异常
|
||||
### 1. IMultiTypesException - 多类型异常
|
||||
异常在不同场景下被抛出,可以用不同的枚举值,表示不同的场景类型。
|
||||
|
||||
异常实现 `MultiTypesException` 的 `MultiTypesException#getType` 方法,返回对应的场景类型。
|
||||
异常实现 `IMultiTypesException` 的 `IMultiTypesException#getType` 方法,返回对应的场景类型。
|
||||
|
||||
表示场景类型的枚举实现 `MultiTypesException.ExceptionType`,其中的工厂方法用于创建对应类型的异常。
|
||||
表示场景类型的枚举实现 `IMultiTypesException.IExceptionType`,其中的工厂方法用于创建对应类型的异常。
|
||||
```java
|
||||
public final class LoginException
|
||||
extends RuntimeException
|
||||
implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
implements IMultiTypesException<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 ExceptionType {
|
||||
public enum Type implements IExceptionType<String>, IExceptionFactory<LoginException> {
|
||||
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://zhouxy.xyz:3000/plusone/plusone-commons/issues/22)。
|
||||
可使用 `UnifiedResponses` 快速构建 `UnifiedResponse` 对象。 `UnifiedResponses` 默认的成功代码为 "2000000", 用户按测试类 `CustomUnifiedResponseFactoryTests` 中所示范的,继承 `UnifiedResponses` 实现自己的工厂类, 自定义 `SUCCESS_CODE` 和 `DEFAULT_SUCCESS_MSG` 和工厂方法。 见 [issue#22](http://gitea.zhouxy.xyz/plusone/plusone-commons/issues/22)。
|
||||
|
||||
## 八、time - 时间 API
|
||||
### 1. 季度
|
||||
|
||||
@@ -5,8 +5,7 @@
|
||||
],
|
||||
"dictionaryDefinitions": [],
|
||||
"dictionaries": [],
|
||||
"words": [],
|
||||
"ignoreWords": [
|
||||
"words": [
|
||||
"aliyun",
|
||||
"baomidou",
|
||||
"Batis",
|
||||
@@ -33,6 +32,7 @@
|
||||
"Nonnull",
|
||||
"NOSONAR",
|
||||
"okhttp",
|
||||
"okio",
|
||||
"ooxml",
|
||||
"overriden",
|
||||
"plusone",
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-parent</artifactId>
|
||||
<version>1.1.0-RC1</version>
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>plusone-commons</artifactId>
|
||||
@@ -17,7 +17,6 @@
|
||||
</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>
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.lang.annotation.Target;
|
||||
* <p>
|
||||
* 标识方法是读方法,如 getter。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see WriterMethod
|
||||
*/
|
||||
|
||||
@@ -26,7 +26,7 @@ import java.lang.annotation.Target;
|
||||
*
|
||||
* <p>标识方法为静态工厂方法
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.lang.annotation.Documented;
|
||||
*
|
||||
* <p>标识方法为不支持的操作。该方法将抛出 {@link UnsupportedOperationException}。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @version 1.0
|
||||
* @since 1.0.0
|
||||
* @see UnsupportedOperationException
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.lang.annotation.Target;
|
||||
/**
|
||||
* ValueObject - 值对象
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Inherited
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.lang.annotation.Target;
|
||||
* 标识该方法是虚方法。
|
||||
* <p>该注解用于提醒、强调父类虽然有默认实现,但子类可以根据自己的需要覆写。</p>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
|
||||
@@ -27,7 +27,7 @@ import java.lang.annotation.Target;
|
||||
* <p>
|
||||
* 标识方法是写方法,如 setter。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see ReaderMethod
|
||||
*/
|
||||
|
||||
@@ -54,6 +54,6 @@
|
||||
* <p>
|
||||
* 标记一个类,表示其作为值对象,区别于 Entity。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.annotation;
|
||||
|
||||
@@ -26,7 +26,7 @@ import javax.annotation.Nullable;
|
||||
* 用于像自定义异常等需要带有 {@code code} 字段的类,
|
||||
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IWithCode<T> {
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import javax.annotation.Nullable;
|
||||
* 用于像自定义异常等需要带有 {@code code} 字段的类,
|
||||
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IWithIntCode {
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ import javax.annotation.Nullable;
|
||||
* 用于像自定义异常等需要带有 {@code code} 字段的类,
|
||||
* 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IWithLongCode {
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ import javax.annotation.Nullable;
|
||||
* System.out.println(result); // Output: Return string
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class Ref<T> {
|
||||
|
||||
@@ -66,7 +66,7 @@
|
||||
* 类似于枚举这样的类型,通常需要设置固定的码值表示对应的含义。
|
||||
* 可实现 {@link IWithCode}、{@link IWithIntCode}、{@link IWithLongCode},便于在需要的地方对这些接口的实现进行处理。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@CheckReturnValue
|
||||
@ParametersAreNonnullByDefault
|
||||
|
||||
@@ -33,7 +33,7 @@ import com.google.common.collect.Table;
|
||||
/**
|
||||
* 集合工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class CollectionTools {
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
* </h3>
|
||||
* 集合工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
package xyz.zhouxy.plusone.commons.collection;
|
||||
|
||||
@@ -21,7 +21,7 @@ import java.util.regex.Pattern;
|
||||
/**
|
||||
* 正则表达式常量
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @see RegexConsts
|
||||
* @see xyz.zhouxy.plusone.commons.util.RegexTools
|
||||
*/
|
||||
|
||||
@@ -19,7 +19,7 @@ package xyz.zhouxy.plusone.commons.constant;
|
||||
/**
|
||||
* 正则表达式常量
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @see PatternConsts
|
||||
*/
|
||||
public final class RegexConsts {
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
* </h3>
|
||||
* {@link RegexConsts} 包含常见正则表达式;{@link PatternConsts} 包含对应的 {@link Pattern} 对象。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.constant;
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ package xyz.zhouxy.plusone.commons.exception;
|
||||
/**
|
||||
* 数据不存在异常
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class DataNotExistsException extends Exception {
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 异常工厂
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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 xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
|
||||
/**
|
||||
* 异常场景
|
||||
*
|
||||
* @param <TCode> 场景编码
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IExceptionType<TCode> extends IWithCode<TCode> {
|
||||
|
||||
/**
|
||||
* 默认异常信息
|
||||
*/
|
||||
String getDefaultMessage();
|
||||
|
||||
@Virtual
|
||||
default String getDescription() {
|
||||
return getDefaultMessage();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,24 +17,23 @@ package xyz.zhouxy.plusone.commons.exception;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
|
||||
/**
|
||||
* MultiTypesException
|
||||
* IMultiTypesException
|
||||
*
|
||||
* <p>
|
||||
* 异常在不同场景下被抛出,可以用不同的枚举值,表示不同的场景类型。
|
||||
*
|
||||
* <p>
|
||||
* 异常实现 {@link MultiTypesException} 的 {@link #getType} 方法,返回对应的场景类型。
|
||||
* 异常实现 {@link IMultiTypesException} 的 {@link #getType} 方法,返回对应的场景类型。
|
||||
*
|
||||
* <p>
|
||||
* 表示场景类型的枚举实现 {@link ExceptionType},其中的工厂方法用于创建对应类型的异常。
|
||||
* 表示场景类型的枚举实现 {@link IExceptionType},各个枚举值本身就是该场景的异常的工厂实例,
|
||||
* 使用其中的工厂方法用于创建对应类型的异常。
|
||||
*
|
||||
* <pre>
|
||||
* public final class LoginException
|
||||
* extends RuntimeException
|
||||
* implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
* implements IMultiTypesException<LoginException.Type> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
@@ -61,7 +60,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* public enum Type implements ExceptionType<LoginException> {
|
||||
* public enum Type implements IExceptionType<String>, IExceptionFactory<LoginException> {
|
||||
* DEFAULT("00", "当前会话未登录"),
|
||||
* NOT_TOKEN("10", "未提供token"),
|
||||
* INVALID_TOKEN("20", "token无效"),
|
||||
@@ -118,73 +117,17 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
* throw LoginException.Type.TOKEN_TIMEOUT.create();
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @param <T> 异常场景
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface MultiTypesException<E extends Exception, T extends MultiTypesException.ExceptionType<E>> {
|
||||
public interface IMultiTypesException<T extends IExceptionType<?>> {
|
||||
|
||||
/**
|
||||
* 异常类型
|
||||
*
|
||||
* @return 异常类型。通常是实现了 {@link ExceptionType} 的枚举。
|
||||
* @return 异常类型。通常是实现了 {@link IExceptionType} 的枚举。
|
||||
*/
|
||||
@Nonnull
|
||||
T getType();
|
||||
|
||||
/**
|
||||
* 获取异常类型编码
|
||||
*
|
||||
* @return 异常类型编码
|
||||
*/
|
||||
default @Nonnull String getTypeCode() {
|
||||
return getType().getCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常类型
|
||||
*/
|
||||
public static interface ExceptionType<E extends Exception> extends IWithCode<String> {
|
||||
|
||||
/**
|
||||
* 默认异常信息
|
||||
*/
|
||||
String 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);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,6 @@ import java.time.format.DateTimeParseException;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
|
||||
import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
|
||||
/**
|
||||
* 解析失败异常
|
||||
@@ -34,12 +33,12 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
* throw new RequestParamsException(ParsingFailureException.Type.NUMBER_PARSING_FAILURE.create());
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class ParsingFailureException
|
||||
extends Exception
|
||||
implements MultiTypesException<ParsingFailureException, ParsingFailureException.Type> {
|
||||
implements IMultiTypesException<ParsingFailureException.Type> {
|
||||
private static final long serialVersionUID = 795996090625132616L;
|
||||
|
||||
private final Type type;
|
||||
@@ -171,7 +170,7 @@ public final class ParsingFailureException
|
||||
/** XML 解析失败 */
|
||||
public static final Type XML_PARSING_FAILURE = Type.XML_PARSING_FAILURE;
|
||||
|
||||
public enum Type implements ExceptionType<ParsingFailureException> {
|
||||
public enum Type implements IExceptionType<String>, IExceptionFactory<ParsingFailureException> {
|
||||
DEFAULT("00", "解析失败"),
|
||||
NUMBER_PARSING_FAILURE("10", "数字转换失败"),
|
||||
DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),
|
||||
|
||||
@@ -25,7 +25,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
* <p>
|
||||
* <b>NOTE: 通常表示业务中的意外情况。如:用户错误输入、缺失必填字段、用户余额不足等。</b>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class BizException extends RuntimeException {
|
||||
@@ -33,21 +33,13 @@ public class BizException extends RuntimeException {
|
||||
|
||||
private static final String DEFAULT_MSG = "业务异常";
|
||||
|
||||
/**
|
||||
* 使用默认 message 构造新的业务异常。
|
||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
||||
*/
|
||||
public BizException() {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用指定的 {@code message} 构造新的业务异常。
|
||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
||||
*
|
||||
* @param message 异常信息
|
||||
*/
|
||||
public BizException(String message) {
|
||||
protected BizException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
@@ -57,7 +49,7 @@ public class BizException extends RuntimeException {
|
||||
*
|
||||
* @param cause 包装的异常
|
||||
*/
|
||||
public BizException(Throwable cause) {
|
||||
protected BizException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@@ -67,8 +59,27 @@ public class BizException extends RuntimeException {
|
||||
* @param message 异常信息
|
||||
* @param cause 包装的异常
|
||||
*/
|
||||
public BizException(String message, Throwable cause) {
|
||||
protected BizException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public static BizException of() {
|
||||
return new BizException(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public static BizException of(String message) {
|
||||
return new BizException(message);
|
||||
}
|
||||
|
||||
public static BizException of(String errorMessageFormat, Object... errorMessageArgs) {
|
||||
return new BizException(String.format(errorMessageFormat, errorMessageArgs));
|
||||
}
|
||||
|
||||
public static BizException of(Throwable cause) {
|
||||
return new BizException(cause);
|
||||
}
|
||||
|
||||
public static BizException of(String message, Throwable cause) {
|
||||
return new BizException(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,8 +18,9 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
|
||||
import xyz.zhouxy.plusone.commons.exception.IExceptionFactory;
|
||||
import xyz.zhouxy.plusone.commons.exception.IExceptionType;
|
||||
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException;
|
||||
|
||||
/**
|
||||
* InvalidInputException
|
||||
@@ -30,12 +31,12 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
|
||||
* <p>
|
||||
* <b>NOTE: 属业务异常</b>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class InvalidInputException
|
||||
extends RequestParamsException
|
||||
implements MultiTypesException<InvalidInputException, InvalidInputException.Type> {
|
||||
implements IMultiTypesException<InvalidInputException.Type> {
|
||||
private static final long serialVersionUID = -28994090625082516L;
|
||||
|
||||
private final Type type;
|
||||
@@ -109,7 +110,7 @@ public final class InvalidInputException
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public enum Type implements ExceptionType<InvalidInputException> {
|
||||
public enum Type implements IExceptionType<String>, IExceptionFactory<InvalidInputException> {
|
||||
DEFAULT("00", "用户输入内容非法"),
|
||||
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
||||
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
||||
|
||||
@@ -22,7 +22,7 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
* <p>
|
||||
* 用户请求参数错误
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class RequestParamsException extends BizException {
|
||||
|
||||
@@ -17,6 +17,6 @@
|
||||
/**
|
||||
* 业务异常
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.exception.business;
|
||||
|
||||
@@ -17,20 +17,21 @@
|
||||
/**
|
||||
* <h2>异常</h2>
|
||||
*
|
||||
* <h3>1. {@link MultiTypesException} - 多类型异常</h3>
|
||||
* <h3>1. {@link IMultiTypesException} - 多类型异常</h3>
|
||||
* <p>
|
||||
* 异常在不同场景下被抛出,可以用不同的枚举值,表示不同的场景类型。
|
||||
*
|
||||
* <p>
|
||||
* 异常实现 {@link MultiTypesException} 的 {@link MultiTypesException#getType} 方法,返回对应的场景类型。
|
||||
* 异常实现 {@link IMultiTypesException} 的 {@link IMultiTypesException#getType} 方法,返回对应的场景类型。
|
||||
*
|
||||
* <p>
|
||||
* 表示场景类型的枚举实现 {@link MultiTypesException.ExceptionType},其中的工厂方法用于创建对应类型的异常。
|
||||
* 表示场景类型的枚举实现 {@link IMultiTypesException.IExceptionType},各个枚举值本身就是该场景的异常的工厂实例,
|
||||
* 使用其中的工厂方法用于创建对应类型的异常。
|
||||
*
|
||||
* <pre>
|
||||
* public final class LoginException
|
||||
* extends RuntimeException
|
||||
* implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
* implements IMultiTypesException<LoginException, LoginException.Type, String> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
@@ -57,7 +58,7 @@
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* public enum Type implements ExceptionType<LoginException> {
|
||||
* public enum Type implements IExceptionType<LoginException, String> {
|
||||
* DEFAULT("00", "当前会话未登录"),
|
||||
* NOT_TOKEN("10", "未提供token"),
|
||||
* INVALID_TOKEN("20", "token无效"),
|
||||
@@ -120,7 +121,7 @@
|
||||
* <h3>3. 系统异常</h3>
|
||||
* 预设常见的系统异常。可继承 {@link SysException} 自定义系统异常。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
|
||||
* 当出现这种始料未及的诡异情况时,抛出 {@link DataOperationResultException} 并回滚事务。
|
||||
* 后续需要排查原因。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class DataOperationResultException extends SysException {
|
||||
|
||||
@@ -22,7 +22,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
|
||||
* <p>
|
||||
* 在无法找到可访问的 Mac 地址时抛出
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class NoAvailableMacFoundException extends SysException {
|
||||
@@ -33,7 +33,7 @@ public class NoAvailableMacFoundException extends SysException {
|
||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
||||
*/
|
||||
public NoAvailableMacFoundException() {
|
||||
super();
|
||||
super("无法找到可访问的 Mac 地址");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,7 +22,7 @@ package xyz.zhouxy.plusone.commons.exception.system;
|
||||
* <p>
|
||||
* 通常表示应用代码存在问题,或因环境问题,引发异常。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class SysException extends RuntimeException {
|
||||
@@ -30,21 +30,13 @@ public class SysException extends RuntimeException {
|
||||
|
||||
private static final String DEFAULT_MSG = "系统异常";
|
||||
|
||||
/**
|
||||
* 使用默认 message 构造新的系统异常。
|
||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
||||
*/
|
||||
public SysException() {
|
||||
super(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用指定的 {@code message} 构造新的系统异常。
|
||||
* {@code cause} 未初始化,后面可能会通过调用 {@link #initCause} 进行初始化。
|
||||
*
|
||||
* @param message 异常信息
|
||||
*/
|
||||
public SysException(String message) {
|
||||
protected SysException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
@@ -54,7 +46,7 @@ public class SysException extends RuntimeException {
|
||||
*
|
||||
* @param cause 包装的异常
|
||||
*/
|
||||
public SysException(Throwable cause) {
|
||||
protected SysException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
@@ -64,7 +56,27 @@ public class SysException extends RuntimeException {
|
||||
* @param message 异常信息
|
||||
* @param cause 包装的异常
|
||||
*/
|
||||
public SysException(String message, Throwable cause) {
|
||||
protected SysException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public static SysException of() {
|
||||
return new SysException(DEFAULT_MSG);
|
||||
}
|
||||
|
||||
public static SysException of(String message) {
|
||||
return new SysException(message);
|
||||
}
|
||||
|
||||
public static SysException of(String errorMessageFormat, Object... errorMessageArgs) {
|
||||
return new SysException(String.format(errorMessageFormat, errorMessageArgs));
|
||||
}
|
||||
|
||||
public static SysException of(Throwable cause) {
|
||||
return new SysException(cause);
|
||||
}
|
||||
|
||||
public static SysException of(String message, Throwable cause) {
|
||||
return new SysException(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,6 @@
|
||||
/**
|
||||
* 系统异常
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.exception.system;
|
||||
|
||||
@@ -25,7 +25,7 @@ import com.google.common.annotations.Beta;
|
||||
* 一个特殊的 {@link java.util.function.UnaryOperator}。
|
||||
* 表示对 {@code boolean} 值的一元操作。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see java.util.function.UnaryOperator
|
||||
*/
|
||||
|
||||
@@ -25,7 +25,7 @@ import com.google.common.annotations.Beta;
|
||||
* 一个特殊的 {@link java.util.function.UnaryOperator}。
|
||||
* 表示对 {@code char} 的一元操作。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see java.util.function.UnaryOperator
|
||||
*/
|
||||
|
||||
@@ -24,7 +24,7 @@ package xyz.zhouxy.plusone.commons.function;
|
||||
*
|
||||
* @param <E> 可抛出的异常类型
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.util.function.Supplier;
|
||||
* <p>
|
||||
* 返回 {@code Optional<T>} 对象。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see Optional
|
||||
* @see Supplier
|
||||
|
||||
@@ -24,7 +24,7 @@ import java.util.function.Predicate;
|
||||
* <p>
|
||||
* {@link Predicate} 相关操作。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see Predicate
|
||||
*/
|
||||
|
||||
@@ -22,7 +22,7 @@ package xyz.zhouxy.plusone.commons.function;
|
||||
* <p>
|
||||
* 允许抛出异常的消费操作。是一个特殊的 {@link java.util.function.Consumer}。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see java.util.function.Consumer
|
||||
*/
|
||||
|
||||
@@ -25,7 +25,7 @@ package xyz.zhouxy.plusone.commons.function;
|
||||
* @param <R> 返回结果类型
|
||||
* @param <E> 异常类型
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0
|
||||
* @see java.util.function.Function
|
||||
*/
|
||||
|
||||
@@ -22,7 +22,7 @@ package xyz.zhouxy.plusone.commons.function;
|
||||
* <p>
|
||||
* 接收一个参数,返回一个布尔值,可抛出异常。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see java.util.function.Predicate
|
||||
*/
|
||||
|
||||
@@ -25,7 +25,7 @@ package xyz.zhouxy.plusone.commons.function;
|
||||
* @param <T> 结果类型
|
||||
* @param <E> 异常类型
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see java.util.function.Supplier
|
||||
*/
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.util.function.BiFunction;
|
||||
* <p>
|
||||
* 接受类型为 T 和 U 的两个参数,返回 {@code Optional<R>} 对象。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see Optional
|
||||
* @see BiFunction
|
||||
|
||||
@@ -25,7 +25,7 @@ import java.util.function.Function;
|
||||
* <p>
|
||||
* 接受类型为 T 的参数,返回 {@code Optional<R>} 对象。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see Optional
|
||||
* @see Function
|
||||
|
||||
@@ -39,6 +39,6 @@
|
||||
* | Optional | ToOptionalFunction | Optional<R> apply(T) |
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.function;
|
||||
|
||||
@@ -33,7 +33,7 @@ import com.google.gson.stream.JsonWriter;
|
||||
/**
|
||||
* 包含 JSR-310 相关数据类型的 {@code TypeAdapter}
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.1.0
|
||||
* @see TypeAdapter
|
||||
* @see com.google.gson.GsonBuilder
|
||||
|
||||
@@ -42,7 +42,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
* <p>
|
||||
* 中国第二代居民身份证号
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see xyz.zhouxy.plusone.commons.constant.PatternConsts#CHINESE_2ND_ID_CARD_NUMBER
|
||||
*/
|
||||
|
||||
@@ -23,7 +23,7 @@ import xyz.zhouxy.plusone.commons.base.IWithIntCode;
|
||||
/**
|
||||
* 性别
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public enum Gender implements IWithIntCode {
|
||||
UNKNOWN(0, "Unknown", "未知"),
|
||||
|
||||
@@ -24,7 +24,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
/**
|
||||
* 身份证号
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IDCardNumber {
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
@@ -31,7 +32,7 @@ import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
|
||||
/**
|
||||
* 带校验的字符串值对象
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*
|
||||
* @deprecated 弃用。使用工厂方法创建对象,并在其中进行校验即可。
|
||||
@@ -75,8 +76,8 @@ public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<
|
||||
* @param errorMessage 正则不匹配时的错误信息
|
||||
*/
|
||||
protected ValidatableStringRecord(String value, Pattern pattern, String errorMessage) {
|
||||
checkArgument(Objects.nonNull(value), "The value cannot be null.");
|
||||
checkArgument(Objects.nonNull(pattern), "The pattern cannot be null.");
|
||||
checkArgumentNotNull(value, "The value cannot be null.");
|
||||
checkArgumentNotNull(pattern, "The pattern cannot be null.");
|
||||
this.matcher = pattern.matcher(value);
|
||||
checkArgument(this.matcher.matches(), errorMessage);
|
||||
this.value = value;
|
||||
|
||||
@@ -29,7 +29,7 @@ import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
*
|
||||
* @param <T> 内容列表的元素类型
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @see PagingAndSortingQueryParams
|
||||
*/
|
||||
public class PageResult<T> {
|
||||
|
||||
@@ -27,7 +27,6 @@ import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
@@ -39,36 +38,16 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
* 根据传入的 {@code size} 和 {@code pageNum},
|
||||
* 提供 {@code getOffset} 方法计算 SQL 语句中 {@code offset} 的值。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @see PagingParams
|
||||
* @see PageResult
|
||||
*/
|
||||
public class PagingAndSortingQueryParams {
|
||||
|
||||
private static final int DEFAULT_PAGE_SIZE = 15;
|
||||
|
||||
private Integer size;
|
||||
private Long pageNum;
|
||||
private List<String> orderBy;
|
||||
|
||||
private static final Pattern SORT_STR_PATTERN = Pattern.compile("^[a-zA-Z][\\w-]{0,63}-(desc|asc|DESC|ASC)$");
|
||||
|
||||
private final Map<String, String> sortableProperties;
|
||||
|
||||
/**
|
||||
* 构造分页排序查询参数
|
||||
*
|
||||
* @param sortableProperties 可排序的属性。不可为空。
|
||||
*/
|
||||
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) {
|
||||
checkArgument(CollectionTools.isNotEmpty(sortableProperties),
|
||||
"Sortable properties can not be empty.");
|
||||
sortableProperties.forEach((k, v) ->
|
||||
checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
|
||||
"Property name must not be blank."));
|
||||
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
|
||||
}
|
||||
|
||||
// Setters
|
||||
|
||||
/**
|
||||
@@ -100,56 +79,18 @@ public class PagingAndSortingQueryParams {
|
||||
|
||||
// Setters end
|
||||
|
||||
/**
|
||||
* 构建分页参数
|
||||
*
|
||||
* @return {@code PagingParams} 对象
|
||||
*/
|
||||
public final PagingParams buildPagingParams() {
|
||||
final int sizeValue = this.size != null ? this.size : defaultSizeInternal();
|
||||
final long pageNumValue = this.pageNum != null ? this.pageNum : 1L;
|
||||
checkArgument(CollectionTools.isNotEmpty(this.orderBy),
|
||||
"The 'orderBy' cannot be empty");
|
||||
final List<SortableProperty> propertiesToSort = this.orderBy.stream()
|
||||
.map(this::generateSortableProperty)
|
||||
.collect(Collectors.toList());
|
||||
return new PagingParams(sizeValue, pageNumValue, propertiesToSort);
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认每页大小
|
||||
*
|
||||
* <p>NOTE: 可覆写此方法</p>
|
||||
*
|
||||
* @return 默认每页大小
|
||||
*/
|
||||
@Virtual
|
||||
protected int defaultSizeInternal() {
|
||||
return DEFAULT_PAGE_SIZE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PagingAndSortingQueryParams ["
|
||||
+ "size=" + size
|
||||
+ ", pageNum=" + pageNum
|
||||
+ ", orderBy=" + orderBy
|
||||
+ ", sortableProperties=" + sortableProperties
|
||||
+ "]";
|
||||
}
|
||||
|
||||
private SortableProperty generateSortableProperty(String orderByStr) {
|
||||
checkArgument(StringTools.isNotBlank(orderByStr));
|
||||
checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
|
||||
String[] propertyNameAndOrderType = orderByStr.split("-");
|
||||
checkArgument(propertyNameAndOrderType.length == 2);
|
||||
|
||||
String propertyName = propertyNameAndOrderType[0];
|
||||
checkArgument(sortableProperties.containsKey(propertyName),
|
||||
"The property name must be in the set of sortable properties.");
|
||||
String columnName = sortableProperties.get(propertyName);
|
||||
String orderType = propertyNameAndOrderType[1];
|
||||
return new SortableProperty(propertyName, columnName, orderType);
|
||||
protected static PagingParamsBuilder pagingParamsBuilder(
|
||||
int defaultSize, int maxSize, Map<String, String> sortableProperties) {
|
||||
return new PagingParamsBuilder(defaultSize, maxSize, sortableProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -217,4 +158,47 @@ public class PagingAndSortingQueryParams {
|
||||
}
|
||||
}
|
||||
|
||||
protected static final class PagingParamsBuilder {
|
||||
private static final Pattern SORT_STR_PATTERN = Pattern.compile("^[a-zA-Z][\\w-]{0,63}-(desc|asc|DESC|ASC)$");
|
||||
|
||||
private final Map<String, String> sortableProperties;
|
||||
protected final int defaultSize;
|
||||
protected final int maxSize;
|
||||
|
||||
private PagingParamsBuilder(int defaultSize, int maxSize, Map<String, String> sortableProperties) {
|
||||
this.defaultSize = defaultSize;
|
||||
this.maxSize = maxSize;
|
||||
checkArgument(CollectionTools.isNotEmpty(sortableProperties),
|
||||
"Sortable properties can not be empty.");
|
||||
sortableProperties.forEach((k, v) ->
|
||||
checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
|
||||
"Property name must not be blank."));
|
||||
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
|
||||
}
|
||||
|
||||
public PagingParams buildPagingParams(PagingAndSortingQueryParams params) {
|
||||
final int sizeValue = params.size != null ? params.size : this.defaultSize;
|
||||
final long pageNumValue = params.pageNum != null ? params.pageNum : 1L;
|
||||
checkArgument(CollectionTools.isNotEmpty(params.orderBy),
|
||||
"The 'orderBy' cannot be empty");
|
||||
final List<SortableProperty> propertiesToSort = params.orderBy.stream()
|
||||
.map(this::generateSortableProperty)
|
||||
.collect(Collectors.toList());
|
||||
return new PagingParams(sizeValue, pageNumValue, propertiesToSort);
|
||||
}
|
||||
|
||||
private SortableProperty generateSortableProperty(String orderByStr) {
|
||||
checkArgument(StringTools.isNotBlank(orderByStr));
|
||||
checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
|
||||
String[] propertyNameAndOrderType = orderByStr.split("-");
|
||||
checkArgument(propertyNameAndOrderType.length == 2);
|
||||
|
||||
String propertyName = propertyNameAndOrderType[0];
|
||||
checkArgument(sortableProperties.containsKey(propertyName),
|
||||
"The property name must be in the set of sortable properties.");
|
||||
String columnName = sortableProperties.get(propertyName);
|
||||
String orderType = propertyNameAndOrderType[1];
|
||||
return new SortableProperty(propertyName, columnName, orderType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams.Sortable
|
||||
/**
|
||||
* 分页参数
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @see PagingAndSortingQueryParams
|
||||
*/
|
||||
public class PagingParams {
|
||||
|
||||
@@ -22,7 +22,7 @@ import javax.annotation.Nullable;
|
||||
/**
|
||||
* 统一结果,对返回给前端的数据进行封装。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class UnifiedResponse<T> {
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import javax.annotation.Nullable;
|
||||
/**
|
||||
* UnifiedResponse 工厂
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see UnifiedResponse
|
||||
*/
|
||||
|
||||
@@ -60,7 +60,7 @@
|
||||
* 自定义 SUCCESS_CODE 和 DEFAULT_SUCCESS_MSG 和工厂方法。
|
||||
* 见 <a href="http://zhouxy.xyz:3000/plusone/plusone-commons/issues/22">issue#22</a>。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
package xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
* <p>
|
||||
* 包含业务建模可能用到的性别、身份证等元素,也包含 DTO 相关类,如分页查询参数,响应结果,分页结果等。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
@@ -32,7 +32,7 @@ import xyz.zhouxy.plusone.commons.base.IWithIntCode;
|
||||
/**
|
||||
* 季度
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public enum Quarter implements IWithIntCode {
|
||||
/** 第一季度 */
|
||||
|
||||
@@ -37,7 +37,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
/**
|
||||
* 表示年份与季度
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@Immutable
|
||||
public final class YearQuarter implements Comparable<YearQuarter>, Serializable {
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
* 模仿 JDK 的 {@link java.time.Month} 和 {@link java.time.YearMonth},
|
||||
* 实现 {@link Quarter},{@link YearQuarter},对季度进行建模。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
package xyz.zhouxy.plusone.commons.time;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
@@ -37,7 +38,7 @@ import javax.annotation.Nullable;
|
||||
* <p>
|
||||
* 数组工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ArrayTools {
|
||||
@@ -491,7 +492,7 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static char[] repeat(char[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgumentNotNull(arr);
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
@@ -526,7 +527,7 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static byte[] repeat(byte[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgumentNotNull(arr);
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
@@ -561,7 +562,7 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static short[] repeat(short[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgumentNotNull(arr);
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
@@ -596,7 +597,7 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static int[] repeat(int[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgumentNotNull(arr);
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
@@ -631,7 +632,7 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static long[] repeat(long[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgumentNotNull(arr);
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
@@ -666,7 +667,7 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static float[] repeat(float[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgumentNotNull(arr);
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
@@ -701,7 +702,7 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static double[] repeat(double[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgumentNotNull(arr);
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
@@ -750,7 +751,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
checkArgumentNotNull(a);
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -793,7 +794,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
checkArgumentNotNull(a);
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -836,7 +837,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
checkArgumentNotNull(a);
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -879,7 +880,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
checkArgumentNotNull(a);
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -922,7 +923,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
checkArgumentNotNull(a);
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -965,7 +966,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
checkArgumentNotNull(a);
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -1008,7 +1009,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
checkArgumentNotNull(a);
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -1063,7 +1064,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
checkArgumentNotNull(a);
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
|
||||
* "must be a well-formed email address");
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class AssertTools {
|
||||
|
||||
@@ -54,7 +54,9 @@ public class AssertTools {
|
||||
* @throws IllegalArgumentException 当条件不满足时抛出
|
||||
*/
|
||||
public static void checkArgument(boolean condition) {
|
||||
checkCondition(condition, IllegalArgumentException::new);
|
||||
if (!condition) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,7 +67,9 @@ public class AssertTools {
|
||||
* @throws IllegalArgumentException 当条件不满足时抛出
|
||||
*/
|
||||
public static void checkArgument(boolean condition, @Nullable String errorMessage) {
|
||||
checkCondition(condition, () -> new IllegalArgumentException(errorMessage));
|
||||
if (!condition) {
|
||||
throw new IllegalArgumentException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,7 +80,9 @@ public class AssertTools {
|
||||
* @throws IllegalArgumentException 当条件不满足时抛出
|
||||
*/
|
||||
public static void checkArgument(boolean condition, Supplier<String> errorMessageSupplier) {
|
||||
checkCondition(condition, () -> new IllegalArgumentException(errorMessageSupplier.get()));
|
||||
if (!condition) {
|
||||
throw new IllegalArgumentException(errorMessageSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,8 +95,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static void checkArgument(boolean condition,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
checkCondition(condition,
|
||||
() -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)));
|
||||
if (!condition) {
|
||||
throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
}
|
||||
|
||||
// ================================
|
||||
@@ -109,7 +116,9 @@ public class AssertTools {
|
||||
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
|
||||
*/
|
||||
public static <T> T checkArgumentNotNull(@Nullable T obj) {
|
||||
checkCondition(obj != null, IllegalArgumentException::new);
|
||||
if (obj == null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -122,7 +131,9 @@ public class AssertTools {
|
||||
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
|
||||
*/
|
||||
public static <T> T checkArgumentNotNull(@Nullable T obj, String errorMessage) {
|
||||
checkCondition(obj != null, () -> new IllegalArgumentException(errorMessage));
|
||||
if (obj == null) {
|
||||
throw new IllegalArgumentException(errorMessage);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -135,7 +146,9 @@ public class AssertTools {
|
||||
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
|
||||
*/
|
||||
public static <T> T checkArgumentNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) {
|
||||
checkCondition(obj != null, () -> new IllegalArgumentException(errorMessageSupplier.get()));
|
||||
if (obj == null) {
|
||||
throw new IllegalArgumentException(errorMessageSupplier.get());
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -150,8 +163,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static <T> T checkArgumentNotNull(@Nullable T obj,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
checkCondition(obj != null,
|
||||
() -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)));
|
||||
if (obj == null) {
|
||||
throw new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -170,7 +184,9 @@ public class AssertTools {
|
||||
* @throws IllegalStateException 当条件不满足时抛出
|
||||
*/
|
||||
public static void checkState(boolean condition) {
|
||||
checkCondition(condition, IllegalStateException::new);
|
||||
if (!condition) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,7 +197,9 @@ public class AssertTools {
|
||||
* @throws IllegalStateException 当条件不满足时抛出
|
||||
*/
|
||||
public static void checkState(boolean condition, @Nullable String errorMessage) {
|
||||
checkCondition(condition, () -> new IllegalStateException(errorMessage));
|
||||
if (!condition) {
|
||||
throw new IllegalStateException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,7 +210,9 @@ public class AssertTools {
|
||||
* @throws IllegalStateException 当条件不满足时抛出
|
||||
*/
|
||||
public static void checkState(boolean condition, Supplier<String> errorMessageSupplier) {
|
||||
checkCondition(condition, () -> new IllegalStateException(errorMessageSupplier.get()));
|
||||
if (!condition) {
|
||||
throw new IllegalStateException(errorMessageSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,8 +225,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static void checkState(boolean condition,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
checkCondition(condition,
|
||||
() -> new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs)));
|
||||
if (!condition) {
|
||||
throw new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
}
|
||||
|
||||
// ================================
|
||||
@@ -225,7 +246,9 @@ public class AssertTools {
|
||||
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
|
||||
*/
|
||||
public static <T> void checkNotNull(@Nullable T obj) {
|
||||
checkCondition(obj != null, NullPointerException::new);
|
||||
if (obj == null) {
|
||||
throw new NullPointerException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -237,7 +260,9 @@ public class AssertTools {
|
||||
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
|
||||
*/
|
||||
public static <T> void checkNotNull(@Nullable T obj, String errorMessage) {
|
||||
checkCondition(obj != null, () -> new NullPointerException(errorMessage));
|
||||
if (obj == null) {
|
||||
throw new NullPointerException(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,7 +274,9 @@ public class AssertTools {
|
||||
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
|
||||
*/
|
||||
public static <T> void checkNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) {
|
||||
checkCondition(obj != null, () -> new NullPointerException(errorMessageSupplier.get()));
|
||||
if (obj == null) {
|
||||
throw new NullPointerException(errorMessageSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,8 +290,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static <T> void checkNotNull(@Nullable T obj,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
checkCondition(obj != null,
|
||||
() -> new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs)));
|
||||
if (obj == null) {
|
||||
throw new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
}
|
||||
|
||||
// ================================
|
||||
@@ -285,7 +313,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static <T> T checkExists(@Nullable T obj)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(obj != null, DataNotExistsException::new);
|
||||
if (obj == null) {
|
||||
throw new DataNotExistsException();
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -300,7 +330,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static <T> T checkExists(@Nullable T obj, String errorMessage)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(obj != null, () -> new DataNotExistsException(errorMessage));
|
||||
if (obj == null) {
|
||||
throw new DataNotExistsException(errorMessage);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -315,7 +347,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static <T> T checkExists(@Nullable T obj, Supplier<String> errorMessageSupplier)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(obj != null, () -> new DataNotExistsException(errorMessageSupplier.get()));
|
||||
if (obj == null) {
|
||||
throw new DataNotExistsException(errorMessageSupplier.get());
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -332,8 +366,9 @@ public class AssertTools {
|
||||
public static <T> T checkExists(@Nullable T obj,
|
||||
String errorMessageTemplate, Object... errorMessageArgs)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(obj != null,
|
||||
() -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs)));
|
||||
if (obj == null) {
|
||||
throw new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -347,7 +382,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static <T> T checkExists(Optional<T> optional)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(optional.isPresent(), DataNotExistsException::new);
|
||||
if (!optional.isPresent()) {
|
||||
throw new DataNotExistsException();
|
||||
}
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
@@ -362,7 +399,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static <T> T checkExists(Optional<T> optional, String errorMessage)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessage));
|
||||
if (!optional.isPresent()) {
|
||||
throw new DataNotExistsException(errorMessage);
|
||||
}
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
@@ -377,7 +416,9 @@ public class AssertTools {
|
||||
*/
|
||||
public static <T> T checkExists(Optional<T> optional, Supplier<String> errorMessageSupplier)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessageSupplier.get()));
|
||||
if (!optional.isPresent()) {
|
||||
throw new DataNotExistsException(errorMessageSupplier.get());
|
||||
}
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
@@ -394,8 +435,9 @@ public class AssertTools {
|
||||
public static <T> T checkExists(Optional<T> optional,
|
||||
String errorMessageTemplate, Object... errorMessageArgs)
|
||||
throws DataNotExistsException {
|
||||
checkCondition(optional.isPresent(),
|
||||
() -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs)));
|
||||
if (!optional.isPresent()) {
|
||||
throw new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
return optional.get();
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
* <p>
|
||||
* BigDecimal 工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class BigDecimals {
|
||||
|
||||
@@ -27,11 +27,11 @@ import java.time.Year;
|
||||
import java.time.YearMonth;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.chrono.IsoChronology;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.google.common.collect.BoundType;
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||
@@ -40,7 +40,7 @@ import xyz.zhouxy.plusone.commons.time.YearQuarter;
|
||||
/**
|
||||
* 日期时间工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class DateTimeTools {
|
||||
|
||||
@@ -649,7 +649,7 @@ public class DateTimeTools {
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// #region - range
|
||||
// ================================
|
||||
|
||||
/**
|
||||
@@ -673,6 +673,52 @@ public class DateTimeTools {
|
||||
return Range.closedOpen(date.atStartOfDay(zone), startOfNextDate(date, zone));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定日期范围转为日期时间范围
|
||||
*
|
||||
* @param dateRange 日期范围
|
||||
* @return 对应的日期时间范围
|
||||
*/
|
||||
public static Range<LocalDateTime> toDateTimeRange(Range<LocalDate> dateRange) {
|
||||
BoundType lowerBoundType = dateRange.lowerBoundType();
|
||||
LocalDateTime lowerEndpoint = lowerBoundType == BoundType.CLOSED
|
||||
? dateRange.lowerEndpoint().atStartOfDay()
|
||||
: dateRange.lowerEndpoint().plusDays(1).atStartOfDay();
|
||||
BoundType upperBoundType = dateRange.upperBoundType();
|
||||
LocalDateTime upperEndpoint = upperBoundType == BoundType.CLOSED
|
||||
? dateRange.upperEndpoint().plusDays(1).atStartOfDay()
|
||||
: dateRange.upperEndpoint().atStartOfDay();
|
||||
|
||||
return Range.closedOpen(lowerEndpoint, upperEndpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定日期范围转为日期时间范围
|
||||
*
|
||||
* @param dateRange 日期范围
|
||||
* @return 对应的日期时间范围
|
||||
*/
|
||||
public static Range<ZonedDateTime> toDateTimeRange(Range<LocalDate> dateRange, ZoneId zone) {
|
||||
BoundType lowerBoundType = dateRange.lowerBoundType();
|
||||
ZonedDateTime lowerEndpoint = lowerBoundType == BoundType.CLOSED
|
||||
? dateRange.lowerEndpoint().atStartOfDay(zone)
|
||||
: dateRange.lowerEndpoint().plusDays(1).atStartOfDay(zone);
|
||||
BoundType upperBoundType = dateRange.upperBoundType();
|
||||
ZonedDateTime upperEndpoint = upperBoundType == BoundType.CLOSED
|
||||
? dateRange.upperEndpoint().plusDays(1).atStartOfDay(zone)
|
||||
: dateRange.upperEndpoint().atStartOfDay(zone);
|
||||
|
||||
return Range.closedOpen(lowerEndpoint, upperEndpoint);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - range
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 判断指定年份是否为闰年
|
||||
*
|
||||
@@ -680,7 +726,7 @@ public class DateTimeTools {
|
||||
* @return 指定年份是否为闰年
|
||||
*/
|
||||
public static boolean isLeapYear(int year) {
|
||||
return IsoChronology.INSTANCE.isLeapYear(year);
|
||||
return Year.isLeap(year);
|
||||
}
|
||||
|
||||
// ================================
|
||||
|
||||
@@ -26,7 +26,7 @@ import javax.annotation.Nullable;
|
||||
/**
|
||||
* 枚举工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public final class EnumTools {
|
||||
|
||||
@@ -154,7 +154,7 @@ public final class EnumTools {
|
||||
*/
|
||||
@Nullable
|
||||
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> enumType, @Nullable Integer ordinal) {
|
||||
return checkOrdinalOrDefault(enumType, ordinal, (Integer) null);
|
||||
return checkOrdinalOrDefault(enumType, ordinal, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,9 +33,10 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
/**
|
||||
* 枚举类
|
||||
*
|
||||
* <p>
|
||||
* 参考 <a href="https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/">Enumeration classes</a>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @deprecated 设计 Enumeration 的灵感来自于 .net 社区,因为 C# 的枚举不带行为。
|
||||
* 但 Java 的枚举可以带行为,故大多数情况下不需要这种设计。
|
||||
*/
|
||||
|
||||
@@ -30,7 +30,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*
|
||||
* @see UUID
|
||||
* @see IdWorker
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class IdGenerator {
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ package xyz.zhouxy.plusone.commons.util;
|
||||
/**
|
||||
* Joda-Time 工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class JodaTimeTools {
|
||||
|
||||
@@ -54,7 +54,9 @@ public class JodaTimeTools {
|
||||
* @param zone 时区
|
||||
* @return {@link org.joda.time.Instant} 对象
|
||||
*/
|
||||
public static org.joda.time.Instant toJodaInstant(java.time.LocalDateTime localDateTime, java.time.ZoneId zone) {
|
||||
public static org.joda.time.Instant toJodaInstant(
|
||||
java.time.LocalDateTime localDateTime,
|
||||
java.time.ZoneId zone) {
|
||||
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
|
||||
}
|
||||
|
||||
@@ -92,9 +94,9 @@ public class JodaTimeTools {
|
||||
* {@link org.joda.time.DateTimeZone} 对象
|
||||
* 转换为 Java 中的 {@link java.time.Instant} 对象
|
||||
*
|
||||
* @param localDateTime
|
||||
* @param zone
|
||||
* @return
|
||||
* @param localDateTime {@link org.joda.time.LocalDateTime} 对象
|
||||
* @param zone {@link org.joda.time.DateTimeZone} 对象
|
||||
* @return Java 表示时间戳的 {@link java.time.Instant} 对象
|
||||
*/
|
||||
public static java.time.Instant toJavaInstant(
|
||||
org.joda.time.LocalDateTime localDateTime,
|
||||
@@ -135,8 +137,9 @@ public class JodaTimeTools {
|
||||
public static org.joda.time.DateTime toJodaDateTime(
|
||||
java.time.LocalDateTime localDateTime,
|
||||
java.time.ZoneId zone) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone);
|
||||
org.joda.time.LocalDateTime jodaLocalDateTime = toJodaLocalDateTime(localDateTime);
|
||||
org.joda.time.DateTimeZone jodaZone = toJodaZone(zone);
|
||||
return jodaLocalDateTime.toDateTime(jodaZone);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,9 +221,15 @@ public class JodaTimeTools {
|
||||
* @return joda-time LocalDateTime
|
||||
*/
|
||||
public static org.joda.time.LocalDateTime toJodaLocalDateTime(java.time.LocalDateTime localDateTime) {
|
||||
java.time.ZoneId javaZone = java.time.ZoneId.systemDefault();
|
||||
org.joda.time.DateTimeZone jodaZone = toJodaZone(javaZone);
|
||||
return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime();
|
||||
return new org.joda.time.LocalDateTime(
|
||||
localDateTime.getYear(),
|
||||
localDateTime.getMonthValue(),
|
||||
localDateTime.getDayOfMonth(),
|
||||
localDateTime.getHour(),
|
||||
localDateTime.getMinute(),
|
||||
localDateTime.getSecond(),
|
||||
localDateTime.getNano() / 1_000_000 // 毫秒转纳秒
|
||||
);
|
||||
}
|
||||
|
||||
// ================================
|
||||
@@ -238,9 +247,15 @@ public class JodaTimeTools {
|
||||
* @return Java 8 LocalDateTime
|
||||
*/
|
||||
public static java.time.LocalDateTime toJavaLocalDateTime(org.joda.time.LocalDateTime localDateTime) {
|
||||
org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault();
|
||||
java.time.ZoneId javaZone = toJavaZone(jodaZone);
|
||||
return toJavaInstant(localDateTime, jodaZone).atZone(javaZone).toLocalDateTime();
|
||||
return java.time.LocalDateTime.of(
|
||||
localDateTime.getYear(),
|
||||
localDateTime.getMonthOfYear(),
|
||||
localDateTime.getDayOfMonth(),
|
||||
localDateTime.getHourOfDay(),
|
||||
localDateTime.getMinuteOfHour(),
|
||||
localDateTime.getSecondOfMinute(),
|
||||
localDateTime.getMillisOfSecond() * 1_000_000 // 毫秒转纳秒
|
||||
);
|
||||
}
|
||||
|
||||
// ================================
|
||||
|
||||
@@ -25,11 +25,13 @@ import javax.annotation.Nullable;
|
||||
/**
|
||||
* 数字工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class Numbers {
|
||||
|
||||
// ================================
|
||||
// #region - sum
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 求和
|
||||
@@ -131,9 +133,13 @@ public class Numbers {
|
||||
return BigDecimals.sum(numbers);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - nullToZero
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@code null} 转换为 {@code 0}
|
||||
@@ -217,7 +223,122 @@ public class Numbers {
|
||||
return BigDecimals.nullToZero(val);
|
||||
}
|
||||
|
||||
// #endregion
|
||||
// ================================
|
||||
// #endregion - nullToZero
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - parse
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将字符串转为对应 {@link Short},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Short parseShort(@Nullable String str, @Nullable Short defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Short.parseShort(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转为 {@link Integer},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Integer parseInteger(@Nullable String str, @Nullable Integer defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转为 {@link Long},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Long parseLong(@Nullable String str, @Nullable Long defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转为 {@link Float},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Float parseFloat(@Nullable String str, @Nullable Float defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Float.parseFloat(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转为 {@link Double},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Double parseDouble(@Nullable String str, @Nullable Double defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Double.parseDouble(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - parse
|
||||
// ================================
|
||||
|
||||
private Numbers() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
|
||||
@@ -31,7 +31,7 @@ import com.google.common.annotations.Beta;
|
||||
* <p>
|
||||
* 提供一些 Optional 相关的方法
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
* @see Optional
|
||||
* @see OptionalInt
|
||||
|
||||
@@ -17,26 +17,29 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Objects;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
import com.google.common.collect.BoundType;
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
/**
|
||||
* 随机工具类
|
||||
* <p>
|
||||
* 建议调用方自行维护 Random 对象
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public final class RandomTools {
|
||||
|
||||
private static final SecureRandom DEFAULT_SECURE_RANDOM;
|
||||
|
||||
static {
|
||||
SecureRandom secureRandom = null;
|
||||
SecureRandom secureRandom;
|
||||
try {
|
||||
secureRandom = SecureRandom.getInstanceStrong(); // 获取高强度安全随机数生成器
|
||||
}
|
||||
@@ -69,20 +72,20 @@ public final class RandomTools {
|
||||
* @return 随机字符串
|
||||
*/
|
||||
public static String randomStr(Random random, char[] sourceCharacters, int length) {
|
||||
checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgumentNotNull(random, "Random cannot be null.");
|
||||
checkArgumentNotNull(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) {
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgumentNotNull(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) {
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgumentNotNull(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);
|
||||
}
|
||||
@@ -98,24 +101,70 @@ public final class RandomTools {
|
||||
* @return 随机字符串
|
||||
*/
|
||||
public static String randomStr(Random random, String sourceCharacters, int length) {
|
||||
checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgumentNotNull(random, "Random cannot be null.");
|
||||
checkArgumentNotNull(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) {
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgumentNotNull(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) {
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgumentNotNull(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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机整数
|
||||
*
|
||||
* @param min 最小值(包含)
|
||||
* @param max 最大值(不包含)
|
||||
* @return 在区间 {@code [min, max)} 内的随机整数
|
||||
*/
|
||||
public static int randomInt(int min, int max) {
|
||||
return randomIntInternal(ThreadLocalRandom.current(), min, max);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机整数
|
||||
*
|
||||
* @param min 最小值(包含)
|
||||
* @param max 最大值(不包含)
|
||||
* @return 在区间 {@code [min, max)} 内的随机整数
|
||||
*/
|
||||
public static int randomInt(Random random, int min, int max) {
|
||||
checkArgumentNotNull(random, "Random cannot be null.");
|
||||
return randomIntInternal(random, min, max);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机整数
|
||||
*
|
||||
* @param range 整数区间
|
||||
* @return 在指定区间内的随机整数
|
||||
*/
|
||||
public static int randomInt(Range<Integer> range) {
|
||||
checkArgumentNotNull(range, "Range cannot be null.");
|
||||
return randomIntInternal(ThreadLocalRandom.current(), range);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机整数
|
||||
*
|
||||
* @param range 整数区间
|
||||
* @return 在指定区间内的随机整数
|
||||
*/
|
||||
public static int randomInt(Random random, Range<Integer> range) {
|
||||
checkArgumentNotNull(random, "Random cannot be null.");
|
||||
checkArgumentNotNull(range, "Range cannot be null.");
|
||||
return randomIntInternal(random, range);
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用传入的随机数生成器,生成指定长度的字符串
|
||||
*
|
||||
@@ -158,6 +207,31 @@ public final class RandomTools {
|
||||
return String.valueOf(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机整数
|
||||
*
|
||||
* @param min 最小值(包含)
|
||||
* @param max 最大值(不包含)
|
||||
* @return 在区间 {@code [min, max)} 内的随机整数
|
||||
*/
|
||||
private static int randomIntInternal(Random random, int min, int max) {
|
||||
return random.nextInt(max - min) + min;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随机整数
|
||||
*
|
||||
* @param range 整数区间
|
||||
* @return 在指定区间内的随机整数
|
||||
*/
|
||||
private static int randomIntInternal(Random random, Range<Integer> range) {
|
||||
Integer lowerEndpoint = range.lowerEndpoint();
|
||||
Integer upperEndpoint = range.upperEndpoint();
|
||||
int min = range.lowerBoundType() == BoundType.CLOSED ? lowerEndpoint : lowerEndpoint + 1;
|
||||
int max = range.upperBoundType() == BoundType.OPEN ? upperEndpoint : upperEndpoint + 1;
|
||||
return random.nextInt(max - min) + min;
|
||||
}
|
||||
|
||||
private RandomTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
|
||||
@@ -35,7 +35,7 @@ import com.google.common.cache.LoadingCache;
|
||||
/**
|
||||
* 封装一些常用的正则操作,并可以缓存 {@link Pattern} 实例以复用。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public final class RegexTools {
|
||||
|
||||
@@ -363,7 +363,7 @@ public final class RegexTools {
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
private final Pattern compilePattern() {
|
||||
private Pattern compilePattern() {
|
||||
return Pattern.compile(regex, flags);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,10 +17,10 @@
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Objects;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@@ -34,7 +34,7 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
* <p>
|
||||
* 字符串工具类。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class StringTools {
|
||||
@@ -112,7 +112,7 @@ public class StringTools {
|
||||
* @return 结果
|
||||
*/
|
||||
public static String repeat(final String str, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(str));
|
||||
checkArgumentNotNull(str);
|
||||
return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength));
|
||||
}
|
||||
|
||||
@@ -177,6 +177,9 @@ public class StringTools {
|
||||
*/
|
||||
@Beta
|
||||
public static boolean isURL(@Nullable final String cs) {
|
||||
if (cs == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
new URL(cs);
|
||||
} catch (MalformedURLException e) {
|
||||
|
||||
@@ -32,7 +32,7 @@ import javax.annotation.Nullable;
|
||||
/**
|
||||
* TreeBuilder
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
* <p>
|
||||
* 包含树构建器({@link TreeBuilder})、断言工具({@link AssertTools})、ID 生成器({@link IdGenerator})及其它实用工具类。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@ParametersAreNonnullByDefault
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
@@ -38,7 +38,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.create();
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getCode(), e.getTypeCode());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -50,7 +49,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -62,7 +60,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION.create(message);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION, e.getType());
|
||||
assertEquals(InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -77,7 +74,6 @@ public class InvalidInputExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(InvalidInputException.Type.INFRINGE_COPYRIGHT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.INFRINGE_COPYRIGHT.getCode(), e.getTypeCode());
|
||||
log.info("{}", e.getMessage());
|
||||
assertEquals(nfe.toString(), e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
@@ -92,7 +88,6 @@ public class InvalidInputExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -106,7 +101,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.create(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -120,7 +114,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -134,7 +127,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, npe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -148,7 +140,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -167,7 +158,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException();
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -179,7 +169,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -191,7 +180,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -206,7 +194,6 @@ public class InvalidInputExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
log.info("{}", e.getMessage());
|
||||
assertEquals(nfe.toString(), e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
@@ -221,7 +208,6 @@ public class InvalidInputExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -235,7 +221,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -249,7 +234,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -263,7 +247,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message, npe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -277,7 +260,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
|
||||
@@ -41,7 +41,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create();
|
||||
});
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -53,7 +52,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.JSON_PARSING_FAILURE.create(message);
|
||||
});
|
||||
assertSame(ParsingFailureException.Type.JSON_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.Type.JSON_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -65,7 +63,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.XML_PARSING_FAILURE.create(message);
|
||||
});
|
||||
assertSame(ParsingFailureException.XML_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.XML_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -80,7 +77,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
log.info("{}", e.getMessage());
|
||||
assertEquals(nfe.toString(), e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
@@ -95,7 +91,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -109,7 +104,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.NUMBER_PARSING_FAILURE.create(message, nfe);
|
||||
});
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -123,7 +117,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, nfe);
|
||||
});
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -137,7 +130,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, npe);
|
||||
});
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -151,7 +143,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, nfe);
|
||||
});
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -175,7 +166,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(dtpe.getMessage(), e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -189,7 +179,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -206,7 +195,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -223,7 +211,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -238,7 +225,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -253,7 +239,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -277,7 +262,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(dtpe.getMessage(), e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -291,7 +275,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -308,7 +291,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -325,7 +307,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -340,7 +321,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -355,7 +335,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
|
||||
@@ -381,7 +381,7 @@ class CustomUnifiedResponseFactoryTests {
|
||||
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, "查询失败", user));
|
||||
|
||||
// Throwable
|
||||
BizException bizException = new BizException("业务异常");
|
||||
BizException bizException = BizException.of("业务异常");
|
||||
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, bizException));
|
||||
assertThrows(NullPointerException.class, () -> CustomUnifiedResponses.error(nullStatus, (Throwable) null));
|
||||
}
|
||||
|
||||
@@ -379,7 +379,7 @@ class UnifiedResponseTests {
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, "查询失败", user));
|
||||
|
||||
// Throwable
|
||||
BizException bizException = new BizException("业务异常");
|
||||
BizException bizException = BizException.of("业务异常");
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, bizException));
|
||||
assertThrows(NullPointerException.class, () -> UnifiedResponses.error(nullStatus, (Throwable) null));
|
||||
}
|
||||
|
||||
@@ -220,7 +220,7 @@ public class PagingAndSortingQueryParamsTests {
|
||||
/**
|
||||
* 账号信息查询参数
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@ToString(callSuper = true)
|
||||
class AccountQueryParams extends PagingAndSortingQueryParams {
|
||||
@@ -231,9 +231,8 @@ class AccountQueryParams extends PagingAndSortingQueryParams {
|
||||
.put("createTime", "create_time")
|
||||
.build();
|
||||
|
||||
public AccountQueryParams() {
|
||||
super(PROPERTY_COLUMN_MAP);
|
||||
}
|
||||
private static final PagingParamsBuilder PAGING_PARAMS_BUILDER = PagingAndSortingQueryParams
|
||||
.pagingParamsBuilder(20, 100, PROPERTY_COLUMN_MAP);
|
||||
|
||||
private @Getter @Setter Long id;
|
||||
private @Getter @Setter String username;
|
||||
@@ -249,6 +248,10 @@ class AccountQueryParams extends PagingAndSortingQueryParams {
|
||||
}
|
||||
return this.createTimeEnd.plusDays(1);
|
||||
}
|
||||
|
||||
public PagingParams buildPagingParams() {
|
||||
return PAGING_PARAMS_BUILDER.buildPagingParams(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Data
|
||||
|
||||
@@ -43,6 +43,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.BoundType;
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||
@@ -83,24 +84,6 @@ class DateTimeToolsTests {
|
||||
CALENDAR.setTime(DATE);
|
||||
}
|
||||
|
||||
// Joda
|
||||
static final org.joda.time.LocalDateTime JODA_LOCAL_DATE_TIME
|
||||
= new org.joda.time.LocalDateTime(2024, 12, 29, 12, 58, 30, 333);
|
||||
static final org.joda.time.LocalDate JODA_LOCAL_DATE = JODA_LOCAL_DATE_TIME.toLocalDate();
|
||||
static final org.joda.time.LocalTime JODA_LOCAL_TIME = JODA_LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
// Joda - 2024-12-29 12:58:30.333 SystemDefaultZone
|
||||
static final org.joda.time.DateTimeZone JODA_SYS_ZONE = org.joda.time.DateTimeZone.getDefault();
|
||||
static final org.joda.time.DateTime JODA_DATE_TIME_WITH_SYS_ZONE = JODA_LOCAL_DATE_TIME.toDateTime(JODA_SYS_ZONE);
|
||||
static final org.joda.time.Instant JODA_INSTANT_WITH_SYS_ZONE = JODA_DATE_TIME_WITH_SYS_ZONE.toInstant();
|
||||
static final long JODA_INSTANT_MILLIS = JODA_INSTANT_WITH_SYS_ZONE.getMillis();
|
||||
|
||||
// Joda - 2024-12-29 12:58:30.333 GMT+04:00
|
||||
static final org.joda.time.DateTimeZone JODA_ZONE = org.joda.time.DateTimeZone.forID("GMT+04:00");
|
||||
static final org.joda.time.DateTime JODA_DATE_TIME = JODA_LOCAL_DATE_TIME.toDateTime(JODA_ZONE);
|
||||
static final org.joda.time.Instant JODA_INSTANT = JODA_DATE_TIME.toInstant();
|
||||
static final long JODA_MILLIS = JODA_INSTANT.getMillis();
|
||||
|
||||
// ================================
|
||||
// #region - toDate
|
||||
// ================================
|
||||
@@ -109,9 +92,7 @@ class DateTimeToolsTests {
|
||||
void toDate_timeMillis() {
|
||||
assertNotEquals(SYS_DATE, DATE);
|
||||
log.info("SYS_DATE: {}, DATE: {}", SYS_DATE, DATE);
|
||||
assertEquals(SYS_DATE, JODA_DATE_TIME_WITH_SYS_ZONE.toDate());
|
||||
assertEquals(SYS_DATE, DateTimeTools.toDate(INSTANT_MILLIS));
|
||||
assertEquals(DATE, JODA_DATE_TIME.toDate());
|
||||
assertEquals(DATE, DateTimeTools.toDate(MILLIS));
|
||||
}
|
||||
|
||||
@@ -393,11 +374,11 @@ class DateTimeToolsTests {
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// #region - range
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void startDateTimeRange() {
|
||||
void toDateTimeRange_specifiedDate() {
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE);
|
||||
assertEquals(LOCAL_DATE.atStartOfDay(), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
||||
@@ -411,8 +392,96 @@ class DateTimeToolsTests {
|
||||
assertFalse(zonedDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay().atZone(SYS_ZONE_ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDateTimeRange_dateRange_openRange() {
|
||||
// (2000-01-01..2025-10-01) -> [2000-01-02T00:00..2025-10-01T00:00)
|
||||
Range<LocalDate> dateRange = Range.open(
|
||||
LocalDate.of(2000, 1, 1),
|
||||
LocalDate.of(2025, 10, 1));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0), localDateTimeRange.upperEndpoint());
|
||||
log.info(localDateTimeRange.toString());
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
log.info(zonedDateTimeRange.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDateTimeRange_dateRange_openClosedRange() {
|
||||
// (2000-01-01..2025-10-01] -> [2025-01-02T00-00..2025-10-02 00:00)
|
||||
Range<LocalDate> dateRange = Range.openClosed(
|
||||
LocalDate.of(2000, 1, 1),
|
||||
LocalDate.of(2025, 10, 1));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0), localDateTimeRange.upperEndpoint());
|
||||
log.info(localDateTimeRange.toString());
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
log.info(zonedDateTimeRange.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDateTimeRange_dateRange_closedRange() {
|
||||
// [2000-01-01..2025-10-01] -> [2025-01-01T00-00..2025-10-02 00:00)
|
||||
Range<LocalDate> dateRange = Range.closed(
|
||||
LocalDate.of(2000, 1, 1),
|
||||
LocalDate.of(2025, 10, 1));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0), localDateTimeRange.upperEndpoint());
|
||||
log.info(localDateTimeRange.toString());
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
log.info(zonedDateTimeRange.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDateTimeRange_dateRange_closedOpenRange() {
|
||||
// [2025-01-01..2025-10-01) -> [2025-01-01T00-00..2025-10-01 00:00)
|
||||
Range<LocalDate> dateRange = Range.closedOpen(
|
||||
LocalDate.of(2000, 1, 1),
|
||||
LocalDate.of(2025, 10, 1));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0), localDateTimeRange.upperEndpoint());
|
||||
log.info(localDateTimeRange.toString());
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
log.info(zonedDateTimeRange.toString());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - others
|
||||
// #endregion - range
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
|
||||
@@ -33,7 +33,7 @@ import org.junit.jupiter.api.Test;
|
||||
public class JodaTimeToolsTests {
|
||||
|
||||
// Java
|
||||
static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2024, 12, 29, 12, 58, 30, 333000000);
|
||||
static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2024, 12, 29, 12, 58, 30, 333 * 1_000_000);
|
||||
static final LocalDate LOCAL_DATE = LOCAL_DATE_TIME.toLocalDate();
|
||||
static final LocalTime LOCAL_TIME = LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ import java.util.Arrays;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public
|
||||
@@ -68,9 +69,16 @@ class NumbersTests {
|
||||
|
||||
@Test
|
||||
public void sum_BigIntegerArray_ReturnsCorrectSum() {
|
||||
BigInteger[] numbers = {new BigInteger("1"), new BigInteger("2"), new BigInteger("3")};
|
||||
BigInteger[] numbers = {
|
||||
new BigInteger("1"),
|
||||
new BigInteger("2"),
|
||||
null,
|
||||
new BigInteger("3")
|
||||
};
|
||||
BigInteger result = Numbers.sum(numbers);
|
||||
assertEquals(new BigInteger("6"), result);
|
||||
|
||||
assertEquals(BigInteger.ZERO, Numbers.sum(new BigInteger[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -192,6 +200,100 @@ class NumbersTests {
|
||||
assertEquals(BigDecimal.ZERO, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseShort(String, Short)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseShort() {
|
||||
assertEquals((short) 12345, Numbers.parseShort("12345", (short) 5));
|
||||
assertEquals((short) 5, Numbers.parseShort("1234.5", (short) 5));
|
||||
assertEquals((short) 5, Numbers.parseShort("", (short) 5));
|
||||
assertEquals((short) 5, Numbers.parseShort(null, (short) 5));
|
||||
|
||||
assertEquals((short) 12345, Numbers.parseShort("12345", null));
|
||||
assertNull(Numbers.parseShort("1234.5", null));
|
||||
assertNull(Numbers.parseShort("", null));
|
||||
assertNull(Numbers.parseShort(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseInteger(String, Integer)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseInteger() {
|
||||
assertEquals(12345, Numbers.parseInteger("12345", 5));
|
||||
assertEquals(5, Numbers.parseInteger("1234.5", 5));
|
||||
assertEquals(5, Numbers.parseInteger("", 5));
|
||||
assertEquals(5, Numbers.parseInteger(null, 5));
|
||||
|
||||
assertEquals(12345, Numbers.parseInteger("12345", null));
|
||||
assertNull(Numbers.parseInteger("1234.5", null));
|
||||
assertNull(Numbers.parseInteger("", null));
|
||||
assertNull(Numbers.parseInteger(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseLong(String, Long)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseLong() {
|
||||
assertEquals(12345L, Numbers.parseLong("12345", 5L));
|
||||
assertEquals(5L, Numbers.parseLong("1234.5", 5L));
|
||||
assertEquals(5L, Numbers.parseLong("", 5L));
|
||||
assertEquals(5L, Numbers.parseLong(null, 5L));
|
||||
|
||||
assertEquals(12345L, Numbers.parseLong("12345", null));
|
||||
assertNull(Numbers.parseLong("1234.5", null));
|
||||
assertNull(Numbers.parseLong("", null));
|
||||
assertNull(Numbers.parseLong(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseFloat(String, Float)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseFloat() {
|
||||
assertEquals(1.2345f, Numbers.parseFloat("1.2345", 5.1f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("a", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("-001Z.2345", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("+001AB.2345", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("001Z.2345", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat(null, 5.0f));
|
||||
|
||||
assertEquals(1.2345f, Numbers.parseFloat("1.2345", null));
|
||||
assertNull(Numbers.parseFloat("a", null));
|
||||
assertNull(Numbers.parseFloat("-001Z.2345", null));
|
||||
assertNull(Numbers.parseFloat("+001AB.2345", null));
|
||||
assertNull(Numbers.parseFloat("001Z.2345", null));
|
||||
assertNull(Numbers.parseFloat("", null));
|
||||
assertNull(Numbers.parseFloat(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseDouble(String, Double)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseDouble() {
|
||||
assertEquals(1.2345d, Numbers.parseDouble("1.2345", 5.1d));
|
||||
assertEquals(5.0d, Numbers.parseDouble("a", 5.0d));
|
||||
assertEquals(1.2345d, Numbers.parseDouble("001.2345", 5.1d));
|
||||
assertEquals(-1.2345d, Numbers.parseDouble("-001.2345", 5.1d));
|
||||
assertEquals(1.2345d, Numbers.parseDouble("+001.2345", 5.1d));
|
||||
assertEquals(0d, Numbers.parseDouble("000.00", 5.1d));
|
||||
assertEquals(5.1d, Numbers.parseDouble("", 5.1d));
|
||||
assertEquals(5.1d, Numbers.parseDouble((String) null, 5.1d));
|
||||
|
||||
assertEquals(1.2345d, Numbers.parseDouble("1.2345", null));
|
||||
assertEquals(null, Numbers.parseDouble("a", null));
|
||||
assertEquals(1.2345d, Numbers.parseDouble("001.2345", null));
|
||||
assertEquals(-1.2345d, Numbers.parseDouble("-001.2345", null));
|
||||
assertEquals(1.2345d, Numbers.parseDouble("+001.2345", null));
|
||||
assertEquals(0d, Numbers.parseDouble("000.00", null));
|
||||
assertEquals(null, Numbers.parseDouble("", null));
|
||||
assertEquals(null, Numbers.parseDouble((String) null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
||||
Constructor<?>[] constructors = Numbers.class.getDeclaredConstructors();
|
||||
|
||||
@@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.security.SecureRandom;
|
||||
@@ -30,6 +31,8 @@ import java.util.Random;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
@SuppressWarnings("null")
|
||||
public class RandomToolsTests {
|
||||
|
||||
@@ -107,6 +110,50 @@ public class RandomToolsTests {
|
||||
assertEquals(5, result.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomInt_WithMinAndMax() {
|
||||
for (int i = 0; i < 1000_0000; i++) {
|
||||
int r = RandomTools.randomInt(random, -2, 3);
|
||||
assertTrue(r >= -2 && r < 3);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomInt_WithClosedOpenRange() {
|
||||
Range<Integer> co = Range.closedOpen(-2, 3);
|
||||
for (int i = 0; i < 1000_0000; i++) {
|
||||
int rco = RandomTools.randomInt(random, co);
|
||||
assertTrue(rco >= -2 && rco < 3);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomInt_WithClosedRange() {
|
||||
Range<Integer> cc = Range.closed(-2, 3);
|
||||
for (int i = 0; i < 1000_0000; i++) {
|
||||
int rcc = RandomTools.randomInt(random, cc);
|
||||
assertTrue(rcc >= -2 && rcc <= 3);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomInt_WithOpenClosedRange() {
|
||||
Range<Integer> oc = Range.openClosed(-2, 3);
|
||||
for (int i = 0; i < 1000_0000; i++) {
|
||||
int roc = RandomTools.randomInt(random, oc);
|
||||
assertTrue(roc > -2 && roc <= 3);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void randomInt_WithOpenRange() {
|
||||
Range<Integer> oo = Range.open(-2, 3);
|
||||
for (int i = 0; i < 1000_0000; i++) {
|
||||
int roo = RandomTools.randomInt(random, oo);
|
||||
assertTrue(roo > -2 && roo < 3);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
||||
Constructor<?>[] constructors = RandomTools.class.getDeclaredConstructors();
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-parent</artifactId>
|
||||
<version>1.1.0-RC1</version>
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>plusone-dependencies</artifactId>
|
||||
@@ -18,11 +18,6 @@
|
||||
</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>
|
||||
|
||||
@@ -35,6 +30,9 @@
|
||||
<commons-dbutils.version>1.8.1</commons-dbutils.version>
|
||||
<commons-crypto.version>1.2.0</commons-crypto.version>
|
||||
|
||||
<okhttp.version>5.2.0</okhttp.version>
|
||||
<okio.version>3.16.0</okio.version>
|
||||
|
||||
<logback.version>1.3.15</logback.version>
|
||||
|
||||
<jackson.version>2.18.3</jackson.version>
|
||||
@@ -111,6 +109,18 @@
|
||||
<version>${commons-crypto.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<version>${okhttp.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.squareup.okio</groupId>
|
||||
<artifactId>okio</artifactId>
|
||||
<version>${okio.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
@@ -128,6 +138,7 @@
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Gson -->
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
@@ -214,7 +225,7 @@
|
||||
<version>${junit.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@@ -223,7 +234,7 @@
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-clean-plugin</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.4.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
@@ -231,31 +242,31 @@
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<version>3.13.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.4.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<version>3.1.1</version>
|
||||
<version>3.1.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>3.1.1</version>
|
||||
<version>3.1.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-site-plugin</artifactId>
|
||||
<version>4.0.0-M7</version>
|
||||
<version>3.12.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-project-info-reports-plugin</artifactId>
|
||||
<version>3.4.2</version>
|
||||
<version>3.6.1</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
|
||||
6
pom.xml
6
pom.xml
@@ -6,9 +6,13 @@
|
||||
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-parent</artifactId>
|
||||
<version>1.1.0-RC1</version>
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>plusone-commons</module>
|
||||
<module>plusone-dependencies</module>
|
||||
|
||||
Reference in New Issue
Block a user