Compare commits
3 Commits
dev
...
refactor/M
Author | SHA1 | Date | |
---|---|---|---|
f4e3684a3f | |||
ce56f297f3 | |||
577b73874b |
11
README.md
11
README.md
@ -68,17 +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> {
|
||||
private static final long serialVersionUID = 881293090625085616L;
|
||||
implements IMultiTypesException<LoginException, String, LoginException.Type> {
|
||||
private final Type type;
|
||||
private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
super(message);
|
||||
@ -104,7 +103,7 @@ public final class LoginException
|
||||
|
||||
// ...
|
||||
|
||||
public enum Type implements ExceptionType {
|
||||
public enum Type implements IExceptionType<LoginException, String> {
|
||||
DEFAULT("00", "当前会话未登录"),
|
||||
NOT_TOKEN("10", "未提供token"),
|
||||
INVALID_TOKEN("20", "token无效"),
|
||||
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* 异常工厂
|
||||
*
|
||||
* @author ZhouXY
|
||||
*/
|
||||
public interface IExceptionFactory<X extends Exception> {
|
||||
/**
|
||||
* 创建异常
|
||||
*
|
||||
* @return 异常对象
|
||||
*/
|
||||
@Nonnull
|
||||
X create();
|
||||
|
||||
/**
|
||||
* 使用指定 {@code message} 创建异常
|
||||
*
|
||||
* @param message 异常信息
|
||||
* @return 异常对象
|
||||
*/
|
||||
@Nonnull
|
||||
X create(String message);
|
||||
|
||||
/**
|
||||
* 使用指定 {@code cause} 创建异常
|
||||
*
|
||||
* @param cause 包装的异常
|
||||
* @return 异常对象
|
||||
*/
|
||||
@Nonnull
|
||||
X create(Throwable cause);
|
||||
|
||||
/**
|
||||
* 使用指定 {@code message} 和 {@code cause} 创建异常
|
||||
*
|
||||
* @param message 异常信息
|
||||
* @param cause 包装的异常
|
||||
* @return 异常对象
|
||||
*/
|
||||
@Nonnull
|
||||
X create(String message, Throwable cause);
|
||||
}
|
@ -15,27 +15,30 @@
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* implements IMultiTypesException<LoginException, String, LoginException.Type> {
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
* super(message);
|
||||
@ -61,7 +64,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* public enum Type implements ExceptionType<LoginException> {
|
||||
* public enum Type implements IExceptionType<LoginException, String> {
|
||||
* DEFAULT("00", "当前会话未登录"),
|
||||
* NOT_TOKEN("10", "未提供token"),
|
||||
* INVALID_TOKEN("20", "token无效"),
|
||||
@ -118,15 +121,20 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
* throw LoginException.Type.TOKEN_TIMEOUT.create();
|
||||
* </pre>
|
||||
*
|
||||
* @param <X> 具体异常类
|
||||
* @param <T> 异常场景
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface MultiTypesException<E extends Exception, T extends MultiTypesException.ExceptionType<E>> {
|
||||
public interface IMultiTypesException<
|
||||
X extends Exception,
|
||||
TCode extends Serializable,
|
||||
T extends IMultiTypesException.IExceptionType<X, TCode>> {
|
||||
|
||||
/**
|
||||
* 异常类型
|
||||
*
|
||||
* @return 异常类型。通常是实现了 {@link ExceptionType} 的枚举。
|
||||
* @return 异常类型。通常是实现了 {@link IExceptionType} 的枚举。
|
||||
*/
|
||||
@Nonnull
|
||||
T getType();
|
||||
@ -136,55 +144,25 @@ public interface MultiTypesException<E extends Exception, T extends MultiTypesEx
|
||||
*
|
||||
* @return 异常类型编码
|
||||
*/
|
||||
default @Nonnull String getTypeCode() {
|
||||
default @Nonnull TCode getTypeCode() {
|
||||
return getType().getCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常类型
|
||||
*/
|
||||
public static interface ExceptionType<E extends Exception> extends IWithCode<String> {
|
||||
public static interface IExceptionType<X extends Exception, TCode extends Serializable>
|
||||
extends IWithCode<TCode>, IExceptionFactory<X> {
|
||||
|
||||
/**
|
||||
* 默认异常信息
|
||||
*/
|
||||
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);
|
||||
@Virtual
|
||||
default String getDescription() {
|
||||
return getDefaultMessage();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -21,7 +21,7 @@ import java.time.format.DateTimeParseException;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
|
||||
import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException.IExceptionType;
|
||||
|
||||
/**
|
||||
* 解析失败异常
|
||||
@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
* 如果表示用户传参造成的解析失败,可使用 {@link RequestParamsException#RequestParamsException(Throwable)},
|
||||
* 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出。
|
||||
* <pre>
|
||||
* throw new RequestParamsException(ParsingFailureException.Type.NUMBER_PARSING_FAILURE.create());
|
||||
* throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE));
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
@ -39,8 +39,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
*/
|
||||
public final class ParsingFailureException
|
||||
extends RuntimeException
|
||||
implements MultiTypesException<ParsingFailureException, ParsingFailureException.Type> {
|
||||
private static final long serialVersionUID = 795996090625132616L;
|
||||
implements IMultiTypesException<ParsingFailureException, String, ParsingFailureException.Type> {
|
||||
|
||||
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<ParsingFailureException, String> {
|
||||
DEFAULT("00", "解析失败"),
|
||||
NUMBER_PARSING_FAILURE("10", "数字转换失败"),
|
||||
DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),
|
||||
|
@ -29,7 +29,6 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class BizException extends RuntimeException {
|
||||
private static final long serialVersionUID = 982585090625482416L;
|
||||
|
||||
private static final String DEFAULT_MSG = "业务异常";
|
||||
|
||||
|
@ -18,8 +18,8 @@ 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.IMultiTypesException.IExceptionType;
|
||||
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException;
|
||||
|
||||
/**
|
||||
* InvalidInputException
|
||||
@ -35,8 +35,7 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException;
|
||||
*/
|
||||
public final class InvalidInputException
|
||||
extends RequestParamsException
|
||||
implements MultiTypesException<InvalidInputException, InvalidInputException.Type> {
|
||||
private static final long serialVersionUID = -28994090625082516L;
|
||||
implements IMultiTypesException<InvalidInputException, String, InvalidInputException.Type> {
|
||||
|
||||
private final Type type;
|
||||
|
||||
@ -109,7 +108,7 @@ public final class InvalidInputException
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public enum Type implements ExceptionType<InvalidInputException> {
|
||||
public enum Type implements IExceptionType<InvalidInputException, String> {
|
||||
DEFAULT("00", "用户输入内容非法"),
|
||||
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
||||
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
||||
|
@ -26,7 +26,6 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class RequestParamsException extends BizException {
|
||||
private static final long serialVersionUID = 448337090625192516L;
|
||||
|
||||
private static final String DEFAULT_MSG = "用户请求参数错误";
|
||||
|
||||
|
@ -17,21 +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> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* implements IMultiTypesException<LoginException, String, LoginException.Type> {
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
* super(message);
|
||||
@ -57,7 +57,7 @@
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* public enum Type implements ExceptionType<LoginException> {
|
||||
* public enum Type implements IExceptionType<LoginException, String> {
|
||||
* DEFAULT("00", "当前会话未登录"),
|
||||
* NOT_TOKEN("10", "未提供token"),
|
||||
* INVALID_TOKEN("20", "token无效"),
|
||||
|
@ -31,7 +31,6 @@ package xyz.zhouxy.plusone.commons.exception.system;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class DataOperationResultException extends SysException {
|
||||
private static final long serialVersionUID = 992754090625352516L;
|
||||
|
||||
private final long expected;
|
||||
private final long actual;
|
||||
|
@ -26,7 +26,6 @@ package xyz.zhouxy.plusone.commons.exception.system;
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class SysException extends RuntimeException {
|
||||
private static final long serialVersionUID = -936435090625482516L;
|
||||
|
||||
private static final String DEFAULT_MSG = "系统异常";
|
||||
|
||||
|
@ -15,21 +15,19 @@
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.gson.adapter;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.time.temporal.TemporalQuery;
|
||||
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 包含 JSR-310 相关数据类型的 {@code TypeAdapter}
|
||||
*
|
||||
@ -44,15 +42,16 @@ public class JSR310TypeAdapters {
|
||||
* {@code LocalDate} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code LocalDate} 进行相互转换。
|
||||
*/
|
||||
public static final class LocalDateTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<LocalDate, LocalDateTypeAdapter> {
|
||||
public static final class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE} 进行 {@link LocalDate} 的序列化与反序列化。
|
||||
*/
|
||||
public LocalDateTypeAdapter() {
|
||||
this(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,7 +61,20 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link LocalDate} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
|
||||
super(LocalDate::from, formatter);
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
return LocalDate.parse(in.nextString(), dateTimeFormatter);
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,15 +82,16 @@ public class JSR310TypeAdapters {
|
||||
* {@code LocalDateTime} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code LocalDateTime} 进行相互转换。
|
||||
*/
|
||||
public static final class LocalDateTimeTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<LocalDateTime, LocalDateTimeTypeAdapter> {
|
||||
public static final class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} 进行 {@link LocalDateTime} 的序列化与反序列化。
|
||||
*/
|
||||
public LocalDateTimeTypeAdapter() {
|
||||
this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -88,7 +101,20 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link LocalDateTime} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
|
||||
super(LocalDateTime::from, formatter);
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDateTime value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public LocalDateTime read(JsonReader in) throws IOException {
|
||||
return LocalDateTime.parse(in.nextString(), dateTimeFormatter);
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,15 +122,16 @@ public class JSR310TypeAdapters {
|
||||
* {@code ZonedDateTime} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code ZonedDateTime} 进行相互转换。
|
||||
*/
|
||||
public static final class ZonedDateTimeTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<ZonedDateTime, ZonedDateTimeTypeAdapter> {
|
||||
public static final class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_ZONED_DATE_TIME} 进行 {@link ZonedDateTime} 的序列化与反序列化。
|
||||
*/
|
||||
public ZonedDateTimeTypeAdapter() {
|
||||
this(DateTimeFormatter.ISO_ZONED_DATE_TIME);
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -114,7 +141,20 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link ZonedDateTime} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) {
|
||||
super(ZonedDateTime::from, formatter);
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, ZonedDateTime value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public ZonedDateTime read(JsonReader in) throws IOException {
|
||||
return ZonedDateTime.parse(in.nextString(), dateTimeFormatter);
|
||||
}
|
||||
}
|
||||
|
||||
@ -126,40 +166,18 @@ public class JSR310TypeAdapters {
|
||||
* 使用 {@link DateTimeFormatter#ISO_INSTANT} 进行 {@link Instant} 的序列化与反序列化。
|
||||
*
|
||||
*/
|
||||
public static final class InstantTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<Instant, InstantTypeAdapter> {
|
||||
public static final class InstantTypeAdapter extends TypeAdapter<Instant> {
|
||||
|
||||
public InstantTypeAdapter() {
|
||||
super(Instant::from, DateTimeFormatter.ISO_INSTANT);
|
||||
}
|
||||
}
|
||||
|
||||
private abstract static class TemporalAccessorTypeAdapter<
|
||||
T extends TemporalAccessor,
|
||||
TTypeAdapter extends TemporalAccessorTypeAdapter<T, TTypeAdapter>>
|
||||
extends TypeAdapter<T> {
|
||||
|
||||
private final TemporalQuery<T> temporalQuery;
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
|
||||
protected TemporalAccessorTypeAdapter(
|
||||
TemporalQuery<T> temporalQuery, DateTimeFormatter dateTimeFormatter) {
|
||||
checkArgumentNotNull(dateTimeFormatter, "formatter must not be null.");
|
||||
this.temporalQuery = temporalQuery;
|
||||
this.dateTimeFormatter = dateTimeFormatter;
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, Instant value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_INSTANT.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, T value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public T read(JsonReader in) throws IOException {
|
||||
return dateTimeFormatter.parse(in.nextString(), temporalQuery);
|
||||
public Instant read(JsonReader in) throws IOException {
|
||||
return Instant.parse(in.nextString());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@ -34,6 +32,7 @@ import com.google.errorprone.annotations.Immutable;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
/**
|
||||
@ -87,13 +86,13 @@ public class Chinese2ndGenIDCardNumber
|
||||
*/
|
||||
public static Chinese2ndGenIDCardNumber of(final String idCardNumber) {
|
||||
try {
|
||||
checkArgument(StringTools.isNotBlank(idCardNumber), "二代居民身份证校验失败:号码为空");
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(idCardNumber), "二代居民身份证校验失败:号码为空");
|
||||
final String value = idCardNumber.toUpperCase();
|
||||
final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(value);
|
||||
checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
|
||||
AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
|
||||
|
||||
final String provinceCode = matcher.group("province");
|
||||
checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
|
||||
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
|
||||
|
||||
final String cityCode = matcher.group("city");
|
||||
final String countyCode = matcher.group("county");
|
||||
|
@ -16,9 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.IWithIntCode;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 性别
|
||||
@ -51,7 +50,7 @@ public enum Gender implements IWithIntCode {
|
||||
* @return 枚举值
|
||||
*/
|
||||
public static Gender of(int value) {
|
||||
checkCondition(0 <= value && value < VALUES.length,
|
||||
AssertTools.checkCondition(0 <= value && value < VALUES.length,
|
||||
() -> new EnumConstantNotPresentException(Gender.class, String.valueOf(value)));
|
||||
return VALUES[value];
|
||||
}
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
@ -27,6 +25,7 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 带校验的字符串值对象
|
||||
@ -75,10 +74,10 @@ 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.");
|
||||
AssertTools.checkArgument(Objects.nonNull(value), "The value cannot be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(pattern), "The pattern cannot be null.");
|
||||
this.matcher = pattern.matcher(value);
|
||||
checkArgument(this.matcher.matches(), errorMessage);
|
||||
AssertTools.checkArgument(this.matcher.matches(), errorMessage);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
@ -29,6 +27,7 @@ import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
@ -61,10 +60,10 @@ public class PagingAndSortingQueryParams {
|
||||
* @param sortableProperties 可排序的属性。不可为空。
|
||||
*/
|
||||
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) {
|
||||
checkArgument(CollectionTools.isNotEmpty(sortableProperties),
|
||||
AssertTools.checkArgument(CollectionTools.isNotEmpty(sortableProperties),
|
||||
"Sortable properties can not be empty.");
|
||||
sortableProperties.forEach((k, v) ->
|
||||
checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
|
||||
"Property name must not be blank."));
|
||||
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
|
||||
}
|
||||
@ -108,7 +107,7 @@ public class PagingAndSortingQueryParams {
|
||||
public final PagingParams buildPagingParams() {
|
||||
final int sizeValue = this.size != null ? this.size : defaultSizeInternal();
|
||||
final long pageNumValue = this.pageNum != null ? this.pageNum : 1L;
|
||||
checkArgument(CollectionTools.isNotEmpty(this.orderBy),
|
||||
AssertTools.checkArgument(CollectionTools.isNotEmpty(this.orderBy),
|
||||
"The 'orderBy' cannot be empty");
|
||||
final List<SortableProperty> propertiesToSort = this.orderBy.stream()
|
||||
.map(this::generateSortableProperty)
|
||||
@ -139,13 +138,13 @@ public class PagingAndSortingQueryParams {
|
||||
}
|
||||
|
||||
private SortableProperty generateSortableProperty(String orderByStr) {
|
||||
checkArgument(StringTools.isNotBlank(orderByStr));
|
||||
checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(orderByStr));
|
||||
AssertTools.checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
|
||||
String[] propertyNameAndOrderType = orderByStr.split("-");
|
||||
checkArgument(propertyNameAndOrderType.length == 2);
|
||||
AssertTools.checkArgument(propertyNameAndOrderType.length == 2);
|
||||
|
||||
String propertyName = propertyNameAndOrderType[0];
|
||||
checkArgument(sortableProperties.containsKey(propertyName),
|
||||
AssertTools.checkArgument(sortableProperties.containsKey(propertyName),
|
||||
"The property name must be in the set of sortable properties.");
|
||||
String columnName = sortableProperties.get(propertyName);
|
||||
String orderType = propertyNameAndOrderType[1];
|
||||
@ -165,7 +164,7 @@ public class PagingAndSortingQueryParams {
|
||||
SortableProperty(String propertyName, String columnName, String orderType) {
|
||||
this.propertyName = propertyName;
|
||||
this.columnName = columnName;
|
||||
checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
|
||||
AssertTools.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
|
||||
this.orderType = orderType.toUpperCase();
|
||||
|
||||
this.sqlSnippet = this.propertyName + " " + this.orderType;
|
||||
|
@ -16,9 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.time;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
@ -28,6 +25,7 @@ import com.google.common.collect.Range;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.base.IWithIntCode;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 季度
|
||||
@ -91,7 +89,7 @@ public enum Quarter implements IWithIntCode {
|
||||
*/
|
||||
@StaticFactoryMethod(Quarter.class)
|
||||
public static Quarter fromMonth(Month month) {
|
||||
checkNotNull(month);
|
||||
AssertTools.checkNotNull(month);
|
||||
final int monthValue = month.getValue();
|
||||
return of(computeQuarterValueInternal(monthValue));
|
||||
}
|
||||
@ -248,7 +246,7 @@ public enum Quarter implements IWithIntCode {
|
||||
* @throws DateTimeException 如果给定的季度值不在有效范围内(1到4),将抛出异常
|
||||
*/
|
||||
public static int checkValidIntValue(int value) {
|
||||
checkCondition(value >= 1 && value <= 4,
|
||||
AssertTools.checkCondition(value >= 1 && value <= 4,
|
||||
() -> new DateTimeException("Invalid value for Quarter: " + value));
|
||||
return value;
|
||||
}
|
||||
|
@ -17,7 +17,6 @@
|
||||
package xyz.zhouxy.plusone.commons.time;
|
||||
|
||||
import static java.time.temporal.ChronoField.YEAR;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
@ -33,6 +32,7 @@ import javax.annotation.Nullable;
|
||||
import com.google.errorprone.annotations.Immutable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 表示年份与季度
|
||||
@ -93,7 +93,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(LocalDate date) {
|
||||
checkNotNull(date);
|
||||
AssertTools.checkNotNull(date);
|
||||
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
|
||||
}
|
||||
|
||||
@ -105,7 +105,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(Date date) {
|
||||
checkNotNull(date);
|
||||
AssertTools.checkNotNull(date);
|
||||
@SuppressWarnings("deprecation")
|
||||
final int yearValue = YEAR.checkValidIntValue(date.getYear() + 1900L);
|
||||
@SuppressWarnings("deprecation")
|
||||
@ -121,7 +121,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(Calendar date) {
|
||||
checkNotNull(date);
|
||||
AssertTools.checkNotNull(date);
|
||||
final int yearValue = ChronoField.YEAR.checkValidIntValue(date.get(Calendar.YEAR));
|
||||
final int monthValue = date.get(Calendar.MONTH) + 1;
|
||||
return new YearQuarter(yearValue, Quarter.fromMonth(monthValue));
|
||||
@ -135,7 +135,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(YearMonth yearMonth) {
|
||||
checkNotNull(yearMonth);
|
||||
AssertTools.checkNotNull(yearMonth);
|
||||
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@ -261,7 +258,7 @@ public class ArrayTools {
|
||||
* @throws IllegalArgumentException 当参数为空时抛出
|
||||
*/
|
||||
public static <T> boolean isAllElementsNotNull(final T[] arr) {
|
||||
checkArgument(arr != null, "The array cannot be null.");
|
||||
AssertTools.checkArgument(arr != null, "The array cannot be null.");
|
||||
return Arrays.stream(arr).allMatch(Objects::nonNull);
|
||||
}
|
||||
|
||||
@ -491,10 +488,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static char[] repeat(char[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_CHAR_ARRAY;
|
||||
@ -526,10 +523,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static byte[] repeat(byte[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_BYTE_ARRAY;
|
||||
@ -561,10 +558,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static short[] repeat(short[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_SHORT_ARRAY;
|
||||
@ -596,10 +593,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static int[] repeat(int[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_INT_ARRAY;
|
||||
@ -631,10 +628,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static long[] repeat(long[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_LONG_ARRAY;
|
||||
@ -666,10 +663,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static float[] repeat(float[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_FLOAT_ARRAY;
|
||||
@ -701,10 +698,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static double[] repeat(double[] arr, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
checkArgument(maxLength >= 0,
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_DOUBLE_ARRAY;
|
||||
@ -750,7 +747,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -793,7 +790,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -836,7 +833,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -879,7 +876,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -922,7 +919,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -965,7 +962,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -1008,7 +1005,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -1063,7 +1060,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
|
||||
checkArgument(Objects.nonNull(a));
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@ -1090,7 +1087,7 @@ public class ArrayTools {
|
||||
// #region - indexOf
|
||||
|
||||
public static <T> int indexOf(@Nullable T[] arr, Predicate<? super T> predicate) {
|
||||
checkNotNull(predicate);
|
||||
AssertTools.checkNotNull(predicate);
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
@ -1195,7 +1192,7 @@ public class ArrayTools {
|
||||
// #region - lastIndexOf
|
||||
|
||||
public static <T> int lastIndexOf(@Nullable T[] arr, Predicate<? super T> predicate) {
|
||||
checkNotNull(predicate);
|
||||
AssertTools.checkNotNull(predicate);
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
@ -31,11 +31,11 @@ import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
|
||||
* 本工具类不封装过多判断逻辑,鼓励充分使用项目中的工具类进行逻辑判断。
|
||||
*
|
||||
* <pre>
|
||||
* checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
|
||||
* checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
|
||||
* checkCondition(!CollectionUtils.isEmpty(roles),
|
||||
* AssertTools.checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
|
||||
* AssertTools.checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
|
||||
* AssertTools.checkCondition(!CollectionUtils.isEmpty(roles),
|
||||
* () -> new InvalidInputException("The roles cannot be empty."));
|
||||
* checkCondition(RegexTools.matches(email, PatternConsts.EMAIL),
|
||||
* AssertTools.checkCondition(RegexTools.matches(email, PatternConsts.EMAIL),
|
||||
* "must be a well-formed email address");
|
||||
* </pre>
|
||||
*
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@ -55,8 +53,8 @@ public class BigDecimals {
|
||||
* @return 当 {@code a} 大于 {@code b} 时返回 {@code true}
|
||||
*/
|
||||
public static boolean gt(BigDecimal a, BigDecimal b) {
|
||||
checkNotNull(a, "Parameter could not be null.");
|
||||
checkNotNull(b, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
return (a != b) && (a.compareTo(b) > 0);
|
||||
}
|
||||
|
||||
@ -68,8 +66,8 @@ public class BigDecimals {
|
||||
* @return 当 {@code a} 大于等于 {@code b} 时返回 {@code true}
|
||||
*/
|
||||
public static boolean ge(BigDecimal a, BigDecimal b) {
|
||||
checkNotNull(a, "Parameter could not be null.");
|
||||
checkNotNull(b, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
return (a == b) || (a.compareTo(b) >= 0);
|
||||
}
|
||||
|
||||
@ -81,8 +79,8 @@ public class BigDecimals {
|
||||
* @return 当 {@code a} 小于 {@code b} 时返回 {@code true}
|
||||
*/
|
||||
public static boolean lt(BigDecimal a, BigDecimal b) {
|
||||
checkNotNull(a, "Parameter could not be null.");
|
||||
checkNotNull(b, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
return (a != b) && (a.compareTo(b) < 0);
|
||||
}
|
||||
|
||||
@ -94,8 +92,8 @@ public class BigDecimals {
|
||||
* @return 当 {@code a} 小于等于 {@code b} 时返回 {@code true}
|
||||
*/
|
||||
public static boolean le(BigDecimal a, BigDecimal b) {
|
||||
checkNotNull(a, "Parameter could not be null.");
|
||||
checkNotNull(b, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
return (a == b) || (a.compareTo(b) <= 0);
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@ -46,7 +43,7 @@ public final class EnumTools {
|
||||
@Deprecated
|
||||
private static <E extends Enum<?>> E valueOfInternal(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||
E[] values = enumType.getEnumConstants();
|
||||
checkCondition((ordinal >= 0 && ordinal < values.length),
|
||||
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
|
||||
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
|
||||
return values[ordinal];
|
||||
}
|
||||
@ -62,7 +59,7 @@ public final class EnumTools {
|
||||
*/
|
||||
@Deprecated
|
||||
public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||
checkNotNull(enumType, "Enum type must not be null.");
|
||||
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||
return valueOfInternal(enumType, ordinal);
|
||||
}
|
||||
|
||||
@ -79,7 +76,7 @@ public final class EnumTools {
|
||||
@Deprecated
|
||||
public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用,但不删掉
|
||||
@Nullable Integer ordinal, @Nullable E defaultValue) {
|
||||
checkNotNull(enumType);
|
||||
AssertTools.checkNotNull(enumType);
|
||||
return null == ordinal ? defaultValue : valueOfInternal(enumType, ordinal);
|
||||
}
|
||||
|
||||
@ -98,8 +95,8 @@ public final class EnumTools {
|
||||
Class<E> enumType,
|
||||
@Nullable Integer ordinal,
|
||||
Supplier<E> defaultValue) {
|
||||
checkNotNull(enumType);
|
||||
checkNotNull(defaultValue);
|
||||
AssertTools.checkNotNull(enumType);
|
||||
AssertTools.checkNotNull(defaultValue);
|
||||
return null == ordinal ? defaultValue.get() : valueOfInternal(enumType, ordinal);
|
||||
}
|
||||
|
||||
@ -115,7 +112,7 @@ public final class EnumTools {
|
||||
@Deprecated
|
||||
public static <E extends Enum<?>> E getValueOrDefault(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||
return getValueOrDefault(enumType, ordinal, () -> {
|
||||
checkNotNull(enumType, "Enum type must not be null.");
|
||||
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||
E[] values = enumType.getEnumConstants();
|
||||
return values[0];
|
||||
});
|
||||
@ -136,10 +133,10 @@ public final class EnumTools {
|
||||
}
|
||||
|
||||
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> enumType, Integer ordinal) {
|
||||
checkNotNull(enumType, "Enum type must not be null.");
|
||||
checkNotNull(ordinal, "Ordinal must not be null.");
|
||||
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||
AssertTools.checkNotNull(ordinal, "Ordinal must not be null.");
|
||||
E[] values = enumType.getEnumConstants();
|
||||
checkCondition(ordinal >= 0 && ordinal < values.length,
|
||||
AssertTools.checkCondition(ordinal >= 0 && ordinal < values.length,
|
||||
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
|
||||
return ordinal;
|
||||
}
|
||||
@ -183,7 +180,7 @@ public final class EnumTools {
|
||||
Class<E> enumType,
|
||||
@Nullable Integer ordinal,
|
||||
@Nullable Integer defaultValue) {
|
||||
checkNotNull(enumType);
|
||||
AssertTools.checkNotNull(enumType);
|
||||
return checkOrdinalOrGetInternal(enumType, ordinal, () -> checkOrdinalOrDefaultInternal(enumType, defaultValue, null));
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@ -46,7 +44,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
|
||||
protected final String name;
|
||||
|
||||
protected Enumeration(final int id, final String name) {
|
||||
checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
@ -128,7 +126,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public T get(int id) {
|
||||
checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
|
||||
AssertTools.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
|
||||
return this.valueMap.get(id);
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ -70,7 +69,7 @@ public class IdGenerator {
|
||||
* @return UUID 字符串
|
||||
*/
|
||||
public static String toSimpleString(UUID uuid) {
|
||||
checkArgumentNotNull(uuid);
|
||||
AssertTools.checkArgument(Objects.nonNull(uuid));
|
||||
return (uuidDigits(uuid.getMostSignificantBits() >> 32, 8) +
|
||||
uuidDigits(uuid.getMostSignificantBits() >> 16, 4) +
|
||||
uuidDigits(uuid.getMostSignificantBits(), 4) +
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Objects;
|
||||
@ -69,21 +67,21 @@ 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.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(random, sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String randomStr(char[] sourceCharacters, int length) {
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.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.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
|
||||
}
|
||||
|
||||
@ -98,21 +96,21 @@ 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.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(random, sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String randomStr(String sourceCharacters, int length) {
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.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.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@ -75,7 +72,7 @@ public final class RegexTools {
|
||||
* @return {@link Pattern} 实例
|
||||
*/
|
||||
public static Pattern getPattern(final String pattern, final int flags, final boolean cachePattern) {
|
||||
checkNotNull(pattern);
|
||||
AssertTools.checkNotNull(pattern);
|
||||
return cachePattern ? cacheAndGetPatternInternal(pattern, flags) : getPatternInternal(pattern, flags);
|
||||
}
|
||||
|
||||
@ -98,7 +95,7 @@ public final class RegexTools {
|
||||
*/
|
||||
@Nonnull
|
||||
public static Pattern getPattern(final String pattern, final int flags) {
|
||||
checkNotNull(pattern);
|
||||
AssertTools.checkNotNull(pattern);
|
||||
return getPatternInternal(pattern, flags);
|
||||
}
|
||||
|
||||
@ -118,7 +115,7 @@ public final class RegexTools {
|
||||
* @return 判断结果
|
||||
*/
|
||||
public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) {
|
||||
checkNotNull(pattern);
|
||||
AssertTools.checkNotNull(pattern);
|
||||
return matchesInternal(input, pattern);
|
||||
}
|
||||
|
||||
@ -130,7 +127,7 @@ public final class RegexTools {
|
||||
* @return 判断结果
|
||||
*/
|
||||
public static boolean matchesAny(@Nullable final CharSequence input, final Pattern[] patterns) {
|
||||
checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
||||
return matchesAnyInternal(input, patterns);
|
||||
}
|
||||
|
||||
@ -142,7 +139,7 @@ public final class RegexTools {
|
||||
* @return 判断结果
|
||||
*/
|
||||
public static boolean matchesAll(@Nullable final CharSequence input, final Pattern[] patterns) {
|
||||
checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
||||
return matchesAllInternal(input, patterns);
|
||||
}
|
||||
|
||||
@ -213,8 +210,8 @@ public final class RegexTools {
|
||||
* @return 结果
|
||||
*/
|
||||
public static Matcher getMatcher(final CharSequence input, final Pattern pattern) {
|
||||
checkNotNull(input);
|
||||
checkNotNull(pattern);
|
||||
AssertTools.checkNotNull(input);
|
||||
AssertTools.checkNotNull(pattern);
|
||||
return pattern.matcher(input);
|
||||
}
|
||||
|
||||
@ -264,8 +261,8 @@ public final class RegexTools {
|
||||
* @return 结果
|
||||
*/
|
||||
public static Matcher getMatcher(final CharSequence input, final String pattern, final int flags) {
|
||||
checkNotNull(input);
|
||||
checkNotNull(pattern);
|
||||
AssertTools.checkNotNull(input);
|
||||
AssertTools.checkNotNull(pattern);
|
||||
return getPatternInternal(pattern, flags).matcher(input);
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@ -75,9 +73,9 @@ public class SnowflakeIdGenerator {
|
||||
* @param datacenterId 数据中心ID (0~31)
|
||||
*/
|
||||
public SnowflakeIdGenerator(final long workerId, final long datacenterId) {
|
||||
checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0),
|
||||
AssertTools.checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0),
|
||||
"WorkerId can't be greater than %s or less than 0.", MAX_WORKER_ID);
|
||||
checkArgument((datacenterId <= MAX_DATACENTER_ID && datacenterId >= 0),
|
||||
AssertTools.checkArgument((datacenterId <= MAX_DATACENTER_ID && datacenterId >= 0),
|
||||
"DatacenterId can't be greater than %s or less than 0.", MAX_DATACENTER_ID);
|
||||
this.datacenterIdAndWorkerId
|
||||
= (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT);
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Objects;
|
||||
@ -112,7 +110,7 @@ public class StringTools {
|
||||
* @return 结果
|
||||
*/
|
||||
public static String repeat(final String str, int times, int maxLength) {
|
||||
checkArgument(Objects.nonNull(str));
|
||||
AssertTools.checkArgument(Objects.nonNull(str));
|
||||
return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength));
|
||||
}
|
||||
|
||||
@ -212,8 +210,8 @@ public class StringTools {
|
||||
if (src == null || src.isEmpty()) {
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
checkArgument(front >= 0 && end >= 0);
|
||||
checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度");
|
||||
AssertTools.checkArgument(front >= 0 && end >= 0);
|
||||
AssertTools.checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度");
|
||||
final char[] charArray = src.toCharArray();
|
||||
for (int i = front; i < charArray.length - end; i++) {
|
||||
charArray[i] = replacedChar;
|
||||
|
@ -16,8 +16,6 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@ -78,7 +76,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||
* @param nodes 平铺的节点列表
|
||||
*/
|
||||
public List<T> buildTree(Collection<T> nodes) {
|
||||
checkNotNull(nodes);
|
||||
AssertTools.checkNotNull(nodes);
|
||||
return buildTreeInternal(nodes, this.defaultComparator);
|
||||
}
|
||||
|
||||
@ -95,7 +93,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||
* <b>仅影响调用 addChild 的顺序,如果操作对象本身对应的控制了子节点的顺序,无法影响其相关逻辑。</b>
|
||||
*/
|
||||
public List<T> buildTree(Collection<T> nodes, @Nullable Comparator<? super T> comparator) {
|
||||
checkNotNull(nodes);
|
||||
AssertTools.checkNotNull(nodes);
|
||||
final Comparator<? super T> c = (comparator != null) ? comparator : this.defaultComparator;
|
||||
return buildTreeInternal(nodes, c);
|
||||
}
|
||||
|
@ -17,12 +17,12 @@
|
||||
package xyz.zhouxy.plusone.commons.base;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
class IWithCodeTests {
|
||||
|
||||
@ -91,7 +91,7 @@ class IWithCodeTests {
|
||||
private final String code;
|
||||
|
||||
WithCode(String code) {
|
||||
checkNotNull(code);
|
||||
AssertTools.checkNotNull(code);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
|
@ -19,12 +19,14 @@ package xyz.zhouxy.plusone.commons.model.dto.test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Statement;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@ -43,6 +45,9 @@ import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@ -52,9 +57,6 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.LocalDateTimeTypeAdapter;
|
||||
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.LocalDateTypeAdapter;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PageResult;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingParams;
|
||||
@ -185,8 +187,31 @@ public class PagingAndSortingQueryParamsTests {
|
||||
@Test
|
||||
void testGson() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter().nullSafe())
|
||||
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter().nullSafe())
|
||||
.registerTypeAdapter(LocalDate.class, new TypeAdapter<LocalDate>() {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_DATE.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
return LocalDate.parse(in.nextString(), DateTimeFormatter.ISO_DATE);
|
||||
}
|
||||
|
||||
})
|
||||
.registerTypeAdapter(LocalDateTime.class, new TypeAdapter<LocalDateTime>() {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDateTime value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime read(JsonReader in) throws IOException {
|
||||
return LocalDateTime.parse(in.nextString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
}
|
||||
})
|
||||
.create();
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
AccountQueryParams params = gson.fromJson(JSON_STR, AccountQueryParams.class);
|
||||
|
Loading…
x
Reference in New Issue
Block a user