From 672a16ad19e5ddb4d34239414d948aaaf9287abe Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 29 Jun 2023 01:18:08 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20RegexUtil=20=E5=81=9A?= =?UTF-8?q?=E6=AD=A3=E5=88=99=E6=A0=A1=E9=AA=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/commons/domain/ValidatableStringRecord.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java index 6b66a6f..df041c4 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java @@ -21,6 +21,7 @@ import java.util.regex.Pattern; import com.fasterxml.jackson.annotation.JsonValue; import xyz.zhouxy.plusone.commons.util.Assert; +import xyz.zhouxy.plusone.commons.util.RegexUtil; /** * 带校验的字符串值对象 @@ -34,7 +35,7 @@ public abstract class ValidatableStringRecord { protected ValidatableStringRecord(String value, Pattern pattern) { Assert.notNull(pattern, "The pattern must not be null."); Assert.isNotBlank(value, "The value must be has text."); - Assert.isTrue(pattern.matcher(value).matches()); + Assert.isTrue(RegexUtil.matches(value, pattern)); this.value = value; } From 1cbe89f39b8891734e1b5e6a19436fce6c39da83 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 29 Jun 2023 01:18:48 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=B0=83=E6=95=B4=20IWithCode=20=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/exception/BaseException.java | 2 +- .../plusone/commons/exception/IWithCode.java | 8 +++-- .../commons/exception/IWithIntCode.java | 29 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/exception/IWithIntCode.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java index 1e4e930..33390ab 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/BaseException.java @@ -21,7 +21,7 @@ package xyz.zhouxy.plusone.commons.exception; * * @author ZhouXY */ -public abstract class BaseException extends RuntimeException implements IWithCode { +public abstract class BaseException extends RuntimeException implements IWithIntCode { private static final long serialVersionUID = -2546365325001947203L; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/IWithCode.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/IWithCode.java index d061c36..3deeae8 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/exception/IWithCode.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/IWithCode.java @@ -16,14 +16,16 @@ package xyz.zhouxy.plusone.commons.exception; +import javax.annotation.Nonnull; + /** * 规定实现类带有 {@code getCode} 方法。 * 用于像自定义异常等需要带有 {@code code} 字段的类, * 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。 * * @author ZhouXY - * @see BaseException */ -public interface IWithCode { - int getCode(); +public interface IWithCode { + @Nonnull + T getCode(); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/exception/IWithIntCode.java b/src/main/java/xyz/zhouxy/plusone/commons/exception/IWithIntCode.java new file mode 100644 index 0000000..a2258f8 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/exception/IWithIntCode.java @@ -0,0 +1,29 @@ +/* + * Copyright 2022-2023 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; + +/** + * 规定实现类带有 {@code getCode} 方法。 + * 用于像自定义异常等需要带有 {@code code} 字段的类, + * 方便其它地方的程序判断该类的是否实现了此接口,以此获取其实例的 {@code code} 字段的值。 + * + * @author ZhouXY + * @see BaseException + */ +public interface IWithIntCode { + int getCode(); +} From 809148716502aca9271e58c899fe26035b0a180b Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Thu, 29 Jun 2023 01:19:21 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=A7=BB=E5=8A=A8=20MapWrapper=20=E6=89=80?= =?UTF-8?q?=E5=9C=A8=E5=8C=85=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/{util => collection}/AbstractMapWrapper.java | 4 ++-- .../plusone/commons/{util => collection}/MapWrapper.java | 2 +- src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java | 2 +- .../java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java | 2 ++ 4 files changed, 6 insertions(+), 4 deletions(-) rename src/main/java/xyz/zhouxy/plusone/commons/{util => collection}/AbstractMapWrapper.java (97%) rename src/main/java/xyz/zhouxy/plusone/commons/{util => collection}/MapWrapper.java (98%) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java similarity index 97% rename from src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java rename to src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java index 31623e5..ca6f2f5 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/AbstractMapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package xyz.zhouxy.plusone.commons.util; +package xyz.zhouxy.plusone.commons.collection; import java.util.Collection; import java.util.Collections; @@ -164,7 +164,7 @@ public abstract class AbstractMapWrapper> { + public abstract static class Builder> { protected final Map map; protected Consumer keyChecker; protected Consumer valueChecker; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java similarity index 98% rename from src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java rename to src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java index 151b0a9..6330673 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/MapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/MapWrapper.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package xyz.zhouxy.plusone.commons.util; +package xyz.zhouxy.plusone.commons.collection; import java.util.Collections; import java.util.Comparator; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java index a57327c..e8c1892 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/jdbc/DbRecord.java @@ -31,7 +31,7 @@ import java.util.Set; import com.google.common.annotations.Beta; -import xyz.zhouxy.plusone.commons.util.AbstractMapWrapper; +import xyz.zhouxy.plusone.commons.collection.AbstractMapWrapper; import xyz.zhouxy.plusone.commons.util.Assert; import xyz.zhouxy.plusone.commons.util.OptionalUtil; diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java index ffebe8a..51099c0 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java @@ -14,6 +14,8 @@ import java.util.TimeZone; import org.apache.commons.lang3.StringUtils; +import xyz.zhouxy.plusone.commons.collection.MapWrapper; + public class DateTimeUtil { private static final MapWrapper DATE_TIME_FORMATTER_CHCHE = MapWrapper From 6d6ef63b06d3ae23f1c2925d199991cbf2d41f5a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 30 Jun 2023 23:09:28 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/DateTimeUtil.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java index 2fcfcfc..f808353 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java @@ -11,6 +11,8 @@ import java.util.Calendar; import java.util.Date; import java.util.TimeZone; +import org.joda.time.DateTimeZone; + import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; import xyz.zhouxy.plusone.commons.collection.MapWrapper; @@ -116,7 +118,7 @@ public class DateTimeUtil { *

* * @param timeMillis 时间戳 - * @param zone 时区 + * @param zone 时区 * @return 带时区信息的地区时间 */ public static ZonedDateTime toZonedDateTime(long timeMillis, ZoneId zone) { @@ -181,7 +183,7 @@ public class DateTimeUtil { * 获取时间戳在指定时区的地区时间。 * * @param timeMillis 时间戳 - * @param zone 时区 + * @param zone 时区 * @return 地区时间 */ public static LocalDateTime toLocalDateTime(long timeMillis, ZoneId zone) { @@ -223,6 +225,7 @@ public class DateTimeUtil { } // ==================== + // toJodaInstant public static org.joda.time.Instant toJodaInstant(java.time.Instant instant) { @@ -263,14 +266,14 @@ public class DateTimeUtil { public static org.joda.time.DateTime toJodaDateTime( java.time.LocalDateTime localDateTime, java.time.ZoneId zone) { - org.joda.time.DateTimeZone dateTimeZone = org.joda.time.DateTimeZone.forID(zone.getId()); + org.joda.time.DateTimeZone dateTimeZone = toJodaTime(zone); return toJodaInstant(ZonedDateTime.of(localDateTime, zone).toInstant()).toDateTime(dateTimeZone); } public static org.joda.time.DateTime toJodaDateTime( java.time.Instant instant, java.time.ZoneId zone) { - org.joda.time.DateTimeZone dateTimeZone = org.joda.time.DateTimeZone.forID(zone.getId()); + org.joda.time.DateTimeZone dateTimeZone = toJodaTime(zone); return toJodaInstant(instant).toDateTime(dateTimeZone); } @@ -284,31 +287,39 @@ public class DateTimeUtil { public static java.time.ZonedDateTime toZonedDateTime( org.joda.time.LocalDateTime localDateTime, org.joda.time.DateTimeZone dateTimeZone) { - java.time.ZoneId zone = dateTimeZone.toTimeZone().toZoneId(); + java.time.ZoneId zone = toJavaZone(dateTimeZone); return toJavaInstant(localDateTime, dateTimeZone).atZone(zone); } public static java.time.ZonedDateTime toZonedDateTime( org.joda.time.Instant instant, org.joda.time.DateTimeZone dateTimeZone) { - java.time.ZoneId zone = dateTimeZone.toTimeZone().toZoneId(); + java.time.ZoneId zone = toJavaZone(dateTimeZone); return toJavaInstant(instant).atZone(zone); } // toJodaLocalDateTime public static org.joda.time.LocalDateTime toJodaLocalDateTime(java.time.LocalDateTime localDateTime) { - return toJodaInstant(localDateTime, ZoneId.systemDefault()) - .toDateTime(org.joda.time.DateTimeZone.getDefault()) - .toLocalDateTime(); + java.time.ZoneId javaZone = java.time.ZoneId.systemDefault(); + org.joda.time.DateTimeZone jodaZone = toJodaTime(javaZone); + return toJodaInstant(localDateTime, javaZone).toDateTime(jodaZone).toLocalDateTime(); } // toJavaLocalDateTime public static java.time.LocalDateTime toJavaLocalDateTime(org.joda.time.LocalDateTime localDateTime) { - return toJavaInstant(localDateTime, org.joda.time.DateTimeZone.getDefault()) - .atZone(java.time.ZoneId.systemDefault()) - .toLocalDateTime(); + org.joda.time.DateTimeZone jodaZone = org.joda.time.DateTimeZone.getDefault(); + java.time.ZoneId javaZone = toJavaZone(jodaZone); + return toJavaInstant(localDateTime, jodaZone).atZone(javaZone).toLocalDateTime(); + } + + public static java.time.ZoneId toJavaZone(org.joda.time.DateTimeZone jodaZone) { + return jodaZone.toTimeZone().toZoneId(); + } + + public static DateTimeZone toJodaTime(java.time.ZoneId zone) { + return org.joda.time.DateTimeZone.forID(zone.getId()); } private DateTimeUtil() {