add methods

This commit is contained in:
Looly 2022-07-05 23:20:36 +08:00
parent 440e3ebc2a
commit b35e0981ec
7 changed files with 240 additions and 8 deletions

View File

@ -305,9 +305,9 @@ public class CollUtil {
*/
public static <T> Collection<T> union(final Collection<T> coll1, final Collection<T> 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<T> list = new ArrayList<>(Math.max(coll1.size(), coll2.size()));
@ -341,7 +341,9 @@ public class CollUtil {
public static <T> Collection<T> union(final Collection<T> coll1, final Collection<T> coll2, final Collection<T>... otherColls) {
Collection<T> union = union(coll1, coll2);
for (final Collection<T> 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<T> 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<T> otherColl : otherColls) {
result.addAll(otherColl);
if (isNotEmpty(otherColl)) {
result.addAll(otherColl);
}
}
}

View File

@ -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

View File

@ -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;
/**
* 数字转中文类<br>
@ -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;
}
/**
* 查找对应的权对象
*

View File

@ -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;
}
/**

View File

@ -925,4 +925,53 @@ public class CollUtilTest {
final Collection<String> trans = CollUtil.trans(people, Person::getName);
Assert.assertEquals("[aa, bb, cc, dd]", trans.toString());
}
@Test
public void unionNullTest() {
final List<String> list1 = new ArrayList<>();
final List<String> list2 = null;
final List<String> list3 = null;
final Collection<String> union = CollUtil.union(list1, list2, list3);
Assert.assertNotNull(union);
}
@Test
public void unionDistinctNullTest() {
final List<String> list1 = new ArrayList<>();
final List<String> list2 = null;
final List<String> list3 = null;
final Set<String> set = CollUtil.unionDistinct(list1, list2, list3);
Assert.assertNotNull(set);
}
@Test
public void unionAllNullTest() {
final List<String> list1 = new ArrayList<>();
final List<String> list2 = null;
final List<String> list3 = null;
final List<String> list = CollUtil.unionAll(list1, list2, list3);
Assert.assertNotNull(list);
}
@Test
public void intersectionNullTest() {
final List<String> list1 = new ArrayList<>();
list1.add("aa");
final List<String> list2 = new ArrayList<>();
list2.add("aa");
final List<String> list3 = null;
final Collection<String> collection = CollUtil.intersection(list1, list2, list3);
Assert.assertNotNull(collection);
}
@Test
public void intersectionDistinctNullTest() {
final List<String> list1 = new ArrayList<>();
list1.add("aa");
final List<String> list2 = null;
// list2.add("aa");
final List<String> list3 = null;
final Collection<String> collection = CollUtil.intersectionDistinct(list1, list2, list3);
Assert.assertNotNull(collection);
}
}

View File

@ -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);
}
}

View File

@ -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"));
}
}