From b35e0981ec25f4674af024a30b6ef58fe39dd252 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 5 Jul 2022 23:20:36 +0800 Subject: [PATCH] add methods --- .../cn/hutool/core/collection/CollUtil.java | 16 ++-- .../java/cn/hutool/core/convert/Convert.java | 15 ++++ .../core/convert/NumberChineseFormatter.java | 84 ++++++++++++++++++- .../java/cn/hutool/core/text/StrChecker.java | 18 +++- .../hutool/core/collection/CollUtilTest.java | 49 +++++++++++ .../convert/NumberChineseFormatterTest.java | 23 +++++ .../cn/hutool/core/text/StrCheckerTest.java | 43 ++++++++++ 7 files changed, 240 insertions(+), 8 deletions(-) create mode 100644 hutool-core/src/test/java/cn/hutool/core/text/StrCheckerTest.java diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index 555fc7533..2bbf0fe8d 100755 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -305,9 +305,9 @@ public class CollUtil { */ public static Collection union(final Collection coll1, final Collection coll2) { if (isEmpty(coll1)) { - return new ArrayList<>(coll2); + return ListUtil.of(coll2); } else if (isEmpty(coll2)) { - return new ArrayList<>(coll1); + return ListUtil.of(coll1); } final ArrayList list = new ArrayList<>(Math.max(coll1.size(), coll2.size())); @@ -341,7 +341,9 @@ public class CollUtil { public static Collection union(final Collection coll1, final Collection coll2, final Collection... otherColls) { Collection union = union(coll1, coll2); for (final Collection coll : otherColls) { - union = union(union, coll); + if (isNotEmpty(coll)) { + union = union(union, coll); + } } return union; } @@ -373,7 +375,9 @@ public class CollUtil { if (ArrayUtil.isNotEmpty(otherColls)) { for (final Collection otherColl : otherColls) { - result.addAll(otherColl); + if (isNotEmpty(otherColl)) { + result.addAll(otherColl); + } } } @@ -407,7 +411,9 @@ public class CollUtil { if (ArrayUtil.isNotEmpty(otherColls)) { for (final Collection otherColl : otherColls) { - result.addAll(otherColl); + if (isNotEmpty(otherColl)) { + result.addAll(otherColl); + } } } diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java b/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java index 260b618a5..915f23ec4 100755 --- a/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/Convert.java @@ -1028,6 +1028,21 @@ public class Convert { return NumberChineseFormatter.format(n.doubleValue(), true, true); } + /** + * 中文大写数字金额转换为数字,返回结果以元为单位的BigDecimal类型数字 + * + * 如: + * “陆万柒仟伍佰伍拾陆元叁角贰分”返回“67556.32” + * “叁角贰分”返回“0.32” + * + * @param chineseMoneyAmount 中文大写数字金额 + * @return 返回结果以元为单位的BigDecimal类型数字 + * @since 5.8.5 + */ + public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount){ + return NumberChineseFormatter.chineseMoneyToNumber(chineseMoneyAmount); + } + // -------------------------------------------------------------------------- 数字转换 /** * int转byte diff --git a/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java b/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java index 4f1db677e..61ccd91a5 100644 --- a/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java +++ b/hutool-core/src/main/java/cn/hutool/core/convert/NumberChineseFormatter.java @@ -1,9 +1,12 @@ package cn.hutool.core.convert; import cn.hutool.core.lang.Assert; -import cn.hutool.core.util.ArrayUtil; import cn.hutool.core.math.NumberUtil; import cn.hutool.core.text.StrUtil; +import cn.hutool.core.util.ArrayUtil; + +import java.math.BigDecimal; +import java.math.RoundingMode; /** * 数字转中文类
@@ -426,6 +429,85 @@ public class NumberChineseFormatter { return result + section + number; } + /** + * 中文大写数字金额转换为数字,返回结果以元为单位的BigDecimal类型数字 + * + * 如: + * “陆万柒仟伍佰伍拾陆元叁角贰分”返回“67556.32” + * “叁角贰分”返回“0.32” + * + * @param chineseMoneyAmount 中文大写数字金额 + * @return 返回结果以元为单位的BigDecimal类型数字 + */ + public static BigDecimal chineseMoneyToNumber(final String chineseMoneyAmount){ + if(StrUtil.isBlank(chineseMoneyAmount)){ + return null; + } + + int yi = chineseMoneyAmount.indexOf("元"); + if(yi == -1){ + yi = chineseMoneyAmount.indexOf("圆"); + } + final int ji = chineseMoneyAmount.indexOf("角"); + final int fi = chineseMoneyAmount.indexOf("分"); + + // 先找到单位为元的数字 + String yStr = null; + if(yi > 0) { + yStr = chineseMoneyAmount.substring(0, yi); + } + + // 再找到单位为角的数字 + String jStr = null; + if(ji > 0){ + if(yi >= 0){ + //前面有元,角肯定要在元后面 + if(ji > yi){ + jStr = chineseMoneyAmount.substring(yi+1, ji); + } + }else{ + //没有元,只有角 + jStr = chineseMoneyAmount.substring(0, ji); + } + } + + // 再找到单位为分的数字 + String fStr = null; + if(fi > 0){ + if(ji >= 0){ + //有角,分肯定在角后面 + if(fi > ji){ + fStr = chineseMoneyAmount.substring(ji+1, fi); + } + }else if(yi > 0){ + //没有角,有元,那就坐元后面找 + if(fi > yi){ + fStr = chineseMoneyAmount.substring(yi+1, fi); + } + }else { + //没有元、角,只有分 + fStr = chineseMoneyAmount.substring(0, fi); + } + } + + //元、角、分 + int y = 0, j = 0, f = 0; + if(StrUtil.isNotBlank(yStr)) { + y = NumberChineseFormatter.chineseToNumber(yStr); + } + if(StrUtil.isNotBlank(jStr)){ + j = NumberChineseFormatter.chineseToNumber(jStr); + } + if(StrUtil.isNotBlank(fStr)){ + f = NumberChineseFormatter.chineseToNumber(fStr); + } + + BigDecimal amount = new BigDecimal(y); + amount = amount.add(BigDecimal.valueOf(j).divide(BigDecimal.TEN, RoundingMode.HALF_UP)); + amount = amount.add(BigDecimal.valueOf(f).divide(BigDecimal.valueOf(100), RoundingMode.HALF_UP)); + return amount; + } + /** * 查找对应的权对象 * 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 index a00d3da28..a3603fbca 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/StrChecker.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/StrChecker.java @@ -98,7 +98,21 @@ public class StrChecker { * @see #isBlank(CharSequence) */ public static boolean isNotBlank(final CharSequence str) { - return false == isBlank(str); + final int length; + + if ((str == null) || ((length = str.length()) == 0)) { + // empty + return false; + } + + for (int i = 0; i < length; i++) { + // 只要有一个非空字符即为非空字符串 + if (false == CharUtil.isBlankChar(str.charAt(i))) { + return true; + } + } + + return false; } /** @@ -261,7 +275,7 @@ public class StrChecker { * @see #isEmpty(CharSequence) */ public static boolean isNotEmpty(final CharSequence str) { - return false == isEmpty(str); + return str != null && str.length() > 0; } /** 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 138fc304a..1db211d56 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 @@ -925,4 +925,53 @@ public class CollUtilTest { final Collection trans = CollUtil.trans(people, Person::getName); Assert.assertEquals("[aa, bb, cc, dd]", trans.toString()); } + + @Test + public void unionNullTest() { + final List list1 = new ArrayList<>(); + final List list2 = null; + final List list3 = null; + final Collection union = CollUtil.union(list1, list2, list3); + Assert.assertNotNull(union); + } + + @Test + public void unionDistinctNullTest() { + final List list1 = new ArrayList<>(); + final List list2 = null; + final List list3 = null; + final Set set = CollUtil.unionDistinct(list1, list2, list3); + Assert.assertNotNull(set); + } + + @Test + public void unionAllNullTest() { + final List list1 = new ArrayList<>(); + final List list2 = null; + final List list3 = null; + final List list = CollUtil.unionAll(list1, list2, list3); + Assert.assertNotNull(list); + } + + @Test + public void intersectionNullTest() { + final List list1 = new ArrayList<>(); + list1.add("aa"); + final List list2 = new ArrayList<>(); + list2.add("aa"); + final List list3 = null; + final Collection collection = CollUtil.intersection(list1, list2, list3); + Assert.assertNotNull(collection); + } + + @Test + public void intersectionDistinctNullTest() { + final List list1 = new ArrayList<>(); + list1.add("aa"); + final List list2 = null; + // list2.add("aa"); + final List list3 = null; + final Collection collection = CollUtil.intersectionDistinct(list1, list2, list3); + Assert.assertNotNull(collection); + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/convert/NumberChineseFormatterTest.java b/hutool-core/src/test/java/cn/hutool/core/convert/NumberChineseFormatterTest.java index ddba78fda..ad7af507a 100644 --- a/hutool-core/src/test/java/cn/hutool/core/convert/NumberChineseFormatterTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/convert/NumberChineseFormatterTest.java @@ -342,4 +342,27 @@ public class NumberChineseFormatterTest { format = NumberChineseFormatter.format(1.02, false, false); Assert.assertEquals("一点零二", format); } + + @SuppressWarnings("ConstantConditions") + @Test + public void testChineseMoneyToNumber(){ + /* + * s=陆万柒仟伍佰伍拾陆圆, n=67556 + * s=陆万柒仟伍佰伍拾陆元, n=67556 + * s=叁角, n=0.3 + * s=贰分, n=0.02 + * s=陆万柒仟伍佰伍拾陆元叁角, n=67556.3 + * s=陆万柒仟伍佰伍拾陆元贰分, n=67556.02 + * s=叁角贰分, n=0.32 + * s=陆万柒仟伍佰伍拾陆元叁角贰分, n=67556.32 + */ + Assert.assertEquals(67556, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆圆").longValue()); + Assert.assertEquals(67556, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆元").longValue()); + Assert.assertEquals(0.3D, NumberChineseFormatter.chineseMoneyToNumber("叁角").doubleValue(), 2); + Assert.assertEquals(0.02, NumberChineseFormatter.chineseMoneyToNumber("贰分").doubleValue(), 2); + Assert.assertEquals(67556.3, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆元叁角").doubleValue(), 2); + Assert.assertEquals(67556.02, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆元贰分").doubleValue(), 2); + Assert.assertEquals(0.32, NumberChineseFormatter.chineseMoneyToNumber("叁角贰分").doubleValue(), 2); + Assert.assertEquals(67556.32, NumberChineseFormatter.chineseMoneyToNumber("陆万柒仟伍佰伍拾陆元叁角贰分").doubleValue(), 2); + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/text/StrCheckerTest.java b/hutool-core/src/test/java/cn/hutool/core/text/StrCheckerTest.java new file mode 100644 index 000000000..1d9ee4240 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/text/StrCheckerTest.java @@ -0,0 +1,43 @@ +package cn.hutool.core.text; + +import org.junit.Assert; +import org.junit.Test; + +public class StrCheckerTest { + + @Test + public void isEmptyTest() { + Assert.assertTrue(StrUtil.isEmpty(null)); + Assert.assertTrue(StrUtil.isEmpty("")); + + Assert.assertFalse(StrUtil.isEmpty(" \t\n")); + Assert.assertFalse(StrUtil.isEmpty("abc")); + } + + @Test + public void isNotEmptyTest() { + Assert.assertFalse(StrUtil.isNotEmpty(null)); + Assert.assertFalse(StrUtil.isNotEmpty("")); + + Assert.assertTrue(StrUtil.isNotEmpty(" \t\n")); + Assert.assertTrue(StrUtil.isNotEmpty("abc")); + } + + @Test + public void isBlankTest() { + Assert.assertTrue(StrUtil.isBlank(null)); + Assert.assertTrue(StrUtil.isBlank("")); + Assert.assertTrue(StrUtil.isBlank(" \t\n")); + + Assert.assertFalse(StrUtil.isBlank("abc")); + } + + @Test + public void isNotBlankTest() { + Assert.assertFalse(StrUtil.isNotBlank(null)); + Assert.assertFalse(StrUtil.isNotBlank("")); + Assert.assertFalse(StrUtil.isNotBlank(" \t\n")); + + Assert.assertTrue(StrUtil.isNotBlank("abc")); + } +}