mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix bug and add method
This commit is contained in:
parent
2bfa2cebf2
commit
e389227964
@ -12,11 +12,13 @@
|
||||
* 【core 】 增加Table实现(issue#2179@Github)
|
||||
* 【core 】 增加UniqueKeySet(issue#I4WUWR@Gitee)
|
||||
* 【core 】 阿拉伯数字转换成中文对发票票面金额转换的扩展(pr#570@Gitee)
|
||||
* 【core 】 ArrayUtil增加replace方法(pr#570@Gitee)
|
||||
*
|
||||
### 🐞Bug修复
|
||||
* 【core 】 修复ObjectUtil.hasNull传入null返回true的问题(pr#555@Gitee)
|
||||
* 【core 】 修复NumberConverter对数字转换的问题(issue#I4WPF4@Gitee)
|
||||
* 【core 】 修复ReflectUtil.getMethods获取接口方法问题(issue#I4WUWR@Gitee)
|
||||
* 【core 】 修复NamingCase中大写转换问题(pr#572@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
# 5.7.22 (2022-03-01)
|
||||
|
@ -68,6 +68,7 @@ public class NumberChineseFormatter {
|
||||
* @param unitName 单位名称 如:元、圆
|
||||
* @return java.lang.String
|
||||
* @author machuanpeng
|
||||
* @since 5.7.23
|
||||
*/
|
||||
public static String format(double amount, boolean isUseTraditional, boolean isMoneyMode, String negativeName, String unitName) {
|
||||
if (0 == amount) {
|
||||
|
@ -157,7 +157,7 @@ public class NamingCase {
|
||||
* 将连接符方式命名的字符串转换为驼峰式。如果转换前的下划线大写方式命名的字符串为空,则返回空字符串。
|
||||
*
|
||||
* @param name 转换前的自定义方式命名的字符串
|
||||
* @param symbol 连接符
|
||||
* @param symbol 原字符串中的连接符连接符
|
||||
* @return 转换后的驼峰式命名的字符串
|
||||
* @since 5.7.17
|
||||
*/
|
||||
|
@ -375,36 +375,38 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
*
|
||||
* @param <T> 数组元素类型
|
||||
* @param buffer 已有数组
|
||||
* @param index 位置,大于长度追加,否则替换
|
||||
* @param index 位置,大于长度追加,否则替换,<0表示从头部追加
|
||||
* @param values 新值
|
||||
* @return 新数组或原有数组
|
||||
* @since 5.7.23
|
||||
*/
|
||||
@SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
|
||||
@SuppressWarnings({"unchecked"})
|
||||
public static <T> T[] replace(T[] buffer, int index, T... values) {
|
||||
if (index < 0) {
|
||||
return insert(buffer, 0, values);
|
||||
}
|
||||
if (isEmpty(buffer) || index == 0 && isNotEmpty(values)) {
|
||||
return values;
|
||||
}
|
||||
if (index >= buffer.length || isEmpty(values)) {
|
||||
return append(buffer, values);
|
||||
}
|
||||
int replaceSpace = buffer.length - index - 1;
|
||||
if (replaceSpace > values.length) {
|
||||
for (int i = index - 1; i < values.length; i++) {
|
||||
Array.set(buffer, i + 1, values[i]);
|
||||
}
|
||||
if(isEmpty(values)){
|
||||
return buffer;
|
||||
}
|
||||
int newArrayLength = index + values.length;
|
||||
final T[] result = (T[]) Array.newInstance(buffer.getClass().getComponentType(), newArrayLength);
|
||||
System.arraycopy(buffer, 0, result, 0, index);
|
||||
int valueIndex = 0;
|
||||
for (int i = index; i < newArrayLength; i++) {
|
||||
Array.set(result, i, values[valueIndex]);
|
||||
valueIndex = valueIndex + 1;
|
||||
if(isEmpty(buffer)){
|
||||
return values;
|
||||
}
|
||||
if (index < 0) {
|
||||
// 从头部追加
|
||||
return insert(buffer, 0, values);
|
||||
}
|
||||
if (index >= buffer.length) {
|
||||
// 超出长度,尾部追加
|
||||
return append(buffer, values);
|
||||
}
|
||||
|
||||
if (buffer.length >= values.length + index) {
|
||||
System.arraycopy(values, 0, buffer, index, values.length);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// 替换长度大于原数组长度,新建数组
|
||||
int newArrayLength = index + values.length;
|
||||
final T[] result = newArray(buffer.getClass().getComponentType(), newArrayLength);
|
||||
System.arraycopy(buffer, 0, result, 0, index);
|
||||
System.arraycopy(values, 0, result, index, values.length);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -1,21 +1,44 @@
|
||||
package cn.hutool.core.text;
|
||||
|
||||
import cn.hutool.core.lang.Dict;
|
||||
import cn.hutool.core.util.CharUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NamingCaseTest {
|
||||
|
||||
@Test
|
||||
public void toUnderlineCaseTest(){
|
||||
// https://github.com/dromara/hutool/issues/2070
|
||||
public void toCamelCaseTest() {
|
||||
Dict.create()
|
||||
.set("customerNickV2", "customer_nick_v2")
|
||||
.forEach((key, value) -> Assert.assertEquals(value, NamingCase.toUnderlineCase(key)));
|
||||
.set("Table_Test_Of_day","tableTestOfDay")
|
||||
.set("TableTestOfDay","TableTestOfDay")
|
||||
.set("abc_1d","abc1d")
|
||||
.forEach((key, value) -> Assert.assertEquals(value, NamingCase.toCamelCase(key)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUnderLineCaseTest2(){
|
||||
final String wPRunOZTime = NamingCase.toUnderlineCase("wPRunOZTime");
|
||||
Assert.assertEquals("w_P_run_OZ_time", wPRunOZTime);
|
||||
public void toCamelCaseFromDashedTest() {
|
||||
Dict.create()
|
||||
.set("Table-Test-Of-day","tableTestOfDay")
|
||||
.forEach((key, value) -> Assert.assertEquals(value, NamingCase.toCamelCase(key, CharUtil.DASHED)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUnderLineCaseTest() {
|
||||
Dict.create()
|
||||
.set("Table_Test_Of_day", "table_test_of_day")
|
||||
.set("_Table_Test_Of_day_", "_table_test_of_day_")
|
||||
.set("_Table_Test_Of_DAY_", "_table_test_of_DAY_")
|
||||
.set("_TableTestOfDAYToday", "_table_test_of_DAY_today")
|
||||
.set("HelloWorld_test", "hello_world_test")
|
||||
.set("H2", "H2")
|
||||
.set("H#case", "H#case")
|
||||
.set("PNLabel", "PN_label")
|
||||
.set("wPRunOZTime", "w_P_run_OZ_time")
|
||||
// https://github.com/dromara/hutool/issues/2070
|
||||
.set("customerNickV2", "customer_nick_v2")
|
||||
// https://gitee.com/dromara/hutool/issues/I4X9TT
|
||||
.set("DEPT_NAME","DEPT_NAME")
|
||||
.forEach((key, value) -> Assert.assertEquals(value, NamingCase.toUnderlineCase(key)));
|
||||
}
|
||||
}
|
||||
|
@ -472,36 +472,32 @@ public class ArrayUtilTest {
|
||||
String[] a = {"1", "2", "3", "4"};
|
||||
String[] b = {"a", "b", "c"};
|
||||
|
||||
// 在小于0的位置,-1位置插入,返回b+a
|
||||
// 在小于0的位置,-1位置插入,返回b+a,新数组
|
||||
String[] result = ArrayUtil.replace(a, -1, b);
|
||||
Assert.assertArrayEquals(new String[]{"a", "b", "c", "1", "2", "3", "4"}, result);
|
||||
|
||||
// 在第0个位置插入,即覆盖a,直接返回b
|
||||
result = ArrayUtil.replace(a, 0, b);
|
||||
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
|
||||
// 在第0个位置开始替换,返回a
|
||||
result = ArrayUtil.replace(ArrayUtil.clone(a), 0, b);
|
||||
Assert.assertArrayEquals(new String[]{"a", "b", "c", "4"}, result);
|
||||
|
||||
// 在第1个位置插入,即"2"之前
|
||||
result = ArrayUtil.replace(a, 1, b);
|
||||
// 在第1个位置替换,即"2"开始
|
||||
result = ArrayUtil.replace(ArrayUtil.clone(a), 1, b);
|
||||
Assert.assertArrayEquals(new String[]{"1", "a", "b", "c"}, result);
|
||||
|
||||
//上一步测试修改了原数组结构
|
||||
String[] c = {"1", "2", "3", "4"};
|
||||
String[] d = {"a", "b", "c"};
|
||||
|
||||
// 在第2个位置插入,即"3"之后
|
||||
result = ArrayUtil.replace(c, 2, d);
|
||||
result = ArrayUtil.replace(ArrayUtil.clone(a), 2, b);
|
||||
Assert.assertArrayEquals(new String[]{"1", "2", "a", "b", "c"}, result);
|
||||
|
||||
// 在第3个位置插入,即"4"之后
|
||||
result = ArrayUtil.replace(c, 3, d);
|
||||
result = ArrayUtil.replace(ArrayUtil.clone(a), 3, b);
|
||||
Assert.assertArrayEquals(new String[]{"1", "2", "3", "a", "b", "c"}, result);
|
||||
|
||||
// 在第4个位置插入,数组长度为4,在索引4出替换即两个数组相加
|
||||
result = ArrayUtil.replace(c, 4, d);
|
||||
result = ArrayUtil.replace(ArrayUtil.clone(a), 4, b);
|
||||
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
|
||||
|
||||
// 在大于3个位置插入,数组长度为4,即两个数组相加
|
||||
result = ArrayUtil.replace(c, 5, d);
|
||||
result = ArrayUtil.replace(ArrayUtil.clone(a), 5, b);
|
||||
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
|
||||
|
||||
String[] e = null;
|
||||
@ -509,13 +505,13 @@ public class ArrayUtilTest {
|
||||
|
||||
// e为null 返回 f
|
||||
result = ArrayUtil.replace(e, -1, f);
|
||||
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
|
||||
Assert.assertArrayEquals(f, result);
|
||||
|
||||
String[] g = {"a", "b", "c"};
|
||||
String[] h = null;
|
||||
|
||||
// h为null 返回 g
|
||||
result = ArrayUtil.replace(g, 0, h);
|
||||
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
|
||||
Assert.assertArrayEquals(g, result);
|
||||
}
|
||||
}
|
||||
|
@ -391,41 +391,6 @@ public class StrUtilTest {
|
||||
Assert.assertEquals(text, str);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toCamelCaseTest() {
|
||||
String str = "Table_Test_Of_day";
|
||||
String result = StrUtil.toCamelCase(str);
|
||||
Assert.assertEquals("tableTestOfDay", result);
|
||||
|
||||
String str1 = "TableTestOfDay";
|
||||
String result1 = StrUtil.toCamelCase(str1);
|
||||
Assert.assertEquals("TableTestOfDay", result1);
|
||||
|
||||
String abc1d = StrUtil.toCamelCase("abc_1d");
|
||||
Assert.assertEquals("abc1d", abc1d);
|
||||
|
||||
|
||||
String str2 = "Table-Test-Of-day";
|
||||
String result2 = StrUtil.toCamelCase(str2, CharUtil.DASHED);
|
||||
System.out.println(result2);
|
||||
Assert.assertEquals("tableTestOfDay", result2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toUnderLineCaseTest() {
|
||||
Dict.create()
|
||||
.set("Table_Test_Of_day", "table_test_of_day")
|
||||
.set("_Table_Test_Of_day_", "_table_test_of_day_")
|
||||
.set("_Table_Test_Of_DAY_", "_table_test_of_DAY_")
|
||||
.set("_TableTestOfDAYToday", "_table_test_of_DAY_today")
|
||||
.set("HelloWorld_test", "hello_world_test")
|
||||
.set("H2", "H2")
|
||||
.set("H#case", "H#case")
|
||||
.set("PNLabel", "PN_label")
|
||||
.set("DEPT_NAME","DEPT_NAME")
|
||||
.forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void containsAnyTest() {
|
||||
//字符
|
||||
|
Loading…
x
Reference in New Issue
Block a user