This commit is contained in:
Looly 2024-01-24 16:39:59 +08:00
parent dd1ec90fd4
commit 63fbef4183
3 changed files with 106 additions and 7 deletions

View File

@ -305,9 +305,9 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @param <T> 元素类型
* @param args 被检查对象
* @return 是否存在 {@code null} 或空对象
* @since 6.0.0
* @author dazer
* @throws IllegalArgumentException 如果提供的args的item存在数组或集合抛出异常
* @author dazer
* @since 6.0.0
*/
@SafeVarargs
public static <T> boolean hasEmptyVarargs(final T... args) {
@ -340,9 +340,9 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @param <T> 元素类型
* @param args 被检查的对象,一个或者多个
* @return 是否都为空
* @since 6.0.0
* @author dazer
* @throws IllegalArgumentException 如果提供的args的item存在数组或集合抛出异常
* @author dazer
* @since 6.0.0
*/
@SafeVarargs
public static <T> boolean isAllEmptyVarargs(final T... args) {
@ -705,6 +705,25 @@ public class ArrayUtil extends PrimitiveArrayUtil {
return ArrayWrapper.of(array).setOrPadding(index, value).getRaw();
}
/**
* 将元素值设置为数组的某个位置当index小于数组的长度时替换指定位置的值否则追加paddingValue直到到达index后设置值
*
* @param <A> 数组类型
* @param <E> 元素类型
* @param array 已有数组
* @param index 位置大于等于长度则追加否则替换
* @param value 新值
* @param paddingValue 填充值
* @return 新数组或原有数组
* @since 6.0.0
*/
public static <A, E> A setOrPadding(final A array, final int index, final E value, final E paddingValue) {
if (index == 0 && isEmpty(array)) {
return ofArray(value, null == array ? null : array.getClass().getComponentType());
}
return ArrayWrapper.of(array).setOrPadding(index, value, paddingValue).getRaw();
}
/**
* 合并所有数组返回合并后的新数组<br>
* 忽略null的数组

View File

@ -221,7 +221,7 @@ public class ObjUtil {
* <li>{@code null}默认返回{@code true}</li>
* <li>数组等同于{@link ArrayUtil#isEmpty(Object)}</li>
* <li>{@link CharSequence}等同于{@link CharSequenceUtil#isEmpty(CharSequence)}</li>
* <li>{@link Collection}等同于{@link CollUtil#isEmpty(Collection)}</li>
* <li>{@link Collection}等同于{@link CollUtil#isEmpty(Collection)}</li>
* <li>{@link Map}等同于{@link MapUtil#isEmpty(Map)}</li>
* <li>
* {@link Iterator}{@link Iterable}等同于{@link IterUtil#isEmpty(Iterator)}

View File

@ -15,7 +15,6 @@ package org.dromara.hutool.core.array;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -55,6 +54,10 @@ public class ArrayUtilTest {
final Object[] e = new Object[]{"1", "2", 3, 4D};
final boolean empty = ArrayUtil.isEmpty(e);
Assertions.assertFalse(empty);
// 当这个对象并非数组对象且非`null`返回`false`即当用户传入非数组对象理解为单个元素的数组
final Object nonArrayObj = "a";
Assertions.assertFalse(ArrayUtil.isEmpty(nonArrayObj));
}
@Test
@ -231,6 +234,14 @@ public class ArrayUtilTest {
Assertions.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
}
@Test
public void appendTest2() {
final String[] a = {"1", "2", "3", "4"};
final String[] result = ArrayUtil.append(a, "a", "b", "c");
Assertions.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
}
@Test
public void insertTest() {
final String[] a = {"1", "2", "3", "4"};
@ -344,6 +355,13 @@ public class ArrayUtilTest {
Assertions.assertFalse(ArrayUtil.isAllNotNull(hasNull));
}
@Test
void firstNonNullTest() {
final String[] a = {null, null, "cc", null, "bb", "dd"};
final String s = ArrayUtil.firstNonNull(a);
Assertions.assertEquals("cc", s);
}
@Test
public void indexOfSubTest() {
final Integer[] a = {0x12, 0x34, 0x56, 0x78, 0x9A};
@ -464,6 +482,23 @@ public class ArrayUtilTest {
}
}
@Test
public void wrapIntTest() {
final int[] a = new int[]{1, 2, 3, 4};
final Integer[] wrapA = ArrayUtil.wrap(a);
for (final Integer o : wrapA) {
Assertions.assertInstanceOf(Integer.class, o);
}
}
@Test
public void unWrapIntTest() {
final Integer[] a = new Integer[]{1, 2, 3, 4};
final int[] wrapA = ArrayUtil.unWrap(a);
final Class<?> componentType = wrapA.getClass().getComponentType();
Assertions.assertEquals(int.class, componentType);
}
@Test
public void splitTest() {
final byte[] array = new byte[1024];
@ -567,6 +602,14 @@ public class ArrayUtilTest {
Assertions.assertArrayEquals(new String[]{null, null, "Good"}, newArr);
}
@Test
void setOrPaddingTest2(){
final String[] arr = new String[0];
final String[] newArr = ArrayUtil.setOrPadding(arr, 2, "Good");
Console.log(newArr);
Assertions.assertArrayEquals(new String[]{null, null, "Good"}, newArr);
}
@Test
public void getAnyTest() {
final String[] a = {"a", "b", "c", "d", "e"};
@ -576,6 +619,12 @@ public class ArrayUtilTest {
Assertions.assertTrue(ArrayUtil.containsAll(c, resultO[0], resultO[1]));
}
@Test
void hasNullTest() {
final String[] a = {"e", null};
Assertions.assertTrue(ArrayUtil.hasNull(a));
}
@Test
public void hasNonNullTest() {
String[] a = {null, "e"};
@ -758,7 +807,7 @@ public class ArrayUtilTest {
@Test
public void equalsTest() {
final boolean b = ObjUtil.equals(new int[]{1, 2, 3}, new int[]{1, 2, 3});
final boolean b = ArrayUtil.equals(new int[]{1, 2, 3}, new int[]{1, 2, 3});
Assertions.assertTrue(b);
}
@ -792,8 +841,39 @@ public class ArrayUtilTest {
Assertions.assertTrue(ArrayUtil.hasEmptyVarargs("", "apple", "pear"));
}
@Test
void hasEmptyTest() {
final String[] a = {"", "a"};
Assertions.assertTrue(ArrayUtil.hasEmpty(a));
Object[] b = {"a", new ArrayList<>()};
Assertions.assertTrue(ArrayUtil.hasEmpty(b));
b = new Object[]{"a", new HashMap<>()};
Assertions.assertTrue(ArrayUtil.hasEmpty(b));
}
@Test
public void isAllEmptyTest() {
Assertions.assertFalse(ArrayUtil.isAllEmptyVarargs("apple", "pear", "", "orange"));
}
@Test
void emptyCountTest() {
final Object[] b = {"a", new ArrayList<>(), new HashMap<>(), new int[0]};
final int emptyCount = ArrayUtil.emptyCount(b);
Assertions.assertEquals(3, emptyCount);
}
@Test
void hasBlankTest() {
final String[] a = {" ", "aa"};
Assertions.assertTrue(ArrayUtil.hasBlank(a));
}
@Test
void isAllBlankTest() {
final String[] a = {" ", " ", ""};
Assertions.assertTrue(ArrayUtil.isAllBlank(a));
}
}