diff --git a/hutool-core/src/main/java/cn/hutool/core/math/NumberUtil.java b/hutool-core/src/main/java/cn/hutool/core/math/NumberUtil.java index 1ad8b765c..923058e4c 100644 --- a/hutool-core/src/main/java/cn/hutool/core/math/NumberUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/math/NumberUtil.java @@ -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 * diff --git a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java index f6280bfde..122362c22 100755 --- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java @@ -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"}
- * 注意:{@code "null" != null} - */ - public static final String NULL = "null"; - - /** - * 字符串常量:空字符串 {@code ""} - */ - public static final String EMPTY = ""; - /** * 字符串常量:空格符 {@code " "} */ public static final String SPACE = " "; - /** - *

字符串是否为空白,空白的定义如下:

- *
    - *
  1. {@code null}
  2. - *
  3. 空字符串:{@code ""}
  4. - *
  5. 空格、全角空格、制表符、换行符,等不可见字符
  6. - *
- * - *

例:

- * - * - *

注意:该方法与 {@link #isEmpty(CharSequence)} 的区别是: - * 该方法会校验空白字符,且性能相对于 {@link #isEmpty(CharSequence)} 略慢。

- *
- * - *

建议:

- * - * - * @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; - } - - /** - *

字符串是否为非空白,非空白的定义如下:

- *
    - *
  1. 不为 {@code null}
  2. - *
  3. 不为空字符串:{@code ""}
  4. - *
  5. 不为空格、全角空格、制表符、换行符,等不可见字符
  6. - *
- * - *

例:

- * - * - *

注意:该方法与 {@link #isNotEmpty(CharSequence)} 的区别是: - * 该方法会校验空白字符,且性能相对于 {@link #isNotEmpty(CharSequence)} 略慢。

- *

建议:仅对于客户端(或第三方接口)传入的参数使用该方法。

- * - * @param str 被检测的字符串 - * @return 是否为非空 - * @see #isBlank(CharSequence) - */ - public static boolean isNotBlank(final CharSequence str) { - return false == isBlank(str); - } - - /** - *

指定字符串数组中,是否包含空字符串。

- *

如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。

- *
- * - *

例:

- * - * - *

注意:该方法与 {@link #isAllBlank(CharSequence...)} 的区别在于:

- * - * - * @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; - } - - /** - *

指定字符串数组中的元素,是否全部为空字符串。

- *

如果指定的字符串数组的长度为 0,或者所有元素都是空字符串,则返回 true。

- *
- * - *

例:

- * - * - *

注意:该方法与 {@link #hasBlank(CharSequence...)} 的区别在于:

- * - * - * @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; - } - - /** - *

字符串是否为空,空的定义如下:

- *
    - *
  1. {@code null}
  2. - *
  3. 空字符串:{@code ""}
  4. - *
- * - *

例:

- * - * - *

注意:该方法与 {@link #isBlank(CharSequence)} 的区别是:该方法不校验空白字符。

- *

建议:

- * - * - * @param str 被检测的字符串 - * @return 是否为空 - * @see #isBlank(CharSequence) - */ - public static boolean isEmpty(final CharSequence str) { - return str == null || str.length() == 0; - } - - /** - *

字符串是否为非空白,非空白的定义如下:

- *
    - *
  1. 不为 {@code null}
  2. - *
  3. 不为空字符串:{@code ""}
  4. - *
- * - *

例:

- * - * - *

注意:该方法与 {@link #isNotBlank(CharSequence)} 的区别是:该方法不校验空白字符。

- *

建议:该方法建议用于工具类或任何可以预期的方法参数的校验中。

- * - * @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(); } - /** - *

是否包含空字符串。

- *

如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。

- *
- * - *

例:

- * - * - *

注意:该方法与 {@link #isAllEmpty(CharSequence...)} 的区别在于:

- * - * - * @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; - } - - /** - *

指定字符串数组中的元素,是否全部为空字符串。

- *

如果指定的字符串数组的长度为 0,或者所有元素都是空字符串,则返回 true。

- *
- * - *

例:

- * - * - *

注意:该方法与 {@link #hasEmpty(CharSequence...)} 的区别在于:

- * - * - * @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; - } - - /** - *

指定字符串数组中的元素,是否都不为空字符串。

- *

如果指定的字符串数组的长度不为 0,或者所有元素都不是空字符串,则返回 true。

- *
- * - *

例:

- * - * - *

注意:该方法与 {@link #isAllEmpty(CharSequence...)} 的区别在于:

- * - * - * @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); - } - - /** - * 检查字符串是否为null、“null”、“undefined” - * - * @param str 被检查的字符串 - * @return 是否为null、“null”、“undefined” - * @since 4.0.10 - */ - public static boolean isNullOrUndefined(final CharSequence str) { - if (null == str) { - return true; - } - return isNullOrUndefinedStr(str); - } - - /** - * 检查字符串是否为null、“”、“null”、“undefined” - * - * @param str 被检查的字符串 - * @return 是否为null、“”、“null”、“undefined” - * @since 4.0.10 - */ - public static boolean isEmptyOrUndefined(final CharSequence str) { - if (isEmpty(str)) { - return true; - } - return isNullOrUndefinedStr(str); - } - - /** - * 检查字符串是否为null、空白串、“null”、“undefined” - * - * @param str 被检查的字符串 - * @return 是否为null、空白串、“null”、“undefined” - * @since 4.0.10 - */ - public static boolean isBlankOrUndefined(final CharSequence str) { - if (isBlank(str)) { - return true; - } - return isNullOrUndefinedStr(str); - } - - /** - * 是否为“null”、“undefined”,不做空指针检查 - * - * @param str 字符串 - * @return 是否为“null”、“undefined” - */ - 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 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; - } - /** * 检查字符串是否都为数字组成 * diff --git a/hutool-core/src/main/java/cn/hutool/core/text/StrChecker.java b/hutool-core/src/main/java/cn/hutool/core/text/StrChecker.java new file mode 100644 index 000000000..a00d3da28 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/text/StrChecker.java @@ -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等检查
+ * + */ +public class StrChecker { + + /** + * 字符串常量:{@code "null"}
+ * 注意:{@code "null" != null} + */ + public static final String NULL = "null"; + + /** + * 字符串常量:空字符串 {@code ""} + */ + public static final String EMPTY = ""; + + /** + *

字符串是否为空白,空白的定义如下:

+ *
    + *
  1. {@code null}
  2. + *
  3. 空字符串:{@code ""}
  4. + *
  5. 空格、全角空格、制表符、换行符,等不可见字符
  6. + *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #isEmpty(CharSequence)} 的区别是: + * 该方法会校验空白字符,且性能相对于 {@link #isEmpty(CharSequence)} 略慢。

+ *
+ * + *

建议:

+ * + * + * @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; + } + + /** + *

字符串是否为非空白,非空白的定义如下:

+ *
    + *
  1. 不为 {@code null}
  2. + *
  3. 不为空字符串:{@code ""}
  4. + *
  5. 不为空格、全角空格、制表符、换行符,等不可见字符
  6. + *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #isNotEmpty(CharSequence)} 的区别是: + * 该方法会校验空白字符,且性能相对于 {@link #isNotEmpty(CharSequence)} 略慢。

+ *

建议:仅对于客户端(或第三方接口)传入的参数使用该方法。

+ * + * @param str 被检测的字符串 + * @return 是否为非空 + * @see #isBlank(CharSequence) + */ + public static boolean isNotBlank(final CharSequence str) { + return false == isBlank(str); + } + + /** + *

指定字符串数组中,是否包含空字符串。

+ *

如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。

+ *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #isAllBlank(CharSequence...)} 的区别在于:

+ * + * + * @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 strs) { + if (CollUtil.isEmpty(strs)) { + return true; + } + for (final CharSequence str : strs) { + if (isBlank(str)) { + return true; + } + } + return false; + } + + /** + *

指定字符串数组中的元素,是否全部为空字符串。

+ *

如果指定的字符串数组的长度为 0,或者所有元素都是空字符串,则返回 true。

+ *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #hasBlank(CharSequence...)} 的区别在于:

+ * + * + * @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 strs) { + if (CollUtil.isNotEmpty(strs)) { + for (final CharSequence str : strs) { + if (isNotBlank(str)) { + return false; + } + } + } + return true; + } + + /** + *

字符串是否为空,空的定义如下:

+ *
    + *
  1. {@code null}
  2. + *
  3. 空字符串:{@code ""}
  4. + *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #isBlank(CharSequence)} 的区别是:该方法不校验空白字符。

+ *

建议:

+ * + * + * @param str 被检测的字符串 + * @return 是否为空 + * @see #isBlank(CharSequence) + */ + public static boolean isEmpty(final CharSequence str) { + return str == null || str.length() == 0; + } + + /** + *

字符串是否为非空白,非空白的定义如下:

+ *
    + *
  1. 不为 {@code null}
  2. + *
  3. 不为空字符串:{@code ""}
  4. + *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #isNotBlank(CharSequence)} 的区别是:该方法不校验空白字符。

+ *

建议:该方法建议用于工具类或任何可以预期的方法参数的校验中。

+ * + * @param str 被检测的字符串 + * @return 是否为非空 + * @see #isEmpty(CharSequence) + */ + public static boolean isNotEmpty(final CharSequence str) { + return false == isEmpty(str); + } + + /** + *

是否包含空字符串。

+ *

如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。

+ *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #isAllEmpty(CharSequence...)} 的区别在于:

+ * + * + * @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; + } + + /** + *

是否包含空字符串。

+ *

如果指定的字符串数组的长度为 0,或者其中的任意一个元素是空字符串,则返回 true。

+ * + * @param strs 字符串列表 + * @return 是否包含空字符串 + * @since 6.0.0 + */ + public static boolean hasEmpty(final Iterable strs) { + if (CollUtil.isEmpty(strs)) { + return true; + } + + for (final CharSequence str : strs) { + if (isEmpty(str)) { + return true; + } + } + return false; + } + + /** + *

指定字符串数组中的元素,是否全部为空字符串。

+ *

如果指定的字符串数组的长度为 0,或者所有元素都是空字符串,则返回 true。

+ *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #hasEmpty(CharSequence...)} 的区别在于:

+ * + * + * @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; + } + + /** + *

指定字符串数组中的元素,是否全部为空字符串。

+ *

如果指定的字符串数组的长度为 0,或者所有元素都是空字符串,则返回 true。

+ * + * @param strs 字符串列表 + * @return 所有字符串是否为空白 + */ + public static boolean isAllEmpty(final Iterable strs) { + if (CollUtil.isNotEmpty(strs)) { + for (final CharSequence str : strs) { + if (isNotEmpty(str)) { + return false; + } + } + } + + return true; + } + + /** + *

指定字符串数组中的元素,是否都不为空字符串。

+ *

如果指定的字符串数组的长度不为 0,或者所有元素都不是空字符串,则返回 true。

+ *
+ * + *

例:

+ * + * + *

注意:该方法与 {@link #isAllEmpty(CharSequence...)} 的区别在于:

+ * + * + * @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); + } + + /** + * 检查字符串是否为null、“null”、“undefined” + * + * @param str 被检查的字符串 + * @return 是否为null、“null”、“undefined” + * @since 4.0.10 + */ + public static boolean isNullOrUndefined(final CharSequence str) { + if (null == str) { + return true; + } + return isNullOrUndefinedStr(str); + } + + /** + * 检查字符串是否为null、“”、“null”、“undefined” + * + * @param str 被检查的字符串 + * @return 是否为null、“”、“null”、“undefined” + * @since 4.0.10 + */ + public static boolean isEmptyOrUndefined(final CharSequence str) { + if (isEmpty(str)) { + return true; + } + return isNullOrUndefinedStr(str); + } + + /** + * 检查字符串是否为null、空白串、“null”、“undefined” + * + * @param str 被检查的字符串 + * @return 是否为null、空白串、“null”、“undefined” + * @since 4.0.10 + */ + public static boolean isBlankOrUndefined(final CharSequence str) { + if (isBlank(str)) { + return true; + } + return isNullOrUndefinedStr(str); + } + + /** + * 是否为“null”、“undefined”,不做空指针检查 + * + * @param str 字符串 + * @return 是否为“null”、“undefined” + */ + 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 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; + } +} diff --git a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java index 46f98a870..138fc304a 100755 --- a/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/collection/CollUtilTest.java @@ -912,4 +912,17 @@ public class CollUtilTest { Console.log(collection.getClass()); Assert.assertNotNull(collection); } + + @Test + public void transTest(){ + final List 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 trans = CollUtil.trans(people, Person::getName); + Assert.assertEquals("[aa, bb, cc, dd]", trans.toString()); + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/SimpleCacheTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/SimpleCacheTest.java index ff0087074..198f09c4b 100755 --- a/hutool-core/src/test/java/cn/hutool/core/lang/SimpleCacheTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/SimpleCacheTest.java @@ -57,7 +57,7 @@ public class SimpleCacheTest { @Test public void getConcurrencyTest(){ final SimpleCache 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"; diff --git a/hutool-core/src/test/java/cn/hutool/core/map/WeakConcurrentMapTest.java b/hutool-core/src/test/java/cn/hutool/core/map/WeakConcurrentMapTest.java index d2aab2848..d0670794a 100755 --- a/hutool-core/src/test/java/cn/hutool/core/map/WeakConcurrentMapTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/map/WeakConcurrentMapTest.java @@ -46,7 +46,7 @@ public class WeakConcurrentMapTest { @Test public void getConcurrencyTest(){ final WeakConcurrentMap 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); diff --git a/hutool-core/src/test/java/cn/hutool/core/text/StrUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/text/StrUtilTest.java new file mode 100644 index 000000000..47aea4ad5 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/text/StrUtilTest.java @@ -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 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")); + } +} diff --git a/hutool-db/src/main/java/cn/hutool/db/StatementUtil.java b/hutool-db/src/main/java/cn/hutool/db/StatementUtil.java index 935aeb575..ad8e63825 100644 --- a/hutool-db/src/main/java/cn/hutool/db/StatementUtil.java +++ b/hutool-db/src/main/java/cn/hutool/db/StatementUtil.java @@ -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;