From b4115aae950ad5030422ab09fe312a17ee643f76 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 4 Dec 2024 01:17:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=20AssertTools=20=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3=20Preconditions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commons/model/dto/UnifiedResponse.java | 12 ++-- .../plusone/commons/time/YearQuarter.java | 4 +- .../plusone/commons/util/RegexTools.java | 58 +++++++++---------- .../commons/util/SnowflakeIdGenerator.java | 5 +- .../plusone/commons/util/TreeBuilder.java | 6 +- 5 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/UnifiedResponse.java b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/UnifiedResponse.java index 143fc86..d33d0df 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/model/dto/UnifiedResponse.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/model/dto/UnifiedResponse.java @@ -22,7 +22,7 @@ import java.util.function.Supplier; import javax.annotation.Nullable; -import com.google.common.base.Preconditions; +import xyz.zhouxy.plusone.commons.util.AssertTools; /** * 统一结果,对返回给前端的数据进行封装。 @@ -78,16 +78,16 @@ public abstract class UnifiedResponse { public static UnifiedResponse of(final boolean isSuccess, final Supplier successResult, final Supplier errorResult) { - Preconditions.checkNotNull(successResult, "Success supplier must not be null."); - Preconditions.checkNotNull(errorResult, "Error supplier must not be null."); + AssertTools.checkNotNull(successResult, "Success supplier must not be null."); + AssertTools.checkNotNull(errorResult, "Error supplier must not be null."); return isSuccess ? successResult.get() : errorResult.get(); } public static UnifiedResponse of(final BooleanSupplier isSuccess, final Supplier successResult, final Supplier errorResult) { - Preconditions.checkNotNull(isSuccess, "Conditions for success must not be null."); - Preconditions.checkNotNull(successResult, "Success supplier must not be null."); - Preconditions.checkNotNull(errorResult, "Error supplier must not be null."); + AssertTools.checkNotNull(isSuccess, "Conditions for success must not be null."); + AssertTools.checkNotNull(successResult, "Success supplier must not be null."); + AssertTools.checkNotNull(errorResult, "Error supplier must not be null."); return isSuccess.getAsBoolean() ? successResult.get() : errorResult.get(); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java b/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java index 2925d99..aed5093 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/time/YearQuarter.java @@ -28,10 +28,10 @@ import java.util.Objects; import javax.annotation.Nonnull; -import com.google.common.base.Preconditions; import com.google.errorprone.annotations.Immutable; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; +import xyz.zhouxy.plusone.commons.util.AssertTools; /** * 表示年份与季度 @@ -52,7 +52,7 @@ public final class YearQuarter implements Comparable, Serializable private final LocalDate lastDate; private YearQuarter(int year, @Nonnull Quarter quarter) { - Preconditions.checkNotNull(quarter, "Quarter can not be null."); + AssertTools.checkNotNull(quarter, "Quarter can not be null."); this.year = year; this.quarter = quarter; this.firstDate = quarter.firstMonthDay().atYear(year); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexTools.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexTools.java index 141a044..34dfb5c 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/RegexTools.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RegexTools.java @@ -26,8 +26,6 @@ import java.util.regex.Pattern; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import com.google.common.base.Preconditions; - /** * 封装一些常用的正则操作,并可以缓存 {@link Pattern} 实例以复用(最多缓存大概 256 个)。 * @@ -49,7 +47,7 @@ public final class RegexTools { * @return {@link Pattern} 实例 */ public static Pattern getPattern(final String pattern, final boolean cachePattern) { - Preconditions.checkNotNull(pattern); + AssertTools.checkNotNull(pattern); return cachePattern ? cacheAndGetPatternInternal(pattern) : getPatternInternal(pattern); } @@ -60,7 +58,7 @@ public final class RegexTools { * @return {@link Pattern} 实例 */ public static Pattern getPattern(final String pattern) { - Preconditions.checkNotNull(pattern); + AssertTools.checkNotNull(pattern); return getPatternInternal(pattern); } @@ -72,8 +70,8 @@ public final class RegexTools { * @return {@link Pattern} 实例数组 */ public static Pattern[] getPatterns(final String[] patterns, final boolean cachePattern) { - Preconditions.checkNotNull(patterns); - Preconditions.checkArgument(allNotNull(patterns)); + AssertTools.checkNotNull(patterns); + AssertTools.checkArgument(allNotNull(patterns)); return cachePattern ? cacheAndGetPatternsInternal(patterns) : getPatternsInternal(patterns); @@ -86,8 +84,8 @@ public final class RegexTools { * @return {@link Pattern} 实例数组 */ public static Pattern[] getPatterns(final String[] patterns) { - Preconditions.checkNotNull(patterns); - Preconditions.checkArgument(allNotNull(patterns)); + AssertTools.checkNotNull(patterns); + AssertTools.checkArgument(allNotNull(patterns)); return getPatternsInternal(patterns); } @@ -98,7 +96,7 @@ public final class RegexTools { * @return 缓存的 Pattern 实例。如果缓存已满,则返回 {@code null}。 */ public static Pattern cachePattern(final Pattern pattern) { - Preconditions.checkNotNull(pattern, "The pattern can not be null."); + AssertTools.checkNotNull(pattern, "The pattern can not be null."); if (PATTERN_CACHE.size() >= MAX_CACHE_SIZE) { return null; } @@ -115,7 +113,7 @@ public final class RegexTools { * @return 判断结果 */ public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) { - Preconditions.checkNotNull(pattern); + AssertTools.checkNotNull(pattern); return matchesInternal(input, pattern); } @@ -127,8 +125,8 @@ public final class RegexTools { * @return 判断结果 */ public static boolean matchesOne(@Nullable final CharSequence input, final Pattern[] patterns) { - Preconditions.checkNotNull(patterns); - Preconditions.checkArgument(allNotNull(patterns)); + AssertTools.checkNotNull(patterns); + AssertTools.checkArgument(allNotNull(patterns)); return matchesOneInternal(input, patterns); } @@ -140,8 +138,8 @@ public final class RegexTools { * @return 判断结果 */ public static boolean matchesAll(@Nullable final CharSequence input, final Pattern[] patterns) { - Preconditions.checkNotNull(patterns); - Preconditions.checkArgument(allNotNull(patterns)); + AssertTools.checkNotNull(patterns); + AssertTools.checkArgument(allNotNull(patterns)); return matchesAllInternal(input, patterns); } @@ -155,7 +153,7 @@ public final class RegexTools { */ public static boolean matches(@Nullable final CharSequence input, final String pattern, final boolean cachePattern) { - Preconditions.checkNotNull(pattern); + AssertTools.checkNotNull(pattern); Pattern p = cachePattern ? cacheAndGetPatternInternal(pattern) : getPatternInternal(pattern); @@ -170,7 +168,7 @@ public final class RegexTools { * @return 判断结果 */ public static boolean matches(@Nullable final CharSequence input, final String pattern) { - Preconditions.checkNotNull(pattern); + AssertTools.checkNotNull(pattern); return matchesInternal(input, getPatternInternal(pattern)); } @@ -184,8 +182,8 @@ public final class RegexTools { */ public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns, final boolean cachePattern) { - Preconditions.checkNotNull(patterns); - Preconditions.checkArgument(allNotNull(patterns)); + AssertTools.checkNotNull(patterns); + AssertTools.checkArgument(allNotNull(patterns)); final Pattern[] patternSet = cachePattern ? cacheAndGetPatternsInternal(patterns) : getPatternsInternal(patterns); @@ -200,8 +198,8 @@ public final class RegexTools { * @return 判断结果 */ public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns) { - Preconditions.checkNotNull(patterns); - Preconditions.checkArgument(allNotNull(patterns)); + AssertTools.checkNotNull(patterns); + AssertTools.checkArgument(allNotNull(patterns)); final Pattern[] patternSet = getPatternsInternal(patterns); return matchesOneInternal(input, patternSet); } @@ -216,8 +214,8 @@ public final class RegexTools { */ public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns, final boolean cachePattern) { - Preconditions.checkNotNull(patterns); - Preconditions.checkArgument(allNotNull(patterns)); + AssertTools.checkNotNull(patterns); + AssertTools.checkArgument(allNotNull(patterns)); final Pattern[] patternSet = cachePattern ? cacheAndGetPatternsInternal(patterns) : getPatternsInternal(patterns); @@ -232,8 +230,8 @@ public final class RegexTools { * @return 判断结果 */ public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns) { - Preconditions.checkNotNull(patterns); - Preconditions.checkArgument(allNotNull(patterns)); + AssertTools.checkNotNull(patterns); + AssertTools.checkArgument(allNotNull(patterns)); final Pattern[] patternSet = getPatternsInternal(patterns); return matchesAllInternal(input, patternSet); } @@ -246,8 +244,8 @@ public final class RegexTools { * @return 结果 */ public static Matcher getMatcher(final CharSequence input, final Pattern pattern) { - Preconditions.checkNotNull(input); - Preconditions.checkNotNull(pattern); + AssertTools.checkNotNull(input); + AssertTools.checkNotNull(pattern); return pattern.matcher(input); } @@ -260,8 +258,8 @@ public final class RegexTools { * @return 结果 */ public static Matcher getMatcher(final CharSequence input, final String pattern, boolean cachePattern) { - Preconditions.checkNotNull(input); - Preconditions.checkNotNull(pattern); + AssertTools.checkNotNull(input); + AssertTools.checkNotNull(pattern); final Pattern p = cachePattern ? cacheAndGetPatternInternal(pattern) : getPatternInternal(pattern); @@ -276,8 +274,8 @@ public final class RegexTools { * @return 结果 */ public static Matcher getMatcher(final CharSequence input, final String pattern) { - Preconditions.checkNotNull(input); - Preconditions.checkNotNull(pattern); + AssertTools.checkNotNull(input); + AssertTools.checkNotNull(pattern); return getPatternInternal(pattern).matcher(input); } diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/SnowflakeIdGenerator.java b/src/main/java/xyz/zhouxy/plusone/commons/util/SnowflakeIdGenerator.java index 383a6dd..c89a4bc 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/SnowflakeIdGenerator.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/SnowflakeIdGenerator.java @@ -19,7 +19,6 @@ package xyz.zhouxy.plusone.commons.util; import java.util.concurrent.TimeUnit; import com.google.common.annotations.Beta; -import com.google.common.base.Preconditions; /** * Twitter_Snowflake @@ -80,9 +79,9 @@ public class SnowflakeIdGenerator { * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdGenerator(final long workerId, final long datacenterId) { - Preconditions.checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0), + AssertTools.checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0), "WorkerId can't be greater than %s or less than 0.", MAX_WORKER_ID); - Preconditions.checkArgument((datacenterId <= MAX_DATACENTER_ID && datacenterId >= 0), + AssertTools.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); diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java b/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java index 83b8af9..10e8e7a 100644 --- a/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/TreeBuilder.java @@ -27,8 +27,6 @@ import java.util.stream.Collectors; import javax.annotation.Nullable; -import com.google.common.base.Preconditions; - /** * TreeBuilder * @@ -63,7 +61,7 @@ public class TreeBuilder { * @param nodes 平铺的节点列表 */ public List buildTree(Collection nodes) { - Preconditions.checkNotNull(nodes); + AssertTools.checkNotNull(nodes); return buildTreeInternal(nodes, this.defaultComparator); } @@ -80,7 +78,7 @@ public class TreeBuilder { * 仅影响调用 addChild 的顺序,如果操作对象本身对应的控制了子节点的顺序,无法影响其相关逻辑。 */ public List buildTree(Collection nodes, @Nullable Comparator comparator) { - Preconditions.checkNotNull(nodes); + AssertTools.checkNotNull(nodes); final Comparator c = (comparator != null) ? comparator : this.defaultComparator; return buildTreeInternal(nodes, c); }