mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
d2b08250ff
commit
3e21370503
@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
|
|||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import cn.hutool.core.collection.UniqueKeySet;
|
import cn.hutool.core.collection.UniqueKeySet;
|
||||||
import cn.hutool.core.comparator.CompareUtil;
|
import cn.hutool.core.comparator.CompareUtil;
|
||||||
|
import cn.hutool.core.convert.Convert;
|
||||||
import cn.hutool.core.exceptions.UtilException;
|
import cn.hutool.core.exceptions.UtilException;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
@ -139,6 +140,20 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
return null == firstNonNull(array);
|
return null == firstNonNull(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否包含非{@code null}元素<br>
|
||||||
|
* 如果列表是{@code null}或者空,返回{@code false},否则当列表中有非{@code null}字符时返回{@code true}
|
||||||
|
*
|
||||||
|
* @param <T> 数组元素类型
|
||||||
|
* @param array 被检查的数组
|
||||||
|
* @return 是否包含非{@code null}元素
|
||||||
|
* @since 5.4.0
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> boolean hasNonNull(final T... array) {
|
||||||
|
return null != firstNonNull(array);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回数组中第一个非空元素
|
* 返回数组中第一个非空元素
|
||||||
*
|
*
|
||||||
@ -317,15 +332,18 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
* 将新元素添加到已有数组中<br>
|
* 将新元素添加到已有数组中<br>
|
||||||
* 添加新元素会生成一个新的数组,不影响原数组
|
* 添加新元素会生成一个新的数组,不影响原数组
|
||||||
*
|
*
|
||||||
|
* @param <A> 数组类型
|
||||||
* @param <T> 数组元素类型
|
* @param <T> 数组元素类型
|
||||||
* @param array 已有数组
|
* @param array 已有数组
|
||||||
* @param newElements 新元素
|
* @param newElements 新元素
|
||||||
* @return 新数组
|
* @return 新数组
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <T> Object append(final Object array, final T... newElements) {
|
public static <A, T> A append(final A array, final T... newElements) {
|
||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return newElements;
|
// 可变长参数可能为包装类型,如果array是原始类型,则此处强转不合适,采用万能转换器完成转换
|
||||||
|
return (A) Convert.convert(array.getClass(), newElements);
|
||||||
}
|
}
|
||||||
return insert(array, length(array), newElements);
|
return insert(array, length(array), newElements);
|
||||||
}
|
}
|
||||||
@ -357,13 +375,14 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
/**
|
/**
|
||||||
* 将元素值设置为数组的某个位置,当给定的index大于数组长度,则追加
|
* 将元素值设置为数组的某个位置,当给定的index大于数组长度,则追加
|
||||||
*
|
*
|
||||||
|
* @param <A> 数组类型
|
||||||
* @param array 已有数组
|
* @param array 已有数组
|
||||||
* @param index 位置,大于长度追加,否则替换
|
* @param index 位置,大于长度追加,否则替换
|
||||||
* @param value 新值
|
* @param value 新值
|
||||||
* @return 新数组或原有数组
|
* @return 新数组或原有数组
|
||||||
* @since 4.1.2
|
* @since 4.1.2
|
||||||
*/
|
*/
|
||||||
public static Object setOrAppend(final Object array, final int index, final Object value) {
|
public static <A> A setOrAppend(final A array, final int index, final Object value) {
|
||||||
if (index < length(array)) {
|
if (index < length(array)) {
|
||||||
Array.set(array, index, value);
|
Array.set(array, index, value);
|
||||||
return array;
|
return array;
|
||||||
@ -436,6 +455,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
* 添加新元素会生成一个新的数组,不影响原数组<br>
|
* 添加新元素会生成一个新的数组,不影响原数组<br>
|
||||||
* 如果插入位置为为负数,从原数组从后向前计数,若大于原数组长度,则空白处用null填充
|
* 如果插入位置为为负数,从原数组从后向前计数,若大于原数组长度,则空白处用null填充
|
||||||
*
|
*
|
||||||
|
* @param <A> 数组类型
|
||||||
* @param <T> 数组元素类型
|
* @param <T> 数组元素类型
|
||||||
* @param array 已有数组
|
* @param array 已有数组
|
||||||
* @param index 插入位置,此位置为对应此位置元素之前的空档
|
* @param index 插入位置,此位置为对应此位置元素之前的空档
|
||||||
@ -444,12 +464,12 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
* @since 4.0.8
|
* @since 4.0.8
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
|
@SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
|
||||||
public static <T> Object insert(final Object array, int index, final T... newElements) {
|
public static <A, T> A insert(final A array, int index, final T... newElements) {
|
||||||
if (isEmpty(newElements)) {
|
if (isEmpty(newElements)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return newElements;
|
return (A) Convert.convert(array.getClass(), newElements);
|
||||||
}
|
}
|
||||||
|
|
||||||
final int len = length(array);
|
final int len = length(array);
|
||||||
@ -457,13 +477,13 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
index = (index % len) + len;
|
index = (index % len) + len;
|
||||||
}
|
}
|
||||||
|
|
||||||
final T[] result = newArray(array.getClass().getComponentType(), Math.max(len, index) + newElements.length);
|
final Object result = Array.newInstance(array.getClass().getComponentType(), Math.max(len, index) + newElements.length);
|
||||||
System.arraycopy(array, 0, result, 0, Math.min(len, index));
|
System.arraycopy(array, 0, result, 0, Math.min(len, index));
|
||||||
System.arraycopy(newElements, 0, result, index, newElements.length);
|
System.arraycopy(newElements, 0, result, index, newElements.length);
|
||||||
if (index < len) {
|
if (index < len) {
|
||||||
System.arraycopy(array, index, result, index + newElements.length, len - index);
|
System.arraycopy(array, index, result, index + newElements.length, len - index);
|
||||||
}
|
}
|
||||||
return result;
|
return (A) result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -496,6 +516,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
* @param newSize 新的数组大小
|
* @param newSize 新的数组大小
|
||||||
* @return 调整后的新数组
|
* @return 调整后的新数组
|
||||||
* @since 4.6.7
|
* @since 4.6.7
|
||||||
|
* @see System#arraycopy(Object, int, Object, int, int)
|
||||||
*/
|
*/
|
||||||
public static Object resize(final Object array, final int newSize) {
|
public static Object resize(final Object array, final int newSize) {
|
||||||
if (newSize < 0) {
|
if (newSize < 0) {
|
||||||
|
@ -63,6 +63,9 @@ public class ArrayUtilTest {
|
|||||||
public void newArrayTest() {
|
public void newArrayTest() {
|
||||||
final String[] newArray = ArrayUtil.newArray(String.class, 3);
|
final String[] newArray = ArrayUtil.newArray(String.class, 3);
|
||||||
Assert.assertEquals(3, newArray.length);
|
Assert.assertEquals(3, newArray.length);
|
||||||
|
|
||||||
|
final Object[] newArray2 = ArrayUtil.newArray(3);
|
||||||
|
Assert.assertEquals(3, newArray2.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -508,10 +511,20 @@ public class ArrayUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void setOrAppendTest(){
|
public void setOrAppendTest() {
|
||||||
String[] arr = new String[0];
|
final String[] arr = new String[0];
|
||||||
String[] newArr = ArrayUtil.setOrAppend(arr, 0, "Good");// ClassCastException
|
final String[] newArr = ArrayUtil.setOrAppend(arr, 0, "Good");// ClassCastException
|
||||||
Assert.assertArrayEquals(new String[]{"Good"}, newArr);
|
Assert.assertArrayEquals(new String[]{"Good"}, newArr);
|
||||||
|
|
||||||
|
// 非空数组替换第一个元素
|
||||||
|
int[] arr2 = new int[]{1};
|
||||||
|
int[] o = ArrayUtil.setOrAppend(arr2, 0, 2);
|
||||||
|
Assert.assertArrayEquals(new int[]{2}, o);
|
||||||
|
|
||||||
|
// 空数组追加
|
||||||
|
arr2 = new int[0];
|
||||||
|
o = ArrayUtil.setOrAppend(arr2, 0, 2);
|
||||||
|
Assert.assertArrayEquals(new int[]{2}, o);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -522,4 +535,46 @@ public class ArrayUtilTest {
|
|||||||
final String[] c = {"d", "e"};
|
final String[] c = {"d", "e"};
|
||||||
Assert.assertTrue(ArrayUtil.containsAll(c, resultO[0], resultO[1]));
|
Assert.assertTrue(ArrayUtil.containsAll(c, resultO[0], resultO[1]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasNonNullTest() {
|
||||||
|
String[] a = {null, "e"};
|
||||||
|
Assert.assertTrue(ArrayUtil.hasNonNull(a));
|
||||||
|
|
||||||
|
a = new String[]{null, null};
|
||||||
|
Assert.assertFalse(ArrayUtil.hasNonNull(a));
|
||||||
|
|
||||||
|
a = new String[]{"", null};
|
||||||
|
Assert.assertTrue(ArrayUtil.hasNonNull(a));
|
||||||
|
|
||||||
|
a = new String[]{null};
|
||||||
|
Assert.assertFalse(ArrayUtil.hasNonNull(a));
|
||||||
|
|
||||||
|
a = new String[]{};
|
||||||
|
Assert.assertFalse(ArrayUtil.hasNonNull(a));
|
||||||
|
|
||||||
|
a = null;
|
||||||
|
Assert.assertFalse(ArrayUtil.hasNonNull(a));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAllNullTest() {
|
||||||
|
String[] a = {null, "e"};
|
||||||
|
Assert.assertFalse(ArrayUtil.isAllNull(a));
|
||||||
|
|
||||||
|
a = new String[]{null, null};
|
||||||
|
Assert.assertTrue(ArrayUtil.isAllNull(a));
|
||||||
|
|
||||||
|
a = new String[]{"", null};
|
||||||
|
Assert.assertFalse(ArrayUtil.isAllNull(a));
|
||||||
|
|
||||||
|
a = new String[]{null};
|
||||||
|
Assert.assertTrue(ArrayUtil.isAllNull(a));
|
||||||
|
|
||||||
|
a = new String[]{};
|
||||||
|
Assert.assertTrue(ArrayUtil.isAllNull(a));
|
||||||
|
|
||||||
|
a = null;
|
||||||
|
Assert.assertTrue(ArrayUtil.isAllNull(a));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user