Compare commits
7 Commits
8e7ee5b448
...
97a4ae2279
Author | SHA1 | Date | |
---|---|---|---|
97a4ae2279 | |||
af66cd2380 | |||
3b519105bf | |||
b70e526509 | |||
a2781012be | |||
9e410029b1 | |||
ee7213a687 |
@@ -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 进行降序。
|
||||||
|
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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)
|
||||||
|
@@ -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]);
|
||||||
|
@@ -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]);
|
||||||
|
@@ -17,26 +17,40 @@
|
|||||||
package xyz.zhouxy.plusone.commons.util;
|
package xyz.zhouxy.plusone.commons.util;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Map;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.Optional;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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.cache.CacheBuilder;
|
||||||
|
import com.google.common.cache.CacheLoader;
|
||||||
|
import com.google.common.cache.LoadingCache;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 封装一些常用的正则操作,并可以缓存 {@link Pattern} 实例以复用(最多缓存大概 256 个)。
|
* 封装一些常用的正则操作,并可以缓存 {@link Pattern} 实例以复用。
|
||||||
*
|
*
|
||||||
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public final class RegexTools {
|
public final class RegexTools {
|
||||||
|
|
||||||
private static final int DEFAULT_CACHE_INITIAL_CAPACITY = 64;
|
|
||||||
private static final int MAX_CACHE_SIZE = 256;
|
private static final int MAX_CACHE_SIZE = 256;
|
||||||
private static final Map<String, Pattern> PATTERN_CACHE
|
private static final int DEFAULT_FLAG = 0;
|
||||||
= new ConcurrentHashMap<>(DEFAULT_CACHE_INITIAL_CAPACITY);
|
private static final LoadingCache<RegexAndFlags, Pattern> PATTERN_CACHE = CacheBuilder
|
||||||
|
.newBuilder()
|
||||||
|
.maximumSize(MAX_CACHE_SIZE)
|
||||||
|
.build(new CacheLoader<RegexAndFlags, Pattern>() {
|
||||||
|
@SuppressWarnings("null")
|
||||||
|
public Pattern load(@Nonnull RegexAndFlags regexAndFlags) {
|
||||||
|
return regexAndFlags.compilePattern();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - getPattern
|
||||||
|
// ================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 {@link Pattern} 实例。
|
* 获取 {@link Pattern} 实例。
|
||||||
@@ -46,8 +60,20 @@ 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) {
|
||||||
|
return getPattern(pattern, DEFAULT_FLAG, cachePattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 {@link Pattern} 实例。
|
||||||
|
*
|
||||||
|
* @param pattern 正则表达式
|
||||||
|
* @param flags 正则表达式匹配标识
|
||||||
|
* @param cachePattern 是否缓存 {@link Pattern} 实例
|
||||||
|
* @return {@link Pattern} 实例
|
||||||
|
*/
|
||||||
|
public static Pattern getPattern(final String pattern, final int flags, final boolean cachePattern) {
|
||||||
AssertTools.checkNotNull(pattern);
|
AssertTools.checkNotNull(pattern);
|
||||||
return cachePattern ? cacheAndGetPatternInternal(pattern) : getPatternInternal(pattern);
|
return cachePattern ? cacheAndGetPatternInternal(pattern, flags) : getPatternInternal(pattern, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,34 +83,29 @@ public final class RegexTools {
|
|||||||
* @return {@link Pattern} 实例
|
* @return {@link Pattern} 实例
|
||||||
*/
|
*/
|
||||||
public static Pattern getPattern(final String pattern) {
|
public static Pattern getPattern(final String pattern) {
|
||||||
|
return getPattern(pattern, DEFAULT_FLAG);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取 {@link Pattern} 实例,不缓存。
|
||||||
|
*
|
||||||
|
* @param pattern 正则表达式
|
||||||
|
* @param flags 正则表达式匹配标识
|
||||||
|
* @return {@link Pattern} 实例
|
||||||
|
*/
|
||||||
|
@Nonnull
|
||||||
|
public static Pattern getPattern(final String pattern, final int flags) {
|
||||||
AssertTools.checkNotNull(pattern);
|
AssertTools.checkNotNull(pattern);
|
||||||
return getPatternInternal(pattern);
|
return getPatternInternal(pattern, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// ================================
|
||||||
* 将各个正则表达式转为 {@link Pattern} 实例。
|
// #endregion - getPattern
|
||||||
*
|
// ================================
|
||||||
* @param patterns 正则表达式
|
|
||||||
* @param cachePattern 是否缓存 {@link Pattern} 实例
|
|
||||||
* @return {@link Pattern} 实例数组
|
|
||||||
*/
|
|
||||||
public static Pattern[] getPatterns(final String[] patterns, final boolean cachePattern) {
|
|
||||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
|
||||||
return cachePattern
|
|
||||||
? cacheAndGetPatternsInternal(patterns)
|
|
||||||
: getPatternsInternal(patterns);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// ================================
|
||||||
* 将各个正则表达式转为 {@link Pattern} 实例,不缓存。
|
// #region - matches
|
||||||
*
|
// ================================
|
||||||
* @param patterns 正则表达式
|
|
||||||
* @return {@link Pattern} 实例数组
|
|
||||||
*/
|
|
||||||
public static Pattern[] getPatterns(final String[] patterns) {
|
|
||||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
|
||||||
return getPatternsInternal(patterns);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断 {@code input} 是否匹配 {@code pattern}。
|
* 判断 {@code input} 是否匹配 {@code pattern}。
|
||||||
@@ -132,11 +153,21 @@ 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) {
|
||||||
AssertTools.checkNotNull(pattern);
|
return matches(input, pattern, DEFAULT_FLAG, cachePattern);
|
||||||
Pattern p = cachePattern
|
}
|
||||||
? cacheAndGetPatternInternal(pattern)
|
|
||||||
: getPatternInternal(pattern);
|
/**
|
||||||
return matchesInternal(input, p);
|
* 判断 {@code input} 是否匹配 {@code pattern}。
|
||||||
|
*
|
||||||
|
* @param input 输入
|
||||||
|
* @param pattern 正则表达式
|
||||||
|
* @param flags 正则表达式匹配标识
|
||||||
|
* @param cachePattern 是否缓存 {@link Pattern} 实例
|
||||||
|
* @return 判断结果
|
||||||
|
*/
|
||||||
|
public static boolean matches(@Nullable final CharSequence input, final String pattern, final int flags,
|
||||||
|
final boolean cachePattern) {
|
||||||
|
return matchesInternal(input, getPattern(pattern, flags, cachePattern));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -147,69 +178,29 @@ 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) {
|
||||||
AssertTools.checkNotNull(pattern);
|
return matches(input, pattern, DEFAULT_FLAG);
|
||||||
return matchesInternal(input, getPatternInternal(pattern));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断 {@code input} 是否匹配 {@code patterns} 中的一个。
|
* 判断 {@code input} 是否匹配 {@code pattern}。不缓存 {@link Pattern} 实例。
|
||||||
*
|
*
|
||||||
* @param input 输入
|
* @param input 输入
|
||||||
* @param patterns 正则表达式
|
* @param pattern 正则表达式
|
||||||
* @param cachePattern 是否缓存 {@link Pattern} 实例
|
* @param flags 正则表达式匹配标识
|
||||||
* @return 判断结果
|
* @return 判断结果
|
||||||
*/
|
*/
|
||||||
public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns,
|
public static boolean matches(@Nullable final CharSequence input,
|
||||||
final boolean cachePattern) {
|
final String pattern, final int flags) {
|
||||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
return matchesInternal(input, getPattern(pattern, flags));
|
||||||
final Pattern[] patternSet = cachePattern
|
|
||||||
? cacheAndGetPatternsInternal(patterns)
|
|
||||||
: getPatternsInternal(patterns);
|
|
||||||
return matchesOneInternal(input, patternSet);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// ================================
|
||||||
* 判断 {@code input} 是否匹配 {@code patterns} 中的一个。不缓存 {@link Pattern} 实例。
|
// #endregion - matches
|
||||||
*
|
// ================================
|
||||||
* @param input 输入
|
|
||||||
* @param patterns 正则表达式
|
|
||||||
* @return 判断结果
|
|
||||||
*/
|
|
||||||
public static boolean matchesOne(@Nullable final CharSequence input, final String[] patterns) {
|
|
||||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
|
||||||
final Pattern[] patternSet = getPatternsInternal(patterns);
|
|
||||||
return matchesOneInternal(input, patternSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// ================================
|
||||||
* 判断 {@code input} 是否匹配全部正则。
|
// #region - getMatcher
|
||||||
*
|
// ================================
|
||||||
* @param input 输入
|
|
||||||
* @param patterns 正则表达式
|
|
||||||
* @param cachePattern 是否缓存 {@link Pattern} 实例
|
|
||||||
* @return 判断结果
|
|
||||||
*/
|
|
||||||
public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns,
|
|
||||||
final boolean cachePattern) {
|
|
||||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
|
||||||
final Pattern[] patternSet = cachePattern
|
|
||||||
? cacheAndGetPatternsInternal(patterns)
|
|
||||||
: getPatternsInternal(patterns);
|
|
||||||
return matchesAllInternal(input, patternSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 判断 {@code input} 是否匹配全部正则。不缓存 {@link Pattern} 实例。
|
|
||||||
*
|
|
||||||
* @param input 输入
|
|
||||||
* @param patterns 正则表达式
|
|
||||||
* @return 判断结果
|
|
||||||
*/
|
|
||||||
public static boolean matchesAll(@Nullable final CharSequence input, final String[] patterns) {
|
|
||||||
AssertTools.checkArgument(ArrayTools.isAllElementsNotNull(patterns));
|
|
||||||
final Pattern[] patternSet = getPatternsInternal(patterns);
|
|
||||||
return matchesAllInternal(input, patternSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成 Matcher。
|
* 生成 Matcher。
|
||||||
@@ -233,12 +224,21 @@ 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) {
|
||||||
AssertTools.checkNotNull(input);
|
return getMatcher(input, pattern, DEFAULT_FLAG, cachePattern);
|
||||||
AssertTools.checkNotNull(pattern);
|
}
|
||||||
final Pattern p = cachePattern
|
|
||||||
? cacheAndGetPatternInternal(pattern)
|
/**
|
||||||
: getPatternInternal(pattern);
|
* 生成 Matcher。
|
||||||
return p.matcher(input);
|
*
|
||||||
|
* @param input 输入
|
||||||
|
* @param pattern 正则表达式
|
||||||
|
* @param flags 正则表达式匹配标识
|
||||||
|
* @param cachePattern 是否缓存 {@link Pattern} 实例
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public static Matcher getMatcher(final CharSequence input,
|
||||||
|
final String pattern, final int flags, boolean cachePattern) {
|
||||||
|
return getMatcher(input, getPattern(pattern, flags, cachePattern));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -249,70 +249,56 @@ 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) {
|
||||||
AssertTools.checkNotNull(input);
|
return getMatcher(input, pattern, DEFAULT_FLAG);
|
||||||
AssertTools.checkNotNull(pattern);
|
|
||||||
return getPatternInternal(pattern).matcher(input);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ========== internal methods ==========
|
/**
|
||||||
|
* 生成 Matcher。不缓存 {@link Pattern} 实例。
|
||||||
|
*
|
||||||
|
* @param input 输入
|
||||||
|
* @param pattern 正则表达式
|
||||||
|
* @param flags 正则表达式匹配标识
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public static Matcher getMatcher(final CharSequence input, final String pattern, final int flags) {
|
||||||
|
AssertTools.checkNotNull(input);
|
||||||
|
AssertTools.checkNotNull(pattern);
|
||||||
|
return getPatternInternal(pattern, flags).matcher(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - getMatcher
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - internal methods
|
||||||
|
// ================================
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 {@link Pattern} 实例。
|
* 获取 {@link Pattern} 实例。
|
||||||
*
|
*
|
||||||
* @param pattern 正则表达式
|
* @param pattern 正则表达式
|
||||||
|
* @param flags 正则表达式匹配标识
|
||||||
* @return {@link Pattern} 实例
|
* @return {@link Pattern} 实例
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private static Pattern cacheAndGetPatternInternal(final String pattern) {
|
private static Pattern cacheAndGetPatternInternal(final String pattern, final int flags) {
|
||||||
if (PATTERN_CACHE.size() < MAX_CACHE_SIZE) {
|
final RegexAndFlags regexAndFlags = new RegexAndFlags(pattern, flags);
|
||||||
return PATTERN_CACHE.computeIfAbsent(pattern, Pattern::compile);
|
return PATTERN_CACHE.getUnchecked(regexAndFlags);
|
||||||
}
|
|
||||||
Pattern result = PATTERN_CACHE.get(pattern);
|
|
||||||
if (result != null) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
return Pattern.compile(pattern);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取 {@link Pattern} 实例,不缓存。
|
* 获取 {@link Pattern} 实例,不缓存。
|
||||||
*
|
*
|
||||||
* @param pattern 正则表达式
|
* @param pattern 正则表达式
|
||||||
|
* @param flags 正则表达式匹配标识
|
||||||
* @return {@link Pattern} 实例
|
* @return {@link Pattern} 实例
|
||||||
*/
|
*/
|
||||||
@Nonnull
|
@Nonnull
|
||||||
private static Pattern getPatternInternal(final String pattern) {
|
private static Pattern getPatternInternal(final String pattern, final int flags) {
|
||||||
Pattern result = PATTERN_CACHE.get(pattern);
|
final RegexAndFlags regexAndFlags = new RegexAndFlags(pattern, flags);
|
||||||
if (result == null) {
|
return Optional.ofNullable(PATTERN_CACHE.getIfPresent(regexAndFlags))
|
||||||
result = Pattern.compile(pattern);
|
.orElseGet(regexAndFlags::compilePattern);
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将各个正则表达式转为 {@link Pattern} 实例。
|
|
||||||
*
|
|
||||||
* @param patterns 正则表达式
|
|
||||||
* @return {@link Pattern} 实例数组
|
|
||||||
*/
|
|
||||||
@Nonnull
|
|
||||||
private static Pattern[] cacheAndGetPatternsInternal(final String[] patterns) {
|
|
||||||
return Arrays.stream(patterns)
|
|
||||||
.map(RegexTools::cacheAndGetPatternInternal)
|
|
||||||
.toArray(Pattern[]::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将各个正则表达式转为 {@link Pattern} 实例。
|
|
||||||
*
|
|
||||||
* @param patterns 正则表达式
|
|
||||||
* @return {@link Pattern} 实例数组
|
|
||||||
*/
|
|
||||||
@Nonnull
|
|
||||||
private static Pattern[] getPatternsInternal(final String[] patterns) {
|
|
||||||
return Arrays.stream(patterns)
|
|
||||||
.map(RegexTools::getPatternInternal)
|
|
||||||
.toArray(Pattern[]::new);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -352,8 +338,49 @@ public final class RegexTools {
|
|||||||
.allMatch(pattern -> pattern.matcher(input).matches());
|
.allMatch(pattern -> pattern.matcher(input).matches());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - internal methods
|
||||||
|
// ================================
|
||||||
|
|
||||||
private RegexTools() {
|
private RegexTools() {
|
||||||
// 不允许实例化
|
// 不允许实例化
|
||||||
throw new IllegalStateException("Utility class");
|
throw new IllegalStateException("Utility class");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #region - RegexAndFlags
|
||||||
|
// ================================
|
||||||
|
|
||||||
|
private static final class RegexAndFlags {
|
||||||
|
private final String regex;
|
||||||
|
private final int flags;
|
||||||
|
|
||||||
|
private RegexAndFlags(String regex, int flags) {
|
||||||
|
this.regex = regex;
|
||||||
|
this.flags = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final Pattern compilePattern() {
|
||||||
|
return Pattern.compile(regex, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(regex, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(@Nullable Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (!(obj instanceof RegexAndFlags))
|
||||||
|
return false;
|
||||||
|
RegexAndFlags other = (RegexAndFlags) obj;
|
||||||
|
return Objects.equals(regex, other.regex) && flags == other.flags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================================
|
||||||
|
// #endregion - RegexAndFlags
|
||||||
|
// ================================
|
||||||
}
|
}
|
||||||
|
@@ -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
|
||||||
// ================================
|
// ================================
|
||||||
|
|
||||||
// ================================
|
// ================================
|
||||||
|
File diff suppressed because it is too large
Load Diff
@@ -33,19 +33,42 @@ public
|
|||||||
class RegexToolsTests {
|
class RegexToolsTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPattern_CachePatternTrue_ReturnsCachedPattern() {
|
void getPattern_SameRegexAndFlag_CachePatternIsTrue_ReturnsCachedPattern() {
|
||||||
String pattern = "abc";
|
String pattern = "abc";
|
||||||
Pattern cachedPattern = RegexTools.getPattern(pattern, true);
|
Pattern cachedPattern = RegexTools.getPattern(pattern, true);
|
||||||
Pattern patternFromCache = RegexTools.getPattern(pattern, true);
|
Pattern patternFromCache = RegexTools.getPattern(pattern);
|
||||||
assertSame(cachedPattern, patternFromCache, "Pattern should be cached");
|
assertSame(cachedPattern, patternFromCache, "Pattern should be cached");
|
||||||
|
|
||||||
|
Pattern cachedPatternWithFlag = RegexTools.getPattern(pattern, Pattern.CASE_INSENSITIVE, true);
|
||||||
|
Pattern patternFromCacheWithFlag = RegexTools.getPattern(pattern, Pattern.CASE_INSENSITIVE);
|
||||||
|
assertSame(cachedPatternWithFlag, patternFromCacheWithFlag, "Pattern should be cached");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getPattern_CachePatternFalse_ReturnsNewPattern() {
|
void getPattern_SameRegexAndFlag_CachePatternFalse_ReturnsNewPattern() {
|
||||||
String pattern = "getPattern_CachePatternFalse_ReturnsNewPattern";
|
String pattern = "getPattern_SameRegexAndFlag_CachePatternFalse_ReturnsNewPattern";
|
||||||
Pattern pattern1 = RegexTools.getPattern(pattern, false);
|
Pattern pattern1 = RegexTools.getPattern(pattern, false);
|
||||||
Pattern pattern2 = RegexTools.getPattern(pattern, false);
|
Pattern pattern2 = RegexTools.getPattern(pattern, false);
|
||||||
|
Pattern pattern3 = RegexTools.getPattern(pattern);
|
||||||
assertNotSame(pattern1, pattern2, "Pattern should not be cached");
|
assertNotSame(pattern1, pattern2, "Pattern should not be cached");
|
||||||
|
assertNotSame(pattern1, pattern3, "Pattern should not be cached");
|
||||||
|
assertNotSame(pattern2, pattern3, "Pattern should not be cached");
|
||||||
|
|
||||||
|
Pattern pattern1WithFlag = RegexTools.getPattern(pattern, Pattern.CASE_INSENSITIVE, false);
|
||||||
|
Pattern pattern2WithFlag = RegexTools.getPattern(pattern, Pattern.CASE_INSENSITIVE, false);
|
||||||
|
Pattern pattern3WithFlag = RegexTools.getPattern(pattern, Pattern.CASE_INSENSITIVE);
|
||||||
|
assertNotSame(pattern1WithFlag, pattern2WithFlag, "Pattern should not be cached");
|
||||||
|
assertNotSame(pattern1WithFlag, pattern3WithFlag, "Pattern should not be cached");
|
||||||
|
assertNotSame(pattern2WithFlag, pattern3WithFlag, "Pattern should not be cached");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getPattern_SameRegexAndDifferentFlag_ReturnsNewPattern() {
|
||||||
|
String pattern = "getPattern_SameRegexAndDifferentFlag_CachePatternFalse_ReturnsNewPattern";
|
||||||
|
|
||||||
|
Pattern pattern1WithFlag = RegexTools.getPattern(pattern, Pattern.CASE_INSENSITIVE, true);
|
||||||
|
Pattern pattern2WithFlag = RegexTools.getPattern(pattern, 0, true);
|
||||||
|
assertNotSame(pattern1WithFlag, pattern2WithFlag, "Patterns should not be the same");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -53,52 +76,38 @@ class RegexToolsTests {
|
|||||||
assertThrows(NullPointerException.class, () -> {
|
assertThrows(NullPointerException.class, () -> {
|
||||||
RegexTools.getPattern(null, true);
|
RegexTools.getPattern(null, true);
|
||||||
});
|
});
|
||||||
}
|
assertThrows(NullPointerException.class, () -> {
|
||||||
|
RegexTools.getPattern(null, Pattern.CASE_INSENSITIVE, true);
|
||||||
@Test
|
|
||||||
void getPatterns_CachePatternTrue_ReturnsCachedPatterns() {
|
|
||||||
String[] patterns = {"abc", "def"};
|
|
||||||
Pattern[] cachedPatterns = RegexTools.getPatterns(patterns, true);
|
|
||||||
Pattern[] patternsFromCache = RegexTools.getPatterns(patterns, true);
|
|
||||||
assertSame(cachedPatterns[0], patternsFromCache[0]);
|
|
||||||
assertSame(cachedPatterns[1], patternsFromCache[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getPatterns_CachePatternFalse_ReturnsNewPatterns() {
|
|
||||||
String[] patterns = {"getPatterns_CachePatternFalse_ReturnsNewPatterns1", "getPatterns_CachePatternFalse_ReturnsNewPatterns2"};
|
|
||||||
Pattern[] patterns1 = RegexTools.getPatterns(patterns, false);
|
|
||||||
Pattern[] patterns2 = RegexTools.getPatterns(patterns, false);
|
|
||||||
assertNotSame(patterns1[0], patterns2[0]);
|
|
||||||
assertNotSame(patterns1[1], patterns2[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
void getPatterns_NullPatterns_ThrowsException() {
|
|
||||||
assertThrows(IllegalArgumentException.class, () -> {
|
|
||||||
RegexTools.getPatterns(null, true);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void matches_InputMatchesPattern_ReturnsTrue() {
|
void matches_InputMatchesPattern_ReturnsTrue() {
|
||||||
String pattern = "abc";
|
String pattern = "abc";
|
||||||
|
assertTrue(RegexTools.matches("abc", pattern), "Input should match pattern");
|
||||||
|
assertFalse(RegexTools.matches("ABC", pattern), "Input should match pattern");
|
||||||
|
assertTrue(RegexTools.matches("ABC", pattern, Pattern.CASE_INSENSITIVE), "Input should match pattern");
|
||||||
|
|
||||||
Pattern compiledPattern = Pattern.compile(pattern);
|
Pattern compiledPattern = Pattern.compile(pattern);
|
||||||
assertTrue(RegexTools.matches("abc", compiledPattern), "Input should match pattern");
|
assertTrue(RegexTools.matches("abc", compiledPattern), "Input should match pattern");
|
||||||
|
assertFalse(RegexTools.matches("ABC", compiledPattern), "Input should match pattern");
|
||||||
|
|
||||||
|
assertTrue(RegexTools.matches("abc", pattern, true), "Input should match pattern");
|
||||||
|
Pattern cachedPattern1 = RegexTools.getPattern(pattern);
|
||||||
|
Pattern cachedPattern2 = RegexTools.getPattern(pattern);
|
||||||
|
assertSame(cachedPattern1, cachedPattern2, "Cached pattern should be the same");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void matches_InputDoesNotMatchPattern_ReturnsFalse() {
|
void matches_InputDoesNotMatchPattern_ReturnsFalse() {
|
||||||
String pattern = "abc";
|
String pattern = "abc";
|
||||||
Pattern compiledPattern = Pattern.compile(pattern);
|
assertFalse(RegexTools.matches("abcd", pattern), "Input should not match pattern");
|
||||||
assertFalse(RegexTools.matches("abcd", compiledPattern), "Input should not match pattern");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void matches_NullInput_ReturnsFalse() {
|
void matches_NullInput_ReturnsFalse() {
|
||||||
String pattern = "abc";
|
String pattern = "abc";
|
||||||
Pattern compiledPattern = Pattern.compile(pattern);
|
assertFalse(RegexTools.matches(null, pattern), "Null input should return false");
|
||||||
assertFalse(RegexTools.matches(null, compiledPattern), "Null input should return false");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -119,6 +128,7 @@ class RegexToolsTests {
|
|||||||
compiledPatterns[i] = Pattern.compile(patterns[i]);
|
compiledPatterns[i] = Pattern.compile(patterns[i]);
|
||||||
}
|
}
|
||||||
assertFalse(RegexTools.matchesOne("xyz", compiledPatterns), "Input should not match any pattern");
|
assertFalse(RegexTools.matchesOne("xyz", compiledPatterns), "Input should not match any pattern");
|
||||||
|
assertFalse(RegexTools.matchesOne(null, compiledPatterns), "Input should not match any pattern");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -139,30 +149,40 @@ class RegexToolsTests {
|
|||||||
compiledPatterns[i] = Pattern.compile(patterns[i]);
|
compiledPatterns[i] = Pattern.compile(patterns[i]);
|
||||||
}
|
}
|
||||||
assertFalse(RegexTools.matchesAll("abc", compiledPatterns), "Input should not match all patterns");
|
assertFalse(RegexTools.matchesAll("abc", compiledPatterns), "Input should not match all patterns");
|
||||||
|
assertFalse(RegexTools.matchesAll(null, compiledPatterns), "Input should not match all patterns");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getMatcher_ValidInputAndPattern_ReturnsMatcher() {
|
void getMatcher_ValidInputAndPattern_ReturnsMatcher() {
|
||||||
String pattern = "abc";
|
String pattern = "abc";
|
||||||
Pattern compiledPattern = Pattern.compile(pattern);
|
Matcher matcher1 = RegexTools.getMatcher("abc", pattern);
|
||||||
Matcher matcher = RegexTools.getMatcher("abc", compiledPattern);
|
assertNotNull(matcher1, "Matcher should not be null");
|
||||||
assertNotNull(matcher, "Matcher should not be null");
|
assertTrue(matcher1.matches(), "Should be matches");
|
||||||
|
|
||||||
|
Matcher matcher2 = RegexTools.getMatcher("ABC", pattern, true);
|
||||||
|
assertNotNull(matcher2, "Matcher should not be null");
|
||||||
|
assertFalse(matcher2.matches(), "Should be matches");
|
||||||
|
|
||||||
|
Pattern cachedPattern = RegexTools.getPattern(pattern);
|
||||||
|
Pattern patternFromCache = RegexTools.getPattern(pattern);
|
||||||
|
assertSame(cachedPattern, patternFromCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getMatcher_NullInput_ThrowsException() {
|
void getMatcher_NullInput_ThrowsException() {
|
||||||
String pattern = "abc";
|
String pattern = "abc";
|
||||||
Pattern compiledPattern = Pattern.compile(pattern);
|
|
||||||
assertThrows(NullPointerException.class, () -> {
|
assertThrows(NullPointerException.class, () -> {
|
||||||
RegexTools.getMatcher(null, compiledPattern);
|
RegexTools.getMatcher(null, pattern);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void getMatcher_NullPattern_ThrowsException() {
|
void getMatcher_NullPattern_ThrowsException() {
|
||||||
final Pattern pattern = null;
|
|
||||||
assertThrows(NullPointerException.class, () -> {
|
assertThrows(NullPointerException.class, () -> {
|
||||||
RegexTools.getMatcher("abc", pattern);
|
RegexTools.getMatcher("abc", (String) null);
|
||||||
|
});
|
||||||
|
assertThrows(NullPointerException.class, () -> {
|
||||||
|
RegexTools.getMatcher("abc", (Pattern) null);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user