4 Commits

Author SHA1 Message Date
b70e526509 docs: 修改段落问题
Signed-off-by: zhouxy108 <luquanlion@outlook.com>
2025-04-30 22:47:49 +08:00
a2781012be feat: 在 AssertTools 中新增 checkArgumentNotNull 系列方法 2025-04-30 22:44:29 +08:00
9e410029b1 refactor: 修改 AssertTools 中的参数名称 2025-04-30 22:17:18 +08:00
ee7213a687 refactor(util): ArrayTools 中的 isNullOrEmpty 重命名为 isEmpty
保持方法命名的一致性

BREAKING CHANGE: `ArrayTools#isNullOrEmpty` 重命名为 `ArrayTools#isEmpty`
2025-04-29 11:26:12 +08:00
7 changed files with 553 additions and 359 deletions

View File

@@ -198,6 +198,7 @@ throw LoginException.Type.TOKEN_TIMEOUT.create();
- **size** - 每页显示的记录数 - **size** - 每页显示的记录数
- **pageNum** - 当前页码 - **pageNum** - 当前页码
- **orderBy** - 排序条件 - **orderBy** - 排序条件
其中 `orderBy` 是一个 List可以指定多个排序条件每个排序条件是一个字符串 格式为“**属性名-ASC**”或“**属性名-DESC**”,分别表示升序和降序。 其中 `orderBy` 是一个 List可以指定多个排序条件每个排序条件是一个字符串 格式为“**属性名-ASC**”或“**属性名-DESC**”,分别表示升序和降序。
比如前端传入的 orderBy 为 ["name-ASC","age-DESC"],意味着要按 name 进行升序name 相同的情况下则按 age 进行降序。 比如前端传入的 orderBy 为 ["name-ASC","age-DESC"],意味着要按 name 进行升序name 相同的情况下则按 age 进行降序。

View File

