From db84d98eea208bb0bd94519ae78241a19857ffd1 Mon Sep 17 00:00:00 2001 From: Looly Date: Fri, 14 Apr 2023 23:33:48 +0800 Subject: [PATCH] fix code --- .../dromara/hutool/core/bean/BeanUtil.java | 83 +++++++++++++------ .../hutool/core/bean/BeanUtilTest.java | 57 +++++++++++++ 2 files changed, 114 insertions(+), 26 deletions(-) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/bean/BeanUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/bean/BeanUtil.java index 062d89323..5afd6d969 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/bean/BeanUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/bean/BeanUtil.java @@ -46,6 +46,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Consumer; +import java.util.function.Predicate; import java.util.function.Supplier; import java.util.function.UnaryOperator; import java.util.stream.Collectors; @@ -831,18 +832,6 @@ public class BeanUtil { }); } - /** - * 判断Bean是否为非空对象,非空对象表示本身不为{@code null}或者含有非{@code null}属性的对象 - * - * @param bean Bean对象 - * @param ignoreFieldNames 忽略检查的字段名 - * @return 是否为非空,{@code true} - 非空 / {@code false} - 空 - * @since 5.0.7 - */ - public static boolean isNotEmpty(final Object bean, final String... ignoreFieldNames) { - return !isEmpty(bean, ignoreFieldNames); - } - /** * 判断Bean是否为空对象,空对象表示本身为{@code null}或者所有属性都为{@code null}
* 此方法不判断static属性 @@ -853,18 +842,28 @@ public class BeanUtil { * @since 4.1.10 */ public static boolean isEmpty(final Object bean, final String... ignoreFieldNames) { - if (null != bean) { - for (final Field field : FieldUtil.getFields(bean.getClass())) { - if (ModifierUtil.isStatic(field)) { - continue; - } - if ((!ArrayUtil.contains(ignoreFieldNames, field.getName())) - && null != FieldUtil.getFieldValue(bean, field)) { - return false; - } - } + // 不含有非空字段 + return !isNotEmpty(bean, ignoreFieldNames); + } + + /** + * 判断Bean是否为非空对象,非空对象表示本身不为{@code null}或者含有非{@code null}属性的对象 + * + * @param bean Bean对象 + * @param ignoreFieldNames 忽略检查的字段名 + * @return 是否为非空,{@code true} - 非空 / {@code false} - 空 + * @since 5.0.7 + */ + public static boolean isNotEmpty(final Object bean, final String... ignoreFieldNames) { + if(null == bean){ + return false; } - return true; + + // 相当于 hasNoneNullField + return checkBean(bean, field -> + (!ArrayUtil.contains(ignoreFieldNames, field.getName())) + && null != FieldUtil.getFieldValue(bean, field) + ); } /** @@ -873,10 +872,43 @@ public class BeanUtil { * * @param bean Bean对象 * @param ignoreFieldNames 忽略检查的字段名 - * @return 是否包含值为null的属性,{@code true} - 包含 / {@code false} - 不包含 + * @return 是否包含值为{@code null}的属性,{@code true} - 包含 / {@code false} - 不包含 * @since 4.1.10 */ public static boolean hasNullField(final Object bean, final String... ignoreFieldNames) { + return checkBean(bean, field -> + (!ArrayUtil.contains(ignoreFieldNames, field.getName())) + && null == FieldUtil.getFieldValue(bean, field) + ); + } + + /** + * 判断Bean是否包含值为{@code null}的属性,或当字段为{@link CharSequence}时,是否为isEmpty(null或"")
+ * 对象本身为{@code null}也返回true + * + * @param bean Bean对象 + * @param ignoreFieldNames 忽略检查的字段名 + * @return 是否包含值为{@code null}的属性,{@code true} - 包含 / {@code false} - 不包含 + * @since 4.1.10 + */ + public static boolean hasEmptyField(final Object bean, final String... ignoreFieldNames) { + return checkBean(bean, field -> + (!ArrayUtil.contains(ignoreFieldNames, field.getName())) + && StrUtil.isEmptyIfStr(FieldUtil.getFieldValue(bean, field)) + ); + } + + /** + * 检查Bean
+ * 遍历Bean的字段并断言检查字段,当某个字段: + * 断言为{@code true} 时,返回{@code true}并不再检查后续字段;
+ * 断言为{@code false}时,继续检查后续字段 + * + * @param bean Bean + * @param predicate 断言 + * @return 是否触发断言为真 + */ + public static boolean checkBean(final Object bean, final Predicate predicate){ if (null == bean) { return true; } @@ -884,8 +916,7 @@ public class BeanUtil { if (ModifierUtil.isStatic(field)) { continue; } - if ((!ArrayUtil.contains(ignoreFieldNames, field.getName())) - && null == FieldUtil.getFieldValue(bean, field)) { + if (predicate.test(field)) { return true; } } diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/bean/BeanUtilTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/bean/BeanUtilTest.java index e5c2f167d..cfb7bb121 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/bean/BeanUtilTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/bean/BeanUtilTest.java @@ -761,6 +761,63 @@ public class BeanUtilTest { Assertions.assertEquals(Integer.valueOf(3), ArrayUtil.get(o, 1)); } + @Test + void hasNullFieldTest() { + Assertions.assertTrue(BeanUtil.hasNullField(null)); + + + final TestPojo testPojo = new TestPojo(); + Assertions.assertTrue(BeanUtil.hasNullField(testPojo)); + + testPojo.setName("test"); + Assertions.assertTrue(BeanUtil.hasNullField(testPojo)); + // 忽略testPojo2List,则只剩下name属性,非空,返回false + Assertions.assertFalse(BeanUtil.hasNullField(testPojo, "testPojo2List")); + + testPojo.setTestPojo2List(new TestPojo2[0]); + // 所有字段都有值 + Assertions.assertFalse(BeanUtil.hasNullField(testPojo)); + } + + @Test + void hasEmptyFieldTest() { + Assertions.assertTrue(BeanUtil.hasEmptyField(null)); + + + final TestPojo testPojo = new TestPojo(); + Assertions.assertTrue(BeanUtil.hasEmptyField(testPojo)); + + testPojo.setName("test"); + Assertions.assertTrue(BeanUtil.hasEmptyField(testPojo)); + // 忽略testPojo2List,则只剩下name属性,非空,返回false + Assertions.assertFalse(BeanUtil.hasEmptyField(testPojo, "testPojo2List")); + + testPojo.setTestPojo2List(new TestPojo2[0]); + // 所有字段都有值 + Assertions.assertFalse(BeanUtil.hasEmptyField(testPojo)); + + // 给空字段值 + testPojo.setName(""); + Assertions.assertTrue(BeanUtil.hasEmptyField(testPojo)); + } + + @Test + void isEmptyTest() { + Assertions.assertTrue(BeanUtil.isEmpty(null)); + + final TestPojo testPojo = new TestPojo(); + Assertions.assertTrue(BeanUtil.isEmpty(testPojo)); + + testPojo.setName("test"); + Assertions.assertFalse(BeanUtil.isEmpty(testPojo)); + // 忽略name属性判断 + Assertions.assertTrue(BeanUtil.isEmpty(testPojo, "name")); + + testPojo.setTestPojo2List(new TestPojo2[0]); + // 所有字段都有值 + Assertions.assertFalse(BeanUtil.isEmpty(testPojo)); + } + @Data public static class TestPojo { private String name;