使用 AssertTools 替代 Preconditions
parent
10761e92ec
commit
b4115aae95
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue