Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
a7b1067ebb | |||
1dbfb36146 | |||
1fc0b198c9 | |||
15e07901e6 | |||
8f451e7eb9 | |||
2b6f946759 | |||
ce9f3edfbc |
@@ -77,7 +77,7 @@ System.out.println(result); // Output: Return string
|
||||
```java
|
||||
public final class LoginException
|
||||
extends RuntimeException
|
||||
implements IMultiTypesException<LoginException, LoginException.Type, String> {
|
||||
implements IMultiTypesException<LoginException.Type> {
|
||||
private static final long serialVersionUID = 881293090625085616L;
|
||||
private final Type type;
|
||||
private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
@@ -104,7 +104,7 @@ public final class LoginException
|
||||
|
||||
// ...
|
||||
|
||||
public enum Type implements IExceptionType<LoginException, String> {
|
||||
public enum Type implements IExceptionType<String>, IExceptionFactory<LoginException> {
|
||||
DEFAULT("00", "当前会话未登录"),
|
||||
NOT_TOKEN("10", "未提供token"),
|
||||
INVALID_TOKEN("20", "token无效"),
|
||||
|
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-parent</artifactId>
|
||||
<version>1.1.0-RC2</version>
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>plusone-commons</artifactId>
|
||||
|
@@ -20,7 +20,8 @@ import javax.annotation.Nonnull;
|
||||
/**
|
||||
* 异常工厂
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <X> 异常类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IExceptionFactory<X extends Exception> {
|
||||
/**
|
||||
|
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
|
||||
/**
|
||||
* 异常场景
|
||||
*
|
||||
* @param <TCode> 场景编码
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IExceptionType<TCode> extends IWithCode<TCode> {
|
||||
|
||||
/**
|
||||
* 默认异常信息
|
||||
*/
|
||||
String getDefaultMessage();
|
||||
|
||||
@Virtual
|
||||
default String getDescription() {
|
||||
return getDefaultMessage();
|
||||
}
|
||||
|
||||
}
|
@@ -15,13 +15,8 @@
|
||||
*/
|
||||
package xyz.zhouxy.plusone.commons.exception;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.annotation.Virtual;
|
||||
import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
|
||||
/**
|
||||
* IMultiTypesException
|
||||
*
|
||||
@@ -38,7 +33,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
* <pre>
|
||||
* public final class LoginException
|
||||
* extends RuntimeException
|
||||
* implements IMultiTypesException<LoginException, LoginException.Type, String> {
|
||||
* implements IMultiTypesException<LoginException.Type> {
|
||||
* private static final long serialVersionUID = 881293090625085616L;
|
||||
* private final Type type;
|
||||
* private LoginException(@Nonnull Type type, @Nonnull String message) {
|
||||
@@ -65,7 +60,7 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
*
|
||||
* // ...
|
||||
*
|
||||
* public enum Type implements IExceptionType<LoginException, String> {
|
||||
* public enum Type implements IExceptionType<String>, IExceptionFactory<LoginException> {
|
||||
* DEFAULT("00", "当前会话未登录"),
|
||||
* NOT_TOKEN("10", "未提供token"),
|
||||
* INVALID_TOKEN("20", "token无效"),
|
||||
@@ -122,15 +117,11 @@ import xyz.zhouxy.plusone.commons.base.IWithCode;
|
||||
* throw LoginException.Type.TOKEN_TIMEOUT.create();
|
||||
* </pre>
|
||||
*
|
||||
* @param <X> 具体异常类
|
||||
* @param <T> 异常场景
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface IMultiTypesException<
|
||||
X extends Exception,
|
||||
T extends IMultiTypesException.IExceptionType<X, TCode>,
|
||||
TCode extends Serializable> {
|
||||
public interface IMultiTypesException<T extends IExceptionType<?>> {
|
||||
|
||||
/**
|
||||
* 异常类型
|
||||
@@ -139,31 +130,4 @@ public interface IMultiTypesException<
|
||||
*/
|
||||
@Nonnull
|
||||
T getType();
|
||||
|
||||
/**
|
||||
* 获取异常类型编码
|
||||
*
|
||||
* @return 异常类型编码
|
||||
*/
|
||||
default @Nonnull TCode getTypeCode() {
|
||||
return getType().getCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异常类型
|
||||
*/
|
||||
public static interface IExceptionType<X extends Exception, TCode extends Serializable>
|
||||
extends IWithCode<TCode>, IExceptionFactory<X> {
|
||||
|
||||
/**
|
||||
* 默认异常信息
|
||||
*/
|
||||
String getDefaultMessage();
|
||||
|
||||
@Virtual
|
||||
default String getDescription() {
|
||||
return getDefaultMessage();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -21,7 +21,6 @@ import java.time.format.DateTimeParseException;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.business.RequestParamsException;
|
||||
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException.IExceptionType;
|
||||
|
||||
/**
|
||||
* 解析失败异常
|
||||
@@ -39,7 +38,7 @@ import xyz.zhouxy.plusone.commons.exception.IMultiTypesException.IExceptionType;
|
||||
*/
|
||||
public final class ParsingFailureException
|
||||
extends Exception
|
||||
implements IMultiTypesException<ParsingFailureException, ParsingFailureException.Type, String> {
|
||||
implements IMultiTypesException<ParsingFailureException.Type> {
|
||||
private static final long serialVersionUID = 795996090625132616L;
|
||||
|
||||
private final Type type;
|
||||
@@ -171,7 +170,7 @@ public final class ParsingFailureException
|
||||
/** XML 解析失败 */
|
||||
public static final Type XML_PARSING_FAILURE = Type.XML_PARSING_FAILURE;
|
||||
|
||||
public enum Type implements IExceptionType<ParsingFailureException, String> {
|
||||
public enum Type implements IExceptionType<String>, IExceptionFactory<ParsingFailureException> {
|
||||
DEFAULT("00", "解析失败"),
|
||||
NUMBER_PARSING_FAILURE("10", "数字转换失败"),
|
||||
DATE_TIME_PARSING_FAILURE("20", "时间解析失败"),
|
||||
|
@@ -18,7 +18,8 @@ package xyz.zhouxy.plusone.commons.exception.business;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException.IExceptionType;
|
||||
import xyz.zhouxy.plusone.commons.exception.IExceptionFactory;
|
||||
import xyz.zhouxy.plusone.commons.exception.IExceptionType;
|
||||
import xyz.zhouxy.plusone.commons.exception.IMultiTypesException;
|
||||
|
||||
/**
|
||||
@@ -35,7 +36,7 @@ import xyz.zhouxy.plusone.commons.exception.IMultiTypesException;
|
||||
*/
|
||||
public final class InvalidInputException
|
||||
extends RequestParamsException
|
||||
implements IMultiTypesException<InvalidInputException, InvalidInputException.Type, String> {
|
||||
implements IMultiTypesException<InvalidInputException.Type> {
|
||||
private static final long serialVersionUID = -28994090625082516L;
|
||||
|
||||
private final Type type;
|
||||
@@ -109,7 +110,7 @@ public final class InvalidInputException
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public enum Type implements IExceptionType<InvalidInputException, String> {
|
||||
public enum Type implements IExceptionType<String>, IExceptionFactory<InvalidInputException> {
|
||||
DEFAULT("00", "用户输入内容非法"),
|
||||
CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS("01", "包含非法恶意跳转链接"),
|
||||
CONTAINS_ILLEGAL_WORDS("02", "包含违禁敏感词"),
|
||||
|
@@ -27,11 +27,11 @@ import java.time.Year;
|
||||
import java.time.YearMonth;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.chrono.IsoChronology;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.google.common.collect.BoundType;
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||
@@ -649,7 +649,7 @@ public class DateTimeTools {
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// #region - range
|
||||
// ================================
|
||||
|
||||
/**
|
||||
@@ -673,6 +673,52 @@ public class DateTimeTools {
|
||||
return Range.closedOpen(date.atStartOfDay(zone), startOfNextDate(date, zone));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定日期范围转为日期时间范围
|
||||
*
|
||||
* @param dateRange 日期范围
|
||||
* @return 对应的日期时间范围
|
||||
*/
|
||||
public static Range<LocalDateTime> toDateTimeRange(Range<LocalDate> dateRange) {
|
||||
BoundType lowerBoundType = dateRange.lowerBoundType();
|
||||
LocalDateTime lowerEndpoint = lowerBoundType == BoundType.CLOSED
|
||||
? dateRange.lowerEndpoint().atStartOfDay()
|
||||
: dateRange.lowerEndpoint().plusDays(1).atStartOfDay();
|
||||
BoundType upperBoundType = dateRange.upperBoundType();
|
||||
LocalDateTime upperEndpoint = upperBoundType == BoundType.CLOSED
|
||||
? dateRange.upperEndpoint().plusDays(1).atStartOfDay()
|
||||
: dateRange.upperEndpoint().atStartOfDay();
|
||||
|
||||
return Range.closedOpen(lowerEndpoint, upperEndpoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定日期范围转为日期时间范围
|
||||
*
|
||||
* @param dateRange 日期范围
|
||||
* @return 对应的日期时间范围
|
||||
*/
|
||||
public static Range<ZonedDateTime> toDateTimeRange(Range<LocalDate> dateRange, ZoneId zone) {
|
||||
BoundType lowerBoundType = dateRange.lowerBoundType();
|
||||
ZonedDateTime lowerEndpoint = lowerBoundType == BoundType.CLOSED
|
||||
? dateRange.lowerEndpoint().atStartOfDay(zone)
|
||||
: dateRange.lowerEndpoint().plusDays(1).atStartOfDay(zone);
|
||||
BoundType upperBoundType = dateRange.upperBoundType();
|
||||
ZonedDateTime upperEndpoint = upperBoundType == BoundType.CLOSED
|
||||
? dateRange.upperEndpoint().plusDays(1).atStartOfDay(zone)
|
||||
: dateRange.upperEndpoint().atStartOfDay(zone);
|
||||
|
||||
return Range.closedOpen(lowerEndpoint, upperEndpoint);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - range
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 判断指定年份是否为闰年
|
||||
*
|
||||
@@ -680,7 +726,7 @@ public class DateTimeTools {
|
||||
* @return 指定年份是否为闰年
|
||||
*/
|
||||
public static boolean isLeapYear(int year) {
|
||||
return IsoChronology.INSTANCE.isLeapYear(year);
|
||||
return Year.isLeap(year);
|
||||
}
|
||||
|
||||
// ================================
|
||||
|
@@ -154,7 +154,7 @@ public final class EnumTools {
|
||||
*/
|
||||
@Nullable
|
||||
public static <E extends Enum<?>> Integer checkOrdinalNullable(Class<E> enumType, @Nullable Integer ordinal) {
|
||||
return checkOrdinalOrDefault(enumType, ordinal, (Integer) null);
|
||||
return checkOrdinalOrDefault(enumType, ordinal, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -33,6 +33,7 @@ import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
|
||||
/**
|
||||
* 枚举类
|
||||
*
|
||||
* <p>
|
||||
* 参考 <a href="https://lostechies.com/jimmybogard/2008/08/12/enumeration-classes/">Enumeration classes</a>
|
||||
*
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
|
@@ -54,7 +54,9 @@ public class JodaTimeTools {
|
||||
* @param zone 时区
|
||||
* @return {@link org.joda.time.Instant} 对象
|
||||
*/
|
||||
public static org.joda.time.Instant toJodaInstant(java.time.LocalDateTime localDateTime, java.time.ZoneId zone) {
|
||||
public static org.joda.time.Instant toJodaInstant(
|
||||
java.time.LocalDateTime localDateTime,
|
||||
java.time.ZoneId zone) {
|
||||
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone));
|
||||
}
|
||||
|
||||
@@ -92,9 +94,9 @@ public class JodaTimeTools {
|
||||
* {@link org.joda.time.DateTimeZone} 对象
|
||||
* 转换为 Java 中的 {@link java.time.Instant} 对象
|
||||
*
|
||||
* @param localDateTime
|
||||
* @param zone
|
||||
* @return
|
||||
* @param localDateTime {@link org.joda.time.LocalDateTime} 对象
|
||||
* @param zone {@link org.joda.time.DateTimeZone} 对象
|
||||
* @return Java 表示时间戳的 {@link java.time.Instant} 对象
|
||||
*/
|
||||
public static java.time.Instant toJavaInstant(
|
||||
org.joda.time.LocalDateTime localDateTime,
|
||||
@@ -135,8 +137,9 @@ public class JodaTimeTools {
|
||||
public static org.joda.time.DateTime toJodaDateTime(
|
||||
java.time.LocalDateTime localDateTime,
|
||||
java.time.ZoneId zone) {
|
||||
org.joda.time.DateTimeZone dateTimeZone = toJodaZone(zone);
|
||||
return toJodaInstant(java.time.ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone);
|
||||
org.joda.time.LocalDateTime jodaLocalDateTime = toJodaLocalDateTime(localDateTime);
|
||||
org.joda.time.DateTimeZone jodaZone = toJodaZone(zone);
|
||||
return jodaLocalDateTime.toDateTime(jodaZone);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -218,9 +221,15 @@ public class JodaTimeTools {
|
||||
* @return joda-time LocalDateTime
|
||||
*/
|
||||
public static org.joda.time.LocalDateTime toJodaLocalDateTime(java.time.LocalDateTime localDateTime) {
|
||||
java.time.ZoneId javaZone = java.time.ZoneId.systemDefault();
|
||||
org.joda.time.DateTimeZone jodaZone = toJodaZone(javaZone);
|
||||
return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime();
|
||||
return new org.joda.time.LocalDateTime(
|
||||
localDateTime.getYear(),
|
||||
localDateTime.getMonthValue(),
|
||||
localDateTime.getDayOfMonth(),
|
||||
localDateTime.getHour(),
|
||||
localDateTime.getMinute(),
|
||||
localDateTime.getSecond(),
|
||||
localDateTime.getNano() / 1_000_000 // 毫秒转纳秒
|
||||
);
|
||||
}
|
||||
|
||||
// ================================
|
||||
@@ -238,9 +247,15 @@ public class JodaTimeTools {
|
||||
* @return Java 8 LocalDateTime
|
||||
*/
|
||||
public static java.time.LocalDateTime toJavaLocalDateTime(org.joda.time.LocalDateTime localDateTime) {
|
||||
org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault();
|
||||
java.time.ZoneId javaZone = toJavaZone(jodaZone);
|
||||
return toJavaInstant(localDateTime, jodaZone).atZone(javaZone).toLocalDateTime();
|
||||
return java.time.LocalDateTime.of(
|
||||
localDateTime.getYear(),
|
||||
localDateTime.getMonthOfYear(),
|
||||
localDateTime.getDayOfMonth(),
|
||||
localDateTime.getHourOfDay(),
|
||||
localDateTime.getMinuteOfHour(),
|
||||
localDateTime.getSecondOfMinute(),
|
||||
localDateTime.getMillisOfSecond() * 1_000_000 // 毫秒转纳秒
|
||||
);
|
||||
}
|
||||
|
||||
// ================================
|
||||
|
@@ -29,7 +29,9 @@ import javax.annotation.Nullable;
|
||||
*/
|
||||
public class Numbers {
|
||||
|
||||
// ================================
|
||||
// #region - sum
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 求和
|
||||
@@ -131,9 +133,13 @@ public class Numbers {
|
||||
return BigDecimals.sum(numbers);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - nullToZero
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将 {@code null} 转换为 {@code 0}
|
||||
@@ -217,7 +223,122 @@ public class Numbers {
|
||||
return BigDecimals.nullToZero(val);
|
||||
}
|
||||
|
||||
// #endregion
|
||||
// ================================
|
||||
// #endregion - nullToZero
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - parse
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 将字符串转为对应 {@link Short},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Short parseShort(@Nullable String str, @Nullable Short defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Short.parseShort(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转为 {@link Integer},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Integer parseInteger(@Nullable String str, @Nullable Integer defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转为 {@link Long},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Long parseLong(@Nullable String str, @Nullable Long defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转为 {@link Float},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Float parseFloat(@Nullable String str, @Nullable Float defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Float.parseFloat(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串转为 {@link Double},转换失败时返回 {@code defaultValue}(允许为 {@code null})。
|
||||
*
|
||||
* @param str 要转换的字符串
|
||||
* @param defaultValue 默认值
|
||||
* @return 转换结果
|
||||
*/
|
||||
@Nullable
|
||||
public static Double parseDouble(@Nullable String str, @Nullable Double defaultValue) {
|
||||
if (StringTools.isBlank(str)) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Double.parseDouble(str);
|
||||
}
|
||||
catch (NumberFormatException ignore) {
|
||||
// ignore
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - parse
|
||||
// ================================
|
||||
|
||||
private Numbers() {
|
||||
throw new IllegalStateException("Utility class");
|
||||
|
@@ -36,7 +36,7 @@ public final class RandomTools {
|
||||
private static final SecureRandom DEFAULT_SECURE_RANDOM;
|
||||
|
||||
static {
|
||||
SecureRandom secureRandom = null;
|
||||
SecureRandom secureRandom;
|
||||
try {
|
||||
secureRandom = SecureRandom.getInstanceStrong(); // 获取高强度安全随机数生成器
|
||||
}
|
||||
|
@@ -363,7 +363,7 @@ public final class RegexTools {
|
||||
this.flags = flags;
|
||||
}
|
||||
|
||||
private final Pattern compilePattern() {
|
||||
private Pattern compilePattern() {
|
||||
return Pattern.compile(regex, flags);
|
||||
}
|
||||
|
||||
|
@@ -177,6 +177,9 @@ public class StringTools {
|
||||
*/
|
||||
@Beta
|
||||
public static boolean isURL(@Nullable final String cs) {
|
||||
if (cs == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
new URL(cs);
|
||||
} catch (MalformedURLException e) {
|
||||
|
@@ -38,7 +38,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.create();
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getCode(), e.getTypeCode());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -50,7 +49,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -62,7 +60,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION.create(message);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION, e.getType());
|
||||
assertEquals(InvalidInputException.Type.PICTURE_CONTAINS_ILLEGAL_INFORMATION.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -77,7 +74,6 @@ public class InvalidInputExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(InvalidInputException.Type.INFRINGE_COPYRIGHT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.INFRINGE_COPYRIGHT.getCode(), e.getTypeCode());
|
||||
log.info("{}", e.getMessage());
|
||||
assertEquals(nfe.toString(), e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
@@ -92,7 +88,6 @@ public class InvalidInputExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -106,7 +101,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.create(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_AND_MALICIOUS_LINKS.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -120,7 +114,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -134,7 +127,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, npe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -148,7 +140,6 @@ public class InvalidInputExceptionTests {
|
||||
throw InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.create(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS, e.getType());
|
||||
assertEquals(InvalidInputException.Type.CONTAINS_ILLEGAL_WORDS.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -167,7 +158,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException();
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -179,7 +169,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -191,7 +180,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -206,7 +194,6 @@ public class InvalidInputExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
log.info("{}", e.getMessage());
|
||||
assertEquals(nfe.toString(), e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
@@ -221,7 +208,6 @@ public class InvalidInputExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -235,7 +221,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -249,7 +234,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -263,7 +247,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message, npe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -277,7 +260,6 @@ public class InvalidInputExceptionTests {
|
||||
throw new InvalidInputException(message, nfe);
|
||||
});
|
||||
assertSame(InvalidInputException.Type.DEFAULT, e.getType());
|
||||
assertEquals(InvalidInputException.Type.DEFAULT.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
|
@@ -41,7 +41,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create();
|
||||
});
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -53,7 +52,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.JSON_PARSING_FAILURE.create(message);
|
||||
});
|
||||
assertSame(ParsingFailureException.Type.JSON_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.Type.JSON_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -65,7 +63,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.XML_PARSING_FAILURE.create(message);
|
||||
});
|
||||
assertSame(ParsingFailureException.XML_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.XML_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -80,7 +77,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
log.info("{}", e.getMessage());
|
||||
assertEquals(nfe.toString(), e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
@@ -95,7 +91,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -109,7 +104,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.NUMBER_PARSING_FAILURE.create(message, nfe);
|
||||
});
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -123,7 +117,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, nfe);
|
||||
});
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(nfe, e.getCause());
|
||||
}
|
||||
@@ -137,7 +130,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, npe);
|
||||
});
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -151,7 +143,6 @@ public class ParsingFailureExceptionTests {
|
||||
throw ParsingFailureException.DATE_TIME_PARSING_FAILURE.create(message, nfe);
|
||||
});
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -175,7 +166,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(dtpe.getMessage(), e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -189,7 +179,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -206,7 +195,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -223,7 +211,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -238,7 +225,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -253,7 +239,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.DATE_TIME_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.DATE_TIME_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -277,7 +262,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(dtpe.getMessage(), e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -291,7 +275,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getDefaultMessage(), e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -308,7 +291,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -325,7 +307,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertSame(dtpe, e.getCause());
|
||||
}
|
||||
@@ -340,7 +321,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertEquals(message, e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
@@ -355,7 +335,6 @@ public class ParsingFailureExceptionTests {
|
||||
});
|
||||
|
||||
assertSame(ParsingFailureException.NUMBER_PARSING_FAILURE, e.getType());
|
||||
assertEquals(ParsingFailureException.NUMBER_PARSING_FAILURE.getCode(), e.getTypeCode());
|
||||
assertNull(e.getMessage());
|
||||
assertNull(e.getCause());
|
||||
}
|
||||
|
@@ -43,6 +43,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.collect.BoundType;
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.time.Quarter;
|
||||
@@ -83,24 +84,6 @@ class DateTimeToolsTests {
|
||||
CALENDAR.setTime(DATE);
|
||||
}
|
||||
|
||||
// Joda
|
||||
static final org.joda.time.LocalDateTime JODA_LOCAL_DATE_TIME
|
||||
= new org.joda.time.LocalDateTime(2024, 12, 29, 12, 58, 30, 333);
|
||||
static final org.joda.time.LocalDate JODA_LOCAL_DATE = JODA_LOCAL_DATE_TIME.toLocalDate();
|
||||
static final org.joda.time.LocalTime JODA_LOCAL_TIME = JODA_LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
// Joda - 2024-12-29 12:58:30.333 SystemDefaultZone
|
||||
static final org.joda.time.DateTimeZone JODA_SYS_ZONE = org.joda.time.DateTimeZone.getDefault();
|
||||
static final org.joda.time.DateTime JODA_DATE_TIME_WITH_SYS_ZONE = JODA_LOCAL_DATE_TIME.toDateTime(JODA_SYS_ZONE);
|
||||
static final org.joda.time.Instant JODA_INSTANT_WITH_SYS_ZONE = JODA_DATE_TIME_WITH_SYS_ZONE.toInstant();
|
||||
static final long JODA_INSTANT_MILLIS = JODA_INSTANT_WITH_SYS_ZONE.getMillis();
|
||||
|
||||
// Joda - 2024-12-29 12:58:30.333 GMT+04:00
|
||||
static final org.joda.time.DateTimeZone JODA_ZONE = org.joda.time.DateTimeZone.forID("GMT+04:00");
|
||||
static final org.joda.time.DateTime JODA_DATE_TIME = JODA_LOCAL_DATE_TIME.toDateTime(JODA_ZONE);
|
||||
static final org.joda.time.Instant JODA_INSTANT = JODA_DATE_TIME.toInstant();
|
||||
static final long JODA_MILLIS = JODA_INSTANT.getMillis();
|
||||
|
||||
// ================================
|
||||
// #region - toDate
|
||||
// ================================
|
||||
@@ -109,9 +92,7 @@ class DateTimeToolsTests {
|
||||
void toDate_timeMillis() {
|
||||
assertNotEquals(SYS_DATE, DATE);
|
||||
log.info("SYS_DATE: {}, DATE: {}", SYS_DATE, DATE);
|
||||
assertEquals(SYS_DATE, JODA_DATE_TIME_WITH_SYS_ZONE.toDate());
|
||||
assertEquals(SYS_DATE, DateTimeTools.toDate(INSTANT_MILLIS));
|
||||
assertEquals(DATE, JODA_DATE_TIME.toDate());
|
||||
assertEquals(DATE, DateTimeTools.toDate(MILLIS));
|
||||
}
|
||||
|
||||
@@ -393,11 +374,11 @@ class DateTimeToolsTests {
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - others
|
||||
// #region - range
|
||||
// ================================
|
||||
|
||||
@Test
|
||||
void startDateTimeRange() {
|
||||
void toDateTimeRange_specifiedDate() {
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(LOCAL_DATE);
|
||||
assertEquals(LOCAL_DATE.atStartOfDay(), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(LocalDate.of(2024, 12, 30).atStartOfDay(), localDateTimeRange.upperEndpoint());
|
||||
@@ -411,8 +392,96 @@ class DateTimeToolsTests {
|
||||
assertFalse(zonedDateTimeRange.contains(LocalDate.of(2024, 12, 30).atStartOfDay().atZone(SYS_ZONE_ID)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDateTimeRange_dateRange_openRange() {
|
||||
// (2000-01-01..2025-10-01) -> [2000-01-02T00:00..2025-10-01T00:00)
|
||||
Range<LocalDate> dateRange = Range.open(
|
||||
LocalDate.of(2000, 1, 1),
|
||||
LocalDate.of(2025, 10, 1));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0), localDateTimeRange.upperEndpoint());
|
||||
log.info(localDateTimeRange.toString());
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
log.info(zonedDateTimeRange.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDateTimeRange_dateRange_openClosedRange() {
|
||||
// (2000-01-01..2025-10-01] -> [2025-01-02T00-00..2025-10-02 00:00)
|
||||
Range<LocalDate> dateRange = Range.openClosed(
|
||||
LocalDate.of(2000, 1, 1),
|
||||
LocalDate.of(2025, 10, 1));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0), localDateTimeRange.upperEndpoint());
|
||||
log.info(localDateTimeRange.toString());
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
log.info(zonedDateTimeRange.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDateTimeRange_dateRange_closedRange() {
|
||||
// [2000-01-01..2025-10-01] -> [2025-01-01T00-00..2025-10-02 00:00)
|
||||
Range<LocalDate> dateRange = Range.closed(
|
||||
LocalDate.of(2000, 1, 1),
|
||||
LocalDate.of(2025, 10, 1));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0), localDateTimeRange.upperEndpoint());
|
||||
log.info(localDateTimeRange.toString());
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 2, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
log.info(zonedDateTimeRange.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void toDateTimeRange_dateRange_closedOpenRange() {
|
||||
// [2025-01-01..2025-10-01) -> [2025-01-01T00-00..2025-10-01 00:00)
|
||||
Range<LocalDate> dateRange = Range.closedOpen(
|
||||
LocalDate.of(2000, 1, 1),
|
||||
LocalDate.of(2025, 10, 1));
|
||||
|
||||
Range<LocalDateTime> localDateTimeRange = DateTimeTools.toDateTimeRange(dateRange);
|
||||
assertEquals(BoundType.CLOSED, localDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0), localDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, localDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0), localDateTimeRange.upperEndpoint());
|
||||
log.info(localDateTimeRange.toString());
|
||||
|
||||
Range<ZonedDateTime> zonedDateTimeRange = DateTimeTools.toDateTimeRange(dateRange, ZONE_ID);
|
||||
assertEquals(BoundType.CLOSED, zonedDateTimeRange.lowerBoundType());
|
||||
assertEquals(LocalDateTime.of(2000, 1, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.lowerEndpoint());
|
||||
assertEquals(BoundType.OPEN, zonedDateTimeRange.upperBoundType());
|
||||
assertEquals(LocalDateTime.of(2025, 10, 1, 0, 0).atZone(ZONE_ID), zonedDateTimeRange.upperEndpoint());
|
||||
log.info(zonedDateTimeRange.toString());
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - others
|
||||
// #endregion - range
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
|
@@ -33,7 +33,7 @@ import org.junit.jupiter.api.Test;
|
||||
public class JodaTimeToolsTests {
|
||||
|
||||
// Java
|
||||
static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2024, 12, 29, 12, 58, 30, 333000000);
|
||||
static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2024, 12, 29, 12, 58, 30, 333 * 1_000_000);
|
||||
static final LocalDate LOCAL_DATE = LOCAL_DATE_TIME.toLocalDate();
|
||||
static final LocalTime LOCAL_TIME = LOCAL_DATE_TIME.toLocalTime();
|
||||
|
||||
|
@@ -26,6 +26,7 @@ import java.util.Arrays;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public
|
||||
@@ -68,9 +69,16 @@ class NumbersTests {
|
||||
|
||||
@Test
|
||||
public void sum_BigIntegerArray_ReturnsCorrectSum() {
|
||||
BigInteger[] numbers = {new BigInteger("1"), new BigInteger("2"), new BigInteger("3")};
|
||||
BigInteger[] numbers = {
|
||||
new BigInteger("1"),
|
||||
new BigInteger("2"),
|
||||
null,
|
||||
new BigInteger("3")
|
||||
};
|
||||
BigInteger result = Numbers.sum(numbers);
|
||||
assertEquals(new BigInteger("6"), result);
|
||||
|
||||
assertEquals(BigInteger.ZERO, Numbers.sum(new BigInteger[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -192,6 +200,100 @@ class NumbersTests {
|
||||
assertEquals(BigDecimal.ZERO, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseShort(String, Short)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseShort() {
|
||||
assertEquals((short) 12345, Numbers.parseShort("12345", (short) 5));
|
||||
assertEquals((short) 5, Numbers.parseShort("1234.5", (short) 5));
|
||||
assertEquals((short) 5, Numbers.parseShort("", (short) 5));
|
||||
assertEquals((short) 5, Numbers.parseShort(null, (short) 5));
|
||||
|
||||
assertEquals((short) 12345, Numbers.parseShort("12345", null));
|
||||
assertNull(Numbers.parseShort("1234.5", null));
|
||||
assertNull(Numbers.parseShort("", null));
|
||||
assertNull(Numbers.parseShort(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseInteger(String, Integer)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseInteger() {
|
||||
assertEquals(12345, Numbers.parseInteger("12345", 5));
|
||||
assertEquals(5, Numbers.parseInteger("1234.5", 5));
|
||||
assertEquals(5, Numbers.parseInteger("", 5));
|
||||
assertEquals(5, Numbers.parseInteger(null, 5));
|
||||
|
||||
assertEquals(12345, Numbers.parseInteger("12345", null));
|
||||
assertNull(Numbers.parseInteger("1234.5", null));
|
||||
assertNull(Numbers.parseInteger("", null));
|
||||
assertNull(Numbers.parseInteger(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseLong(String, Long)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseLong() {
|
||||
assertEquals(12345L, Numbers.parseLong("12345", 5L));
|
||||
assertEquals(5L, Numbers.parseLong("1234.5", 5L));
|
||||
assertEquals(5L, Numbers.parseLong("", 5L));
|
||||
assertEquals(5L, Numbers.parseLong(null, 5L));
|
||||
|
||||
assertEquals(12345L, Numbers.parseLong("12345", null));
|
||||
assertNull(Numbers.parseLong("1234.5", null));
|
||||
assertNull(Numbers.parseLong("", null));
|
||||
assertNull(Numbers.parseLong(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseFloat(String, Float)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseFloat() {
|
||||
assertEquals(1.2345f, Numbers.parseFloat("1.2345", 5.1f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("a", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("-001Z.2345", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("+001AB.2345", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("001Z.2345", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat("", 5.0f));
|
||||
assertEquals(5.0f, Numbers.parseFloat(null, 5.0f));
|
||||
|
||||
assertEquals(1.2345f, Numbers.parseFloat("1.2345", null));
|
||||
assertNull(Numbers.parseFloat("a", null));
|
||||
assertNull(Numbers.parseFloat("-001Z.2345", null));
|
||||
assertNull(Numbers.parseFloat("+001AB.2345", null));
|
||||
assertNull(Numbers.parseFloat("001Z.2345", null));
|
||||
assertNull(Numbers.parseFloat("", null));
|
||||
assertNull(Numbers.parseFloat(null, null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for {@link Numbers#parseDouble(String, Double)}.
|
||||
*/
|
||||
@Test
|
||||
public void parseDouble() {
|
||||
assertEquals(1.2345d, Numbers.parseDouble("1.2345", 5.1d));
|
||||
assertEquals(5.0d, Numbers.parseDouble("a", 5.0d));
|
||||
assertEquals(1.2345d, Numbers.parseDouble("001.2345", 5.1d));
|
||||
assertEquals(-1.2345d, Numbers.parseDouble("-001.2345", 5.1d));
|
||||
assertEquals(1.2345d, Numbers.parseDouble("+001.2345", 5.1d));
|
||||
assertEquals(0d, Numbers.parseDouble("000.00", 5.1d));
|
||||
assertEquals(5.1d, Numbers.parseDouble("", 5.1d));
|
||||
assertEquals(5.1d, Numbers.parseDouble((String) null, 5.1d));
|
||||
|
||||
assertEquals(1.2345d, Numbers.parseDouble("1.2345", null));
|
||||
assertEquals(null, Numbers.parseDouble("a", null));
|
||||
assertEquals(1.2345d, Numbers.parseDouble("001.2345", null));
|
||||
assertEquals(-1.2345d, Numbers.parseDouble("-001.2345", null));
|
||||
assertEquals(1.2345d, Numbers.parseDouble("+001.2345", null));
|
||||
assertEquals(0d, Numbers.parseDouble("000.00", null));
|
||||
assertEquals(null, Numbers.parseDouble("", null));
|
||||
assertEquals(null, Numbers.parseDouble((String) null, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void test_constructor_isNotAccessible_ThrowsIllegalStateException() {
|
||||
Constructor<?>[] constructors = Numbers.class.getDeclaredConstructors();
|
||||
|
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-parent</artifactId>
|
||||
<version>1.1.0-RC2</version>
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>plusone-dependencies</artifactId>
|
||||
|
Reference in New Issue
Block a user