Merge remote-tracking branch 'origin/v6-dev' into v6-dev

This commit is contained in:
VampireAchao 2022-07-03 17:38:38 +08:00
commit fec797aa86
8 changed files with 615 additions and 400 deletions

View File

@ -946,6 +946,22 @@ public class NumberUtil {
return Long.parseLong(binaryStr, 2);
}
/**
* 检查值是否在指定范围内
*
* @param value
* @param minInclude 最小值包含
* @param maxInclude 最大值包含
* @return 经过检查后的值
* @since 5.8.5
*/
public static boolean isIn(final BigDecimal value, final BigDecimal minInclude, final BigDecimal maxInclude) {
Assert.notNull(value);
Assert.notNull(minInclude);
Assert.notNull(maxInclude);
return isGreaterOrEqual(value, minInclude) && isLessOrEqual(value, maxInclude);
}
/**
* 比较大小参数1 > 参数2 返回true
*

View File

@ -32,227 +32,15 @@ import java.util.function.Predicate;
* @author looly
* @since 5.5.3
*/
public class CharSequenceUtil {
public class CharSequenceUtil extends StrChecker{
public static final int INDEX_NOT_FOUND = Finder.INDEX_NOT_FOUND;
/**
* 字符串常量{@code "null"} <br>
* 注意{@code "null" != null}
*/
public static final String NULL = "null";
/**
* 字符串常量空字符串 {@code ""}
*/
public static final String EMPTY = "";
/**
* 字符串常量空格符 {@code " "}
*/
public static final String SPACE = " ";
/**
* <p>字符串是否为空白空白的定义如下</p>
* <ol>
* <li>{@code null}</li>
* <li>空字符串{@code ""}</li>
* <li>空格全角空格制表符换行符等不可见字符</li>
* </ol>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isBlank(null) // true}</li>
* <li>{@code StrUtil.isBlank("") // true}</li>
* <li>{@code StrUtil.isBlank(" \t\n") // true}</li>
* <li>{@code StrUtil.isBlank("abc") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #isEmpty(CharSequence)} 的区别是
* 该方法会校验空白字符且性能相对于 {@link #isEmpty(CharSequence)} 略慢</p>
* <br>
*
* <p>建议</p>
* <ul>
* <li>该方法建议仅对于客户端或第三方接口传入的参数使用该方法</li>
* <li>需要同时校验多个字符串时建议采用 {@link #hasBlank(CharSequence...)} {@link #isAllBlank(CharSequence...)}</li>
* </ul>
*
* @param str 被检测的字符串
* @return 若为空白则返回 true
* @see #isEmpty(CharSequence)
*/
public static boolean isBlank(final CharSequence str) {
final int length;
if ((str == null) || ((length = str.length()) == 0)) {
return true;
}
for (int i = 0; i < length; i++) {
// 只要有一个非空字符即为非空字符串
if (false == CharUtil.isBlankChar(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* <p>字符串是否为非空白非空白的定义如下 </p>
* <ol>
* <li>不为 {@code null}</li>
* <li>不为空字符串{@code ""}</li>
* <li>不为空格全角空格制表符换行符等不可见字符</li>
* </ol>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isNotBlank(null) // false}</li>
* <li>{@code StrUtil.isNotBlank("") // false}</li>
* <li>{@code StrUtil.isNotBlank(" \t\n") // false}</li>
* <li>{@code StrUtil.isNotBlank("abc") // true}</li>
* </ul>
*
* <p>注意该方法与 {@link #isNotEmpty(CharSequence)} 的区别是
* 该方法会校验空白字符且性能相对于 {@link #isNotEmpty(CharSequence)} 略慢</p>
* <p>建议仅对于客户端或第三方接口传入的参数使用该方法</p>
*
* @param str 被检测的字符串
* @return 是否为非空
* @see #isBlank(CharSequence)
*/
public static boolean isNotBlank(final CharSequence str) {
return false == isBlank(str);
}
/**
* <p>指定字符串数组中是否包含空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者其中的任意一个元素是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.hasBlank() // true}</li>
* <li>{@code StrUtil.hasBlank("", null, " ") // true}</li>
* <li>{@code StrUtil.hasBlank("123", " ") // true}</li>
* <li>{@code StrUtil.hasBlank("123", "abc") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #isAllBlank(CharSequence...)} 的区别在于</p>
* <ul>
* <li>hasBlank(CharSequence...) 等价于 {@code isBlank(...) || isBlank(...) || ...}</li>
* <li>{@link #isAllBlank(CharSequence...)} 等价于 {@code isBlank(...) && isBlank(...) && ...}</li>
* </ul>
*
* @param strs 字符串列表
* @return 是否包含空字符串
*/
public static boolean hasBlank(final CharSequence... strs) {
if (ArrayUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isBlank(str)) {
return true;
}
}
return false;
}
/**
* <p>指定字符串数组中的元素是否全部为空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者所有元素都是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isAllBlank() // true}</li>
* <li>{@code StrUtil.isAllBlank("", null, " ") // true}</li>
* <li>{@code StrUtil.isAllBlank("123", " ") // false}</li>
* <li>{@code StrUtil.isAllBlank("123", "abc") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #hasBlank(CharSequence...)} 的区别在于</p>
* <ul>
* <li>{@link #hasBlank(CharSequence...)} 等价于 {@code isBlank(...) || isBlank(...) || ...}</li>
* <li>isAllBlank(CharSequence...) 等价于 {@code isBlank(...) && isBlank(...) && ...}</li>
* </ul>
*
* @param strs 字符串列表
* @return 所有字符串是否为空白
*/
public static boolean isAllBlank(final CharSequence... strs) {
if (ArrayUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isNotBlank(str)) {
return false;
}
}
return true;
}
/**
* <p>字符串是否为空空的定义如下</p>
* <ol>
* <li>{@code null}</li>
* <li>空字符串{@code ""}</li>
* </ol>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isEmpty(null) // true}</li>
* <li>{@code StrUtil.isEmpty("") // true}</li>
* <li>{@code StrUtil.isEmpty(" \t\n") // false}</li>
* <li>{@code StrUtil.isEmpty("abc") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #isBlank(CharSequence)} 的区别是该方法不校验空白字符</p>
* <p>建议</p>
* <ul>
* <li>该方法建议用于工具类或任何可以预期的方法参数的校验中</li>
* <li>需要同时校验多个字符串时建议采用 {@link #hasEmpty(CharSequence...)} {@link #isAllEmpty(CharSequence...)}</li>
* </ul>
*
* @param str 被检测的字符串
* @return 是否为空
* @see #isBlank(CharSequence)
*/
public static boolean isEmpty(final CharSequence str) {
return str == null || str.length() == 0;
}
/**
* <p>字符串是否为非空白非空白的定义如下 </p>
* <ol>
* <li>不为 {@code null}</li>
* <li>不为空字符串{@code ""}</li>
* </ol>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isNotEmpty(null) // false}</li>
* <li>{@code StrUtil.isNotEmpty("") // false}</li>
* <li>{@code StrUtil.isNotEmpty(" \t\n") // true}</li>
* <li>{@code StrUtil.isNotEmpty("abc") // true}</li>
* </ul>
*
* <p>注意该方法与 {@link #isNotBlank(CharSequence)} 的区别是该方法不校验空白字符</p>
* <p>建议该方法建议用于工具类或任何可以预期的方法参数的校验中</p>
*
* @param str 被检测的字符串
* @return 是否为非空
* @see #isEmpty(CharSequence)
*/
public static boolean isNotEmpty(final CharSequence str) {
return false == isEmpty(str);
}
/**
* 当给定字符串为null时转换为Empty
*
@ -341,170 +129,6 @@ public class CharSequenceUtil {
return isEmpty(str) ? null : str.toString();
}
/**
* <p>是否包含空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者其中的任意一个元素是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.hasEmpty() // true}</li>
* <li>{@code StrUtil.hasEmpty("", null) // true}</li>
* <li>{@code StrUtil.hasEmpty("123", "") // true}</li>
* <li>{@code StrUtil.hasEmpty("123", "abc") // false}</li>
* <li>{@code StrUtil.hasEmpty(" ", "\t", "\n") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #isAllEmpty(CharSequence...)} 的区别在于</p>
* <ul>
* <li>hasEmpty(CharSequence...) 等价于 {@code isEmpty(...) || isEmpty(...) || ...}</li>
* <li>{@link #isAllEmpty(CharSequence...)} 等价于 {@code isEmpty(...) && isEmpty(...) && ...}</li>
* </ul>
*
* @param strs 字符串列表
* @return 是否包含空字符串
*/
public static boolean hasEmpty(final CharSequence... strs) {
if (ArrayUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isEmpty(str)) {
return true;
}
}
return false;
}
/**
* <p>指定字符串数组中的元素是否全部为空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者所有元素都是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isAllEmpty() // true}</li>
* <li>{@code StrUtil.isAllEmpty("", null) // true}</li>
* <li>{@code StrUtil.isAllEmpty("123", "") // false}</li>
* <li>{@code StrUtil.isAllEmpty("123", "abc") // false}</li>
* <li>{@code StrUtil.isAllEmpty(" ", "\t", "\n") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #hasEmpty(CharSequence...)} 的区别在于</p>
* <ul>
* <li>{@link #hasEmpty(CharSequence...)} 等价于 {@code isEmpty(...) || isEmpty(...) || ...}</li>
* <li>isAllEmpty(CharSequence...) 等价于 {@code isEmpty(...) && isEmpty(...) && ...}</li>
* </ul>
*
* @param strs 字符串列表
* @return 所有字符串是否为空白
*/
public static boolean isAllEmpty(final CharSequence... strs) {
if (ArrayUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isNotEmpty(str)) {
return false;
}
}
return true;
}
/**
* <p>指定字符串数组中的元素是否都不为空字符串</p>
* <p>如果指定的字符串数组的长度不为 0或者所有元素都不是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isAllNotEmpty() // false}</li>
* <li>{@code StrUtil.isAllNotEmpty("", null) // false}</li>
* <li>{@code StrUtil.isAllNotEmpty("123", "") // false}</li>
* <li>{@code StrUtil.isAllNotEmpty("123", "abc") // true}</li>
* <li>{@code StrUtil.isAllNotEmpty(" ", "\t", "\n") // true}</li>
* </ul>
*
* <p>注意该方法与 {@link #isAllEmpty(CharSequence...)} 的区别在于</p>
* <ul>
* <li>{@link #isAllEmpty(CharSequence...)} 等价于 {@code isEmpty(...) && isEmpty(...) && ...}</li>
* <li>isAllNotEmpty(CharSequence...) 等价于 {@code !isEmpty(...) && !isEmpty(...) && ...}</li>
* </ul>
*
* @param args 字符串数组
* @return 所有字符串是否都不为为空白
* @since 5.3.6
*/
public static boolean isAllNotEmpty(final CharSequence... args) {
return false == hasEmpty(args);
}
/**
* 是否存都不为{@code null}或空对象或空白符的对象通过{@link #hasBlank(CharSequence...)} 判断元素
*
* @param args 被检查的对象,一个或者多个
* @return 是否都不为空
* @since 5.3.6
*/
public static boolean isAllNotBlank(final CharSequence... args) {
return false == hasBlank(args);
}
/**
* 检查字符串是否为nullnullundefined
*
* @param str 被检查的字符串
* @return 是否为nullnullundefined
* @since 4.0.10
*/
public static boolean isNullOrUndefined(final CharSequence str) {
if (null == str) {
return true;
}
return isNullOrUndefinedStr(str);
}
/**
* 检查字符串是否为nullnullundefined
*
* @param str 被检查的字符串
* @return 是否为nullnullundefined
* @since 4.0.10
*/
public static boolean isEmptyOrUndefined(final CharSequence str) {
if (isEmpty(str)) {
return true;
}
return isNullOrUndefinedStr(str);
}
/**
* 检查字符串是否为null空白串nullundefined
*
* @param str 被检查的字符串
* @return 是否为null空白串nullundefined
* @since 4.0.10
*/
public static boolean isBlankOrUndefined(final CharSequence str) {
if (isBlank(str)) {
return true;
}
return isNullOrUndefinedStr(str);
}
/**
* 是否为nullundefined不做空指针检查
*
* @param str 字符串
* @return 是否为nullundefined
*/
private static boolean isNullOrUndefinedStr(final CharSequence str) {
final String strString = str.toString().trim();
return NULL.equals(strString) || "undefined".equals(strString);
}
// ------------------------------------------------------------------------ Trim
/**
@ -3991,7 +3615,7 @@ public class CharSequenceUtil {
/**
* 过滤字符串
*
* @param str 字符串
* @param str 字符串
* @param predicate 过滤器{@link Predicate#test(Object)}{@code true}保留字符
* @return 过滤后的字符串
* @since 5.4.0
@ -4351,26 +3975,6 @@ public class CharSequenceUtil {
return CollUtil.join(iterable, conjunction);
}
/**
* 字符串的每一个字符是否都与定义的匹配器匹配
*
* @param value 字符串
* @param matcher 匹配器
* @return 是否全部匹配
* @since 3.2.3
*/
public static boolean isAllCharMatch(final CharSequence value, final Predicate<Character> matcher) {
if (StrUtil.isBlank(value)) {
return false;
}
for (int i = value.length(); --i >= 0; ) {
if (false == matcher.test(value.charAt(i))) {
return false;
}
}
return true;
}
/**
* 检查字符串是否都为数字组成
*

View File

@ -0,0 +1,489 @@
package cn.hutool.core.text;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
import java.util.function.Predicate;
/**
* 字符串检查工具类提供字符串的blank和empty等检查<br>
* <ul>
* <li>empty定义{@code null} or 空字符串{@code ""}</li>
* <li>blank定义{@code null} or 空字符串{@code ""} or 空格全角空格制表符换行符等不可见字符</li>
* </ul>
*/
public class StrChecker {
/**
* 字符串常量{@code "null"} <br>
* 注意{@code "null" != null}
*/
public static final String NULL = "null";
/**
* 字符串常量空字符串 {@code ""}
*/
public static final String EMPTY = "";
/**
* <p>字符串是否为空白空白的定义如下</p>
* <ol>
* <li>{@code null}</li>
* <li>空字符串{@code ""}</li>
* <li>空格全角空格制表符换行符等不可见字符</li>
* </ol>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isBlank(null) // true}</li>
* <li>{@code StrUtil.isBlank("") // true}</li>
* <li>{@code StrUtil.isBlank(" \t\n") // true}</li>
* <li>{@code StrUtil.isBlank("abc") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #isEmpty(CharSequence)} 的区别是
* 该方法会校验空白字符且性能相对于 {@link #isEmpty(CharSequence)} 略慢</p>
* <br>
*
* <p>建议</p>
* <ul>
* <li>该方法建议仅对于客户端或第三方接口传入的参数使用该方法</li>
* <li>需要同时校验多个字符串时建议采用 {@link #hasBlank(CharSequence...)} {@link #isAllBlank(CharSequence...)}</li>
* </ul>
*
* @param str 被检测的字符串
* @return 若为空白则返回 true
* @see #isEmpty(CharSequence)
*/
public static boolean isBlank(final CharSequence str) {
final int length;
if ((str == null) || ((length = str.length()) == 0)) {
return true;
}
for (int i = 0; i < length; i++) {
// 只要有一个非空字符即为非空字符串
if (false == CharUtil.isBlankChar(str.charAt(i))) {
return false;
}
}
return true;
}
/**
* <p>字符串是否为非空白非空白的定义如下 </p>
* <ol>
* <li>不为 {@code null}</li>
* <li>不为空字符串{@code ""}</li>
* <li>不为空格全角空格制表符换行符等不可见字符</li>
* </ol>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isNotBlank(null) // false}</li>
* <li>{@code StrUtil.isNotBlank("") // false}</li>
* <li>{@code StrUtil.isNotBlank(" \t\n") // false}</li>
* <li>{@code StrUtil.isNotBlank("abc") // true}</li>
* </ul>
*
* <p>注意该方法与 {@link #isNotEmpty(CharSequence)} 的区别是
* 该方法会校验空白字符且性能相对于 {@link #isNotEmpty(CharSequence)} 略慢</p>
* <p>建议仅对于客户端或第三方接口传入的参数使用该方法</p>
*
* @param str 被检测的字符串
* @return 是否为非空
* @see #isBlank(CharSequence)
*/
public static boolean isNotBlank(final CharSequence str) {
return false == isBlank(str);
}
/**
* <p>指定字符串数组中是否包含空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者其中的任意一个元素是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.hasBlank() // true}</li>
* <li>{@code StrUtil.hasBlank("", null, " ") // true}</li>
* <li>{@code StrUtil.hasBlank("123", " ") // true}</li>
* <li>{@code StrUtil.hasBlank("123", "abc") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #isAllBlank(CharSequence...)} 的区别在于</p>
* <ul>
* <li>hasBlank(CharSequence...) 等价于 {@code isBlank(...) || isBlank(...) || ...}</li>
* <li>{@link #isAllBlank(CharSequence...)} 等价于 {@code isBlank(...) && isBlank(...) && ...}</li>
* </ul>
*
* @param strs 字符串列表
* @return 是否包含空字符串
*/
public static boolean hasBlank(final CharSequence... strs) {
if (ArrayUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isBlank(str)) {
return true;
}
}
return false;
}
/**
* 指定字符串集合中是否包含空字符串
*
* @param strs 字符串列表
* @return 批量判断字符串是否全部为空白
* @see CharSequenceUtil#hasBlank(CharSequence...)
* @since 6.0.0
*/
public static boolean hasBlank(final Iterable<? extends CharSequence> strs) {
if (CollUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isBlank(str)) {
return true;
}
}
return false;
}
/**
* <p>指定字符串数组中的元素是否全部为空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者所有元素都是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isAllBlank() // true}</li>
* <li>{@code StrUtil.isAllBlank("", null, " ") // true}</li>
* <li>{@code StrUtil.isAllBlank("123", " ") // false}</li>
* <li>{@code StrUtil.isAllBlank("123", "abc") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #hasBlank(CharSequence...)} 的区别在于</p>
* <ul>
* <li>{@link #hasBlank(CharSequence...)} 等价于 {@code isBlank(...) || isBlank(...) || ...}</li>
* <li>isAllBlank(CharSequence...) 等价于 {@code isBlank(...) && isBlank(...) && ...}</li>
* </ul>
*
* @param strs 字符串列表
* @return 所有字符串是否为空白
*/
public static boolean isAllBlank(final CharSequence... strs) {
if (ArrayUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isNotBlank(str)) {
return false;
}
}
return true;
}
/**
* @param strs 字符串列表
* @return 批量判断字符串是否全部为空白
* @see CharSequenceUtil#isAllBlank(CharSequence...)
* @since 6.0.1
*/
public static boolean isAllBlank(final Iterable<? extends CharSequence> strs) {
if (CollUtil.isNotEmpty(strs)) {
for (final CharSequence str : strs) {
if (isNotBlank(str)) {
return false;
}
}
}
return true;
}
/**
* <p>字符串是否为空空的定义如下</p>
* <ol>
* <li>{@code null}</li>
* <li>空字符串{@code ""}</li>
* </ol>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isEmpty(null) // true}</li>
* <li>{@code StrUtil.isEmpty("") // true}</li>
* <li>{@code StrUtil.isEmpty(" \t\n") // false}</li>
* <li>{@code StrUtil.isEmpty("abc") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #isBlank(CharSequence)} 的区别是该方法不校验空白字符</p>
* <p>建议</p>
* <ul>
* <li>该方法建议用于工具类或任何可以预期的方法参数的校验中</li>
* <li>需要同时校验多个字符串时建议采用 {@link #hasEmpty(CharSequence...)} {@link #isAllEmpty(CharSequence...)}</li>
* </ul>
*
* @param str 被检测的字符串
* @return 是否为空
* @see #isBlank(CharSequence)
*/
public static boolean isEmpty(final CharSequence str) {
return str == null || str.length() == 0;
}
/**
* <p>字符串是否为非空白非空白的定义如下 </p>
* <ol>
* <li>不为 {@code null}</li>
* <li>不为空字符串{@code ""}</li>
* </ol>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isNotEmpty(null) // false}</li>
* <li>{@code StrUtil.isNotEmpty("") // false}</li>
* <li>{@code StrUtil.isNotEmpty(" \t\n") // true}</li>
* <li>{@code StrUtil.isNotEmpty("abc") // true}</li>
* </ul>
*
* <p>注意该方法与 {@link #isNotBlank(CharSequence)} 的区别是该方法不校验空白字符</p>
* <p>建议该方法建议用于工具类或任何可以预期的方法参数的校验中</p>
*
* @param str 被检测的字符串
* @return 是否为非空
* @see #isEmpty(CharSequence)
*/
public static boolean isNotEmpty(final CharSequence str) {
return false == isEmpty(str);
}
/**
* <p>是否包含空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者其中的任意一个元素是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.hasEmpty() // true}</li>
* <li>{@code StrUtil.hasEmpty("", null) // true}</li>
* <li>{@code StrUtil.hasEmpty("123", "") // true}</li>
* <li>{@code StrUtil.hasEmpty("123", "abc") // false}</li>
* <li>{@code StrUtil.hasEmpty(" ", "\t", "\n") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #isAllEmpty(CharSequence...)} 的区别在于</p>
* <ul>
* <li>hasEmpty(CharSequence...) 等价于 {@code isEmpty(...) || isEmpty(...) || ...}</li>
* <li>{@link #isAllEmpty(CharSequence...)} 等价于 {@code isEmpty(...) && isEmpty(...) && ...}</li>
* </ul>
*
* @param strs 字符串列表
* @return 是否包含空字符串
*/
public static boolean hasEmpty(final CharSequence... strs) {
if (ArrayUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isEmpty(str)) {
return true;
}
}
return false;
}
/**
* <p>是否包含空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者其中的任意一个元素是空字符串则返回 true</p>
*
* @param strs 字符串列表
* @return 是否包含空字符串
* @since 6.0.0
*/
public static boolean hasEmpty(final Iterable<? extends CharSequence> strs) {
if (CollUtil.isEmpty(strs)) {
return true;
}
for (final CharSequence str : strs) {
if (isEmpty(str)) {
return true;
}
}
return false;
}
/**
* <p>指定字符串数组中的元素是否全部为空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者所有元素都是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isAllEmpty() // true}</li>
* <li>{@code StrUtil.isAllEmpty("", null) // true}</li>
* <li>{@code StrUtil.isAllEmpty("123", "") // false}</li>
* <li>{@code StrUtil.isAllEmpty("123", "abc") // false}</li>
* <li>{@code StrUtil.isAllEmpty(" ", "\t", "\n") // false}</li>
* </ul>
*
* <p>注意该方法与 {@link #hasEmpty(CharSequence...)} 的区别在于</p>
* <ul>
* <li>{@link #hasEmpty(CharSequence...)} 等价于 {@code isEmpty(...) || isEmpty(...) || ...}</li>
* <li>isAllEmpty(CharSequence...) 等价于 {@code isEmpty(...) && isEmpty(...) && ...}</li>
* </ul>
*
* @param strs 字符串列表
* @return 所有字符串是否为空白
*/
public static boolean isAllEmpty(final CharSequence... strs) {
if (ArrayUtil.isNotEmpty(strs)) {
for (final CharSequence str : strs) {
if (isNotEmpty(str)) {
return false;
}
}
}
return true;
}
/**
* <p>指定字符串数组中的元素是否全部为空字符串</p>
* <p>如果指定的字符串数组的长度为 0或者所有元素都是空字符串则返回 true</p>
*
* @param strs 字符串列表
* @return 所有字符串是否为空白
*/
public static boolean isAllEmpty(final Iterable<? extends CharSequence> strs) {
if (CollUtil.isNotEmpty(strs)) {
for (final CharSequence str : strs) {
if (isNotEmpty(str)) {
return false;
}
}
}
return true;
}
/**
* <p>指定字符串数组中的元素是否都不为空字符串</p>
* <p>如果指定的字符串数组的长度不为 0或者所有元素都不是空字符串则返回 true</p>
* <br>
*
* <p></p>
* <ul>
* <li>{@code StrUtil.isAllNotEmpty() // false}</li>
* <li>{@code StrUtil.isAllNotEmpty("", null) // false}</li>
* <li>{@code StrUtil.isAllNotEmpty("123", "") // false}</li>
* <li>{@code StrUtil.isAllNotEmpty("123", "abc") // true}</li>
* <li>{@code StrUtil.isAllNotEmpty(" ", "\t", "\n") // true}</li>
* </ul>
*
* <p>注意该方法与 {@link #isAllEmpty(CharSequence...)} 的区别在于</p>
* <ul>
* <li>{@link #isAllEmpty(CharSequence...)} 等价于 {@code isEmpty(...) && isEmpty(...) && ...}</li>
* <li>isAllNotEmpty(CharSequence...) 等价于 {@code !isEmpty(...) && !isEmpty(...) && ...}</li>
* </ul>
*
* @param args 字符串数组
* @return 所有字符串是否都不为为空白
* @since 5.3.6
*/
public static boolean isAllNotEmpty(final CharSequence... args) {
return false == hasEmpty(args);
}
/**
* 是否存都不为{@code null}或空对象或空白符的对象通过{@link #hasBlank(CharSequence...)} 判断元素
*
* @param args 被检查的对象,一个或者多个
* @return 是否都不为空
* @since 5.3.6
*/
public static boolean isAllNotBlank(final CharSequence... args) {
return false == hasBlank(args);
}
/**
* 检查字符串是否为nullnullundefined
*
* @param str 被检查的字符串
* @return 是否为nullnullundefined
* @since 4.0.10
*/
public static boolean isNullOrUndefined(final CharSequence str) {
if (null == str) {
return true;
}
return isNullOrUndefinedStr(str);
}
/**
* 检查字符串是否为nullnullundefined
*
* @param str 被检查的字符串
* @return 是否为nullnullundefined
* @since 4.0.10
*/
public static boolean isEmptyOrUndefined(final CharSequence str) {
if (isEmpty(str)) {
return true;
}
return isNullOrUndefinedStr(str);
}
/**
* 检查字符串是否为null空白串nullundefined
*
* @param str 被检查的字符串
* @return 是否为null空白串nullundefined
* @since 4.0.10
*/
public static boolean isBlankOrUndefined(final CharSequence str) {
if (isBlank(str)) {
return true;
}
return isNullOrUndefinedStr(str);
}
/**
* 是否为nullundefined不做空指针检查
*
* @param str 字符串
* @return 是否为nullundefined
*/
private static boolean isNullOrUndefinedStr(final CharSequence str) {
final String strString = str.toString().trim();
return NULL.equals(strString) || "undefined".equals(strString);
}
/**
* 字符串的每一个字符是否都与定义的匹配器匹配
*
* @param value 字符串
* @param matcher 匹配器
* @return 是否全部匹配
* @since 3.2.3
*/
public static boolean isAllCharMatch(final CharSequence value, final Predicate<Character> matcher) {
if (StrUtil.isBlank(value)) {
return false;
}
for (int i = value.length(); --i >= 0; ) {
if (false == matcher.test(value.charAt(i))) {
return false;
}
}
return true;
}
}

View File

@ -912,4 +912,17 @@ public class CollUtilTest {
Console.log(collection.getClass());
Assert.assertNotNull(collection);
}
@Test
public void transTest(){
final List<Person> people = Arrays.asList(
new Person("aa", 12, "man", 1),
new Person("bb", 13, "woman", 2),
new Person("cc", 14, "man", 3),
new Person("dd", 15, "woman", 4)
);
final Collection<String> trans = CollUtil.trans(people, Person::getName);
Assert.assertEquals("[aa, bb, cc, dd]", trans.toString());
}
}

View File

@ -57,7 +57,7 @@ public class SimpleCacheTest {
@Test
public void getConcurrencyTest(){
final SimpleCache<String, String> cache = new SimpleCache<>();
final ConcurrencyTester tester = new ConcurrencyTester(9000);
final ConcurrencyTester tester = new ConcurrencyTester(2000);
tester.test(()-> cache.get("aaa", ()-> {
ThreadUtil.sleep(200);
return "aaaValue";

View File

@ -46,7 +46,7 @@ public class WeakConcurrentMapTest {
@Test
public void getConcurrencyTest(){
final WeakConcurrentMap<String, String> cache = new WeakConcurrentMap<>();
final ConcurrencyTester tester = new ConcurrencyTester(9000);
final ConcurrencyTester tester = new ConcurrencyTester(2000);
tester.test(()-> cache.computeIfAbsent("aaa" + RandomUtil.randomInt(2), (key)-> "aaaValue"));
Assert.assertTrue(tester.getInterval() > 0);

View File

@ -0,0 +1,91 @@
package cn.hutool.core.text;
import org.junit.Assert;
import org.junit.Test;
import java.util.LinkedList;
import java.util.List;
public class StrUtilTest {
@Test
public void testReplace2() {
// https://gitee.com/dromara/hutool/issues/I4M16G
final String replace = "#{A}";
final String result = StrUtil.replace(replace, "#{AAAAAAA}", "1");
Assert.assertEquals(replace, result);
}
@Test
public void testReplaceByStr() {
final String replace = "SSM15930297701BeryAllen";
final String result = StrUtil.replace(replace, 5, 12, "***");
Assert.assertEquals("SSM15***01BeryAllen", result);
}
@Test
public void testAddPrefixIfNot() {
final String str = "hutool";
String result = StrUtil.addPrefixIfNot(str, "hu");
Assert.assertEquals(str, result);
result = StrUtil.addPrefixIfNot(str, "Good");
Assert.assertEquals("Good" + str, result);
}
@Test
public void testAddSuffixIfNot() {
final String str = "hutool";
String result = StrUtil.addSuffixIfNot(str, "tool");
Assert.assertEquals(str, result);
result = StrUtil.addSuffixIfNot(str, " is Good");
Assert.assertEquals(str + " is Good", result);
result = StrUtil.addSuffixIfNot("", "/");
Assert.assertEquals("/", result);
}
@Test
public void testIsAllBlank() {
final List<String> queue = new LinkedList<>();
queue.add("apple");
queue.add("banana");
queue.add("cherry");
queue.add("orange");
queue.add("strawberry");
queue.add("watermelon");
Assert.assertFalse(StrUtil.isAllBlank(queue));
Assert.assertTrue(CharSequenceUtil.isAllBlank(""));
Assert.assertTrue(CharSequenceUtil.isAllBlank(" "));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\t"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\n"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\r"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\f"));
Assert.assertFalse(CharSequenceUtil.isAllBlank("\b"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u00A0"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\uFEFF"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2000"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2001"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2002"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2003"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2004"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2005"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2006"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2007"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2008"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2009"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u200A"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u3000"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\uFEFF"));
// 其他空白字符
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u000B"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u000C"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u00A0"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u1680"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u180E"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2000"));
Assert.assertTrue(CharSequenceUtil.isAllBlank("\u2001"));
}
}

View File

@ -287,6 +287,8 @@ public class StatementUtil {
* @since 4.6.7
*/
public static int getTypeOfNull(final PreparedStatement ps, final int paramIndex) {
Assert.notNull(ps, "ps PreparedStatement must be not null in (getTypeOfNull)!");
int sqlType = Types.VARCHAR;
final ParameterMetaData pmd;