mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-03 12:18:01 +08:00
add test
This commit is contained in:
parent
c891de1f17
commit
09b9e707b9
@ -865,12 +865,9 @@ public class ArrayUtil {
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T> T[] removeNull(T[] array) {
|
||||
return filter(array, new Editor<T>() {
|
||||
@Override
|
||||
public T edit(T t) {
|
||||
// 返回null便不加入集合
|
||||
return t;
|
||||
}
|
||||
return filter(array, (Editor<T>) t -> {
|
||||
// 返回null便不加入集合
|
||||
return t;
|
||||
});
|
||||
}
|
||||
|
||||
@ -883,12 +880,7 @@ public class ArrayUtil {
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T extends CharSequence> T[] removeEmpty(T[] array) {
|
||||
return filter(array, new Filter<T>() {
|
||||
@Override
|
||||
public boolean accept(T t) {
|
||||
return false == StrUtil.isEmpty(t);
|
||||
}
|
||||
});
|
||||
return filter(array, (Filter<T>) t -> false == StrUtil.isEmpty(t));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -900,12 +892,7 @@ public class ArrayUtil {
|
||||
* @since 3.2.2
|
||||
*/
|
||||
public static <T extends CharSequence> T[] removeBlank(T[] array) {
|
||||
return filter(array, new Filter<T>() {
|
||||
@Override
|
||||
public boolean accept(T t) {
|
||||
return false == StrUtil.isBlank(t);
|
||||
}
|
||||
});
|
||||
return filter(array, (Filter<T>) t -> false == StrUtil.isBlank(t));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -916,12 +903,7 @@ public class ArrayUtil {
|
||||
* @since 3.2.1
|
||||
*/
|
||||
public static String[] nullToEmpty(String[] array) {
|
||||
return filter(array, new Editor<String>() {
|
||||
@Override
|
||||
public String edit(String t) {
|
||||
return null == t ? StrUtil.EMPTY : t;
|
||||
}
|
||||
});
|
||||
return filter(array, (Editor<String>) t -> null == t ? StrUtil.EMPTY : t);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -23,6 +23,16 @@ public class ArrayUtilTest {
|
||||
Assert.assertTrue(ArrayUtil.isEmpty(b));
|
||||
Object c = null;
|
||||
Assert.assertTrue(ArrayUtil.isEmpty(c));
|
||||
|
||||
Object d = new Object[]{"1", "2", 3, 4D};
|
||||
boolean isEmpty = ArrayUtil.isEmpty(d);
|
||||
Assert.assertFalse(isEmpty);
|
||||
d = new Object[0];
|
||||
isEmpty = ArrayUtil.isEmpty(d);
|
||||
Assert.assertTrue(isEmpty);
|
||||
d = null;
|
||||
isEmpty = ArrayUtil.isEmpty(d);
|
||||
Assert.assertTrue(isEmpty);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -51,36 +61,21 @@ public class ArrayUtilTest {
|
||||
@Test
|
||||
public void filterTest() {
|
||||
Integer[] a = {1, 2, 3, 4, 5, 6};
|
||||
Integer[] filter = ArrayUtil.filter(a, new Editor<Integer>() {
|
||||
@Override
|
||||
public Integer edit(Integer t) {
|
||||
return (t % 2 == 0) ? t : null;
|
||||
}
|
||||
});
|
||||
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2 == 0) ? t : null);
|
||||
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterTestForFilter() {
|
||||
Integer[] a = {1, 2, 3, 4, 5, 6};
|
||||
Integer[] filter = ArrayUtil.filter(a, new Filter<Integer>() {
|
||||
@Override
|
||||
public boolean accept(Integer t) {
|
||||
return t % 2 == 0;
|
||||
}
|
||||
});
|
||||
Integer[] filter = ArrayUtil.filter(a, (Filter<Integer>) t -> t % 2 == 0);
|
||||
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterTestForEditor() {
|
||||
Integer[] a = {1, 2, 3, 4, 5, 6};
|
||||
Integer[] filter = ArrayUtil.filter(a, new Editor<Integer>() {
|
||||
@Override
|
||||
public Integer edit(Integer t) {
|
||||
return (t % 2 == 0) ? t * 10 : t;
|
||||
}
|
||||
});
|
||||
Integer[] filter = ArrayUtil.filter(a, (Editor<Integer>) t -> (t % 2 == 0) ? t * 10 : t);
|
||||
Assert.assertArrayEquals(filter, new Integer[]{1, 20, 3, 40, 5, 60});
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user