!572 针对issue#I4X9TT修复

Merge pull request !572 from Husky/v5-dev
This commit is contained in:
Looly 2022-03-13 15:47:08 +00:00 committed by Gitee
commit 2bfa2cebf2
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 107 additions and 14 deletions

View File

@ -97,7 +97,7 @@ public class NamingCase {
// 后一个为大写按照专有名词对待如aBC -> a_BC
} else {
//前一个为大写
if (null == nextChar || Character.isLowerCase(nextChar)) {
if (null != nextChar && Character.isLowerCase(nextChar)) {
// 普通首字母大写如ABcc -> A_bcc
sb.append(symbol);
c = Character.toLowerCase(c);

View File

@ -368,6 +368,46 @@ public class ArrayUtil extends PrimitiveArrayUtil {
}
}
/**
* 将新元素插入到到已有数组中的某个位置<br>
* 添加新元素会生成一个新数组或原有数组<br>
* 如果插入位置为为负数那么生成一个由插入元素顺序加已有数组顺序的新数组
*
* @param <T> 数组元素类型
* @param buffer 已有数组
* @param index 位置大于长度追加否则替换
* @param values 新值
* @return 新数组或原有数组
*/
@SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
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]);
}
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;
}
return result;
}
/**
* 将新元素插入到到已有数组中的某个位置<br>
* 添加新元素会生成一个新的数组不影响原数组<br>
@ -1540,7 +1580,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 4.5.18
*/
public static boolean isAllEmpty(Object... args) {
for (Object obj: args) {
for (Object obj : args) {
if (false == ObjectUtil.isEmpty(obj)) {
return false;
}
@ -1758,10 +1798,10 @@ public class ArrayUtil extends PrimitiveArrayUtil {
/**
* 查找最后一个子数组的开始位置
*
* @param array 数组
* @param array 数组
* @param endInclude 查找结束的位置包含
* @param subArray 子数组
* @param <T> 数组元素类型
* @param subArray 子数组
* @param <T> 数组元素类型
* @return 最后一个子数组的开始位置即子数字第一个元素在数组中的位置
* @since 5.4.8
*/

View File

@ -363,7 +363,7 @@ public class ArrayUtilTest {
}
@Test
public void indexOfSubTest2(){
public void indexOfSubTest2() {
Integer[] a = {0x12, 0x56, 0x34, 0x56, 0x78, 0x9A};
Integer[] b = {0x56, 0x78};
int i = ArrayUtil.indexOfSub(a, b);
@ -401,7 +401,7 @@ public class ArrayUtilTest {
}
@Test
public void lastIndexOfSubTest2(){
public void lastIndexOfSubTest2() {
Integer[] a = {0x12, 0x56, 0x78, 0x56, 0x21, 0x9A};
Integer[] b = {0x56, 0x78};
int i = ArrayUtil.indexOfSub(a, b);
@ -409,17 +409,17 @@ public class ArrayUtilTest {
}
@Test
public void reverseTest(){
int[] a = {1,2,3,4};
public void reverseTest() {
int[] a = {1, 2, 3, 4};
final int[] reverse = ArrayUtil.reverse(a);
Assert.assertArrayEquals(new int[]{4,3,2,1}, reverse);
Assert.assertArrayEquals(new int[]{4, 3, 2, 1}, reverse);
}
@Test
public void reverseTest2s(){
Object[] a = {"1",'2',"3",4};
public void reverseTest2s() {
Object[] a = {"1", '2', "3", 4};
final Object[] reverse = ArrayUtil.reverse(a);
Assert.assertArrayEquals(new Object[]{4,"3",'2',"1"}, reverse);
Assert.assertArrayEquals(new Object[]{4, "3", '2', "1"}, reverse);
}
@Test
@ -461,9 +461,61 @@ public class ArrayUtilTest {
}
@Test
public void getTest(){
public void getTest() {
String[] a = {"a", "b", "c"};
final Object o = ArrayUtil.get(a, -1);
Assert.assertEquals("c", o);
}
@Test
public void replaceTest() {
String[] a = {"1", "2", "3", "4"};
String[] b = {"a", "b", "c"};
// 在小于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);
// 在第1个位置插入"2"之前
result = ArrayUtil.replace(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);
Assert.assertArrayEquals(new String[]{"1", "2", "a", "b", "c"}, result);
// 在第3个位置插入"4"之后
result = ArrayUtil.replace(c, 3, d);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "a", "b", "c"}, result);
// 在第4个位置插入数组长度为4在索引4出替换即两个数组相加
result = ArrayUtil.replace(c, 4, d);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
// 在大于3个位置插入数组长度为4即两个数组相加
result = ArrayUtil.replace(c, 5, d);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
String[] e = null;
String[] f = {"a", "b", "c"};
// e为null 返回 f
result = ArrayUtil.replace(e, -1, f);
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, 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);
}
}

View File

@ -422,6 +422,7 @@ public class StrUtilTest {
.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)));
}