使用 AssertTools 替代 Preconditions

pull/1/head
ZhouXY108 2024-12-04 01:17:20 +08:00
parent 10761e92ec
commit b4115aae95
5 changed files with 40 additions and 45 deletions

View File

@ -22,7 +22,7 @@ import java.util.function.Supplier;
import javax.annotation.Nullable; 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, public static UnifiedResponse of(final boolean isSuccess,
final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) { final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) {
Preconditions.checkNotNull(successResult, "Success supplier must not be null."); AssertTools.checkNotNull(successResult, "Success supplier must not be null.");
Preconditions.checkNotNull(errorResult, "Error supplier must not be null."); AssertTools.checkNotNull(errorResult, "Error supplier must not be null.");
return isSuccess ? successResult.get() : errorResult.get(); return isSuccess ? successResult.get() : errorResult.get();
} }
public static UnifiedResponse of(final BooleanSupplier isSuccess, public static UnifiedResponse of(final BooleanSupplier isSuccess,
final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) { final Supplier<SuccessResult> successResult, final Supplier<ErrorResult> errorResult) {
Preconditions.checkNotNull(isSuccess, "Conditions for success must not be null."); AssertTools.checkNotNull(isSuccess, "Conditions for success must not be null.");
Preconditions.checkNotNull(successResult, "Success supplier must not be null."); AssertTools.checkNotNull(successResult, "Success supplier must not be null.");
Preconditions.checkNotNull(errorResult, "Error supplier must not be null."); AssertTools.checkNotNull(errorResult, "Error supplier must not be null.");
return isSuccess.getAsBoolean() ? successResult.get() : errorResult.get(); return isSuccess.getAsBoolean() ? successResult.get() : errorResult.get();
} }

View File

