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 bf960cc91..f16e7d81c 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 @@ -23,7 +23,7 @@ public class BooleanUtil { * 取相反值 * * @param bool Boolean值 - * @return 相反的Boolean值 + * @return 相反的Boolean值,如果传入 {@code null} 则返回 {@code null} */ public static Boolean negate(final Boolean bool) { if (bool == null) { @@ -42,7 +42,7 @@ public class BooleanUtil { * * * @param bool 被检查的Boolean值 - * @return 当值为true且非null时返回{@code true} + * @return 当值非 {@code null} 且为 {@code true} 时返回 {@code true} */ public static boolean isTrue(final Boolean bool) { return Boolean.TRUE.equals(bool); @@ -58,7 +58,7 @@ public class BooleanUtil { * * * @param bool 被检查的Boolean值 - * @return 当值为false且非null时返回{@code true} + * @return 当值非 {@code null} 且为 {@code false} 时返回 {@code true} */ public static boolean isFalse(final Boolean bool) { return Boolean.FALSE.equals(bool); @@ -67,8 +67,8 @@ public class BooleanUtil { /** * 取相反值 * - * @param bool Boolean值 - * @return 相反的Boolean值 + * @param bool boolean值 + * @return 相反的boolean值 */ public static boolean negate(final boolean bool) { return !bool; @@ -76,35 +76,35 @@ public class BooleanUtil { /** * 转换字符串为boolean值 + *
该字符串 是否在 {@link #TRUE_SET} 中,存在则为 {@code true},否则为 {@code false}
* - * @param valueStr 字符串 + * @param valueStr 字符串,不区分大小写,前后可以有空格 {@link String#trim()} * @return boolean值 */ - public static boolean toBoolean(String valueStr) { + public static boolean toBoolean(final String valueStr) { if (StrUtil.isNotBlank(valueStr)) { - valueStr = valueStr.trim().toLowerCase(); - return TRUE_SET.contains(valueStr); + return TRUE_SET.contains(valueStr.trim().toLowerCase()); } return false; } /** - * 转换字符串为boolean值* BooleanUtil.and(true, true) = true @@ -345,8 +345,9 @@ public class BooleanUtil { * BooleanUtil.and(true, true, true) = true ** - * @param array {@code Boolean}数组 - * @return 取与为真返回{@code true} + * @param array {@code boolean}数组 + * @return 数组所有元素相 与 的结果 + * @throws IllegalArgumentException 如果数组为空 */ public static boolean and(final boolean... array) { if (ArrayUtil.isEmpty(array)) { @@ -361,7 +362,8 @@ public class BooleanUtil { } /** - * 对Boolean数组取与 + * Boolean数组所有元素相 与 的结果 + *
注意:{@code null} 元素 被当作 {@code true}
* ** BooleanUtil.and(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE @@ -370,10 +372,12 @@ public class BooleanUtil { * BooleanUtil.and(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE * BooleanUtil.and(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE * BooleanUtil.and(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtil.and(Boolean.TRUE, null) = Boolean.TRUE ** * @param array {@code Boolean}数组 - * @return 取与为真返回{@code true} + * @return 数组所有元素相 与 的结果 + * @throws IllegalArgumentException 如果数组为空 */ public static Boolean andOfWrap(final Boolean... array) { if (ArrayUtil.isEmpty(array)) { @@ -389,7 +393,7 @@ public class BooleanUtil { } /** - * 对Boolean数组取或 + * boolean数组所有元素 或 的结果 * *
* BooleanUtil.or(true, true) = true @@ -400,8 +404,9 @@ public class BooleanUtil { * BooleanUtil.or(false, false, false) = false ** - * @param array {@code Boolean}数组 - * @return 取或为真返回{@code true} + * @param array {@code boolean}数组 + * @return 数组所有元素 或 的结果 + * @throws IllegalArgumentException 如果数组为空 */ public static boolean or(final boolean... array) { if (ArrayUtil.isEmpty(array)) { @@ -416,7 +421,8 @@ public class BooleanUtil { } /** - * 对Boolean数组取或 + * Boolean数组所有元素 或 的结果 + *
注意:{@code null} 元素 被当作 {@code false}
* ** BooleanUtil.or(Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE @@ -426,10 +432,12 @@ public class BooleanUtil { * BooleanUtil.or(Boolean.FALSE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE * BooleanUtil.or(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE) = Boolean.TRUE * BooleanUtil.or(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtil.or(Boolean.FALSE, null) = Boolean.FALSE ** * @param array {@code Boolean}数组 - * @return 取或为真返回{@code true} + * @return 数组所有元素 或 的结果 + * @throws IllegalArgumentException 如果数组为空 */ public static Boolean orOfWrap(final Boolean... array) { if (ArrayUtil.isEmpty(array)) { @@ -445,19 +453,21 @@ public class BooleanUtil { } /** - * 对Boolean数组取异或 + * 对boolean数组取异或 * *
* BooleanUtil.xor(true, true) = false * BooleanUtil.xor(false, false) = false * BooleanUtil.xor(true, false) = true - * BooleanUtil.xor(true, true) = false - * BooleanUtil.xor(false, false) = false - * BooleanUtil.xor(true, false) = true + * BooleanUtil.xor(true, true, true) = true + * BooleanUtil.xor(false, false, false) = false + * BooleanUtil.xor(true, true, false) = false + * BooleanUtil.xor(true, false, false) = true ** * @param array {@code boolean}数组 * @return 如果异或计算为true返回 {@code true} + * @throws IllegalArgumentException 如果数组为空 */ public static boolean xor(final boolean... array) { if (ArrayUtil.isEmpty(array)) { @@ -476,20 +486,31 @@ public class BooleanUtil { * 对Boolean数组取异或 * *
- * BooleanUtil.xor(new Boolean[] { Boolean.TRUE, Boolean.TRUE }) = Boolean.FALSE - * BooleanUtil.xor(new Boolean[] { Boolean.FALSE, Boolean.FALSE }) = Boolean.FALSE - * BooleanUtil.xor(new Boolean[] { Boolean.TRUE, Boolean.FALSE }) = Boolean.TRUE + * BooleanUtil.xor(Boolean.TRUE, Boolean.TRUE) = Boolean.FALSE + * BooleanUtil.xor(Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtil.xor(Boolean.TRUE, Boolean.FALSE) = Boolean.TRUE + * BooleanUtil.xor(Boolean.TRUE, Boolean.TRUE, Boolean.TRUE) = Boolean.TRUE + * BooleanUtil.xor(Boolean.FALSE, Boolean.FALSE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtil.xor(Boolean.TRUE, Boolean.TRUE, Boolean.FALSE) = Boolean.FALSE + * BooleanUtil.xor(Boolean.TRUE, Boolean.FALSE, Boolean.FALSE) = Boolean.TRUE ** * @param array {@code Boolean} 数组 - * @return 异或为真取{@code true} + * @return 异或为真取 {@code true} + * @throws IllegalArgumentException 如果数组为空 + * @see #xor(boolean...) */ public static Boolean xorOfWrap(final Boolean... array) { if (ArrayUtil.isEmpty(array)) { throw new IllegalArgumentException("The Array must not be empty !"); } - final boolean[] primitive = Convert.convert(boolean[].class, array); - return xor(primitive); + + boolean result = false; + for (final Boolean element : array) { + result ^= element; + } + + return result; } /** 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 0d87f7357..d143ab03b 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 @@ -37,7 +37,16 @@ public class BooleanUtilTest { @Test public void xorTest() { Assert.assertTrue(BooleanUtil.xor(true, false)); + Assert.assertTrue(BooleanUtil.xor(true, true, true)); + Assert.assertFalse(BooleanUtil.xor(true, true, false)); + Assert.assertTrue(BooleanUtil.xor(true, false, false)); + Assert.assertFalse(BooleanUtil.xor(false, false, false)); + Assert.assertTrue(BooleanUtil.xorOfWrap(true, false)); + Assert.assertTrue(BooleanUtil.xorOfWrap(true, true, true)); + Assert.assertFalse(BooleanUtil.xorOfWrap(true, true, false)); + Assert.assertTrue(BooleanUtil.xorOfWrap(true, false, false)); + Assert.assertFalse(BooleanUtil.xorOfWrap(false, false, false)); } @SuppressWarnings("ConstantConditions")