refactor: AssertTools 类中的方法使用静态导入

This commit is contained in:
zhouxy108 2025-06-12 16:11:53 +08:00
parent 8eac9054cd
commit e2e5f50162
19 changed files with 148 additions and 120 deletions

View File

@ -15,6 +15,8 @@
*/ */
package xyz.zhouxy.plusone.commons.gson.adapter; package xyz.zhouxy.plusone.commons.gson.adapter;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
@ -28,8 +30,6 @@ import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/** /**
* 包含 JSR-310 相关数据类型的 {@code TypeAdapter} * 包含 JSR-310 相关数据类型的 {@code TypeAdapter}
* *
@ -145,7 +145,7 @@ public class JSR310TypeAdapters {
protected TemporalAccessorTypeAdapter( protected TemporalAccessorTypeAdapter(
TemporalQuery<T> temporalQuery, DateTimeFormatter dateTimeFormatter) { TemporalQuery<T> temporalQuery, DateTimeFormatter dateTimeFormatter) {
AssertTools.checkArgumentNotNull(dateTimeFormatter, "formatter must not be null."); checkArgumentNotNull(dateTimeFormatter, "formatter must not be null.");
this.temporalQuery = temporalQuery; this.temporalQuery = temporalQuery;
this.dateTimeFormatter = dateTimeFormatter; this.dateTimeFormatter = dateTimeFormatter;
} }

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.model; package xyz.zhouxy.plusone.commons.model;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -32,7 +34,6 @@ import com.google.errorprone.annotations.Immutable;
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod; import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
import xyz.zhouxy.plusone.commons.annotation.ValueObject; import xyz.zhouxy.plusone.commons.annotation.ValueObject;
import xyz.zhouxy.plusone.commons.constant.PatternConsts; import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.StringTools; import xyz.zhouxy.plusone.commons.util.StringTools;
/** /**
@ -86,13 +87,13 @@ public class Chinese2ndGenIDCardNumber
*/ */
public static Chinese2ndGenIDCardNumber of(final String idCardNumber) { public static Chinese2ndGenIDCardNumber of(final String idCardNumber) {
try { try {
AssertTools.checkArgument(StringTools.isNotBlank(idCardNumber), "二代居民身份证校验失败:号码为空"); checkArgument(StringTools.isNotBlank(idCardNumber), "二代居民身份证校验失败:号码为空");
final String value = idCardNumber.toUpperCase(); final String value = idCardNumber.toUpperCase();
final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(value); final Matcher matcher = PatternConsts.CHINESE_2ND_ID_CARD_NUMBER.matcher(value);
AssertTools.checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value); checkArgument(matcher.matches(), () -> "二代居民身份证校验失败:" + value);
final String provinceCode = matcher.group("province"); final String provinceCode = matcher.group("province");
AssertTools.checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode)); checkArgument(Chinese2ndGenIDCardNumber.PROVINCE_CODES.containsKey(provinceCode));
final String cityCode = matcher.group("city"); final String cityCode = matcher.group("city");
final String countyCode = matcher.group("county"); final String countyCode = matcher.group("county");

View File

@ -16,8 +16,9 @@
package xyz.zhouxy.plusone.commons.model; package xyz.zhouxy.plusone.commons.model;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
import xyz.zhouxy.plusone.commons.base.IWithIntCode; import xyz.zhouxy.plusone.commons.base.IWithIntCode;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/** /**
* 性别 * 性别
@ -50,7 +51,7 @@ public enum Gender implements IWithIntCode {
* @return 枚举值 * @return 枚举值
*/ */
public static Gender of(int value) { public static Gender of(int value) {
AssertTools.checkCondition(0 <= value && value < VALUES.length, checkCondition(0 <= value && value < VALUES.length,
() -> new EnumConstantNotPresentException(Gender.class, String.valueOf(value))); () -> new EnumConstantNotPresentException(Gender.class, String.valueOf(value)));
return VALUES[value]; return VALUES[value];
} }

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.model; package xyz.zhouxy.plusone.commons.model;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.util.Objects; import java.util.Objects;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -25,7 +27,6 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import xyz.zhouxy.plusone.commons.annotation.ReaderMethod; import xyz.zhouxy.plusone.commons.annotation.ReaderMethod;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/** /**
* 带校验的字符串值对象 * 带校验的字符串值对象
@ -74,10 +75,10 @@ public abstract class ValidatableStringRecord<T extends ValidatableStringRecord<
* @param errorMessage 正则不匹配时的错误信息 * @param errorMessage 正则不匹配时的错误信息
*/ */
protected ValidatableStringRecord(String value, Pattern pattern, String errorMessage) { protected ValidatableStringRecord(String value, Pattern pattern, String errorMessage) {
AssertTools.checkArgument(Objects.nonNull(value), "The value cannot be null."); checkArgument(Objects.nonNull(value), "The value cannot be null.");
AssertTools.checkArgument(Objects.nonNull(pattern), "The pattern cannot be null."); checkArgument(Objects.nonNull(pattern), "The pattern cannot be null.");
this.matcher = pattern.matcher(value); this.matcher = pattern.matcher(value);
AssertTools.checkArgument(this.matcher.matches(), errorMessage); checkArgument(this.matcher.matches(), errorMessage);
this.value = value; this.value = value;
} }

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.model.dto; package xyz.zhouxy.plusone.commons.model.dto;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -27,7 +29,6 @@ import com.google.common.collect.ImmutableMap;
import xyz.zhouxy.plusone.commons.annotation.Virtual; import xyz.zhouxy.plusone.commons.annotation.Virtual;
import xyz.zhouxy.plusone.commons.collection.CollectionTools; import xyz.zhouxy.plusone.commons.collection.CollectionTools;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.RegexTools; import xyz.zhouxy.plusone.commons.util.RegexTools;
import xyz.zhouxy.plusone.commons.util.StringTools; import xyz.zhouxy.plusone.commons.util.StringTools;
@ -60,10 +61,10 @@ public class PagingAndSortingQueryParams {
* @param sortableProperties 可排序的属性不可为空 * @param sortableProperties 可排序的属性不可为空
*/ */
public PagingAndSortingQueryParams(Map<String, String> sortableProperties) { public PagingAndSortingQueryParams(Map<String, String> sortableProperties) {
AssertTools.checkArgument(CollectionTools.isNotEmpty(sortableProperties), checkArgument(CollectionTools.isNotEmpty(sortableProperties),
"Sortable properties can not be empty."); "Sortable properties can not be empty.");
sortableProperties.forEach((k, v) -> sortableProperties.forEach((k, v) ->
AssertTools.checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v), checkArgument(StringTools.isNotBlank(k) && StringTools.isNotBlank(v),
"Property name must not be blank.")); "Property name must not be blank."));
this.sortableProperties = ImmutableMap.copyOf(sortableProperties); this.sortableProperties = ImmutableMap.copyOf(sortableProperties);
} }
@ -107,7 +108,7 @@ public class PagingAndSortingQueryParams {
public final PagingParams buildPagingParams() { public final PagingParams buildPagingParams() {
final int sizeValue = this.size != null ? this.size : defaultSizeInternal(); final int sizeValue = this.size != null ? this.size : defaultSizeInternal();
final long pageNumValue = this.pageNum != null ? this.pageNum : 1L; final long pageNumValue = this.pageNum != null ? this.pageNum : 1L;
AssertTools.checkArgument(CollectionTools.isNotEmpty(this.orderBy), checkArgument(CollectionTools.isNotEmpty(this.orderBy),
"The 'orderBy' cannot be empty"); "The 'orderBy' cannot be empty");
final List<SortableProperty> propertiesToSort = this.orderBy.stream() final List<SortableProperty> propertiesToSort = this.orderBy.stream()
.map(this::generateSortableProperty) .map(this::generateSortableProperty)
@ -138,13 +139,13 @@ public class PagingAndSortingQueryParams {
} }
private SortableProperty generateSortableProperty(String orderByStr) { private SortableProperty generateSortableProperty(String orderByStr) {
AssertTools.checkArgument(StringTools.isNotBlank(orderByStr)); checkArgument(StringTools.isNotBlank(orderByStr));
AssertTools.checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN)); checkArgument(RegexTools.matches(orderByStr, SORT_STR_PATTERN));
String[] propertyNameAndOrderType = orderByStr.split("-"); String[] propertyNameAndOrderType = orderByStr.split("-");
AssertTools.checkArgument(propertyNameAndOrderType.length == 2); checkArgument(propertyNameAndOrderType.length == 2);
String propertyName = propertyNameAndOrderType[0]; String propertyName = propertyNameAndOrderType[0];
AssertTools.checkArgument(sortableProperties.containsKey(propertyName), checkArgument(sortableProperties.containsKey(propertyName),
"The property name must be in the set of sortable properties."); "The property name must be in the set of sortable properties.");
String columnName = sortableProperties.get(propertyName); String columnName = sortableProperties.get(propertyName);
String orderType = propertyNameAndOrderType[1]; String orderType = propertyNameAndOrderType[1];
@ -164,7 +165,7 @@ public class PagingAndSortingQueryParams {
SortableProperty(String propertyName, String columnName, String orderType) { SortableProperty(String propertyName, String columnName, String orderType) {
this.propertyName = propertyName; this.propertyName = propertyName;
this.columnName = columnName; this.columnName = columnName;
AssertTools.checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType)); checkArgument("ASC".equalsIgnoreCase(orderType) || "DESC".equalsIgnoreCase(orderType));
this.orderType = orderType.toUpperCase(); this.orderType = orderType.toUpperCase();
this.sqlSnippet = this.propertyName + " " + this.orderType; this.sqlSnippet = this.propertyName + " " + this.orderType;

View File

@ -16,6 +16,9 @@
package xyz.zhouxy.plusone.commons.time; package xyz.zhouxy.plusone.commons.time;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
import java.time.DateTimeException; import java.time.DateTimeException;
import java.time.Month; import java.time.Month;
import java.time.MonthDay; import java.time.MonthDay;
@ -25,7 +28,6 @@ import com.google.common.collect.Range;
import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod; import xyz.zhouxy.plusone.commons.annotation.StaticFactoryMethod;
import xyz.zhouxy.plusone.commons.base.IWithIntCode; import xyz.zhouxy.plusone.commons.base.IWithIntCode;
import xyz.zhouxy.plusone.commons.util.AssertTools;
/** /**
* 季度 * 季度
@ -89,7 +91,7 @@ public enum Quarter implements IWithIntCode {
*/ */
@StaticFactoryMethod(Quarter.class) @StaticFactoryMethod(Quarter.class)
public static Quarter fromMonth(Month month) { public static Quarter fromMonth(Month month) {
AssertTools.checkNotNull(month); checkNotNull(month);
final int monthValue = month.getValue(); final int monthValue = month.getValue();
return of(computeQuarterValueInternal(monthValue)); return of(computeQuarterValueInternal(monthValue));
} }
@ -246,7 +248,7 @@ public enum Quarter implements IWithIntCode {
* @throws DateTimeException 如果给定的季度值不在有效范围内1到4将抛出异常 * @throws DateTimeException 如果给定的季度值不在有效范围内1到4将抛出异常
*/ */
public static int checkValidIntValue(int value) { public static int checkValidIntValue(int value) {
AssertTools.checkCondition(value >= 1 && value <= 4, checkCondition(value >= 1 && value <= 4,
() -> new DateTimeException("Invalid value for Quarter: " + value)); () -> new DateTimeException("Invalid value for Quarter: " + value));
return value; return value;
} }

View File

@ -17,6 +17,7 @@
package xyz.zhouxy.plusone.commons.time; package xyz.zhouxy.plusone.commons.time;
import static java.time.temporal.ChronoField.YEAR; import static java.time.temporal.ChronoField.YEAR;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.io.Serializable; import java.io.Serializable;
import java.time.LocalDate; import java.time.LocalDate;
@ -32,7 +33,6 @@ import javax.annotation.Nullable;
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;
/** /**
* 表示年份与季度 * 表示年份与季度
@ -93,7 +93,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/ */
@StaticFactoryMethod(YearQuarter.class) @StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(LocalDate date) { public static YearQuarter of(LocalDate date) {
AssertTools.checkNotNull(date); checkNotNull(date);
return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth())); return new YearQuarter(date.getYear(), Quarter.fromMonth(date.getMonth()));
} }
@ -105,7 +105,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/ */
@StaticFactoryMethod(YearQuarter.class) @StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(Date date) { public static YearQuarter of(Date date) {
AssertTools.checkNotNull(date); checkNotNull(date);
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
final int yearValue = YEAR.checkValidIntValue(date.getYear() + 1900L); final int yearValue = YEAR.checkValidIntValue(date.getYear() + 1900L);
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -121,7 +121,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/ */
@StaticFactoryMethod(YearQuarter.class) @StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(Calendar date) { public static YearQuarter of(Calendar date) {
AssertTools.checkNotNull(date); checkNotNull(date);
final int yearValue = ChronoField.YEAR.checkValidIntValue(date.get(Calendar.YEAR)); final int yearValue = ChronoField.YEAR.checkValidIntValue(date.get(Calendar.YEAR));
final int monthValue = date.get(Calendar.MONTH) + 1; final int monthValue = date.get(Calendar.MONTH) + 1;
return new YearQuarter(yearValue, Quarter.fromMonth(monthValue)); return new YearQuarter(yearValue, Quarter.fromMonth(monthValue));
@ -135,7 +135,7 @@ public final class YearQuarter implements Comparable<YearQuarter>, Serializable
*/ */
@StaticFactoryMethod(YearQuarter.class) @StaticFactoryMethod(YearQuarter.class)
public static YearQuarter of(YearMonth yearMonth) { public static YearQuarter of(YearMonth yearMonth) {
AssertTools.checkNotNull(yearMonth); checkNotNull(yearMonth);
return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth())); return of(yearMonth.getYear(), Quarter.fromMonth(yearMonth.getMonth()));
} }

View File

@ -16,6 +16,9 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -258,7 +261,7 @@ public class ArrayTools {
* @throws IllegalArgumentException 当参数为空时抛出 * @throws IllegalArgumentException 当参数为空时抛出
*/ */
public static <T> boolean isAllElementsNotNull(final T[] arr) { public static <T> boolean isAllElementsNotNull(final T[] arr) {
AssertTools.checkArgument(arr != null, "The array cannot be null."); checkArgument(arr != null, "The array cannot be null.");
return Arrays.stream(arr).allMatch(Objects::nonNull); return Arrays.stream(arr).allMatch(Objects::nonNull);
} }
@ -488,10 +491,10 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static char[] repeat(char[] arr, int times, int maxLength) { public static char[] repeat(char[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr)); checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero"); "The max length must be greater than or equal to zero");
if (times == 0) { if (times == 0) {
return EMPTY_CHAR_ARRAY; return EMPTY_CHAR_ARRAY;
@ -523,10 +526,10 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static byte[] repeat(byte[] arr, int times, int maxLength) { public static byte[] repeat(byte[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr)); checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero"); "The max length must be greater than or equal to zero");
if (times == 0) { if (times == 0) {
return EMPTY_BYTE_ARRAY; return EMPTY_BYTE_ARRAY;
@ -558,10 +561,10 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static short[] repeat(short[] arr, int times, int maxLength) { public static short[] repeat(short[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr)); checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero"); "The max length must be greater than or equal to zero");
if (times == 0) { if (times == 0) {
return EMPTY_SHORT_ARRAY; return EMPTY_SHORT_ARRAY;
@ -593,10 +596,10 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static int[] repeat(int[] arr, int times, int maxLength) { public static int[] repeat(int[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr)); checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero"); "The max length must be greater than or equal to zero");
if (times == 0) { if (times == 0) {
return EMPTY_INT_ARRAY; return EMPTY_INT_ARRAY;
@ -628,10 +631,10 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static long[] repeat(long[] arr, int times, int maxLength) { public static long[] repeat(long[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr)); checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero"); "The max length must be greater than or equal to zero");
if (times == 0) { if (times == 0) {
return EMPTY_LONG_ARRAY; return EMPTY_LONG_ARRAY;
@ -663,10 +666,10 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static float[] repeat(float[] arr, int times, int maxLength) { public static float[] repeat(float[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr)); checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero"); "The max length must be greater than or equal to zero");
if (times == 0) { if (times == 0) {
return EMPTY_FLOAT_ARRAY; return EMPTY_FLOAT_ARRAY;
@ -698,10 +701,10 @@ public class ArrayTools {
* @return 重复后的数组 * @return 重复后的数组
*/ */
public static double[] repeat(double[] arr, int times, int maxLength) { public static double[] repeat(double[] arr, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(arr)); checkArgument(Objects.nonNull(arr));
AssertTools.checkArgument(times >= 0, checkArgument(times >= 0,
"The number of times must be greater than or equal to zero"); "The number of times must be greater than or equal to zero");
AssertTools.checkArgument(maxLength >= 0, checkArgument(maxLength >= 0,
"The max length must be greater than or equal to zero"); "The max length must be greater than or equal to zero");
if (times == 0) { if (times == 0) {
return EMPTY_DOUBLE_ARRAY; return EMPTY_DOUBLE_ARRAY;
@ -747,7 +750,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) { public static void fill(char[] a, int fromIndex, int toIndex, @Nullable char[] values) {
AssertTools.checkArgument(Objects.nonNull(a)); checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@ -790,7 +793,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) { public static void fill(byte[] a, int fromIndex, int toIndex, @Nullable byte[] values) {
AssertTools.checkArgument(Objects.nonNull(a)); checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@ -833,7 +836,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) { public static void fill(short[] a, int fromIndex, int toIndex, @Nullable short[] values) {
AssertTools.checkArgument(Objects.nonNull(a)); checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@ -876,7 +879,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) { public static void fill(int[] a, int fromIndex, int toIndex, @Nullable int[] values) {
AssertTools.checkArgument(Objects.nonNull(a)); checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@ -919,7 +922,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) { public static void fill(long[] a, int fromIndex, int toIndex, @Nullable long[] values) {
AssertTools.checkArgument(Objects.nonNull(a)); checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@ -962,7 +965,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) { public static void fill(float[] a, int fromIndex, int toIndex, @Nullable float[] values) {
AssertTools.checkArgument(Objects.nonNull(a)); checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@ -1005,7 +1008,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) { public static void fill(double[] a, int fromIndex, int toIndex, @Nullable double[] values) {
AssertTools.checkArgument(Objects.nonNull(a)); checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@ -1060,7 +1063,7 @@ public class ArrayTools {
* @param values 填充内容 * @param values 填充内容
*/ */
private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) { private static <T> void fillInternal(T[] a, int fromIndex, int toIndex, @Nullable T[] values) {
AssertTools.checkArgument(Objects.nonNull(a)); checkArgument(Objects.nonNull(a));
if (values == null || values.length == 0) { if (values == null || values.length == 0) {
return; return;
} }
@ -1087,7 +1090,7 @@ public class ArrayTools {
// #region - indexOf // #region - indexOf
public static <T> int indexOf(@Nullable T[] arr, Predicate<? super T> predicate) { public static <T> int indexOf(@Nullable T[] arr, Predicate<? super T> predicate) {
AssertTools.checkNotNull(predicate); checkNotNull(predicate);
if (arr == null || arr.length == 0) { if (arr == null || arr.length == 0) {
return NOT_FOUND_INDEX; return NOT_FOUND_INDEX;
} }
@ -1192,7 +1195,7 @@ public class ArrayTools {
// #region - lastIndexOf // #region - lastIndexOf
public static <T> int lastIndexOf(@Nullable T[] arr, Predicate<? super T> predicate) { public static <T> int lastIndexOf(@Nullable T[] arr, Predicate<? super T> predicate) {
AssertTools.checkNotNull(predicate); checkNotNull(predicate);
if (arr == null || arr.length == 0) { if (arr == null || arr.length == 0) {
return NOT_FOUND_INDEX; return NOT_FOUND_INDEX;
} }

View File

@ -31,11 +31,11 @@ import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
* 本工具类不封装过多判断逻辑鼓励充分使用项目中的工具类进行逻辑判断 * 本工具类不封装过多判断逻辑鼓励充分使用项目中的工具类进行逻辑判断
* *
* <pre> * <pre>
* AssertTools.checkArgument(StringUtils.hasText(str), "The argument cannot be blank."); * checkArgument(StringUtils.hasText(str), "The argument cannot be blank.");
* AssertTools.checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty."); * checkState(ArrayUtils.isNotEmpty(result), "The result cannot be empty.");
* AssertTools.checkCondition(!CollectionUtils.isEmpty(roles), * checkCondition(!CollectionUtils.isEmpty(roles),
* () -> new InvalidInputException("The roles cannot be empty.")); * () -> new InvalidInputException("The roles cannot be empty."));
* AssertTools.checkCondition(RegexTools.matches(email, PatternConsts.EMAIL), * checkCondition(RegexTools.matches(email, PatternConsts.EMAIL),
* "must be a well-formed email address"); * "must be a well-formed email address");
* </pre> * </pre>
* *

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.math.BigDecimal; import java.math.BigDecimal;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -53,8 +55,8 @@ public class BigDecimals {
* @return {@code a} 大于 {@code b} 时返回 {@code true} * @return {@code a} 大于 {@code b} 时返回 {@code true}
*/ */
public static boolean gt(BigDecimal a, BigDecimal b) { public static boolean gt(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null."); checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null."); checkNotNull(b, "Parameter could not be null.");
return (a != b) && (a.compareTo(b) > 0); return (a != b) && (a.compareTo(b) > 0);
} }
@ -66,8 +68,8 @@ public class BigDecimals {
* @return {@code a} 大于等于 {@code b} 时返回 {@code true} * @return {@code a} 大于等于 {@code b} 时返回 {@code true}
*/ */
public static boolean ge(BigDecimal a, BigDecimal b) { public static boolean ge(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null."); checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null."); checkNotNull(b, "Parameter could not be null.");
return (a == b) || (a.compareTo(b) >= 0); return (a == b) || (a.compareTo(b) >= 0);
} }
@ -79,8 +81,8 @@ public class BigDecimals {
* @return {@code a} 小于 {@code b} 时返回 {@code true} * @return {@code a} 小于 {@code b} 时返回 {@code true}
*/ */
public static boolean lt(BigDecimal a, BigDecimal b) { public static boolean lt(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null."); checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null."); checkNotNull(b, "Parameter could not be null.");
return (a != b) && (a.compareTo(b) < 0); return (a != b) && (a.compareTo(b) < 0);
} }
@ -92,8 +94,8 @@ public class BigDecimals {
* @return {@code a} 小于等于 {@code b} 时返回 {@code true} * @return {@code a} 小于等于 {@code b} 时返回 {@code true}
*/ */
public static boolean le(BigDecimal a, BigDecimal b) { public static boolean le(BigDecimal a, BigDecimal b) {
AssertTools.checkNotNull(a, "Parameter could not be null."); checkNotNull(a, "Parameter could not be null.");
AssertTools.checkNotNull(b, "Parameter could not be null."); checkNotNull(b, "Parameter could not be null.");
return (a == b) || (a.compareTo(b) <= 0); return (a == b) || (a.compareTo(b) <= 0);
} }

View File

@ -16,6 +16,9 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkCondition;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.util.function.Supplier; import java.util.function.Supplier;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -43,7 +46,7 @@ public final class EnumTools {
@Deprecated @Deprecated
private static <E extends Enum<?>> E valueOfInternal(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用但不删掉 private static <E extends Enum<?>> E valueOfInternal(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用但不删掉
E[] values = enumType.getEnumConstants(); E[] values = enumType.getEnumConstants();
AssertTools.checkCondition((ordinal >= 0 && ordinal < values.length), checkCondition((ordinal >= 0 && ordinal < values.length),
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal))); () -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
return values[ordinal]; return values[ordinal];
} }
@ -59,7 +62,7 @@ public final class EnumTools {
*/ */
@Deprecated @Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用但不删掉 public static <E extends Enum<?>> E valueOf(Class<E> enumType, int ordinal) { // NOSONAR 该方法弃用但不删掉
AssertTools.checkNotNull(enumType, "Enum type must not be null."); checkNotNull(enumType, "Enum type must not be null.");
return valueOfInternal(enumType, ordinal); return valueOfInternal(enumType, ordinal);
} }
@ -76,7 +79,7 @@ public final class EnumTools {
@Deprecated @Deprecated
public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用但不删掉 public static <E extends Enum<?>> E valueOf(Class<E> enumType, // NOSONAR 该方法弃用但不删掉
@Nullable Integer ordinal, @Nullable E defaultValue) { @Nullable Integer ordinal, @Nullable E defaultValue) {
AssertTools.checkNotNull(enumType); checkNotNull(enumType);
return null == ordinal ? defaultValue : valueOfInternal(enumType, ordinal); return null == ordinal ? defaultValue : valueOfInternal(enumType, ordinal);
} }
@ -95,8 +98,8 @@ public final class EnumTools {
Class<E> enumType, Class<E> enumType,
@Nullable Integer ordinal, @Nullable Integer ordinal,
Supplier<E> defaultValue) { Supplier<E> defaultValue) {
AssertTools.checkNotNull(enumType); checkNotNull(enumType);
AssertTools.checkNotNull(defaultValue); checkNotNull(defaultValue);
return null == ordinal ? defaultValue.get() : valueOfInternal(enumType, ordinal); return null == ordinal ? defaultValue.get() : valueOfInternal(enumType, ordinal);
} }
@ -112,7 +115,7 @@ public final class EnumTools {
@Deprecated @Deprecated
public static <E extends Enum<?>> E getValueOrDefault(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用但不删掉 public static <E extends Enum<?>> E getValueOrDefault(Class<E> enumType, @Nullable Integer ordinal) { // NOSONAR 该方法弃用但不删掉
return getValueOrDefault(enumType, ordinal, () -> { return getValueOrDefault(enumType, ordinal, () -> {
AssertTools.checkNotNull(enumType, "Enum type must not be null."); checkNotNull(enumType, "Enum type must not be null.");
E[] values = enumType.getEnumConstants(); E[] values = enumType.getEnumConstants();
return values[0]; return values[0];
}); });
@ -133,10 +136,10 @@ public final class EnumTools {
} }
public static <E extends Enum<?>> Integer checkOrdinal(Class<E> enumType, Integer ordinal) { public static <E extends Enum<?>> Integer checkOrdinal(Class<E> enumType, Integer ordinal) {
AssertTools.checkNotNull(enumType, "Enum type must not be null."); checkNotNull(enumType, "Enum type must not be null.");
AssertTools.checkNotNull(ordinal, "Ordinal must not be null."); checkNotNull(ordinal, "Ordinal must not be null.");
E[] values = enumType.getEnumConstants(); E[] values = enumType.getEnumConstants();
AssertTools.checkCondition(ordinal >= 0 && ordinal < values.length, checkCondition(ordinal >= 0 && ordinal < values.length,
() -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal))); () -> new EnumConstantNotPresentException(enumType, Integer.toString(ordinal)));
return ordinal; return ordinal;
} }
@ -180,7 +183,7 @@ public final class EnumTools {
Class<E> enumType, Class<E> enumType,
@Nullable Integer ordinal, @Nullable Integer ordinal,
@Nullable Integer defaultValue) { @Nullable Integer defaultValue) {
AssertTools.checkNotNull(enumType); checkNotNull(enumType);
return checkOrdinalOrGetInternal(enumType, ordinal, () -> checkOrdinalOrDefaultInternal(enumType, defaultValue, null)); return checkOrdinalOrGetInternal(enumType, ordinal, () -> checkOrdinalOrDefaultInternal(enumType, defaultValue, null));
} }

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -44,7 +46,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
protected final String name; protected final String name;
protected Enumeration(final int id, final String name) { protected Enumeration(final int id, final String name) {
AssertTools.checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text."); checkArgument(StringTools.isNotBlank(name), "Name of enumeration must has text.");
this.id = id; this.id = id;
this.name = name; this.name = name;
} }
@ -126,7 +128,7 @@ public abstract class Enumeration<T extends Enumeration<T>> // NOSONAR 暂不移
* @return 枚举对象 * @return 枚举对象
*/ */
public T get(int id) { public T get(int id) {
AssertTools.checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id); checkArgument(this.valueMap.containsKey(id), "[%s] 对应的值不存在", id);
return this.valueMap.get(id); return this.valueMap.get(id);
} }

View File

@ -16,8 +16,9 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgumentNotNull;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -69,7 +70,7 @@ public class IdGenerator {
* @return UUID 字符串 * @return UUID 字符串
*/ */
public static String toSimpleString(UUID uuid) { public static String toSimpleString(UUID uuid) {
AssertTools.checkArgument(Objects.nonNull(uuid)); checkArgumentNotNull(uuid);
return (uuidDigits(uuid.getMostSignificantBits() >> 32, 8) + return (uuidDigits(uuid.getMostSignificantBits() >> 32, 8) +
uuidDigits(uuid.getMostSignificantBits() >> 16, 4) + uuidDigits(uuid.getMostSignificantBits() >> 16, 4) +
uuidDigits(uuid.getMostSignificantBits(), 4) + uuidDigits(uuid.getMostSignificantBits(), 4) +

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Objects; import java.util.Objects;
@ -67,21 +69,21 @@ public final class RandomTools {
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomStr(Random random, char[] sourceCharacters, int length) { public static String randomStr(Random random, char[] sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null."); checkArgument(Objects.nonNull(random), "Random cannot be null.");
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(random, sourceCharacters, length); return randomStrInternal(random, sourceCharacters, length);
} }
public static String randomStr(char[] sourceCharacters, int length) { public static String randomStr(char[] sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length); return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
} }
public static String secureRandomStr(char[] sourceCharacters, int length) { public static String secureRandomStr(char[] sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length); return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
} }
@ -96,21 +98,21 @@ public final class RandomTools {
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomStr(Random random, String sourceCharacters, int length) { public static String randomStr(Random random, String sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(random), "Random cannot be null."); checkArgument(Objects.nonNull(random), "Random cannot be null.");
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(random, sourceCharacters, length); return randomStrInternal(random, sourceCharacters, length);
} }
public static String randomStr(String sourceCharacters, int length) { public static String randomStr(String sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length); return randomStrInternal(ThreadLocalRandom.current(), sourceCharacters, length);
} }
public static String secureRandomStr(String sourceCharacters, int length) { public static String secureRandomStr(String sourceCharacters, int length) {
AssertTools.checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null."); checkArgument(Objects.nonNull(sourceCharacters), "Source characters cannot be null.");
AssertTools.checkArgument(length >= 0, "The length should be greater than or equal to zero."); checkArgument(length >= 0, "The length should be greater than or equal to zero.");
return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length); return randomStrInternal(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
} }

View File

@ -16,6 +16,9 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@ -72,7 +75,7 @@ public final class RegexTools {
* @return {@link Pattern} 实例 * @return {@link Pattern} 实例
*/ */
public static Pattern getPattern(final String pattern, final int flags, final boolean cachePattern) { public static Pattern getPattern(final String pattern, final int flags, final boolean cachePattern) {
AssertTools.checkNotNull(pattern); checkNotNull(pattern);
return cachePattern ? cacheAndGetPatternInternal(pattern, flags) : getPatternInternal(pattern, flags); return cachePattern ? cacheAndGetPatternInternal(pattern, flags) : getPatternInternal(pattern, flags);
} }
@ -95,7 +98,7 @@ public final class RegexTools {
*/ */
@Nonnull @Nonnull
public static Pattern getPattern(final String pattern, final int flags) { public static Pattern getPattern(final String pattern, final int flags) {
AssertTools.checkNotNull(pattern); checkNotNull(pattern);
return getPatternInternal(pattern, flags); return getPatternInternal(pattern, flags);
} }
@ -115,7 +118,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) {
AssertTools.checkNotNull(pattern); checkNotNull(pattern);
return matchesInternal(input, pattern); return matchesInternal(input, pattern);
} }
@ -127,7 +130,7 @@ public final class RegexTools {
* @return 判断结果 * @return 判断结果
*/ */
public static boolean matchesAny(@Nullable final CharSequence input, final Pattern[] patterns) { public static boolean matchesAny(@Nullable final CharSequence input, final Pattern[] patterns) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns)); checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return matchesAnyInternal(input, patterns); return matchesAnyInternal(input, patterns);
} }
@ -139,7 +142,7 @@ 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) {
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns)); checkArgument(ArrayTools.isAllElementsNotNull(patterns));
return matchesAllInternal(input, patterns); return matchesAllInternal(input, patterns);
} }
@ -210,8 +213,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) {
AssertTools.checkNotNull(input); checkNotNull(input);
AssertTools.checkNotNull(pattern); checkNotNull(pattern);
return pattern.matcher(input); return pattern.matcher(input);
} }
@ -261,8 +264,8 @@ public final class RegexTools {
* @return 结果 * @return 结果
*/ */
public static Matcher getMatcher(final CharSequence input, final String pattern, final int flags) { public static Matcher getMatcher(final CharSequence input, final String pattern, final int flags) {
AssertTools.checkNotNull(input); checkNotNull(input);
AssertTools.checkNotNull(pattern); checkNotNull(pattern);
return getPatternInternal(pattern, flags).matcher(input); return getPatternInternal(pattern, flags).matcher(input);
} }

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
@ -73,9 +75,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) {
AssertTools.checkArgument((workerId <= MAX_WORKER_ID && workerId >= 0), 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);
AssertTools.checkArgument((datacenterId <= MAX_DATACENTER_ID && datacenterId >= 0), 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

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkArgument;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Objects; import java.util.Objects;
@ -110,7 +112,7 @@ public class StringTools {
* @return 结果 * @return 结果
*/ */
public static String repeat(final String str, int times, int maxLength) { public static String repeat(final String str, int times, int maxLength) {
AssertTools.checkArgument(Objects.nonNull(str)); checkArgument(Objects.nonNull(str));
return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength)); return String.valueOf(ArrayTools.repeat(str.toCharArray(), times, maxLength));
} }
@ -210,8 +212,8 @@ public class StringTools {
if (src == null || src.isEmpty()) { if (src == null || src.isEmpty()) {
return EMPTY_STRING; return EMPTY_STRING;
} }
AssertTools.checkArgument(front >= 0 && end >= 0); checkArgument(front >= 0 && end >= 0);
AssertTools.checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度"); checkArgument((front + end) <= src.length(), "需要截取的长度不能大于原字符串长度");
final char[] charArray = src.toCharArray(); final char[] charArray = src.toCharArray();
for (int i = front; i < charArray.length - end; i++) { for (int i = front; i < charArray.length - end; i++) {
charArray[i] = replacedChar; charArray[i] = replacedChar;

View File

@ -16,6 +16,8 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -76,7 +78,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) {
AssertTools.checkNotNull(nodes); checkNotNull(nodes);
return buildTreeInternal(nodes, this.defaultComparator); return buildTreeInternal(nodes, this.defaultComparator);
} }
@ -93,7 +95,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) {
AssertTools.checkNotNull(nodes); 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);
} }

View File

@ -17,12 +17,12 @@
package xyz.zhouxy.plusone.commons.base; package xyz.zhouxy.plusone.commons.base;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static xyz.zhouxy.plusone.commons.util.AssertTools.checkNotNull;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import xyz.zhouxy.plusone.commons.util.AssertTools;
class IWithCodeTests { class IWithCodeTests {
@ -91,7 +91,7 @@ class IWithCodeTests {
private final String code; private final String code;
WithCode(String code) { WithCode(String code) {
AssertTools.checkNotNull(code); checkNotNull(code);
this.code = code; this.code = code;
} }