From cc34af04c2089e412dcd037177d210978efa9e52 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 3 Apr 2024 16:20:41 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=B7=A5=E5=85=B7=E7=B1=BB?= =?UTF-8?q?=E5=91=BD=E5=90=8D=E8=A7=84=E5=88=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collection/AbstractMapWrapper.java | 4 +- .../collection/SafeConcurrentHashMap.java | 6 +-- .../domain/ValidatableStringRecord.java | 4 +- ...pUtil.java => ConcurrentHashMapTools.java} | 12 +++--- .../{DateTimeUtil.java => DateTimeTools.java} | 4 +- .../util/{EnumUtil.java => EnumTools.java} | 4 +- .../zhouxy/plusone/commons/util/Numbers.java | 2 +- .../{OptionalUtil.java => OptionalTools.java} | 4 +- .../{RandomUtil.java => RandomTools.java} | 4 +- .../util/{RegexUtil.java => RegexTools.java} | 8 ++-- ...UtilTests.java => DateTimeToolsTests.java} | 20 +++++----- .../plusone/commons/util/NumberUtilTest.java | 39 ------------------- 12 files changed, 37 insertions(+), 74 deletions(-) rename src/main/java/xyz/zhouxy/plusone/commons/util/{ConcurrentHashMapUtil.java => ConcurrentHashMapTools.java} (86%) rename src/main/java/xyz/zhouxy/plusone/commons/util/{DateTimeUtil.java => DateTimeTools.java} (99%) rename src/main/java/xyz/zhouxy/plusone/commons/util/{EnumUtil.java => EnumTools.java} (99%) rename src/main/java/xyz/zhouxy/plusone/commons/util/{OptionalUtil.java => OptionalTools.java} (98%) rename src/main/java/xyz/zhouxy/plusone/commons/util/{RandomUtil.java => RandomTools.java} (89%) rename src/main/java/xyz/zhouxy/plusone/commons/util/{RegexUtil.java => RegexTools.java} (98%) rename src/test/java/xyz/zhouxy/plusone/commons/util/{DateTimeUtilTests.java => DateTimeToolsTests.java} (64%) delete mode 100644 src/test/java/xyz/zhouxy/plusone/commons/util/NumberUtilTest.java diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java index 90ef4f2..f1e6bc2 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/AbstractMapWrapper.java @@ -30,7 +30,7 @@ import javax.annotation.Nullable; import com.google.common.annotations.Beta; -import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil; +import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools; @Beta public abstract class AbstractMapWrapper> { @@ -130,7 +130,7 @@ public abstract class AbstractMapWrapper mappingFunction) { if (this.map instanceof ConcurrentHashMap) { - return ConcurrentHashMapUtil.computeIfAbsent( + return ConcurrentHashMapTools.computeIfAbsent( (ConcurrentHashMap) this.map, key, mappingFunction); } else { return this.map.computeIfAbsent(key, mappingFunction); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/collection/SafeConcurrentHashMap.java b/src/main/java/xyz/zhouxy/plusone/commons/collection/SafeConcurrentHashMap.java index 43929c7..8753136 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/collection/SafeConcurrentHashMap.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/collection/SafeConcurrentHashMap.java @@ -23,7 +23,7 @@ import java.util.function.Function; import javax.annotation.concurrent.ThreadSafe; import xyz.zhouxy.plusone.commons.base.JRE; -import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil; +import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapTools; /** * SafeConcurrentHashMap @@ -35,7 +35,7 @@ import xyz.zhouxy.plusone.commons.util.ConcurrentHashMapUtil; * @author ZhouXY * @since 1.0 * @see ConcurrentHashMap - * @see ConcurrentHashMapUtil#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function) + * @see ConcurrentHashMapTools#computeIfAbsentForJava8(ConcurrentHashMap, Object, Function) */ @ThreadSafe public class SafeConcurrentHashMap extends ConcurrentHashMap { @@ -115,7 +115,7 @@ public class SafeConcurrentHashMap extends ConcurrentHashMap { @Override public V computeIfAbsent(K key, Function mappingFunction) { return JRE.isJava8() - ? ConcurrentHashMapUtil.computeIfAbsentForJava8(this, key, mappingFunction) + ? ConcurrentHashMapTools.computeIfAbsentForJava8(this, key, mappingFunction) : super.computeIfAbsent(key, mappingFunction); } } 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 5c3ac3c..a4d6b7c 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/domain/ValidatableStringRecord.java @@ -23,7 +23,7 @@ import org.apache.commons.lang3.StringUtils; import com.fasterxml.jackson.annotation.JsonValue; import com.google.common.base.Preconditions; -import xyz.zhouxy.plusone.commons.util.RegexUtil; +import xyz.zhouxy.plusone.commons.util.RegexTools; /** * 带校验的字符串值对象 @@ -38,7 +38,7 @@ public abstract class ValidatableStringRecord protected ValidatableStringRecord(String value, Pattern pattern) { Preconditions.checkNotNull(pattern, "The pattern must not be null."); Preconditions.checkArgument(StringUtils.isNotBlank(value), "The value must be has text."); - Preconditions.checkArgument(RegexUtil.matches(value, pattern)); + Preconditions.checkArgument(RegexTools.matches(value, pattern)); this.value = value; } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapTools.java similarity index 86% rename from src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapUtil.java rename to src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapTools.java index 2d870a4..1d20162 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/ConcurrentHashMapTools.java @@ -24,7 +24,7 @@ import xyz.zhouxy.plusone.commons.base.JRE; import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; /** - * ConcurrentHashMapUtil + * ConcurrentHashMapTools * *

* Java 8 的 {@link ConcurrentHashMap#computeIfAbsent(Object, Function)} 方法有 bug, @@ -38,9 +38,10 @@ import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; * @see ConcurrentHashMap * @see SafeConcurrentHashMap */ -public class ConcurrentHashMapUtil { +public class ConcurrentHashMapTools { - public static V computeIfAbsent(ConcurrentHashMap map, final K key, + public static V computeIfAbsent( + ConcurrentHashMap map, final K key, // NOSONAR final Function mappingFunction) { Objects.requireNonNull(map, "map"); return JRE.isJava8() @@ -48,7 +49,8 @@ public class ConcurrentHashMapUtil { : map.computeIfAbsent(key, mappingFunction); } - public static V computeIfAbsentForJava8(ConcurrentHashMap map, final K key, + public static V computeIfAbsentForJava8( + ConcurrentHashMap map, final K key, // NOSONAR final Function mappingFunction) { Objects.requireNonNull(key); Objects.requireNonNull(mappingFunction); @@ -66,7 +68,7 @@ public class ConcurrentHashMapUtil { return v; } - private ConcurrentHashMapUtil() { + private ConcurrentHashMapTools() { throw new IllegalStateException("Utility class"); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeTools.java similarity index 99% rename from src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java rename to src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeTools.java index 1810eeb..8dd3cbf 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/DateTimeTools.java @@ -20,7 +20,7 @@ import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; import xyz.zhouxy.plusone.commons.collection.MapWrapper; -public class DateTimeUtil { +public class DateTimeTools { private static final MapWrapper DATE_TIME_FORMATTER_CACHE = MapWrapper .wrap(new SafeConcurrentHashMap<>()) @@ -325,7 +325,7 @@ public class DateTimeUtil { return org.joda.time.DateTimeZone.forID(zone.getId()); } - private DateTimeUtil() { + private DateTimeTools() { throw new IllegalStateException("Utility class"); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java similarity index 99% rename from src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java rename to src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java index 1d250df..c292049 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/EnumUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/EnumTools.java @@ -27,9 +27,9 @@ import com.google.common.base.Preconditions; * * @author ZhouXY */ -public final class EnumUtil { +public final class EnumTools { - private EnumUtil() { + private EnumTools() { throw new IllegalStateException("Utility class"); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/Numbers.java b/src/main/java/xyz/zhouxy/plusone/commons/util/Numbers.java index 365e0fd..bdc5590 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/Numbers.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/Numbers.java @@ -17,7 +17,7 @@ package xyz.zhouxy.plusone.commons.util; /** - * NumberUtil + * Numbers * * @author ZhouXY */ diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/OptionalUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/OptionalTools.java similarity index 98% rename from src/main/java/xyz/zhouxy/plusone/commons/util/OptionalUtil.java rename to src/main/java/xyz/zhouxy/plusone/commons/util/OptionalTools.java index 1127ced..c536fbb 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/OptionalUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/OptionalTools.java @@ -38,7 +38,7 @@ import com.google.common.annotations.Beta; * @see OptionalLong * @see OptionalDouble */ -public class OptionalUtil { +public class OptionalTools { /** * 将包装类 {@link Integer} 转为 {@link OptionalInt}(not null)。 @@ -149,7 +149,7 @@ public class OptionalUtil { return optionalObj.isPresent() ? optionalObj.getAsDouble() : null; } - private OptionalUtil() { + private OptionalTools() { throw new IllegalStateException("Utility class"); } } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java similarity index 89% rename from src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java rename to src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java index 9375728..0a977a5 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RandomTools.java @@ -2,8 +2,8 @@ package xyz.zhouxy.plusone.commons.util; import java.security.SecureRandom; -public final class RandomUtil { - private RandomUtil() { +public final class RandomTools { + private RandomTools() { throw new IllegalStateException("Utility class"); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexTools.java similarity index 98% rename from src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java rename to src/main/java/xyz/zhouxy/plusone/commons/util/RegexTools.java index e71e391..a13f2fb 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexTools.java @@ -35,7 +35,7 @@ import xyz.zhouxy.plusone.commons.collection.SafeConcurrentHashMap; * @author ZhouXY * */ -public final class RegexUtil { +public final class RegexTools { private static final int DEFAULT_CACHE_INITIAL_CAPACITY = 64; private static final int MAX_CACHE_SIZE = 256; @@ -330,7 +330,7 @@ public final class RegexUtil { @Nonnull private static Pattern[] getAndCachePatternsInternal(@Nonnull final String[] patterns) { return Arrays.stream(patterns) - .map(RegexUtil::getAndCachePatternInternal) + .map(RegexTools::getAndCachePatternInternal) .toArray(Pattern[]::new); } @@ -344,7 +344,7 @@ public final class RegexUtil { @Nonnull private static Pattern[] getPatternsInternal(@Nonnull final String[] patterns) { return Arrays.stream(patterns) - .map(RegexUtil::getPatternInternal) + .map(RegexTools::getPatternInternal) .toArray(Pattern[]::new); } @@ -389,7 +389,7 @@ public final class RegexUtil { return Arrays.stream(array).allMatch(Objects::nonNull); } - private RegexUtil() { + private RegexTools() { // 不允许实例化 throw new IllegalStateException("Utility class"); } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeUtilTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java similarity index 64% rename from src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeUtilTests.java rename to src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java index ef7bf7f..27db9fb 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeUtilTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java @@ -11,40 +11,40 @@ import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -class DateTimeUtilTests { +class DateTimeToolsTests { - private static final Logger log = LoggerFactory.getLogger(DateTimeUtilTests.class); + private static final Logger log = LoggerFactory.getLogger(DateTimeToolsTests.class); @Test void testLocalNowStr() { - log.info(DateTimeUtil.nowStr("yyyy/MM/dd HH:mm:ss.SSS")); + log.info(DateTimeTools.nowStr("yyyy/MM/dd HH:mm:ss.SSS")); } @Test void testToJoda() { LocalDateTime now = LocalDateTime.now(); log.info("now: {}", now); - org.joda.time.LocalDateTime now2 = DateTimeUtil.toJodaLocalDateTime(now); + org.joda.time.LocalDateTime now2 = DateTimeTools.toJodaLocalDateTime(now); log.info("now2: {}", now2); } @Test void testToInstant() { - Instant now = DateTimeUtil.toInstant(System.currentTimeMillis()); - String str = DateTimeUtil.toString("yy-M-d HH:mm:ss.SSS", now); + Instant now = DateTimeTools.toInstant(System.currentTimeMillis()); + String str = DateTimeTools.toString("yy-M-d HH:mm:ss.SSS", now); log.info(str); } @Test void testToJodaDateTime() { - DateTime jodaDateTime = DateTimeUtil.toJodaDateTime(Instant.now(), ZoneId.of("+08:00")); + DateTime jodaDateTime = DateTimeTools.toJodaDateTime(Instant.now(), ZoneId.of("+08:00")); log.info("jodaDateTime: {}", jodaDateTime); } @Test void test() { Instant now = Instant.now(); - DateTime jodaDateTime = DateTimeUtil.toJodaDateTime(now, ZoneId.of("America/New_York")); + DateTime jodaDateTime = DateTimeTools.toJodaDateTime(now, ZoneId.of("America/New_York")); DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss.SSS"); log.info(formatter.print(jodaDateTime)); log.info(jodaDateTime.getZone().toString()); @@ -52,7 +52,7 @@ class DateTimeUtilTests { log.info("=========================================="); org.joda.time.Instant instant = new org.joda.time.Instant(System.currentTimeMillis() - 500000); log.info(instant.toString()); - log.info(DateTimeUtil.toJavaInstant(instant).toString()); - log.info(DateTimeUtil.toZonedDateTime(instant, DateTimeZone.forID("America/New_York")).toString()); + log.info(DateTimeTools.toJavaInstant(instant).toString()); + log.info(DateTimeTools.toZonedDateTime(instant, DateTimeZone.forID("America/New_York")).toString()); } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/NumberUtilTest.java b/src/test/java/xyz/zhouxy/plusone/commons/util/NumberUtilTest.java deleted file mode 100644 index 1b71e08..0000000 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/NumberUtilTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package xyz.zhouxy.plusone.commons.util; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -class NumberUtilTest { - private static final Logger log = LoggerFactory.getLogger(NumberUtilTest.class); - - @Test - void testSum() { - long result = 0; - for (int i = 0; i < Integer.MAX_VALUE; i++) { - result += Short.MAX_VALUE; - } - log.info("Integer.MAX_VALUE: {}", Integer.MAX_VALUE); - log.info("result: {}", result); - assertFalse(Integer.MAX_VALUE > result); - - result = 0; - for (int i = 0; i < Integer.MAX_VALUE; i++) { - result += Short.MAX_VALUE; - } - log.info("Long.MAX_VALUE: {}", Long.MAX_VALUE); - log.info("result: {}", result); - assertTrue(Long.MAX_VALUE > result); - - result = 0; - for (int i = 0; i < Integer.MAX_VALUE; i++) { - result += Integer.MAX_VALUE; - } - log.info("Long.MAX_VALUE: {}", Long.MAX_VALUE); - log.info("result: {}", result); - assertTrue(Long.MAX_VALUE > result); - } -}