mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add methods
This commit is contained in:
parent
440e3ebc2a
commit
b35e0981ec
@ -305,9 +305,9 @@ public class CollUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T> Collection<T> union(final Collection<T> coll1, final Collection<T> coll2) {
|
public static <T> Collection<T> union(final Collection<T> coll1, final Collection<T> coll2) {
|
||||||
if (isEmpty(coll1)) {
|
if (isEmpty(coll1)) {
|
||||||
return new ArrayList<>(coll2);
|
return ListUtil.of(coll2);
|
||||||
} else if (isEmpty(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()));
|
final ArrayList<T> list = new ArrayList<>(Math.max(coll1.size(), coll2.size()));
|
||||||
@ -341,8 +341,10 @@ public class CollUtil {
|
|||||||
public static <T> Collection<T> union(final Collection<T> coll1, final Collection<T> coll2, final Collection<T>... otherColls) {
|
public static <T> Collection<T> union(final Collection<T> coll1, final Collection<T> coll2, final Collection<T>... otherColls) {
|
||||||
Collection<T> union = union(coll1, coll2);
|
Collection<T> union = union(coll1, coll2);
|
||||||
for (final Collection<T> coll : otherColls) {
|
for (final Collection<T> coll : otherColls) {
|
||||||
|
if (isNotEmpty(coll)) {
|
||||||
union = union(union, coll);
|
union = union(union, coll);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return union;
|
return union;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -373,9 +375,11 @@ public class CollUtil {
|
|||||||
|
|
||||||
if (ArrayUtil.isNotEmpty(otherColls)) {
|
if (ArrayUtil.isNotEmpty(otherColls)) {
|
||||||
for (final Collection<T> otherColl : otherColls) {
|
for (final Collection<T> otherColl : otherColls) {
|
||||||
|
if (isNotEmpty(otherColl)) {
|
||||||
result.addAll(otherColl);
|
result.addAll(otherColl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -407,9 +411,11 @@ public class CollUtil {
|
|||||||
|
|
||||||
if (ArrayUtil.isNotEmpty(otherColls)) {
|
if (ArrayUtil.isNotEmpty(otherColls)) {
|
||||||
for (final Collection<T> otherColl : otherColls) {
|
for (final Collection<T> otherColl : otherColls) {
|
||||||
|
if (isNotEmpty(otherColl)) {
|
||||||
result.addAll(otherColl);
|
result.addAll(otherColl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1028,6 +1028,21 @@ public class Convert {
|
|||||||
return NumberChineseFormatter.format(n.doubleValue(), true, true);
|
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
|
* int转byte
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package cn.hutool.core.convert;
|
package cn.hutool.core.convert;
|
||||||
|
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
|
||||||
import cn.hutool.core.math.NumberUtil;
|
import cn.hutool.core.math.NumberUtil;
|
||||||
import cn.hutool.core.text.StrUtil;
|
import cn.hutool.core.text.StrUtil;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 数字转中文类<br>
|
* 数字转中文类<br>
|
||||||
@ -426,6 +429,85 @@ public class NumberChineseFormatter {
|
|||||||
return result + section + number;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查找对应的权对象
|
* 查找对应的权对象
|
||||||
*
|
*
|
||||||
|
@ -98,7 +98,21 @@ public class StrChecker {
|
|||||||
* @see #isBlank(CharSequence)
|
* @see #isBlank(CharSequence)
|
||||||
*/
|
*/
|
||||||
public static boolean isNotBlank(final CharSequence str) {
|
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)
|
* @see #isEmpty(CharSequence)
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final CharSequence str) {
|
public static boolean isNotEmpty(final CharSequence str) {
|
||||||
return false == isEmpty(str);
|
return str != null && str.length() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -925,4 +925,53 @@ public class CollUtilTest {
|
|||||||
final Collection<String> trans = CollUtil.trans(people, Person::getName);
|
final Collection<String> trans = CollUtil.trans(people, Person::getName);
|
||||||
Assert.assertEquals("[aa, bb, cc, dd]", trans.toString());
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -342,4 +342,27 @@ public class NumberChineseFormatterTest {
|
|||||||
format = NumberChineseFormatter.format(1.02, false, false);
|
format = NumberChineseFormatter.format(1.02, false, false);
|
||||||
Assert.assertEquals("一点零二", format);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user