diff --git a/hutool-core/src/main/java/cn/hutool/core/text/NamingCase.java b/hutool-core/src/main/java/cn/hutool/core/text/NamingCase.java index a35d78f1e..a5c7a5d52 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/NamingCase.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/NamingCase.java @@ -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); diff --git a/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java b/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java index 8c948e8a9..0c67d6522 100644 --- a/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/util/ArrayUtil.java @@ -368,6 +368,46 @@ public class ArrayUtil extends PrimitiveArrayUtil { } } + /** + * 将新元素插入到到已有数组中的某个位置
+ * 添加新元素会生成一个新数组或原有数组
+ * 如果插入位置为为负数,那么生成一个由插入元素顺序加已有数组顺序的新数组 + * + * @param 数组元素类型 + * @param buffer 已有数组 + * @param index 位置,大于长度追加,否则替换 + * @param values 新值 + * @return 新数组或原有数组 + */ + @SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"}) + public static 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; + } + /** * 将新元素插入到到已有数组中的某个位置
* 添加新元素会生成一个新的数组,不影响原数组
@@ -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 数组元素类型 + * @param subArray 子数组 + * @param 数组元素类型 * @return 最后一个子数组的开始位置,即子数字第一个元素在数组中的位置 * @since 5.4.8 */ diff --git a/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java index 3ba18becf..442ce4b91 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/ArrayUtilTest.java @@ -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); + } } diff --git a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java index ee1e1da50..2dcef2850 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java @@ -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))); }