@ -28,10 +28,10 @@ import java.util.Objects;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.Immutable; import com.google.errorprone.annotations.Immutable;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/** /**
* *
@ -52,7 +52,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
private final LocalDate lastDate; private final LocalDate lastDate;
private YearQuarter(int year, @Nonnull Quarter quarter) { 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.year = year;
this.quarter = quarter; this.quarter = quarter;
this.firstDate = quarter.firstMonthDay().atYear(year); this.firstDate = quarter.firstMonthDay().atYear(year);

View File

@ -26,8 +26,6 @@ import java.util.regex.Pattern;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
/** /**
* {@link Pattern} 256 * {@link Pattern} 256
* *
@ -49,7 +47,7 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
public static Pattern getPattern(final String pattern, final boolean cachePattern) { public static Pattern getPattern(final String pattern, final boolean cachePattern) {
Preconditions.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
return cachePattern ? cacheAndGetPatternInternal(pattern) : getPatternInternal(pattern); return cachePattern ? cacheAndGetPatternInternal(pattern) : getPatternInternal(pattern);
} }
@ -60,7 +58,7 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
public static Pattern getPattern(final String pattern) { public static Pattern getPattern(final String pattern) {
Preconditions.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
return getPatternInternal(pattern); return getPatternInternal(pattern);
} }
@ -72,8 +70,8 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
public static Pattern[] getPatterns(final String[] patterns, final boolean cachePattern) { public static Pattern[] getPatterns(final String[] patterns, final boolean cachePattern) {
Preconditions.checkNotNull(patterns); AssertTools.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns)); AssertTools.checkArgument(allNotNull(patterns));
return cachePattern return cachePattern
? cacheAndGetPatternsInternal(patterns) ? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns); : getPatternsInternal(patterns);
@ -86,8 +84,8 @@ public final class RegexTools {
* @return {@link Pattern} * @return {@link Pattern}
*/ */
public static Pattern[] getPatterns(final String[] patterns) { public static Pattern[] getPatterns(final String[] patterns) {
Preconditions.checkNotNull(patterns); AssertTools.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns)); AssertTools.checkArgument(allNotNull(patterns));
return getPatternsInternal(patterns); return getPatternsInternal(patterns);
} }
@ -98,7 +96,7 @@ public final class RegexTools {
* @return Pattern {@code null} * @return Pattern {@code null}
*/ */
public static Pattern cachePattern(final Pattern pattern) { 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) { if (PATTERN_CACHE.size() >= MAX_CACHE_SIZE) {
return null; return null;
} }
@ -115,7 +113,7 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) { public static boolean matches(@Nullable final CharSequence input, final Pattern pattern) {
Preconditions.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
return matchesInternal(input, pattern); return matchesInternal(input, pattern);
} }
@ -127,8 +125,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matchesOne(@Nullable final CharSequence input, final Pattern[] patterns) { public static boolean matchesOne(@Nullable final CharSequence input, final Pattern[] patterns) {
Preconditions.checkNotNull(patterns); AssertTools.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns)); AssertTools.checkArgument(allNotNull(patterns));
return matchesOneInternal(input, patterns); return matchesOneInternal(input, patterns);
} }
@ -140,8 +138,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matchesAll(@Nullable final CharSequence input, final Pattern[] patterns) { public static boolean matchesAll(@Nullable final CharSequence input, final Pattern[] patterns) {
Preconditions.checkNotNull(patterns); AssertTools.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns)); AssertTools.checkArgument(allNotNull(patterns));
return matchesAllInternal(input, patterns); return matchesAllInternal(input, patterns);
} }
@ -155,7 +153,7 @@ public final class RegexTools {
*/ */
public static boolean matches(@Nullable final CharSequence input, final String pattern, public static boolean matches(@Nullable final CharSequence input, final String pattern,
final boolean cachePattern) { final boolean cachePattern) {
Preconditions.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
Pattern p = cachePattern Pattern p = cachePattern
? cacheAndGetPatternInternal(pattern) ? cacheAndGetPatternInternal(pattern)
: getPatternInternal(pattern); : getPatternInternal(pattern);
@ -170,7 +168,7 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matches(@Nullable final CharSequence input, final String pattern) { public static boolean matches(@Nullable final CharSequence input, final String pattern) {
Preconditions.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
return matchesInternal(input, getPatternInternal(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, public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns,
final boolean cachePattern) { final boolean cachePattern) {
Preconditions.checkNotNull(patterns); AssertTools.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns)); AssertTools.checkArgument(allNotNull(patterns));
final Pattern[] patternSet = cachePattern final Pattern[] patternSet = cachePattern
? cacheAndGetPatternsInternal(patterns) ? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns); : getPatternsInternal(patterns);
@ -200,8 +198,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns) { public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns) {
Preconditions.checkNotNull(patterns); AssertTools.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns)); AssertTools.checkArgument(allNotNull(patterns));
final Pattern[] patternSet = getPatternsInternal(patterns); final Pattern[] patternSet = getPatternsInternal(patterns);
return matchesOneInternal(input, patternSet); return matchesOneInternal(input, patternSet);
} }
@ -216,8 +214,8 @@ public final class RegexTools {
*/ */
public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns, public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns,
final boolean cachePattern) { final boolean cachePattern) {
Preconditions.checkNotNull(patterns); AssertTools.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns)); AssertTools.checkArgument(allNotNull(patterns));
final Pattern[] patternSet = cachePattern final Pattern[] patternSet = cachePattern
? cacheAndGetPatternsInternal(patterns) ? cacheAndGetPatternsInternal(patterns)
: getPatternsInternal(patterns); : getPatternsInternal(patterns);
@ -232,8 +230,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns) { public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns) {
Preconditions.checkNotNull(patterns); AssertTools.checkNotNull(patterns);
Preconditions.checkArgument(allNotNull(patterns)); AssertTools.checkArgument(allNotNull(patterns));
final Pattern[] patternSet = getPatternsInternal(patterns); final Pattern[] patternSet = getPatternsInternal(patterns);
return matchesAllInternal(input, patternSet); return matchesAllInternal(input, patternSet);
} }
@ -246,8 +244,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static Matcher getMatcher(final CharSequence input, final Pattern pattern) { public static Matcher getMatcher(final CharSequence input, final Pattern pattern) {
Preconditions.checkNotNull(input); AssertTools.checkNotNull(input);
Preconditions.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
return pattern.matcher(input); return pattern.matcher(input);
} }
@ -260,8 +258,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static Matcher getMatcher(final CharSequence input, final String pattern, boolean cachePattern) { public static Matcher getMatcher(final CharSequence input, final String pattern, boolean cachePattern) {
Preconditions.checkNotNull(input); AssertTools.checkNotNull(input);
Preconditions.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
final Pattern p = cachePattern final Pattern p = cachePattern
? cacheAndGetPatternInternal(pattern) ? cacheAndGetPatternInternal(pattern)
: getPatternInternal(pattern); : getPatternInternal(pattern);
@ -276,8 +274,8 @@ public final class RegexTools {
* @return * @return
*/ */
public static Matcher getMatcher(final CharSequence input, final String pattern) { public static Matcher getMatcher(final CharSequence input, final String pattern) {
Preconditions.checkNotNull(input); AssertTools.checkNotNull(input);
Preconditions.checkNotNull(pattern); AssertTools.checkNotNull(pattern);
return getPatternInternal(pattern).matcher(input); return getPatternInternal(pattern).matcher(input);
} }

View File

@ -19,7 +19,6 @@ package xyz.zhouxy.plusone.commons.util;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import com.google.common.annotations.Beta; import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
/** /**
* Twitter_Snowflake * Twitter_Snowflake
@ -80,9 +79,9 @@ public class SnowflakeIdGenerator {
* @param datacenterId ID (0~31) * @param datacenterId ID (0~31)
*/ */
public SnowflakeIdGenerator(final long workerId, final long datacenterId) { 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); "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); "DatacenterId can't be greater than %s or less than 0.", MAX_DATACENTER_ID);
this.datacenterIdAndWorkerId this.datacenterIdAndWorkerId
= (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT); = (datacenterId << DATACENTER_ID_SHIFT) | (workerId << WORKER_ID_SHIFT);

View File

@ -27,8 +27,6 @@ import java.util.stream.Collectors;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.google.common.base.Preconditions;
/** /**
* TreeBuilder * TreeBuilder
* *
@ -63,7 +61,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
* @param nodes * @param nodes
*/ */
public List<T> buildTree(Collection<T> nodes) { public List<T> buildTree(Collection<T> nodes) {
Preconditions.checkNotNull(nodes); AssertTools.checkNotNull(nodes);
return buildTreeInternal(nodes, this.defaultComparator); return buildTreeInternal(nodes, this.defaultComparator);
} }
@ -80,7 +78,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
* <b> addChild </b> * <b> addChild </b>
*/ */
public List<T> buildTree(Collection<T> nodes, @Nullable Comparator<? super T> comparator) { public List<T> buildTree(Collection<T> nodes, @Nullable Comparator<? super T> comparator) {
Preconditions.checkNotNull(nodes); AssertTools.checkNotNull(nodes);
final Comparator<? super T> c = (comparator != null) ? comparator : this.defaultComparator; final Comparator<? super T> c = (comparator != null) ? comparator : this.defaultComparator;
return buildTreeInternal(nodes, c); return buildTreeInternal(nodes, c);
} }