Compare commits
9 Commits
1.1.0-RC1
...
refactor/p
Author | SHA1 | Date | |
---|---|---|---|
ba3266aaea | |||
56079c29d8 | |||
f111b02c21 | |||
56fd5f0a6a | |||
e2e5f50162 | |||
8eac9054cd | |||
a55c712349 | |||
0eda94a658 | |||
c816696c55 |
@@ -78,6 +78,7 @@ System.out.println(result); // Output: Return string
|
||||
public final class LoginException
|
||||
extends RuntimeException
|
||||
implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
private static final long serialVersionUID = 881293090625085616L;
|
||||
private final Type type;
|
||||
private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
super(message);
|
||||
|
@@ -28,7 +28,7 @@
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-dependencies</artifactId>
|
||||
<version>1.1.0-RC1</version>
|
||||
<version>${project.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
@@ -35,6 +35,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
* public final class LoginException
|
||||
* extends RuntimeException
|
||||
* implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
* super(message);
|
||||
|
@@ -31,15 +31,16 @@ import xyz.zhouxy.plusone.commons.exception.MultiTypesException.ExceptionType;
|
||||
* 如果表示用户传参造成的解析失败,可使用 {@link RequestParamsException#RequestParamsException(Throwable)},
|
||||
* 将 ParsingFailureException 包装成 {@link RequestParamsException} 再抛出。
|
||||
* <pre>
|
||||
* throw new RequestParamsException(ParsingFailureException.of(ParsingFailureException.Type.NUMBER_PARSING_FAILURE));
|
||||
* throw new RequestParamsException(ParsingFailureException.Type.NUMBER_PARSING_FAILURE.create());
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class ParsingFailureException
|
||||
extends RuntimeException
|
||||
extends Exception
|
||||
implements MultiTypesException<ParsingFailureException, ParsingFailureException.Type> {
|
||||
private static final long serialVersionUID = 795996090625132616L;
|
||||
|
||||
private final Type type;
|
||||
|
||||
|
@@ -29,6 +29,7 @@ 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 = "业务异常";
|
||||
|
||||
|
@@ -36,6 +36,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;
|
||||
|
||||
private final Type type;
|
||||
|
||||
|
@@ -26,6 +26,7 @@ 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 = "用户请求参数错误";
|
||||
|
||||
|
@@ -31,6 +31,7 @@
|
||||
* public final class LoginException
|
||||
* extends RuntimeException
|
||||
* implements MultiTypesException<LoginException, LoginException.Type> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
* super(message);
|
||||
|
@@ -31,6 +31,7 @@ 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,6 +26,7 @@ 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,19 +15,21 @@
|
||||
*/
|
||||
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}
|
||||
*
|
||||
@@ -42,16 +44,15 @@ public class JSR310TypeAdapters {
|
||||
* {@code LocalDate} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code LocalDate} 进行相互转换。
|
||||
*/
|
||||
public static final class LocalDateTypeAdapter extends TypeAdapter<LocalDate> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
public static final class LocalDateTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<LocalDate, LocalDateTypeAdapter> {
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE} 进行 {@link LocalDate} 的序列化与反序列化。
|
||||
*/
|
||||
public LocalDateTypeAdapter() {
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;
|
||||
this(DateTimeFormatter.ISO_LOCAL_DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,20 +62,7 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link LocalDate} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public LocalDateTypeAdapter(DateTimeFormatter formatter) {
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
return LocalDate.parse(in.nextString(), dateTimeFormatter);
|
||||
super(LocalDate::from, formatter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,16 +70,15 @@ public class JSR310TypeAdapters {
|
||||
* {@code LocalDateTime} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code LocalDateTime} 进行相互转换。
|
||||
*/
|
||||
public static final class LocalDateTimeTypeAdapter extends TypeAdapter<LocalDateTime> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
public static final class LocalDateTimeTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<LocalDateTime, LocalDateTimeTypeAdapter> {
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME} 进行 {@link LocalDateTime} 的序列化与反序列化。
|
||||
*/
|
||||
public LocalDateTimeTypeAdapter() {
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
||||
this(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,20 +88,7 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link LocalDateTime} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public LocalDateTimeTypeAdapter(DateTimeFormatter formatter) {
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDateTime value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public LocalDateTime read(JsonReader in) throws IOException {
|
||||
return LocalDateTime.parse(in.nextString(), dateTimeFormatter);
|
||||
super(LocalDateTime::from, formatter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,16 +96,15 @@ public class JSR310TypeAdapters {
|
||||
* {@code ZonedDateTime} 的 {@code TypeAdapter},
|
||||
* 用于 Gson 对 {@code ZonedDateTime} 进行相互转换。
|
||||
*/
|
||||
public static final class ZonedDateTimeTypeAdapter extends TypeAdapter<ZonedDateTime> {
|
||||
|
||||
private final DateTimeFormatter dateTimeFormatter;
|
||||
public static final class ZonedDateTimeTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<ZonedDateTime, ZonedDateTimeTypeAdapter> {
|
||||
|
||||
/**
|
||||
* 默认构造函数,
|
||||
* 使用 {@link DateTimeFormatter#ISO_ZONED_DATE_TIME} 进行 {@link ZonedDateTime} 的序列化与反序列化。
|
||||
*/
|
||||
public ZonedDateTimeTypeAdapter() {
|
||||
this.dateTimeFormatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;
|
||||
this(DateTimeFormatter.ISO_ZONED_DATE_TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,20 +114,7 @@ public class JSR310TypeAdapters {
|
||||
* @param formatter 用于序列化 {@link ZonedDateTime} 的格式化器,不可为 {@code null}。
|
||||
*/
|
||||
public ZonedDateTimeTypeAdapter(DateTimeFormatter formatter) {
|
||||
AssertTools.checkArgumentNotNull(formatter, "formatter can not be null.");
|
||||
this.dateTimeFormatter = formatter;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, ZonedDateTime value) throws IOException {
|
||||
out.value(dateTimeFormatter.format(value));
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public ZonedDateTime read(JsonReader in) throws IOException {
|
||||
return ZonedDateTime.parse(in.nextString(), dateTimeFormatter);
|
||||
super(ZonedDateTime::from, formatter);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,18 +126,40 @@ public class JSR310TypeAdapters {
|
||||
* 使用 {@link DateTimeFormatter#ISO_INSTANT} 进行 {@link Instant} 的序列化与反序列化。
|
||||
*
|
||||
*/
|
||||
public static final class InstantTypeAdapter extends TypeAdapter<Instant> {
|
||||
public static final class InstantTypeAdapter
|
||||
extends TemporalAccessorTypeAdapter<Instant, InstantTypeAdapter> {
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void write(JsonWriter out, Instant value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_INSTANT.format(value));
|
||||
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 Instant read(JsonReader in) throws IOException {
|
||||
return Instant.parse(in.nextString());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@@ -31,8 +33,7 @@ import com.google.errorprone.annotations.Immutable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.regex.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
/**
|
||||
@@ -43,7 +44,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
* @since 1.0.0
|
||||
* @see xyz.zhouxy.plusone.commons.constant.PatternConsts#CHINESE_2ND_ID_CARD_NUMBER
|
||||
* @see xyz.zhouxy.plusone.commons.regex.PatternConsts#CHINESE_2ND_ID_CARD_NUMBER
|
||||
*/
|
||||
@ValueObject
|
||||
@Immutable
|
||||
@@ -86,13 +87,13 @@ public class Chinese2ndGenIDCardNumber
|
||||
*/
|
||||
public static Chinese2ndGenIDCardNumber of(final String idCardNumber) {
|
||||
try {
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(idCardNumber), "二代居民身份证校验失败:号码为空");
|
||||
checkArgument(StringTools.isNotBlank(idCardNumber), "二代居民身份证校验失败:号码为空");
|
||||
final String value = idCardNumber.toUpperCase();
|
||||
final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(value);
|
||||
AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
|
||||
checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
|
||||
|
||||
final String provinceCode = matcher.group("province");
|
||||
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
|
||||
checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
|
||||
|
||||
final String cityCode = matcher.group("city");
|
||||
final String countyCode = matcher.group("county");
|
||||
|
@@ -16,8 +16,9 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.base.IWithIntCode;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 性别
|
||||
@@ -50,7 +51,7 @@ public enum Gender implements IWithIntCode {
|
||||
* @return 枚举值
|
||||
*/
|
||||
public static Gender of(int value) {
|
||||
AssertTools.checkCondition(0 <= value && value < VALUES.length,
|
||||
checkCondition(0 <= value && value < VALUES.length,
|
||||
() -> new EnumConstantNotPresentException(Gender.class, String.valueOf(value)));
|
||||
return VALUES[value];
|
||||
}
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -25,7 +27,6 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 带校验的字符串值对象
|
||||
@@ -74,10 +75,10 @@ public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<
|
||||
* @param errorMessage 正则不匹配时的错误信息
|
||||
*/
|
||||
protected ValidatableStringRecord(String value, Pattern pattern, String errorMessage) {
|
||||
AssertTools.checkArgument(Objects.nonNull(value), "The value cannot be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(pattern), "The pattern cannot be null.");
|
||||
checkArgument(Objects.nonNull(value), "The value cannot be null.");
|
||||
checkArgument(Objects.nonNull(pattern), "The pattern cannot be null.");
|
||||
this.matcher = pattern.matcher(value);
|
||||
AssertTools.checkArgument(this.matcher.matches(), errorMessage);
|
||||
checkArgument(this.matcher.matches(), errorMessage);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.model.dto;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -27,7 +29,6 @@ import com.google.common.collect.ImmutableMap;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
@@ -60,10 +61,10 @@ public class PagingAndSortingQueryParams {
|
||||
* @param sortableProperties 可排序的属性。不可为空。
|
||||
*/
|
||||
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) {
|
||||
AssertTools.checkArgument(CollectionTools.isNotEmpty(sortableProperties),
|
||||
checkArgument(CollectionTools.isNotEmpty(sortableProperties),
|
||||
"Sortable properties can not be empty.");
|
||||
sortableProperties.forEach((k, v) ->
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
|
||||
checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
|
||||
"Property name must not be blank."));
|
||||
this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
|
||||
}
|
||||
@@ -107,7 +108,7 @@ public class PagingAndSortingQueryParams {
|
||||
public final PagingParams buildPagingParams() {
|
||||
final int sizeValue = this.size != null ? this.size : defaultSizeInternal();
|
||||
final long pageNumValue = this.pageNum != null ? this.pageNum : 1L;
|
||||
AssertTools.checkArgument(CollectionTools.isNotEmpty(this.orderBy),
|
||||
checkArgument(CollectionTools.isNotEmpty(this.orderBy),
|
||||
"The 'orderBy' cannot be empty");
|
||||
final List<SortableProperty> propertiesToSort = this.orderBy.stream()
|
||||
.map(this::generateSortableProperty)
|
||||
@@ -138,13 +139,13 @@ public class PagingAndSortingQueryParams {
|
||||
}
|
||||
|
||||
private SortableProperty generateSortableProperty(String orderByStr) {
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(orderByStr));
|
||||
AssertTools.checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
|
||||
checkArgument(StringTools.isNotBlank(orderByStr));
|
||||
checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
|
||||
String[] propertyNameAndOrderType = orderByStr.split("-");
|
||||
AssertTools.checkArgument(propertyNameAndOrderType.length == 2);
|
||||
checkArgument(propertyNameAndOrderType.length == 2);
|
||||
|
||||
String propertyName = propertyNameAndOrderType[0];
|
||||
AssertTools.checkArgument(sortableProperties.containsKey(propertyName),
|
||||
checkArgument(sortableProperties.containsKey(propertyName),
|
||||
"The property name must be in the set of sortable properties.");
|
||||
String columnName = sortableProperties.get(propertyName);
|
||||
String orderType = propertyNameAndOrderType[1];
|
||||
@@ -164,7 +165,7 @@ public class PagingAndSortingQueryParams {
|
||||
SortableProperty(String propertyName, String columnName, String orderType) {
|
||||
this.propertyName = propertyName;
|
||||
this.columnName = columnName;
|
||||
AssertTools.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
|
||||
checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
|
||||
this.orderType = orderType.toUpperCase();
|
||||
|
||||
this.sqlSnippet = this.propertyName + " " + this.orderType;
|
||||
|
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public abstract class AbstractMatcher {
|
||||
private final Matcher matcher;
|
||||
|
||||
AbstractMatcher(Matcher matcher) {
|
||||
this.matcher = matcher;
|
||||
}
|
||||
|
||||
public final Matcher matcher() {
|
||||
return this.matcher;
|
||||
}
|
||||
|
||||
public final boolean matches() {
|
||||
return this.matcher.matches();
|
||||
}
|
||||
|
||||
public final String getGroupValue(String groupName) {
|
||||
return this.matcher.group(groupName);
|
||||
}
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.model.Gender;
|
||||
|
||||
public final class Chinese2ndIdCardNumberMatcher extends AbstractMatcher {
|
||||
|
||||
Chinese2ndIdCardNumberMatcher(Matcher matcher) {
|
||||
super(matcher);
|
||||
}
|
||||
|
||||
public final String getProvince() {
|
||||
return getGroupValue("province");
|
||||
}
|
||||
|
||||
public final String getCity() {
|
||||
return getGroupValue("city");
|
||||
}
|
||||
|
||||
public final String getCounty() {
|
||||
return getGroupValue("county");
|
||||
}
|
||||
|
||||
public final String getBirthDate() {
|
||||
return getGroupValue("birthDate");
|
||||
}
|
||||
|
||||
public final String getOrderCode() {
|
||||
return getGroupValue("orderCode");
|
||||
}
|
||||
|
||||
public final String getGenderCode() {
|
||||
return getGroupValue("gender");
|
||||
}
|
||||
|
||||
public final Gender getGender() {
|
||||
final int genderCode = Integer.parseInt(getGenderCode());
|
||||
return genderCode % 2 == 0 ? Gender.FEMALE : Gender.MALE;
|
||||
}
|
||||
|
||||
public final String getCheckDigit() {
|
||||
return getGroupValue("checkDigit");
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public final class LocalDateMatcher extends AbstractMatcher {
|
||||
|
||||
LocalDateMatcher(Matcher matcher) {
|
||||
super(matcher);
|
||||
}
|
||||
|
||||
public String getYear() {
|
||||
return getGroupValue("yyyy");
|
||||
}
|
||||
|
||||
public String getMonth() {
|
||||
return getGroupValue("MM");
|
||||
}
|
||||
|
||||
public String getDay() {
|
||||
return getGroupValue("dd");
|
||||
}
|
||||
}
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.constant;
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class PatternInfo<T> {
|
||||
private final String regex;
|
||||
private final Pattern pattern;
|
||||
|
||||
PatternInfo(String regex, Pattern pattern) {
|
||||
this.regex = regex;
|
||||
this.pattern = pattern;
|
||||
}
|
||||
|
||||
public final String regex() {
|
||||
return this.regex;
|
||||
}
|
||||
|
||||
public final Pattern pattern() {
|
||||
return this.pattern;
|
||||
}
|
||||
|
||||
public abstract T matcher(String input);
|
||||
}
|
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public final class PatternInfos {
|
||||
|
||||
// @see RegexConsts#BASIC_ISO_DATE
|
||||
public static final PatternInfo<LocalDateMatcher> BASIC_ISO_DATE = new PatternInfo<LocalDateMatcher>(
|
||||
RegexConsts.BASIC_ISO_DATE,
|
||||
PatternConsts.BASIC_ISO_DATE) {
|
||||
@Override
|
||||
public LocalDateMatcher matcher(String input) {
|
||||
Matcher matcher = pattern().matcher(input);
|
||||
return new LocalDateMatcher(matcher);
|
||||
}
|
||||
};
|
||||
|
||||
// @see RegexConsts#ISO_LOCAL_DATE
|
||||
public static final PatternInfo<LocalDateMatcher> ISO_LOCAL_DATE = new PatternInfo<LocalDateMatcher>(
|
||||
RegexConsts.ISO_LOCAL_DATE,
|
||||
PatternConsts.ISO_LOCAL_DATE) {
|
||||
@Override
|
||||
public LocalDateMatcher matcher(String input) {
|
||||
Matcher matcher = pattern().matcher(input);
|
||||
return new LocalDateMatcher(matcher);
|
||||
}
|
||||
};
|
||||
|
||||
// @see RegexConsts#PASSWORD
|
||||
public static final PatternInfo<Matcher> PASSWORD = new PatternInfo<Matcher>(
|
||||
RegexConsts.PASSWORD,
|
||||
PatternConsts.PASSWORD) {
|
||||
@Override
|
||||
public Matcher matcher(String input) {
|
||||
return pattern().matcher(input);
|
||||
}
|
||||
};
|
||||
|
||||
// @see RegexConsts#CAPTCHA
|
||||
public static final PatternInfo<Matcher> CAPTCHA = new PatternInfo<Matcher>(
|
||||
RegexConsts.CAPTCHA,
|
||||
PatternConsts.CAPTCHA) {
|
||||
@Override
|
||||
public Matcher matcher(String input) {
|
||||
return pattern().matcher(input);
|
||||
}
|
||||
};
|
||||
|
||||
// @see RegexConsts#EMAIL
|
||||
public static final PatternInfo<Matcher> EMAIL = new PatternInfo<Matcher>(
|
||||
RegexConsts.EMAIL,
|
||||
PatternConsts.EMAIL) {
|
||||
@Override
|
||||
public Matcher matcher(String input) {
|
||||
return pattern().matcher(input);
|
||||
}
|
||||
};
|
||||
|
||||
public static final PatternInfo<Matcher> MOBILE_PHONE = new PatternInfo<Matcher>(
|
||||
RegexConsts.MOBILE_PHONE,
|
||||
PatternConsts.MOBILE_PHONE) {
|
||||
@Override
|
||||
public Matcher matcher(String input) {
|
||||
return pattern().matcher(input);
|
||||
}
|
||||
};
|
||||
|
||||
public static final PatternInfo<Matcher> USERNAME = new PatternInfo<Matcher>(
|
||||
RegexConsts.USERNAME,
|
||||
PatternConsts.USERNAME) {
|
||||
@Override
|
||||
public Matcher matcher(String input) {
|
||||
return pattern().matcher(input);
|
||||
}
|
||||
};
|
||||
|
||||
public static final PatternInfo<Matcher> NICKNAME = new PatternInfo<Matcher>(
|
||||
RegexConsts.NICKNAME,
|
||||
PatternConsts.NICKNAME) {
|
||||
@Override
|
||||
public Matcher matcher(String input) {
|
||||
return pattern().matcher(input);
|
||||
}
|
||||
};
|
||||
|
||||
public static final PatternInfo<Chinese2ndIdCardNumberMatcher> CHINESE_2ND_ID_CARD_NUMBER = new PatternInfo<Chinese2ndIdCardNumberMatcher>(
|
||||
RegexConsts.CHINESE_2ND_ID_CARD_NUMBER,
|
||||
PatternConsts.CHINESE_2ND_ID_CARD_NUMBER) {
|
||||
@Override
|
||||
public Chinese2ndIdCardNumberMatcher matcher(String input) {
|
||||
Matcher matcher = pattern().matcher(input);
|
||||
return new Chinese2ndIdCardNumberMatcher(matcher);
|
||||
}
|
||||
};
|
||||
|
||||
private PatternInfos() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.constant;
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
/**
|
||||
* 正则表达式常量
|
||||
@@ -41,12 +41,15 @@ public final class RegexConsts {
|
||||
|
||||
public static final String MOBILE_PHONE = "^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$";
|
||||
|
||||
public static final String USERNAME = "^[\\w-_.@]{4,36}$";
|
||||
public static final String USERNAME = "^[\\w-_]{4,36}$";
|
||||
|
||||
public static final String NICKNAME = "^[\\w-_.@]{4,36}$";
|
||||
|
||||
public static final String CHINESE_2ND_ID_CARD_NUMBER
|
||||
= "^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})(?<birthDate>\\d{8})\\d{2}(?<gender>\\d)([\\dX])$";
|
||||
= "^(?<county>(?<city>(?<province>\\d{2})\\d{2})\\d{2})"
|
||||
+ "(?<birthDate>\\d{8})"
|
||||
+ "(?<orderCode>\\d{2}(?<gender>\\d))"
|
||||
+ "(?<checkDigit>[\\dX])$";
|
||||
|
||||
private RegexConsts() {
|
||||
throw new IllegalStateException("Utility class");
|
@@ -14,16 +14,4 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* <h2>常量</h2>
|
||||
*
|
||||
* <h3>
|
||||
* 1. 正则常量
|
||||
* </h3>
|
||||
* {@link RegexConsts} 包含常见正则表达式;{@link PatternConsts} 包含对应的 {@link Pattern} 对象。
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.constant;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
@@ -16,6 +16,9 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.time;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
|
||||
|
||||
import java.time.DateTimeException;
|
||||
import java.time.Month;
|
||||
import java.time.MonthDay;
|
||||
@@ -25,7 +28,6 @@ import com.google.common.collect.Range;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.base.IWithIntCode;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 季度
|
||||
@@ -89,7 +91,7 @@ public enum Quarter implements IWithIntCode {
|
||||
*/
|
||||
@StaticFactoryMethod(Quarter.class)
|
||||
public static Quarter fromMonth(Month month) {
|
||||
AssertTools.checkNotNull(month);
|
||||
checkNotNull(month);
|
||||
final int monthValue = month.getValue();
|
||||
return of(computeQuarterValueInternal(monthValue));
|
||||
}
|
||||
@@ -246,7 +248,7 @@ public enum Quarter implements IWithIntCode {
|
||||
* @throws DateTimeException 如果给定的季度值不在有效范围内(1到4),将抛出异常
|
||||
*/
|
||||
public static int checkValidIntValue(int value) {
|
||||
AssertTools.checkCondition(value >= 1 && value <= 4,
|
||||
checkCondition(value >= 1 && value <= 4,
|
||||
() -> new DateTimeException("Invalid value for Quarter: " + value));
|
||||
return value;
|
||||
}
|
||||
|
@@ -17,6 +17,7 @@
|
||||
package xyz.zhouxy.plusone.commons.time;
|
||||
|
||||
import static java.time.temporal.ChronoField.YEAR;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
@@ -32,7 +33,6 @@ import javax.annotation.Nullable;
|
||||
import com.google.errorprone.annotations.Immutable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 表示年份与季度
|
||||
@@ -93,7 +93,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(LocalDate date) {
|
||||
AssertTools.checkNotNull(date);
|
||||
checkNotNull(date);
|
||||
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(Date date) {
|
||||
AssertTools.checkNotNull(date);
|
||||
checkNotNull(date);
|
||||
@SuppressWarnings("deprecation")
|
||||
final int yearValue = YEAR.checkValidIntValue(date.getYear() + 1900L);
|
||||
@SuppressWarnings("deprecation")
|
||||
@@ -121,7 +121,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(Calendar date) {
|
||||
AssertTools.checkNotNull(date);
|
||||
checkNotNull(date);
|
||||
final int yearValue = ChronoField.YEAR.checkValidIntValue(date.get(Calendar.YEAR));
|
||||
final int monthValue = date.get(Calendar.MONTH) + 1;
|
||||
return new YearQuarter(yearValue, Quarter.fromMonth(monthValue));
|
||||
@@ -135,7 +135,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
|
||||
*/
|
||||
@StaticFactoryMethod(YearQuarter.class)
|
||||
public static YearQuarter of(YearMonth yearMonth) {
|
||||
AssertTools.checkNotNull(yearMonth);
|
||||
checkNotNull(yearMonth);
|
||||
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,9 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -258,7 +261,7 @@ public class ArrayTools {
|
||||
* @throws IllegalArgumentException 当参数为空时抛出
|
||||
*/
|
||||
public static <T> boolean isAllElementsNotNull(final T[] arr) {
|
||||
AssertTools.checkArgument(arr != null, "The array cannot be null.");
|
||||
checkArgument(arr != null, "The array cannot be null.");
|
||||
return Arrays.stream(arr).allMatch(Objects::nonNull);
|
||||
}
|
||||
|
||||
@@ -488,10 +491,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static char[] repeat(char[] arr, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_CHAR_ARRAY;
|
||||
@@ -523,10 +526,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static byte[] repeat(byte[] arr, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_BYTE_ARRAY;
|
||||
@@ -558,10 +561,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static short[] repeat(short[] arr, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_SHORT_ARRAY;
|
||||
@@ -593,10 +596,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static int[] repeat(int[] arr, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_INT_ARRAY;
|
||||
@@ -628,10 +631,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static long[] repeat(long[] arr, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_LONG_ARRAY;
|
||||
@@ -663,10 +666,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static float[] repeat(float[] arr, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_FLOAT_ARRAY;
|
||||
@@ -698,10 +701,10 @@ public class ArrayTools {
|
||||
* @return 重复后的数组
|
||||
*/
|
||||
public static double[] repeat(double[] arr, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(arr));
|
||||
AssertTools.checkArgument(times >= 0,
|
||||
checkArgument(Objects.nonNull(arr));
|
||||
checkArgument(times >= 0,
|
||||
"The number of times must be greater than or equal to zero");
|
||||
AssertTools.checkArgument(maxLength >= 0,
|
||||
checkArgument(maxLength >= 0,
|
||||
"The max length must be greater than or equal to zero");
|
||||
if (times == 0) {
|
||||
return EMPTY_DOUBLE_ARRAY;
|
||||
@@ -747,7 +750,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -790,7 +793,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -833,7 +836,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -876,7 +879,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -919,7 +922,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -962,7 +965,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -1005,7 +1008,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -1060,7 +1063,7 @@ public class ArrayTools {
|
||||
* @param values 填充内容
|
||||
*/
|
||||
private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
|
||||
AssertTools.checkArgument(Objects.nonNull(a));
|
||||
checkArgument(Objects.nonNull(a));
|
||||
if (values == null || values.length == 0) {
|
||||
return;
|
||||
}
|
||||
@@ -1087,7 +1090,7 @@ public class ArrayTools {
|
||||
// #region - indexOf
|
||||
|
||||
public static <T> int indexOf(@Nullable T[] arr, Predicate<? super T> predicate) {
|
||||
AssertTools.checkNotNull(predicate);
|
||||
checkNotNull(predicate);
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
@@ -1192,7 +1195,7 @@ public class ArrayTools {
|
||||
// #region - lastIndexOf
|
||||
|
||||
public static <T> int lastIndexOf(@Nullable T[] arr, Predicate<? super T> predicate) {
|
||||
AssertTools.checkNotNull(predicate);
|
||||
checkNotNull(predicate);
|
||||
if (arr == null || arr.length == 0) {
|
||||
return NOT_FOUND_INDEX;
|
||||
}
|
||||
|
@@ -31,11 +31,11 @@ import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
|
||||
* 本工具类不封装过多判断逻辑,鼓励充分使用项目中的工具类进行逻辑判断。
|
||||
*
|
||||
* <pre>
|
||||
* AssertTools.checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
|
||||
* AssertTools.checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
|
||||
* AssertTools.checkCondition(!CollectionUtils.isEmpty(roles),
|
||||
* checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
|
||||
* checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
|
||||
* checkCondition(!CollectionUtils.isEmpty(roles),
|
||||
* () -> new InvalidInputException("The roles cannot be empty."));
|
||||
* AssertTools.checkCondition(RegexTools.matches(email, PatternConsts.EMAIL),
|
||||
* checkCondition(RegexTools.matches(email, PatternConsts.EMAIL),
|
||||
* "must be a well-formed email address");
|
||||
* </pre>
|
||||
*
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
@@ -53,8 +55,8 @@ public class BigDecimals {
|
||||
* @return 当 {@code a} 大于 {@code b} 时返回 {@code true}
|
||||
*/
|
||||
public static boolean gt(BigDecimal a, BigDecimal b) {
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
checkNotNull(a, "Parameter could not be null.");
|
||||
checkNotNull(b, "Parameter could not be null.");
|
||||
return (a != b) && (a.compareTo(b) > 0);
|
||||
}
|
||||
|
||||
@@ -66,8 +68,8 @@ public class BigDecimals {
|
||||
* @return 当 {@code a} 大于等于 {@code b} 时返回 {@code true}
|
||||
*/
|
||||
public static boolean ge(BigDecimal a, BigDecimal b) {
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
checkNotNull(a, "Parameter could not be null.");
|
||||
checkNotNull(b, "Parameter could not be null.");
|
||||
return (a == b) || (a.compareTo(b) >= 0);
|
||||
}
|
||||
|
||||
@@ -79,8 +81,8 @@ public class BigDecimals {
|
||||
* @return 当 {@code a} 小于 {@code b} 时返回 {@code true}
|
||||
*/
|
||||
public static boolean lt(BigDecimal a, BigDecimal b) {
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
checkNotNull(a, "Parameter could not be null.");
|
||||
checkNotNull(b, "Parameter could not be null.");
|
||||
return (a != b) && (a.compareTo(b) < 0);
|
||||
}
|
||||
|
||||
@@ -92,8 +94,8 @@ public class BigDecimals {
|
||||
* @return 当 {@code a} 小于等于 {@code b} 时返回 {@code true}
|
||||
*/
|
||||
public static boolean le(BigDecimal a, BigDecimal b) {
|
||||
AssertTools.checkNotNull(a, "Parameter could not be null.");
|
||||
AssertTools.checkNotNull(b, "Parameter could not be null.");
|
||||
checkNotNull(a, "Parameter could not be null.");
|
||||
checkNotNull(b, "Parameter could not be null.");
|
||||
return (a == b) || (a.compareTo(b) <= 0);
|
||||
}
|
||||
|
||||
|
@@ -374,258 +374,6 @@ public class DateTimeTools {
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaInstant
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link java.time.Instant} 转换为 {@link org.joda.time.Instant}
|
||||
*
|
||||
* @param instant {@link java.time.Instant} 对象
|
||||
* @return {@link org.joda.time.Instant} 对象
|
||||
*/
|
||||
public static org.joda.time.Instant toJodaInstant(java.time.Instant instant) {
|
||||
return new org.joda.time.Instant(instant.toEpochMilli());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 {@link java.time.ZonedDateTime} 转换为 {@link org.joda.time.Instant}
|
||||
*
|
||||
* @param zonedDateTime {@link java.time.ZonedDateTime} 对象
|
||||
* @return {@link org.joda.time.Instant} 对象
|
||||
*/
|
||||
public static org.joda.time.Instant toJodaInstant(java.time.ZonedDateTime zonedDateTime) {
|
||||
return toJodaInstant(zonedDateTime.toInstant());
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算指定时区的地区时间,对应的时间戳。结果为 {@link org.joda.time.Instant} 对象
|
||||
*
|
||||
* @param localDateTime {@link java.time.LocalDateTime} 对象
|
||||
* @param zone 时区
|
||||
* @return {@link org.joda.time.Instant} 对象
|
||||
*/
|
||||
public static org.joda.time.Instant toJodaInstant(java.time.LocalDateTime localDateTime, java.time.ZoneId zone) {
|
||||
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaInstant
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link org.joda.time.Instant} 对象转换为 {@link java.time.Instant} 对象
|
||||
*
|
||||
* @param instant {@link org.joda.time.Instant} 对象
|
||||
* @return {@link java.time.Instant} 对象
|
||||
*/
|
||||
public static java.time.Instant toJavaInstant(org.joda.time.Instant instant) {
|
||||
return toInstant(instant.getMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 joda-time 中的 {@link org.joda.time.DateTime} 对象转换为 Java 的
|
||||
* {@link java.time.Instant} 对象
|
||||
*
|
||||
* @param dateTime joda-time 中表示日期时间的 {@link org.joda.time.DateTime} 对象
|
||||
* @return Java 表示时间戳的 {@link java.time.Instant} 对象
|
||||
*/
|
||||
public static java.time.Instant toJavaInstant(org.joda.time.DateTime dateTime) {
|
||||
return toInstant(dateTime.getMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 joda-time 中的 {@link org.joda.time.LocalDateTime} 对象和
|
||||
* {@link org.joda.time.DateTimeZone} 对象
|
||||
* 转换为 Java 中的 {@link java.time.Instant} 对象
|
||||
*
|
||||
* @param localDateTime
|
||||
* @param zone
|
||||
* @return
|
||||
*/
|
||||
public static java.time.Instant toJavaInstant(
|
||||
org.joda.time.LocalDateTime localDateTime,
|
||||
org.joda.time.DateTimeZone zone) {
|
||||
return toJavaInstant(localDateTime.toDateTime(zone));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 Java 中表示日期时间的 {@link java.time.ZonedDateTime} 对象
|
||||
* 转换为 joda-time 的 {@link org.joda.time.DateTime} 对象
|
||||
*
|
||||
* @param zonedDateTime 日期时间
|
||||
* @return joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||
*/
|
||||
public static org.joda.time.DateTime toJodaDateTime(java.time.ZonedDateTime zonedDateTime) {
|
||||
org.joda.time.DateTimeZone zone = org.joda.time.DateTimeZone.forID(zonedDateTime.getZone().getId());
|
||||
return toJodaInstant(zonedDateTime.toInstant()).toDateTime(zone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 java.time 中表示日期时间的 {@link java.time.LocalDateTime} 对象和表示时区的
|
||||
* {@link java.time.ZoneId} 对象转换为 joda-time 中对应的 {@link org.joda.time.DateTime}
|
||||
* 对象
|
||||
* 转换为 joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||
*
|
||||
* @param localDateTime 日期时间
|
||||
* @param zone 时区
|
||||
* @return joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||
*/
|
||||
public static org.joda.time.DateTime toJodaDateTime(
|
||||
java.time.LocalDateTime localDateTime,
|
||||
java.time.ZoneId zone) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
return toJodaInstant(ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算时间戳在指定时区对应的时间,结果使用 {@link org.joda.time.DateTime} 表示
|
||||
*
|
||||
* @param instant java.time 中的时间戳
|
||||
* @param zone java.time 中的时区
|
||||
* @return joda-time 中带时区的日期时间
|
||||
*/
|
||||
public static org.joda.time.DateTime toJodaDateTime(
|
||||
java.time.Instant instant,
|
||||
java.time.ZoneId zone) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
return toJodaInstant(instant).toDateTime(dateTimeZone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 joda-time 中带时区的日期时间,转换为 java.time 中带时区的日期时间
|
||||
*
|
||||
* @param dateTime joda-time 中带时区的日期时间
|
||||
* @return java.time 中带时区的日期时间
|
||||
*/
|
||||
public static java.time.ZonedDateTime toZonedDateTime(org.joda.time.DateTime dateTime) {
|
||||
java.time.ZoneId zone = dateTime.getZone().toTimeZone().toZoneId();
|
||||
return toJavaInstant(dateTime.toInstant()).atZone(zone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 joda-time 中的 {@link org.joda.time.LocalDateTime} 和
|
||||
* {@link org.joda.time.DateTimeZone}
|
||||
* 转换为 java.time 中的 {@link java.time.ZonedDateTime}
|
||||
*
|
||||
* @param localDateTime joda-time 中的地区时间
|
||||
* @param dateTimeZone joda-time 中的时区
|
||||
* @return java.time 中带时区的日期时间
|
||||
*/
|
||||
public static java.time.ZonedDateTime toZonedDateTime(
|
||||
org.joda.time.LocalDateTime localDateTime,
|
||||
org.joda.time.DateTimeZone dateTimeZone) {
|
||||
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||
return toJavaInstant(localDateTime, dateTimeZone).atZone(zone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 joda-time 中的 {@link org.joda.time.Instant} 在指定时区的时间,用 Java 8+ 的
|
||||
* {@link java.time.ZonedDateTime} 表示
|
||||
*
|
||||
* @param instant joda-time 中的时间戳
|
||||
* @param dateTimeZone joda-time 中的时区
|
||||
* @return
|
||||
*/
|
||||
public static java.time.ZonedDateTime toZonedDateTime(
|
||||
org.joda.time.Instant instant,
|
||||
org.joda.time.DateTimeZone dateTimeZone) {
|
||||
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||
return toJavaInstant(instant).atZone(zone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link java.time.LocalDateTime} 转换为 {@link org.joda.time.LocalDateTime}
|
||||
*
|
||||
* @param localDateTime Java 8 LocalDateTime
|
||||
* @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();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaLocalDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link org.joda.time.LocalDateTime} 转换为 {@link java.time.LocalDateTime}
|
||||
*
|
||||
* @param localDateTime joda-time LocalDateTime
|
||||
* @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();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 转换 Java API 和 joda-time API 表示时区的对象
|
||||
*
|
||||
* @param jodaZone joda-time API 中表示时区的对象
|
||||
* @return Java API 中表示时区的对象
|
||||
*/
|
||||
public static java.time.ZoneId toJavaZone(org.joda.time.DateTimeZone jodaZone) {
|
||||
return jodaZone.toTimeZone().toZoneId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换 Java API 和 joda-time API 表示时区的对象
|
||||
*
|
||||
* @param zone Java API 中表示时区的对象
|
||||
* @return joda-time API 中表示时区的对象
|
||||
*/
|
||||
public static org.joda.time.DateTimeZone toJodaZone(java.time.ZoneId zone) {
|
||||
return org.joda.time.DateTimeZone.forID(zone.getId());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - YearQuarter & Quarter
|
||||
// ================================
|
||||
|
@@ -16,6 +16,9 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -43,7 +46,7 @@ public final class EnumTools {
|
||||
@Deprecated
|
||||
private static <E extends Enum<?>> E valueOfInternal(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||
E[] values = enumType.getEnumConstants();
|
||||
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length),
|
||||
checkCondition((ordinal >= 0 && ordinal < values.length),
|
||||
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
|
||||
return values[ordinal];
|
||||
}
|
||||
@@ -59,7 +62,7 @@ public final class EnumTools {
|
||||
*/
|
||||
@Deprecated
|
||||
public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||
checkNotNull(enumType, "Enum type must not be null.");
|
||||
return valueOfInternal(enumType, ordinal);
|
||||
}
|
||||
|
||||
@@ -76,7 +79,7 @@ public final class EnumTools {
|
||||
@Deprecated
|
||||
public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用,但不删掉
|
||||
@Nullable Integer ordinal, @Nullable E defaultValue) {
|
||||
AssertTools.checkNotNull(enumType);
|
||||
checkNotNull(enumType);
|
||||
return null == ordinal ? defaultValue : valueOfInternal(enumType, ordinal);
|
||||
}
|
||||
|
||||
@@ -95,8 +98,8 @@ public final class EnumTools {
|
||||
Class<E> enumType,
|
||||
@Nullable Integer ordinal,
|
||||
Supplier<E> defaultValue) {
|
||||
AssertTools.checkNotNull(enumType);
|
||||
AssertTools.checkNotNull(defaultValue);
|
||||
checkNotNull(enumType);
|
||||
checkNotNull(defaultValue);
|
||||
return null == ordinal ? defaultValue.get() : valueOfInternal(enumType, ordinal);
|
||||
}
|
||||
|
||||
@@ -112,7 +115,7 @@ public final class EnumTools {
|
||||
@Deprecated
|
||||
public static <E extends Enum<?>> E getValueOrDefault(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用,但不删掉
|
||||
return getValueOrDefault(enumType, ordinal, () -> {
|
||||
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||
checkNotNull(enumType, "Enum type must not be null.");
|
||||
E[] values = enumType.getEnumConstants();
|
||||
return values[0];
|
||||
});
|
||||
@@ -133,10 +136,10 @@ public final class EnumTools {
|
||||
}
|
||||
|
||||
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> enumType, Integer ordinal) {
|
||||
AssertTools.checkNotNull(enumType, "Enum type must not be null.");
|
||||
AssertTools.checkNotNull(ordinal, "Ordinal must not be null.");
|
||||
checkNotNull(enumType, "Enum type must not be null.");
|
||||
checkNotNull(ordinal, "Ordinal must not be null.");
|
||||
E[] values = enumType.getEnumConstants();
|
||||
AssertTools.checkCondition(ordinal >= 0 && ordinal < values.length,
|
||||
checkCondition(ordinal >= 0 && ordinal < values.length,
|
||||
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
|
||||
return ordinal;
|
||||
}
|
||||
@@ -180,7 +183,7 @@ public final class EnumTools {
|
||||
Class<E> enumType,
|
||||
@Nullable Integer ordinal,
|
||||
@Nullable Integer defaultValue) {
|
||||
AssertTools.checkNotNull(enumType);
|
||||
checkNotNull(enumType);
|
||||
return checkOrdinalOrGetInternal(enumType, ordinal, () -> checkOrdinalOrDefaultInternal(enumType, defaultValue, null));
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@@ -44,7 +46,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
|
||||
protected final String name;
|
||||
|
||||
protected Enumeration(final int id, final String name) {
|
||||
AssertTools.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
|
||||
checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
@@ -126,7 +128,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
|
||||
* @return 枚举对象
|
||||
*/
|
||||
public T get(int id) {
|
||||
AssertTools.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
|
||||
checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
|
||||
return this.valueMap.get(id);
|
||||
}
|
||||
|
||||
|
@@ -16,8 +16,9 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@@ -69,7 +70,7 @@ public class IdGenerator {
|
||||
* @return UUID 字符串
|
||||
*/
|
||||
public static String toSimpleString(UUID uuid) {
|
||||
AssertTools.checkArgument(Objects.nonNull(uuid));
|
||||
checkArgumentNotNull(uuid);
|
||||
return (uuidDigits(uuid.getMostSignificantBits() >> 32, 8) +
|
||||
uuidDigits(uuid.getMostSignificantBits() >> 16, 4) +
|
||||
uuidDigits(uuid.getMostSignificantBits(), 4) +
|
||||
|
@@ -0,0 +1,284 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
/**
|
||||
* Joda-Time 工具类
|
||||
*
|
||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||
*/
|
||||
public class JodaTimeTools {
|
||||
|
||||
// ================================
|
||||
// #region - toJodaInstant
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link java.time.Instant} 转换为 {@link org.joda.time.Instant}
|
||||
*
|
||||
* @param instant {@link java.time.Instant} 对象
|
||||
* @return {@link org.joda.time.Instant} 对象
|
||||
*/
|
||||
public static org.joda.time.Instant toJodaInstant(java.time.Instant instant) {
|
||||
return new org.joda.time.Instant(instant.toEpochMilli());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 {@link java.time.ZonedDateTime} 转换为 {@link org.joda.time.Instant}
|
||||
*
|
||||
* @param zonedDateTime {@link java.time.ZonedDateTime} 对象
|
||||
* @return {@link org.joda.time.Instant} 对象
|
||||
*/
|
||||
public static org.joda.time.Instant toJodaInstant(java.time.ZonedDateTime zonedDateTime) {
|
||||
return toJodaInstant(zonedDateTime.toInstant());
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算指定时区的地区时间,对应的时间戳。结果为 {@link org.joda.time.Instant} 对象
|
||||
*
|
||||
* @param localDateTime {@link java.time.LocalDateTime} 对象
|
||||
* @param zone 时区
|
||||
* @return {@link org.joda.time.Instant} 对象
|
||||
*/
|
||||
public static org.joda.time.Instant toJodaInstant(java.time.LocalDateTime localDateTime, java.time.ZoneId zone) {
|
||||
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaInstant
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link org.joda.time.Instant} 对象转换为 {@link java.time.Instant} 对象
|
||||
*
|
||||
* @param instant {@link org.joda.time.Instant} 对象
|
||||
* @return {@link java.time.Instant} 对象
|
||||
*/
|
||||
public static java.time.Instant toJavaInstant(org.joda.time.Instant instant) {
|
||||
return DateTimeTools.toInstant(instant.getMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 joda-time 中的 {@link org.joda.time.DateTime} 对象转换为 Java 的
|
||||
* {@link java.time.Instant} 对象
|
||||
*
|
||||
* @param dateTime joda-time 中表示日期时间的 {@link org.joda.time.DateTime} 对象
|
||||
* @return Java 表示时间戳的 {@link java.time.Instant} 对象
|
||||
*/
|
||||
public static java.time.Instant toJavaInstant(org.joda.time.DateTime dateTime) {
|
||||
return DateTimeTools.toInstant(dateTime.getMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 joda-time 中的 {@link org.joda.time.LocalDateTime} 对象和
|
||||
* {@link org.joda.time.DateTimeZone} 对象
|
||||
* 转换为 Java 中的 {@link java.time.Instant} 对象
|
||||
*
|
||||
* @param localDateTime
|
||||
* @param zone
|
||||
* @return
|
||||
*/
|
||||
public static java.time.Instant toJavaInstant(
|
||||
org.joda.time.LocalDateTime localDateTime,
|
||||
org.joda.time.DateTimeZone zone) {
|
||||
return toJavaInstant(localDateTime.toDateTime(zone));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 Java 中表示日期时间的 {@link java.time.ZonedDateTime} 对象
|
||||
* 转换为 joda-time 的 {@link org.joda.time.DateTime} 对象
|
||||
*
|
||||
* @param zonedDateTime 日期时间
|
||||
* @return joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||
*/
|
||||
public static org.joda.time.DateTime toJodaDateTime(java.time.ZonedDateTime zonedDateTime) {
|
||||
org.joda.time.DateTimeZone zone = org.joda.time.DateTimeZone.forID(zonedDateTime.getZone().getId());
|
||||
return toJodaInstant(zonedDateTime.toInstant()).toDateTime(zone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 java.time 中表示日期时间的 {@link java.time.LocalDateTime} 对象和表示时区的
|
||||
* {@link java.time.ZoneId} 对象转换为 joda-time 中对应的 {@link org.joda.time.DateTime}
|
||||
* 对象
|
||||
* 转换为 joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||
*
|
||||
* @param localDateTime 日期时间
|
||||
* @param zone 时区
|
||||
* @return joda-time 中对应的 {@link org.joda.time.DateTime} 对象
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算时间戳在指定时区对应的时间,结果使用 {@link org.joda.time.DateTime} 表示
|
||||
*
|
||||
* @param instant java.time 中的时间戳
|
||||
* @param zone java.time 中的时区
|
||||
* @return joda-time 中带时区的日期时间
|
||||
*/
|
||||
public static org.joda.time.DateTime toJodaDateTime(
|
||||
java.time.Instant instant,
|
||||
java.time.ZoneId zone) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
return toJodaInstant(instant).toDateTime(dateTimeZone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 joda-time 中带时区的日期时间,转换为 java.time 中带时区的日期时间
|
||||
*
|
||||
* @param dateTime joda-time 中带时区的日期时间
|
||||
* @return java.time 中带时区的日期时间
|
||||
*/
|
||||
public static java.time.ZonedDateTime toZonedDateTime(org.joda.time.DateTime dateTime) {
|
||||
java.time.ZoneId zone = dateTime.getZone().toTimeZone().toZoneId();
|
||||
return toJavaInstant(dateTime.toInstant()).atZone(zone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 joda-time 中的 {@link org.joda.time.LocalDateTime} 和
|
||||
* {@link org.joda.time.DateTimeZone}
|
||||
* 转换为 java.time 中的 {@link java.time.ZonedDateTime}
|
||||
*
|
||||
* @param localDateTime joda-time 中的地区时间
|
||||
* @param dateTimeZone joda-time 中的时区
|
||||
* @return java.time 中带时区的日期时间
|
||||
*/
|
||||
public static java.time.ZonedDateTime toZonedDateTime(
|
||||
org.joda.time.LocalDateTime localDateTime,
|
||||
org.joda.time.DateTimeZone dateTimeZone) {
|
||||
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||
return toJavaInstant(localDateTime, dateTimeZone).atZone(zone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 joda-time 中的 {@link org.joda.time.Instant} 在指定时区的时间,用 Java 8+ 的
|
||||
* {@link java.time.ZonedDateTime} 表示
|
||||
*
|
||||
* @param instant joda-time 中的时间戳
|
||||
* @param dateTimeZone joda-time 中的时区
|
||||
* @return
|
||||
*/
|
||||
public static java.time.ZonedDateTime toZonedDateTime(
|
||||
org.joda.time.Instant instant,
|
||||
org.joda.time.DateTimeZone dateTimeZone) {
|
||||
java.time.ZoneId zone = toJavaZone(dateTimeZone);
|
||||
return toJavaInstant(instant).atZone(zone);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link java.time.LocalDateTime} 转换为 {@link org.joda.time.LocalDateTime}
|
||||
*
|
||||
* @param localDateTime Java 8 LocalDateTime
|
||||
* @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();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaLocalDateTime
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@link org.joda.time.LocalDateTime} 转换为 {@link java.time.LocalDateTime}
|
||||
*
|
||||
* @param localDateTime joda-time LocalDateTime
|
||||
* @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();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 转换 Java API 和 joda-time API 表示时区的对象
|
||||
*
|
||||
* @param jodaZone joda-time API 中表示时区的对象
|
||||
* @return Java API 中表示时区的对象
|
||||
*/
|
||||
public static java.time.ZoneId toJavaZone(org.joda.time.DateTimeZone jodaZone) {
|
||||
return jodaZone.toTimeZone().toZoneId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换 Java API 和 joda-time API 表示时区的对象
|
||||
*
|
||||
* @param zone Java API 中表示时区的对象
|
||||
* @return joda-time API 中表示时区的对象
|
||||
*/
|
||||
public static org.joda.time.DateTimeZone toJodaZone(java.time.ZoneId zone) {
|
||||
return org.joda.time.DateTimeZone.forID(zone.getId());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 私有构造方法
|
||||
*/
|
||||
private JodaTimeTools() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
}
|
||||
}
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Objects;
|
||||
@@ -67,21 +69,21 @@ public final class RandomTools {
|
||||
* @return 随机字符串
|
||||
*/
|
||||
public static String randomStr(Random random, char[] sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(random, sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String randomStr(char[] sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String secureRandomStr(char[] sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
|
||||
}
|
||||
|
||||
@@ -96,21 +98,21 @@ public final class RandomTools {
|
||||
* @return 随机字符串
|
||||
*/
|
||||
public static String randomStr(Random random, String sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
checkArgument(Objects.nonNull(random), "Random cannot be null.");
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(random, sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String randomStr(String sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
|
||||
}
|
||||
|
||||
public static String secureRandomStr(String sourceCharacters, int length) {
|
||||
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
|
||||
checkArgument(length >= 0, "The length should be greater than or equal to zero.");
|
||||
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,9 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
@@ -72,7 +75,7 @@ public final class RegexTools {
|
||||
* @return {@link Pattern} 实例
|
||||
*/
|
||||
public static Pattern getPattern(final String pattern, final int flags, final boolean cachePattern) {
|
||||
AssertTools.checkNotNull(pattern);
|
||||
checkNotNull(pattern);
|
||||
return cachePattern ? cacheAndGetPatternInternal(pattern, flags) : getPatternInternal(pattern, flags);
|
||||
}
|
||||
|
||||
@@ -95,7 +98,7 @@ public final class RegexTools {
|
||||
*/
|
||||
@Nonnull
|
||||
public static Pattern getPattern(final String pattern, final int flags) {
|
||||
AssertTools.checkNotNull(pattern);
|
||||
checkNotNull(pattern);
|
||||
return getPatternInternal(pattern, flags);
|
||||
}
|
||||
|
||||
@@ -115,7 +118,7 @@ public final class RegexTools {
|
||||
* @return 判断结果
|
||||
*/
|
||||
public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) {
|
||||
AssertTools.checkNotNull(pattern);
|
||||
checkNotNull(pattern);
|
||||
return matchesInternal(input, pattern);
|
||||
}
|
||||
|
||||
@@ -127,7 +130,7 @@ public final class RegexTools {
|
||||
* @return 判断结果
|
||||
*/
|
||||
public static boolean matchesAny(@Nullable final CharSequence input, final Pattern[] patterns) {
|
||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
||||
checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
||||
return matchesAnyInternal(input, patterns);
|
||||
}
|
||||
|
||||
@@ -139,7 +142,7 @@ public final class RegexTools {
|
||||
* @return 判断结果
|
||||
*/
|
||||
public static boolean matchesAll(@Nullable final CharSequence input, final Pattern[] patterns) {
|
||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
||||
checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
||||
return matchesAllInternal(input, patterns);
|
||||
}
|
||||
|
||||
@@ -210,8 +213,8 @@ public final class RegexTools {
|
||||
* @return 结果
|
||||
*/
|
||||
public static Matcher getMatcher(final CharSequence input, final Pattern pattern) {
|
||||
AssertTools.checkNotNull(input);
|
||||
AssertTools.checkNotNull(pattern);
|
||||
checkNotNull(input);
|
||||
checkNotNull(pattern);
|
||||
return pattern.matcher(input);
|
||||
}
|
||||
|
||||
@@ -261,8 +264,8 @@ public final class RegexTools {
|
||||
* @return 结果
|
||||
*/
|
||||
public static Matcher getMatcher(final CharSequence input, final String pattern, final int flags) {
|
||||
AssertTools.checkNotNull(input);
|
||||
AssertTools.checkNotNull(pattern);
|
||||
checkNotNull(input);
|
||||
checkNotNull(pattern);
|
||||
return getPatternInternal(pattern, flags).matcher(input);
|
||||
}
|
||||
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
@@ -73,9 +75,9 @@ public class SnowflakeIdGenerator {
|
||||
* @param datacenterId 数据中心ID (0~31)
|
||||
*/
|
||||
public SnowflakeIdGenerator(final long workerId, final long datacenterId) {
|
||||
AssertTools.checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0),
|
||||
checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0),
|
||||
"WorkerId can't be greater than %s or less than 0.", MAX_WORKER_ID);
|
||||
AssertTools.checkArgument((datacenterId <= MAX_DATACENTER_ID && datacenterId >= 0),
|
||||
checkArgument((datacenterId <= MAX_DATACENTER_ID && datacenterId >= 0),
|
||||
"DatacenterId can't be greater than %s or less than 0.", MAX_DATACENTER_ID);
|
||||
this.datacenterIdAndWorkerId
|
||||
= (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT);
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.Objects;
|
||||
@@ -24,7 +26,7 @@ import javax.annotation.Nullable;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.regex.PatternConsts;
|
||||
|
||||
/**
|
||||
* StringTools
|
||||
@@ -110,7 +112,7 @@ public class StringTools {
|
||||
* @return 结果
|
||||
*/
|
||||
public static String repeat(final String str, int times, int maxLength) {
|
||||
AssertTools.checkArgument(Objects.nonNull(str));
|
||||
checkArgument(Objects.nonNull(str));
|
||||
return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength));
|
||||
}
|
||||
|
||||
@@ -210,8 +212,8 @@ public class StringTools {
|
||||
if (src == null || src.isEmpty()) {
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
AssertTools.checkArgument(front >= 0 && end >= 0);
|
||||
AssertTools.checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度");
|
||||
checkArgument(front >= 0 && end >= 0);
|
||||
checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度");
|
||||
final char[] charArray = src.toCharArray();
|
||||
for (int i = front; i < charArray.length - end; i++) {
|
||||
charArray[i] = replacedChar;
|
||||
|
@@ -16,6 +16,8 @@
|
||||
|
||||
package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
@@ -76,7 +78,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||
* @param nodes 平铺的节点列表
|
||||
*/
|
||||
public List<T> buildTree(Collection<T> nodes) {
|
||||
AssertTools.checkNotNull(nodes);
|
||||
checkNotNull(nodes);
|
||||
return buildTreeInternal(nodes, this.defaultComparator);
|
||||
}
|
||||
|
||||
@@ -93,7 +95,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
|
||||
* <b>仅影响调用 addChild 的顺序,如果操作对象本身对应的控制了子节点的顺序,无法影响其相关逻辑。</b>
|
||||
*/
|
||||
public List<T> buildTree(Collection<T> nodes, @Nullable Comparator<? super T> comparator) {
|
||||
AssertTools.checkNotNull(nodes);
|
||||
checkNotNull(nodes);
|
||||
final Comparator<? super T> c = (comparator != null) ? comparator : this.defaultComparator;
|
||||
return buildTreeInternal(nodes, c);
|
||||
}
|
||||
|
@@ -17,12 +17,12 @@
|
||||
package xyz.zhouxy.plusone.commons.base;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
class IWithCodeTests {
|
||||
|
||||
@@ -91,7 +91,7 @@ class IWithCodeTests {
|
||||
private final String code;
|
||||
|
||||
WithCode(String code) {
|
||||
AssertTools.checkNotNull(code);
|
||||
checkNotNull(code);
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
|
@@ -31,7 +31,7 @@ import org.slf4j.LoggerFactory;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
import xyz.zhouxy.plusone.commons.annotation.ValueObject;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.regex.PatternConsts;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
@@ -19,14 +19,12 @@ package xyz.zhouxy.plusone.commons.model.dto.test;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.Statement;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -45,9 +43,6 @@ import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
@@ -57,6 +52,9 @@ import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.LocalDateTimeTypeAdapter;
|
||||
import xyz.zhouxy.plusone.commons.gson.adapter.JSR310TypeAdapters.LocalDateTypeAdapter;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PageResult;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingAndSortingQueryParams;
|
||||
import xyz.zhouxy.plusone.commons.model.dto.PagingParams;
|
||||
@@ -187,31 +185,8 @@ public class PagingAndSortingQueryParamsTests {
|
||||
@Test
|
||||
void testGson() {
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(LocalDate.class, new TypeAdapter<LocalDate>() {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDate value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_DATE.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate read(JsonReader in) throws IOException {
|
||||
return LocalDate.parse(in.nextString(), DateTimeFormatter.ISO_DATE);
|
||||
}
|
||||
|
||||
})
|
||||
.registerTypeAdapter(LocalDateTime.class, new TypeAdapter<LocalDateTime>() {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, LocalDateTime value) throws IOException {
|
||||
out.value(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime read(JsonReader in) throws IOException {
|
||||
return LocalDateTime.parse(in.nextString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
}
|
||||
})
|
||||
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter().nullSafe())
|
||||
.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeTypeAdapter().nullSafe())
|
||||
.create();
|
||||
try (SqlSession session = sqlSessionFactory.openSession()) {
|
||||
AccountQueryParams params = gson.fromJson(JSON_STR, AccountQueryParams.class);
|
||||
|
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.constant;
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright 2024-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.commons.regex;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Arrays;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.commons.model.Gender;
|
||||
|
||||
@Slf4j
|
||||
public //
|
||||
class PatternInfosTests {
|
||||
|
||||
// ================================
|
||||
// #region - BASIC_ISO_DATE
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void testBasicIsoDate_ValidDate() {
|
||||
LocalDateMatcher localDateMatcher = PatternInfos.BASIC_ISO_DATE.matcher("20241229");
|
||||
Matcher matcher = localDateMatcher.matcher();
|
||||
assertTrue(matcher.matches());
|
||||
assertTrue(localDateMatcher.matches());
|
||||
|
||||
assertEquals("2024", localDateMatcher.getYear());
|
||||
assertEquals("12", localDateMatcher.getMonth());
|
||||
assertEquals("29", localDateMatcher.getDay());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"20231301", // InvalidMonth
|
||||
"20230230", // InvalidDay
|
||||
"20210229", // NonLeapYearFeb29
|
||||
})
|
||||
void testBasicIsoDate_InvalidDate_butMatches(String date) {
|
||||
// 虽然日期有误,但这个正则无法判断。实际工作中,应使用日期时间 API。
|
||||
LocalDateMatcher matcher = PatternInfos.BASIC_ISO_DATE.matcher(date);
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"2023041", // TooShort
|
||||
"99999999990415", // TooLong
|
||||
"2023-04-15", // NonNumeric
|
||||
})
|
||||
void testBasicIsoDate_InvalidDate_Mismatches(String date) {
|
||||
LocalDateMatcher matcher = PatternInfos.BASIC_ISO_DATE.matcher(date);
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - BASIC_ISO_DATE
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ISO_LOCAL_DATE
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void testIsoLocalDate_ValidDate() {
|
||||
LocalDateMatcher matcher = PatternInfos.ISO_LOCAL_DATE.matcher("2024-12-29");
|
||||
assertTrue(matcher.matches());
|
||||
assertEquals("2024", matcher.getYear());
|
||||
assertEquals("12", matcher.getMonth());
|
||||
assertEquals("29", matcher.getDay());
|
||||
|
||||
// LeapYearFeb29()
|
||||
assertTrue(PatternInfos.ISO_LOCAL_DATE.matcher("2020-02-29").matches());
|
||||
|
||||
// BoundaryMin()
|
||||
assertTrue(PatternInfos.ISO_LOCAL_DATE.matcher("0000-01-01").matches());
|
||||
|
||||
// BoundaryMax()
|
||||
assertTrue(PatternInfos.ISO_LOCAL_DATE.matcher("999999999-12-31").matches());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"2023-13-01", // InvalidMonth
|
||||
"2023-02-30", // InvalidDay
|
||||
"2021-02-29", // NonLeapYearFeb29
|
||||
})
|
||||
void testIsoLocalDate_InvalidDate_butMatches(String date) {
|
||||
// 虽然日期有误,但这个正则无法判断。实际工作中,应使用日期时间 API。
|
||||
LocalDateMatcher matcher = PatternInfos.ISO_LOCAL_DATE.matcher(date);
|
||||
assertTrue(matcher.matches());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"2023-04-1", // TooShort
|
||||
"9999999999-04-15", // TooLong
|
||||
"20230415",
|
||||
})
|
||||
void testIsoLocalDate_InvalidDate_Mismatches(String date) {
|
||||
LocalDateMatcher matcher = PatternInfos.ISO_LOCAL_DATE.matcher(date);
|
||||
assertFalse(matcher.matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - ISO_LOCAL_DATE
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - PASSWORD
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void testPassword_ValidPassword_Matches() {
|
||||
assertTrue(PatternInfos.PASSWORD.matcher("Abc123!@#").matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testPassword_InvalidPassword_Mismatches() {
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("Abc123 !@#").matches()); // 带空格
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("Abc123!@# ").matches()); // 带空格
|
||||
assertFalse(PatternInfos.PASSWORD.matcher(" Abc123!@#").matches()); // 带空格
|
||||
assertFalse(PatternInfos.PASSWORD.matcher(" Abc123!@# ").matches()); // 带空格
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("77553366998844113322").matches()); // 纯数字
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("poiujhgbfdsazxcfvghj").matches()); // 纯小写字母
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("POIUJHGBFDSAZXCFVGHJ").matches()); // 纯大写字母
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("!#$%&'*\\+-/=?^`{|}~@()[]\",.;':").matches()); // 纯特殊字符
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("sdfrghbv525842582752").matches()); // 没有小写字母
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("SDFRGHBV525842582752").matches()); // 没有小写字母
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("sdfrghbvSDFRGHBV").matches()); // 没有数字
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("Abc1!").matches()); // 太短
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("Abc1!Abc1!Abc1!Abc1!Abc1!Abc1!Abc1!").matches()); // 太长
|
||||
assertFalse(PatternInfos.PASSWORD.matcher("").matches());
|
||||
assertFalse(PatternInfos.PASSWORD.matcher(" ").matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - PASSWORD
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - EMAIL
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
public void testValidEmails() {
|
||||
assertTrue(PatternInfos.EMAIL.matcher("test@example.com").matches());
|
||||
assertTrue(PatternInfos.EMAIL.matcher("user.name+tag+sorting@example.com").matches());
|
||||
assertTrue(PatternInfos.EMAIL.matcher("user@sub.example.com").matches());
|
||||
assertTrue(PatternInfos.EMAIL.matcher("user@123.123.123.123").matches());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidEmails() {
|
||||
assertFalse(PatternInfos.EMAIL.matcher(".username@example.com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("@missingusername.com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("plainaddress").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username..username@example.com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username.@example.com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@-example.com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@-example.com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@.com.com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@.com.my").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@.com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@com.").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@example..com").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@example.com-").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@example.com.").matches());
|
||||
assertFalse(PatternInfos.EMAIL.matcher("username@example").matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - EMAIL
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - Chinese2ndIdCardNumber
|
||||
// ================================
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"44520019900101456X",
|
||||
"44520019900101456x",
|
||||
"445200199001014566",
|
||||
})
|
||||
void testChinese2ndIdCardNumber_ValidChinese2ndIdCardNumber(String value) {
|
||||
Chinese2ndIdCardNumberMatcher chinese2ndIdCardNumberMatcher = PatternInfos.CHINESE_2ND_ID_CARD_NUMBER.matcher(value);
|
||||
Matcher matcher = chinese2ndIdCardNumberMatcher.matcher();
|
||||
assertTrue(matcher.matches());
|
||||
assertEquals("44", chinese2ndIdCardNumberMatcher.getProvince());
|
||||
assertEquals("4452", chinese2ndIdCardNumberMatcher.getCity());
|
||||
assertEquals("445200", chinese2ndIdCardNumberMatcher.getCounty());
|
||||
assertEquals("19900101", chinese2ndIdCardNumberMatcher.getBirthDate());
|
||||
assertEquals("456", chinese2ndIdCardNumberMatcher.getOrderCode());
|
||||
assertEquals("6", chinese2ndIdCardNumberMatcher.getGenderCode());
|
||||
assertEquals(Gender.FEMALE, chinese2ndIdCardNumberMatcher.getGender());
|
||||
|
||||
String checkDigit = value.substring(value.length() - 1);
|
||||
assertEquals(checkDigit, chinese2ndIdCardNumberMatcher.getCheckDigit());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"4452200199001014566",
|
||||
"44520199001014566",
|
||||
" ",
|
||||
"",
|
||||
})
|
||||
void testChinese2ndIdCardNumber_InvalidChinese2ndIdCardNumber(String value) {
|
||||
assertFalse(PatternInfos.CHINESE_2ND_ID_CARD_NUMBER.matcher(value).matches());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - Chinese2ndIdCardNumber
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - invoke constructor
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
||||
Constructor<?>[] constructors;
|
||||
constructors = PatternInfos.class.getDeclaredConstructors();
|
||||
Arrays.stream(constructors)
|
||||
.forEach(constructor -> {
|
||||
assertFalse(constructor.isAccessible());
|
||||
constructor.setAccessible(true);
|
||||
Throwable cause = assertThrows(Exception.class, constructor::newInstance)
|
||||
.getCause();
|
||||
assertInstanceOf(IllegalStateException.class, cause);
|
||||
assertEquals("Utility class", cause.getMessage());
|
||||
});
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - invoke constructor
|
||||
// ================================
|
||||
}
|
@@ -277,144 +277,6 @@ class DateTimeToolsTests {
|
||||
// #endregion - toLocalDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaInstant_JavaInstant() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_ZonedDateTime() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, DateTimeTools.toJodaInstant(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_INSTANT, DateTimeTools.toJodaInstant(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaInstant() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaDateTime() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, DateTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(INSTANT, DateTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJavaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_ZonedDateTime() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_InstantAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toJodaDateTime(INSTANT_WITH_SYS_ZONE, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, DateTimeTools.toJodaDateTime(INSTANT, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaDateTime() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaInstantAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, DateTimeTools.toZonedDateTime(JODA_INSTANT_WITH_SYS_ZONE, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, DateTimeTools.toZonedDateTime(JODA_INSTANT, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaLocalDateTime_JavaLocalDateTime() {
|
||||
assertEquals(JODA_LOCAL_DATE_TIME, DateTimeTools.toJodaLocalDateTime(LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaLocalDateTime_JodaLocalDateTime() {
|
||||
assertEquals(LOCAL_DATE_TIME, DateTimeTools.toJavaLocalDateTime(JODA_LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void convertJavaZoneIdAndJodaDateTimeZone() {
|
||||
assertEquals(SYS_ZONE_ID, DateTimeTools.toJavaZone(JODA_SYS_ZONE));
|
||||
assertEquals(ZONE_ID, DateTimeTools.toJavaZone(JODA_ZONE));
|
||||
assertEquals(JODA_SYS_ZONE, DateTimeTools.toJodaZone(SYS_ZONE_ID));
|
||||
assertEquals(JODA_ZONE, DateTimeTools.toJodaZone(ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - YearQuarter & Quarter
|
||||
// ================================
|
||||
|
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* 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.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
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 LocalDate LOCAL_DATE = LOCAL_DATE_TIME.toLocalDate();
|
||||
static final LocalTime LOCAL_TIME = LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
// Java - 2024-12-29 12:58:30.333333333 SystemDefaultZone
|
||||
static final ZoneId SYS_ZONE_ID = ZoneId.systemDefault();
|
||||
static final ZonedDateTime ZONED_DATE_TIME_WITH_SYS_ZONE = LOCAL_DATE_TIME.atZone(SYS_ZONE_ID);
|
||||
static final Instant INSTANT_WITH_SYS_ZONE = ZONED_DATE_TIME_WITH_SYS_ZONE.toInstant();
|
||||
static final long INSTANT_MILLIS = INSTANT_WITH_SYS_ZONE.toEpochMilli();
|
||||
|
||||
static final TimeZone SYS_TIME_ZONE = TimeZone.getDefault();
|
||||
static final Date SYS_DATE = Date.from(INSTANT_WITH_SYS_ZONE);
|
||||
static final Calendar SYS_CALENDAR = Calendar.getInstance(SYS_TIME_ZONE);
|
||||
static {
|
||||
SYS_CALENDAR.setTime(SYS_DATE);
|
||||
}
|
||||
|
||||
// Java - 2024-12-29 12:58:30.333333333 GMT+04:00
|
||||
static final ZoneId ZONE_ID = ZoneId.of("GMT+04:00");
|
||||
static final ZonedDateTime ZONED_DATE_TIME = LOCAL_DATE_TIME.atZone(ZONE_ID);
|
||||
static final Instant INSTANT = ZONED_DATE_TIME.toInstant();
|
||||
static final long MILLIS = INSTANT.toEpochMilli();
|
||||
|
||||
static final TimeZone TIME_ZONE = TimeZone.getTimeZone(ZONE_ID);
|
||||
static final Date DATE = Date.from(INSTANT);
|
||||
static final Calendar CALENDAR = Calendar.getInstance(TIME_ZONE);
|
||||
static {
|
||||
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 - toJodaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaInstant_JavaInstant() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJodaInstant(INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, JodaTimeTools.toJodaInstant(INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_ZonedDateTime() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJodaInstant(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_INSTANT, JodaTimeTools.toJodaInstant(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaInstant_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJodaInstant(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_INSTANT, JodaTimeTools.toJodaInstant(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJavaInstant
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaInstant() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJavaInstant(JODA_INSTANT_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, JodaTimeTools.toJavaInstant(JODA_INSTANT));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaDateTime() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJavaInstant(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(INSTANT, JodaTimeTools.toJavaInstant(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaInstant_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(INSTANT_WITH_SYS_ZONE, JodaTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(INSTANT, JodaTimeTools.toJavaInstant(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJavaInstant
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_ZonedDateTime() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toJodaDateTime(ZONED_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(JODA_DATE_TIME, JodaTimeTools.toJodaDateTime(ZONED_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_LocalDateTimeAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toJodaDateTime(LOCAL_DATE_TIME, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, JodaTimeTools.toJodaDateTime(LOCAL_DATE_TIME, ZONE_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJodaDateTime_InstantAndZoneId() {
|
||||
assertEquals(JODA_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toJodaDateTime(INSTANT_WITH_SYS_ZONE, SYS_ZONE_ID));
|
||||
assertEquals(JODA_DATE_TIME, JodaTimeTools.toJodaDateTime(INSTANT, ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaDateTime() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toZonedDateTime(JODA_DATE_TIME_WITH_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, JodaTimeTools.toZonedDateTime(JODA_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaLocalDateTimeAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, JodaTimeTools.toZonedDateTime(JODA_LOCAL_DATE_TIME, JODA_ZONE));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toZonedDateTime_JodaInstantAndJodaDateTimeZone() {
|
||||
assertEquals(ZONED_DATE_TIME_WITH_SYS_ZONE, JodaTimeTools.toZonedDateTime(JODA_INSTANT_WITH_SYS_ZONE, JODA_SYS_ZONE));
|
||||
assertEquals(ZONED_DATE_TIME, JodaTimeTools.toZonedDateTime(JODA_INSTANT, JODA_ZONE));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toZonedDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void toJodaLocalDateTime_JavaLocalDateTime() {
|
||||
assertEquals(JODA_LOCAL_DATE_TIME, JodaTimeTools.toJodaLocalDateTime(LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toJavaLocalDateTime_JodaLocalDateTime() {
|
||||
assertEquals(LOCAL_DATE_TIME, JodaTimeTools.toJavaLocalDateTime(JODA_LOCAL_DATE_TIME));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - toJodaLocalDateTime
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void convertJavaZoneIdAndJodaDateTimeZone() {
|
||||
assertEquals(SYS_ZONE_ID, JodaTimeTools.toJavaZone(JODA_SYS_ZONE));
|
||||
assertEquals(ZONE_ID, JodaTimeTools.toJavaZone(JODA_ZONE));
|
||||
assertEquals(JODA_SYS_ZONE, JodaTimeTools.toJodaZone(SYS_ZONE_ID));
|
||||
assertEquals(JODA_ZONE, JodaTimeTools.toJodaZone(ZONE_ID));
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - ZoneId <--> DateTimeZone
|
||||
// ================================
|
||||
|
||||
}
|
Reference in New Issue
Block a user