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 d76c57c..aa99322 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/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; } 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(); +} 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 c1983bc..472c00d 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 bb78567..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,8 +11,12 @@ 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; + public class DateTimeUtil { private static final MapWrapper DATE_TIME_FORMATTER_CACHE = MapWrapper @@ -114,7 +118,7 @@ public class DateTimeUtil { *

* * @param timeMillis 时间戳 - * @param zone 时区 + * @param zone 时区 * @return 带时区信息的地区时间 */ public static ZonedDateTime toZonedDateTime(long timeMillis, ZoneId zone) { @@ -179,7 +183,7 @@ public class DateTimeUtil { * 获取时间戳在指定时区的地区时间。 * * @param timeMillis 时间戳 - * @param zone 时区 + * @param zone 时区 * @return 地区时间 */ public static LocalDateTime toLocalDateTime(long timeMillis, ZoneId zone) { @@ -221,6 +225,7 @@ public class DateTimeUtil { } // ==================== + // toJodaInstant public static org.joda.time.Instant toJodaInstant(java.time.Instant instant) { @@ -261,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); } @@ -282,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() {