@@ -54,9 +54,9 @@ public class ArrayTools {
public static final int NOT_FOUND_INDEX = -1; public static final int NOT_FOUND_INDEX = -1;
// #region - isNullOrEmpty // #region - isEmpty
// isNullOrEmpty // isEmpty
/** /**
* 检查给定数组是否为空 * 检查给定数组是否为空
@@ -65,84 +65,84 @@ public class ArrayTools {
* @param <T> 数组中元素的类型 * @param <T> 数组中元素的类型
* @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false} * @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false}
*/ */
public static <T> boolean isNullOrEmpty(@Nullable T[] arr) { public static <T> boolean isEmpty(@Nullable T[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - char // isEmpty - char
/** /**
* 检查给定数组是否为空 * 检查给定数组是否为空
* *
* @param arr 待检查的数组,可以为 {@code null} * @param arr 待检查的数组,可以为 {@code null}
* @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false} * @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false}
*/ */
public static boolean isNullOrEmpty(@Nullable char[] arr) { public static boolean isEmpty(@Nullable char[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - byte // isEmpty - byte
/** /**
* 检查给定数组是否为空 * 检查给定数组是否为空
* *
* @param arr 待检查的数组,可以为 {@code null} * @param arr 待检查的数组,可以为 {@code null}
* @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false} * @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false}
*/ */
public static boolean isNullOrEmpty(@Nullable byte[] arr) { public static boolean isEmpty(@Nullable byte[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - short // isEmpty - short
/** /**
* 检查给定数组是否为空 * 检查给定数组是否为空
* *
* @param arr 待检查的数组,可以为 {@code null} * @param arr 待检查的数组,可以为 {@code null}
* @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false} * @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false}
*/ */
public static boolean isNullOrEmpty(@Nullable short[] arr) { public static boolean isEmpty(@Nullable short[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - int // isEmpty - int
/** /**
* 检查给定数组是否为空 * 检查给定数组是否为空
* *
* @param arr 待检查的数组,可以为 {@code null} * @param arr 待检查的数组,可以为 {@code null}
* @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false} * @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false}
*/ */
public static boolean isNullOrEmpty(@Nullable int[] arr) { public static boolean isEmpty(@Nullable int[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - long // isEmpty - long
/** /**
* 检查给定数组是否为空 * 检查给定数组是否为空
* *
* @param arr 待检查的数组,可以为 {@code null} * @param arr 待检查的数组,可以为 {@code null}
* @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false} * @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false}
*/ */
public static boolean isNullOrEmpty(@Nullable long[] arr) { public static boolean isEmpty(@Nullable long[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - float // isEmpty - float
/** /**
* 检查给定数组是否为空 * 检查给定数组是否为空
* *
* @param arr 待检查的数组,可以为 {@code null} * @param arr 待检查的数组,可以为 {@code null}
* @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false} * @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false}
*/ */
public static boolean isNullOrEmpty(@Nullable float[] arr) { public static boolean isEmpty(@Nullable float[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - double // isEmpty - double
/** /**
* 检查给定数组是否为空 * 检查给定数组是否为空
* *
* @param arr 待检查的数组,可以为 {@code null} * @param arr 待检查的数组,可以为 {@code null}
* @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false} * @return 如果数组为 {@code null} 或长度为 0则返回 {@code true};否则返回 {@code false}
*/ */
public static boolean isNullOrEmpty(@Nullable double[] arr) { public static boolean isEmpty(@Nullable double[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }

View File

@@ -16,7 +16,6 @@
package xyz.zhouxy.plusone.commons.util; package xyz.zhouxy.plusone.commons.util;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
@@ -63,40 +62,104 @@ public class AssertTools {
* 检查实参 * 检查实参
* *
* @param condition 判断参数是否符合条件的结果 * @param condition 判断参数是否符合条件的结果
* @param errMsg 异常信息 * @param errorMessage 异常信息
* @throws IllegalArgumentException 当条件不满足时抛出 * @throws IllegalArgumentException 当条件不满足时抛出
*/ */
public static void checkArgument(boolean condition, @Nullable String errMsg) { public static void checkArgument(boolean condition, @Nullable String errorMessage) {
checkCondition(condition, () -> new IllegalArgumentException(errMsg)); checkCondition(condition, () -> new IllegalArgumentException(errorMessage));
}
/**
* 检查实参
*
* @param condition 判断参数是否符合条件的结果
* @param messageSupplier 异常信息
* @throws IllegalArgumentException 当条件不满足时抛出
*/
public static void checkArgument(boolean condition, Supplier<String> messageSupplier) {
checkCondition(condition, () -> new IllegalArgumentException(messageSupplier.get()));
} }
/** /**
* 检查实参 * 检查实参
* *
* @param condition 判断参数是否符合条件的结果 * @param condition 判断参数是否符合条件的结果
* @param format 异常信息模板 * @param errorMessageSupplier 异常信息
* @param args 异常信息参数
* @throws IllegalArgumentException 当条件不满足时抛出 * @throws IllegalArgumentException 当条件不满足时抛出
*/ */
public static void checkArgument(boolean condition, String format, Object... args) { public static void checkArgument(boolean condition, Supplier<String> errorMessageSupplier) {
checkCondition(condition, () -> new IllegalArgumentException(String.format(format, args))); checkCondition(condition, () -> new IllegalArgumentException(errorMessageSupplier.get()));
}
/**
* 检查实参
*
* @param condition 判断参数是否符合条件的结果
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
* @throws IllegalArgumentException 当条件不满足时抛出
*/
public static void checkArgument(boolean condition,
String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(condition,
() -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)));
} }
// ================================ // ================================
// #endregion - Argument // #endregion - Argument
// ================================ // ================================
// ================================
// #region - ArgumentNotNull
// ================================
/**
* 判断入参不为 {@code null}
*
* @param <T> 入参类型
* @param obj 入参
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
*/
public static <T> T checkArgumentNotNull(@Nullable T obj) {
checkCondition(obj != null, IllegalArgumentException::new);
return obj;
}
/**
* 判断入参不为 {@code null}
*
* @param <T> 入参类型
* @param obj 入参
* @param errorMessage 异常信息
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
*/
public static <T> T checkArgumentNotNull(@Nullable T obj, String errorMessage) {
checkCondition(obj != null, () -> new IllegalArgumentException(errorMessage));
return obj;
}
/**
* 判断入参不为 {@code null}
*
* @param <T> 入参类型
* @param obj 入参
* @param errorMessageSupplier 异常信息
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
*/
public static <T> T checkArgumentNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) {
checkCondition(obj != null, () -> new IllegalArgumentException(errorMessageSupplier.get()));
return obj;
}
/**
* 判断入参不为 {@code null}
*
* @param <T> 入参类型
* @param obj 入参
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
* @throws IllegalArgumentException 当 {@code obj} 为 {@code null} 时抛出
*/
public static <T> T checkArgumentNotNull(@Nullable T obj,
String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(obj != null,
() -> new IllegalArgumentException(String.format(errorMessageTemplate, errorMessageArgs)));
return obj;
}
// ================================
// #endregion - ArgumentNotNull
// ================================
// ================================ // ================================
// #region - State // #region - State
// ================================ // ================================
@@ -115,38 +178,40 @@ public class AssertTools {
* 检查状态 * 检查状态
* *
* @param condition 判断状态是否符合条件的结果 * @param condition 判断状态是否符合条件的结果
* @param errMsg 异常信息 * @param errorMessage 异常信息
* @throws IllegalStateException 当条件不满足时抛出 * @throws IllegalStateException 当条件不满足时抛出
*/ */
public static void checkState(boolean condition, @Nullable String errMsg) { public static void checkState(boolean condition, @Nullable String errorMessage) {
checkCondition(condition, () -> new IllegalStateException(errMsg)); checkCondition(condition, () -> new IllegalStateException(errorMessage));
}
/**
* 检查状态
*
* @param condition 判断状态是否符合条件的结果
* @param messageSupplier 异常信息
* @throws IllegalStateException 当条件不满足时抛出
*/
public static void checkState(boolean condition, Supplier<String> messageSupplier) {
checkCondition(condition, () -> new IllegalStateException(messageSupplier.get()));
} }
/** /**
* 检查状态 * 检查状态
* *
* @param condition 判断状态是否符合条件的结果 * @param condition 判断状态是否符合条件的结果
* @param format 异常信息模板 * @param errorMessageSupplier 异常信息
* @param args 异常信息参数
* @throws IllegalStateException 当条件不满足时抛出 * @throws IllegalStateException 当条件不满足时抛出
*/ */
public static void checkState(boolean condition, String format, Object... args) { public static void checkState(boolean condition, Supplier<String> errorMessageSupplier) {
checkCondition(condition, () -> new IllegalStateException(String.format(format, args))); checkCondition(condition, () -> new IllegalStateException(errorMessageSupplier.get()));
}
/**
* 检查状态
*
* @param condition 判断状态是否符合条件的结果
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
* @throws IllegalStateException 当条件不满足时抛出
*/
public static void checkState(boolean condition,
String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(condition,
() -> new IllegalStateException(String.format(errorMessageTemplate, errorMessageArgs)));
} }
// ================================ // ================================
// #endregion // #endregion - State
// ================================ // ================================
// ================================ // ================================
@@ -157,7 +222,7 @@ public class AssertTools {
* 判空 * 判空
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * * @param obj 入参
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出 * @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj) { public static <T> void checkNotNull(@Nullable T obj) {
@@ -167,42 +232,44 @@ public class AssertTools {
/** /**
* 判空 * 判空
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param errMsg 异常信息 * * @param errorMessage 异常信息
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出 * @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj, String errMsg) { public static <T> void checkNotNull(@Nullable T obj, String errorMessage) {
checkCondition(obj != null, () -> new NullPointerException(errMsg)); checkCondition(obj != null, () -> new NullPointerException(errorMessage));
} }
/** /**
* 判空 * 判空
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param messageSupplier 异常信息 * * @param errorMessageSupplier 异常信息
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出 * @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj, Supplier<String> messageSupplier) { public static <T> void checkNotNull(@Nullable T obj, Supplier<String> errorMessageSupplier) {
checkCondition(obj != null, () -> new NullPointerException(messageSupplier.get())); checkCondition(obj != null, () -> new NullPointerException(errorMessageSupplier.get()));
} }
/** /**
* 判空 * 判空
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param format 异常信息模板 * @param errorMessageTemplate 异常信息模板
* @param args 异常信息参数 * @param errorMessageArgs 异常信息参数
* @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出 * @throws NullPointerException 当 {@code obj} 为 {@code null} 时抛出
*/ */
public static <T> void checkNotNull(@Nullable T obj, String format, Object... args) { public static <T> void checkNotNull(@Nullable T obj,
checkCondition(obj != null, () -> new NullPointerException(String.format(format, args))); String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(obj != null,
() -> new NullPointerException(String.format(errorMessageTemplate, errorMessageArgs)));
} }
// ================================ // ================================
// #endregion // #endregion - NotNull
// ================================ // ================================
// ================================ // ================================
@@ -219,7 +286,7 @@ public class AssertTools {
*/ */
public static <T> T checkExists(@Nullable T obj) public static <T> T checkExists(@Nullable T obj)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(Objects.nonNull(obj), DataNotExistsException::new); checkCondition(obj != null, DataNotExistsException::new);
return obj; return obj;
} }
@@ -228,51 +295,53 @@ public class AssertTools {
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param message 异常信息 * @param errorMessage 异常信息
* @return 如果 {@code obj} 存在,返回 {@code obj} 本身 * @return 如果 {@code obj} 存在,返回 {@code obj} 本身
* @throws DataNotExistsException 当 {@code obj} 不存在时抛出 * @throws DataNotExistsException 当 {@code obj} 不存在时抛出
*/ */
public static <T> T checkExists(@Nullable T obj, String message) public static <T> T checkExists(@Nullable T obj, String errorMessage)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(message)); checkCondition(obj != null, () -> new DataNotExistsException(errorMessage));
return obj; return obj;
} }
/** /**
* 检查数据是否存在 * 检查数据是否存在
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param messageSupplier 异常信息 * @param errorMessageSupplier 异常信息
* @return 如果 {@code obj} 存在,返回 {@code obj} 本身 * @return 如果 {@code obj} 存在,返回 {@code obj} 本身
* @throws DataNotExistsException 当 {@code obj} 不存在时抛出 * @throws DataNotExistsException 当 {@code obj} 不存在时抛出
*/ */
public static <T> T checkExists(@Nullable T obj, Supplier<String> messageSupplier) public static <T> T checkExists(@Nullable T obj, Supplier<String> errorMessageSupplier)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(messageSupplier.get())); checkCondition(obj != null, () -> new DataNotExistsException(errorMessageSupplier.get()));
return obj; return obj;
} }
/** /**
* 检查数据是否存在 * 检查数据是否存在
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param obj 入参 * @param obj 入参
* @param format 异常信息模板 * @param errorMessageTemplate 异常信息模板
* @param args 异常信息参数 * @param errorMessageArgs 异常信息参数
* @return 如果 {@code obj} 存在,返回 {@code obj} 本身 * @return 如果 {@code obj} 存在,返回 {@code obj} 本身
* @throws DataNotExistsException 当 {@code obj} 不存在时抛出 * @throws DataNotExistsException 当 {@code obj} 不存在时抛出
*/ */
public static <T> T checkExists(@Nullable T obj, String format, Object... args) public static <T> T checkExists(@Nullable T obj,
String errorMessageTemplate, Object... errorMessageArgs)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(Objects.nonNull(obj), () -> new DataNotExistsException(String.format(format, args))); checkCondition(obj != null,
() -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs)));
return obj; return obj;
} }
/** /**
* 检查数据是否存在 * 检查数据是否存在
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param optional 入参 * @param optional 入参
* @return 如果 {@code optional} 存在,返回 {@code optional} 包含的值 * @return 如果 {@code optional} 存在,返回 {@code optional} 包含的值
* @throws DataNotExistsException 当 {@code optional} 的值不存在时抛出 * @throws DataNotExistsException 当 {@code optional} 的值不存在时抛出
@@ -286,46 +355,48 @@ public class AssertTools {
/** /**
* 检查数据是否存在 * 检查数据是否存在
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param optional 入参 * @param optional 入参
* @param message 异常信息 * @param errorMessage 异常信息
* @return 如果 {@code optional} 存在,返回 {@code optional} 包含的值 * @return 如果 {@code optional} 存在,返回 {@code optional} 包含的值
* @throws DataNotExistsException 当 {@code optional} 的值不存在时抛出 * @throws DataNotExistsException 当 {@code optional} 的值不存在时抛出
*/ */
public static <T> T checkExists(Optional<T> optional, String message) public static <T> T checkExists(Optional<T> optional, String errorMessage)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), () -> new DataNotExistsException(message)); checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessage));
return optional.get(); return optional.get();
} }
/** /**
* 检查数据是否存在 * 检查数据是否存在
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param optional 入参 * @param optional 入参
* @param messageSupplier 异常信息 * @param errorMessageSupplier 异常信息
* @return 如果 {@code optional} 存在,返回 {@code optional} 包含的值 * @return 如果 {@code optional} 存在,返回 {@code optional} 包含的值
* @throws DataNotExistsException 当 {@code optional} 的值不存在时抛出 * @throws DataNotExistsException 当 {@code optional} 的值不存在时抛出
*/ */
public static <T> T checkExists(Optional<T> optional, Supplier<String> messageSupplier) public static <T> T checkExists(Optional<T> optional, Supplier<String> errorMessageSupplier)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), () -> new DataNotExistsException(messageSupplier.get())); checkCondition(optional.isPresent(), () -> new DataNotExistsException(errorMessageSupplier.get()));
return optional.get(); return optional.get();
} }
/** /**
* 检查数据是否存在 * 检查数据是否存在
* *
* @param <T> 入参类型 * @param <T> 入参类型
* @param optional 入参 * @param optional 入参
* @param format 异常信息模板 * @param errorMessageTemplate 异常信息模板
* @param args 异常信息参数 * @param errorMessageArgs 异常信息参数
* @return 如果 {@code optional} 存在,返回 {@code optional} 包含的值 * @return 如果 {@code optional} 存在,返回 {@code optional} 包含的值
* @throws DataNotExistsException 当 {@code optional} 的值不存在时抛出 * @throws DataNotExistsException 当 {@code optional} 的值不存在时抛出
*/ */
public static <T> T checkExists(Optional<T> optional, String format, Object... args) public static <T> T checkExists(Optional<T> optional,
String errorMessageTemplate, Object... errorMessageArgs)
throws DataNotExistsException { throws DataNotExistsException {
checkCondition(optional.isPresent(), () -> new DataNotExistsException(String.format(format, args))); checkCondition(optional.isPresent(),
() -> new DataNotExistsException(String.format(errorMessageTemplate, errorMessageArgs)));
return optional.get(); return optional.get();
} }
@@ -341,7 +412,7 @@ public class AssertTools {
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。 * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
* *
* @param expectedValue 预计的数量 * @param expectedValue 预计的数量
* @param result 实际影响的数据量 * @param result 实际影响的数据量
*/ */
public static void checkAffectedRows(int expectedValue, int result) { public static void checkAffectedRows(int expectedValue, int result) {
checkAffectedRows(expectedValue, result, checkAffectedRows(expectedValue, result,
@@ -352,45 +423,45 @@ public class AssertTools {
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。 * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
* *
* @param expectedValue 预计的数量 * @param expectedValue 预计的数量
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param message 异常信息 * @param errorMessage 异常信息
*/ */
public static void checkAffectedRows(int expectedValue, int result, @Nullable String message) { public static void checkAffectedRows(int expectedValue, int result, @Nullable String errorMessage) {
checkCondition(expectedValue == result, () -> new DataOperationResultException(message)); checkCondition(expectedValue == result, () -> new DataOperationResultException(errorMessage));
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param messageSupplier 异常信息
*/
public static void checkAffectedRows(int expectedValue, int result,
Supplier<String> messageSupplier) {
checkCondition(expectedValue == result,
() -> new DataOperationResultException(messageSupplier.get()));
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。 * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
* *
* @param expectedValue 预计的数量 * @param expectedValue 预计的数量
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param format 异常信息模板 * @param errorMessageSupplier 异常信息
* @param args 异常信息参数
*/ */
public static void checkAffectedRows(int expectedValue, int result, public static void checkAffectedRows(int expectedValue, int result,
String format, Object... args) { Supplier<String> errorMessageSupplier) {
checkCondition(expectedValue == result, checkCondition(expectedValue == result,
() -> new DataOperationResultException(String.format(format, args))); () -> new DataOperationResultException(errorMessageSupplier.get()));
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。 * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
* *
* @param expectedValue 预计的数量 * @param expectedValue 预计的数量
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
*/
public static void checkAffectedRows(int expectedValue, int result,
String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(expectedValue == result,
() -> new DataOperationResultException(String.format(errorMessageTemplate, errorMessageArgs)));
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
*/ */
public static void checkAffectedRows(long expectedValue, long result) { public static void checkAffectedRows(long expectedValue, long result) {
checkAffectedRows(expectedValue, result, checkAffectedRows(expectedValue, result,
@@ -401,38 +472,38 @@ public class AssertTools {
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。 * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
* *
* @param expectedValue 预计的数量 * @param expectedValue 预计的数量
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param message 异常信息 * @param errorMessage 异常信息
*/ */
public static void checkAffectedRows(long expectedValue, long result, @Nullable String message) { public static void checkAffectedRows(long expectedValue, long result, @Nullable String errorMessage) {
checkCondition(expectedValue == result, () -> new DataOperationResultException(message)); checkCondition(expectedValue == result, () -> new DataOperationResultException(errorMessage));
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param messageSupplier 异常信息
*/
public static void checkAffectedRows(long expectedValue, long result,
Supplier<String> messageSupplier) {
checkCondition(expectedValue == result,
() -> new DataOperationResultException(messageSupplier.get()));
} }
/** /**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。 * 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
* *
* @param expectedValue 预计的数量 * @param expectedValue 预计的数量
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param format 异常信息模板 * @param errorMessageSupplier 异常信息
* @param args 异常信息参数
*/ */
public static void checkAffectedRows(long expectedValue, long result, public static void checkAffectedRows(long expectedValue, long result,
String format, Object... args) { Supplier<String> errorMessageSupplier) {
checkCondition(expectedValue == result, checkCondition(expectedValue == result,
() -> new DataOperationResultException(String.format(format, args))); () -> new DataOperationResultException(errorMessageSupplier.get()));
}
/**
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
*
* @param expectedValue 预计的数量
* @param result 实际影响的数据量
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
*/
public static void checkAffectedRows(long expectedValue, long result,
String errorMessageTemplate, Object... errorMessageArgs) {
checkCondition(expectedValue == result,
() -> new DataOperationResultException(String.format(errorMessageTemplate, errorMessageArgs)));
} }
/** /**
@@ -448,32 +519,33 @@ public class AssertTools {
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。 * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
* *
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param message 异常信息 * @param errorMessage 异常信息
*/ */
public static void checkAffectedOneRow(int result, String message) { public static void checkAffectedOneRow(int result, String errorMessage) {
checkAffectedRows(1, result, message); checkAffectedRows(1, result, errorMessage);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
*
* @param result 实际影响的数据量
* @param messageSupplier 异常信息
*/
public static void checkAffectedOneRow(int result, Supplier<String> messageSupplier) {
checkAffectedRows(1, result, messageSupplier);
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。 * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
* *
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param format 异常信息模板 * @param errorMessageSupplier 异常信息
* @param args 异常信息参数
*/ */
public static void checkAffectedOneRow(int result, String format, Object... args) { public static void checkAffectedOneRow(int result, Supplier<String> errorMessageSupplier) {
checkAffectedRows(1, result, format, args); checkAffectedRows(1, result, errorMessageSupplier);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
*
* @param result 实际影响的数据量
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
*/
public static void checkAffectedOneRow(int result,
String errorMessageTemplate, Object... errorMessageArgs) {
checkAffectedRows(1, result, errorMessageTemplate, errorMessageArgs);
} }
/** /**
@@ -489,33 +561,33 @@ public class AssertTools {
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。 * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
* *
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param message 异常信息 * @param errorMessage 异常信息
*/ */
public static void checkAffectedOneRow(long result, String message) { public static void checkAffectedOneRow(long result, String errorMessage) {
checkAffectedRows(1L, result, message); checkAffectedRows(1L, result, errorMessage);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
*
* @param result 实际影响的数据量
* @param messageSupplier 异常信息
*/
public static void checkAffectedOneRow(long result, Supplier<String> messageSupplier) {
checkAffectedRows(1L, result, messageSupplier);
} }
/** /**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。 * 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
* *
* @param result 实际影响的数据量 * @param result 实际影响的数据量
* @param format 异常信息模板 * @param errorMessageSupplier 异常信息
* @param args 异常信息参数 */
public static void checkAffectedOneRow(long result, Supplier<String> errorMessageSupplier) {
checkAffectedRows(1L, result, errorMessageSupplier);
}
/**
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
*
* @param result 实际影响的数据量
* @param errorMessageTemplate 异常信息模板
* @param errorMessageArgs 异常信息参数
*/ */
public static void checkAffectedOneRow(long result, public static void checkAffectedOneRow(long result,
String format, Object... args) { String errorMessageTemplate, Object... errorMessageArgs) {
checkAffectedRows(1L, result, format, args); checkAffectedRows(1L, result, errorMessageTemplate, errorMessageArgs);
} }
// ================================ // ================================
@@ -529,9 +601,9 @@ public class AssertTools {
/** /**
* 当条件不满足时抛出异常。 * 当条件不满足时抛出异常。
* *
* @param <T> 异常类型 * @param <T> 异常类型
* @param condition 条件 * @param condition 条件
* @param e 异常 * @param e 异常
* @throws T 当条件不满足时抛出异常 * @throws T 当条件不满足时抛出异常
*/ */
public static <T extends Exception> void checkCondition(boolean condition, Supplier<T> e) public static <T extends Exception> void checkCondition(boolean condition, Supplier<T> e)

View File

@@ -105,7 +105,7 @@ public class BigDecimals {
* @return 求和结果 * @return 求和结果
*/ */
public static BigDecimal sum(final BigDecimal... numbers) { public static BigDecimal sum(final BigDecimal... numbers) {
if (ArrayTools.isNullOrEmpty(numbers)) { if (ArrayTools.isEmpty(numbers)) {
return BigDecimal.ZERO; return BigDecimal.ZERO;
} }
BigDecimal result = BigDecimals.nullToZero(numbers[0]); BigDecimal result = BigDecimals.nullToZero(numbers[0]);

View File

@@ -108,7 +108,7 @@ public class Numbers {
* @return 求和结果 * @return 求和结果
*/ */
public static BigInteger sum(final BigInteger... numbers) { public static BigInteger sum(final BigInteger... numbers) {
if (ArrayTools.isNullOrEmpty(numbers)) { if (ArrayTools.isEmpty(numbers)) {
return BigInteger.ZERO; return BigInteger.ZERO;
} }
BigInteger result = Numbers.nullToZero(numbers[0]); BigInteger result = Numbers.nullToZero(numbers[0]);

View File

@@ -57,53 +57,53 @@ public class ArrayToolsTests {
static final double[] EMPTY_DOUBLE_ARRAY = {}; static final double[] EMPTY_DOUBLE_ARRAY = {};
// ================================ // ================================
// #region - isNullOrEmpty // #region - isEmpty
// ================================ // ================================
@Test @Test
void isNullOrEmpty_NullArray_ReturnsTrue() { void isEmpty_NullArray_ReturnsTrue() {
assertAll( assertAll(
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_STRING_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(NULL_STRING_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_INTEGER_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(NULL_INTEGER_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_CHAR_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(NULL_CHAR_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_BYTE_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(NULL_BYTE_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_SHORT_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(NULL_SHORT_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_INT_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(NULL_INT_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_LONG_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(NULL_LONG_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_FLOAT_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(NULL_FLOAT_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(NULL_DOUBLE_ARRAY))); () -> assertTrue(ArrayTools.isEmpty(NULL_DOUBLE_ARRAY)));
} }
@Test @Test
void isNullOrEmpty_EmptyArray_ReturnsTrue() { void isEmpty_EmptyArray_ReturnsTrue() {
assertAll( assertAll(
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_STRING_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(EMPTY_STRING_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_INTEGER_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(EMPTY_INTEGER_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_CHAR_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(EMPTY_CHAR_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_BYTE_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(EMPTY_BYTE_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_SHORT_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(EMPTY_SHORT_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_INT_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(EMPTY_INT_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_LONG_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(EMPTY_LONG_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_FLOAT_ARRAY)), () -> assertTrue(ArrayTools.isEmpty(EMPTY_FLOAT_ARRAY)),
() -> assertTrue(ArrayTools.isNullOrEmpty(EMPTY_DOUBLE_ARRAY))); () -> assertTrue(ArrayTools.isEmpty(EMPTY_DOUBLE_ARRAY)));
} }
@Test @Test
void isNullOrEmpty_NonEmptyArray_ReturnsFalse() { void isEmpty_NonEmptyArray_ReturnsFalse() {
assertAll( assertAll(
() -> assertFalse(ArrayTools.isNullOrEmpty(new String[] { "a" })), () -> assertFalse(ArrayTools.isEmpty(new String[] { "a" })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new Integer[] { 1 })), () -> assertFalse(ArrayTools.isEmpty(new Integer[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new char[] { 'a' })), () -> assertFalse(ArrayTools.isEmpty(new char[] { 'a' })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new byte[] { 1 })), () -> assertFalse(ArrayTools.isEmpty(new byte[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new short[] { 1 })), () -> assertFalse(ArrayTools.isEmpty(new short[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new int[] { 1 })), () -> assertFalse(ArrayTools.isEmpty(new int[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new long[] { 1 })), () -> assertFalse(ArrayTools.isEmpty(new long[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new float[] { 1 })), () -> assertFalse(ArrayTools.isEmpty(new float[] { 1 })),
() -> assertFalse(ArrayTools.isNullOrEmpty(new double[] { 1 }))); () -> assertFalse(ArrayTools.isEmpty(new double[] { 1 })));
} }
// ================================ // ================================
// #endregion - isNullOrEmpty // #endregion - isEmpty
// ================================ // ================================
// ================================ // ================================