使用 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 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> successResult, final Supplier<ErrorResult> 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> successResult, final Supplier<ErrorResult> 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();
}

View File

@ -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<YearQuarter>, 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);

View File

@ -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);
}

View File

@ -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);

View File

@ -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<T, TSubTree extends T, TIdentity> {
* @param nodes
*/
public List<T> buildTree(Collection<T> nodes) {
Preconditions.checkNotNull(nodes);
AssertTools.checkNotNull(nodes);
return buildTreeInternal(nodes, this.defaultComparator);
}
@ -80,7 +78,7 @@ public class TreeBuilder<T, TSubTree extends T, TIdentity> {
* <b> addChild </b>
*/
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;
return buildTreeInternal(nodes, c);
}