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