diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/array/ArrayUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/array/ArrayUtil.java index 6fd81599f..b75e05fc1 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/array/ArrayUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/array/ArrayUtil.java @@ -21,7 +21,6 @@ import org.dromara.hutool.core.convert.Convert; import org.dromara.hutool.core.exception.ExceptionUtil; import org.dromara.hutool.core.exception.HutoolException; import org.dromara.hutool.core.lang.Assert; -import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.text.StrJoiner; import org.dromara.hutool.core.text.StrUtil; @@ -42,6 +41,8 @@ import java.util.stream.Collectors; */ public class ArrayUtil extends PrimitiveArrayUtil { + // region ----- ofArray + /** * 转为数组,如果values为数组,返回,否则返回一个只有values一个元素的数组 * @@ -102,9 +103,9 @@ public class ArrayUtil extends PrimitiveArrayUtil { public static T[] ofArray(final Iterable iterable, final Class componentType) { return ofArray(IterUtil.getIter(iterable), componentType); } + // endregion - - // ---------------------------------------------------------------------- isBlank + // region ----- isBlank /** *

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

@@ -185,8 +186,9 @@ public class ArrayUtil extends PrimitiveArrayUtil { } return true; } + // endregion - // ---------------------------------------------------------------------- isEmpty + // region ----- isEmpty /** * 数组是否为空 @@ -231,8 +233,6 @@ public class ArrayUtil extends PrimitiveArrayUtil { return true; } - // ---------------------------------------------------------------------- isNotEmpty - /** * 数组是否为非空 * @@ -257,6 +257,79 @@ public class ArrayUtil extends PrimitiveArrayUtil { return !isEmpty(array); } + /** + * 计算{@code null}或空元素对象的个数,通过{@link ObjUtil#isEmpty(Object)} 判断元素 + * + * @param args 被检查的对象,一个或者多个 + * @return {@code null}或空元素对象的个数 + * @since 4.5.18 + */ + public static int emptyCount(final Object... args) { + int count = 0; + if (isNotEmpty(args)) { + for (final Object element : args) { + if (ObjUtil.isEmpty(element)) { + count++; + } + } + } + return count; + } + + /** + * 是否存在{@code null}或空对象,通过{@link ObjUtil#isEmpty(Object)} 判断元素
+ *

如果提供的数组本身为空,则返回{@code false}

+ * + * @param 元素类型 + * @param args 被检查对象 + * @return 是否存在 {@code null} 或空对象 + * @since 4.5.18 + */ + public static boolean hasEmpty(final T[] args) { + if (isNotEmpty(args)) { + for (final T element : args) { + if (ObjUtil.isEmpty(element)) { + return true; + } + } + } + return false; + } + + /** + * 是否所有元素都为{@code null}或空对象,通过{@link ObjUtil#isEmpty(Object)} 判断元素 + *

如果提供的数组本身为空,则返回{@code true}

+ * + * @param 元素类型 + * @param args 被检查的对象,一个或者多个 + * @return 是否都为空 + * @since 4.5.18 + */ + public static boolean isAllEmpty(final T[] args) { + for (final T obj : args) { + if (!ObjUtil.isEmpty(obj)) { + return false; + } + } + return true; + } + + /** + * 是否所有元素都不为{@code null}或空对象,通过{@link ObjUtil#isEmpty(Object)} 判断元素 + *

如果提供的数组本身为空,则返回{@code true}

+ * + * @param args 被检查的对象,一个或者多个 + * @return 是否都不为空 + * @since 4.5.18 + */ + public static boolean isAllNotEmpty(final Object... args) { + return !hasEmpty(args); + } + + // endregion + + // region ----- isNull or hasNull + /** * 是否包含{@code null}元素 *

如果数组为null,则返回{@code true},如果数组为空,则返回{@code false}

@@ -293,6 +366,20 @@ public class ArrayUtil extends PrimitiveArrayUtil { return null == firstNonNull(array); } + /** + * 是否所有元素都不为 {@code null} + *

如果提供的数组为null,则返回{@code false},如果提供的数组为空,则返回{@code true}

+ * + * @param 数组元素类型 + * @param array 被检查的数组 + * @return 是否所有元素都不为 {@code null} + * @since 5.4.0 + */ + @SuppressWarnings("unchecked") + public static boolean isAllNotNull(final T... array) { + return !hasNull(array); + } + /** * 是否包含非{@code null}元素
*

如果数组是{@code null}或者空,返回{@code false},否则当数组中有非{@code null}元素时返回{@code true}

@@ -322,6 +409,9 @@ public class ArrayUtil extends PrimitiveArrayUtil { } return firstMatch(ObjUtil::isNotNull, array); } + // endregion + + // region ----- match /** * 返回数组中第一个匹配规则的值 @@ -374,7 +464,9 @@ public class ArrayUtil extends PrimitiveArrayUtil { final ArrayWrapper arrayWrapper = ArrayWrapper.of(array); return arrayWrapper.matchIndex(beginIndexInclude, matcher); } + // endregion + // region ----- newArray /** * 新建一个空数组 * @@ -398,7 +490,9 @@ public class ArrayUtil extends PrimitiveArrayUtil { public static Object[] newArray(final int newSize) { return new Object[newSize]; } + // endregion + // region ----- type /** * 获取数组对象的元素类型,方法调用参数与返回结果举例: *