From b2c90d7d88ebb72579f56dada2f2d3b247399b32 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 13 Sep 2022 11:17:13 +0800 Subject: [PATCH] =?UTF-8?q?BooleanUtil=E7=9A=84andOfWrap=E5=92=8CorOfWrap(?= =?UTF-8?q?)=E5=BF=BD=E7=95=A5null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 3 +- .../java/cn/hutool/core/util/BooleanUtil.java | 18 +++++++-- .../hutool/core/util/PrimitiveArrayUtil.java | 3 +- .../cn/hutool/core/util/BooleanUtilTest.java | 37 ++++++++++++++++++- 4 files changed, 53 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1565011b6..45c9d46f1 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,9 +3,10 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.7.M1 (2022-09-07) +# 5.8.7.M1 (2022-09-13) ### 🐣新特性 +* 【core 】 BooleanUtil的andOfWrap和orOfWrap()忽略null(issue#2599@Github) ### 🐞Bug修复 ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java index 2d215e82b..68d41acf5 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/BooleanUtil.java @@ -356,8 +356,13 @@ public class BooleanUtil { if (ArrayUtil.isEmpty(array)) { throw new IllegalArgumentException("The Array must not be empty !"); } - final boolean[] primitive = Convert.convert(boolean[].class, array); - return and(primitive); + + for (final Boolean b : array) { + if(isFalse(b)){ + return false; + } + } + return true; } /** @@ -407,8 +412,13 @@ public class BooleanUtil { if (ArrayUtil.isEmpty(array)) { throw new IllegalArgumentException("The Array must not be empty !"); } - final boolean[] primitive = Convert.convert(boolean[].class, array); - return or(primitive); + + for (final Boolean b : array) { + if(isTrue(b)){ + return true; + } + } + return false; } /** diff --git a/hutool-core/src/main/java/cn/hutool/core/util/PrimitiveArrayUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/PrimitiveArrayUtil.java index fd9392ce1..c11a21dea 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/PrimitiveArrayUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/PrimitiveArrayUtil.java @@ -1283,7 +1283,8 @@ public class PrimitiveArrayUtil { } /** - * 包装类数组转为原始类型数组 + * 包装类数组转为原始类型数组
+ * {@code null} 按照 {@code false} 对待 * * @param values 包装类型数组 * @return 原始类型数组 diff --git a/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java index c6f458596..e6f409de3 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/BooleanUtilTest.java @@ -4,7 +4,7 @@ import org.junit.Assert; import org.junit.Test; public class BooleanUtilTest { - + @Test public void toBooleanTest() { Assert.assertTrue(BooleanUtil.toBoolean("true")); @@ -16,7 +16,7 @@ public class BooleanUtilTest { Assert.assertTrue(BooleanUtil.toBoolean("是")); Assert.assertTrue(BooleanUtil.toBoolean("对")); Assert.assertTrue(BooleanUtil.toBoolean("真")); - + Assert.assertFalse(BooleanUtil.toBoolean("false")); Assert.assertFalse(BooleanUtil.toBoolean("6455434")); Assert.assertFalse(BooleanUtil.toBoolean("")); @@ -39,4 +39,37 @@ public class BooleanUtilTest { Assert.assertTrue(BooleanUtil.xor(true,false)); Assert.assertTrue(BooleanUtil.xorOfWrap(true,false)); } + + public void orOfWrapTest() { + Assert.assertFalse(BooleanUtil.orOfWrap(Boolean.FALSE, null)); + Assert.assertTrue(BooleanUtil.orOfWrap(Boolean.TRUE, null)); + } + + @SuppressWarnings("ConstantConditions") + @Test + public void isTrueIsFalseTest() { + Assert.assertFalse(BooleanUtil.isTrue(null)); + Assert.assertFalse(BooleanUtil.isFalse(null)); + } + + @SuppressWarnings("ConstantConditions") + public void negateTest() { + Assert.assertFalse(BooleanUtil.negate(Boolean.TRUE)); + Assert.assertTrue(BooleanUtil.negate(Boolean.FALSE)); + + Assert.assertFalse(BooleanUtil.negate(Boolean.TRUE.booleanValue())); + Assert.assertTrue(BooleanUtil.negate(Boolean.FALSE.booleanValue())); + } + + @Test + public void toStringTest() { + Assert.assertEquals("true", BooleanUtil.toStringTrueFalse(true)); + Assert.assertEquals("false", BooleanUtil.toStringTrueFalse(false)); + + Assert.assertEquals("yes", BooleanUtil.toStringYesNo(true)); + Assert.assertEquals("no", BooleanUtil.toStringYesNo(false)); + + Assert.assertEquals("on", BooleanUtil.toStringOnOff(true)); + Assert.assertEquals("off", BooleanUtil.toStringOnOff(false)); + } }