change style for ArrayUtil

This commit is contained in:
Looly 2021-03-25 15:53:10 +08:00
parent e503824e9c
commit 70ac62660d
3 changed files with 82 additions and 41 deletions

View File

@ -11,6 +11,7 @@
* 【extra 】 增加Wit模板引擎支持 * 【extra 】 增加Wit模板引擎支持
* 【core 】 增加DesensitizedUtilpr#282@Gitee * 【core 】 增加DesensitizedUtilpr#282@Gitee
* 【core 】 增加DateTime字符串构造issue#I3CQZG@Gitee * 【core 】 增加DateTime字符串构造issue#I3CQZG@Gitee
* 【core 】 修改ArrayUtil代码风格pr#287@Gitee
### Bug修复 ### Bug修复
* 【core 】 修复FileTypeUtil中OFD格式判断问题pr#1489@Github * 【core 】 修复FileTypeUtil中OFD格式判断问题pr#1489@Github

View File

@ -89,7 +89,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @return 是否为非空 * @return 是否为非空
*/ */
public static <T> boolean isNotEmpty(T[] array) { public static <T> boolean isNotEmpty(T[] array) {
return (array != null && array.length != 0); return (null != array && array.length != 0);
} }
/** /**
@ -653,7 +653,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @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, (Filter<T>) t -> false == StrUtil.isEmpty(t)); return filter(array, StrUtil::isNotEmpty);
} }
/** /**
@ -665,7 +665,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @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, (Filter<T>) t -> false == StrUtil.isBlank(t)); return filter(array, StrUtil::isNotBlank);
} }
/** /**
@ -900,11 +900,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @return 是否为数组对象如果为{@code null} 返回false * @return 是否为数组对象如果为{@code null} 返回false
*/ */
public static boolean isArray(Object obj) { public static boolean isArray(Object obj) {
if (null == obj) { return null != obj && obj.getClass().isArray();
// throw new NullPointerException("Object check for isArray is null");
return false;
}
return obj.getClass().isArray();
} }
/** /**
@ -1227,35 +1223,39 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @return 连接后的字符串 * @return 连接后的字符串
*/ */
public static String join(Object array, CharSequence conjunction) { public static String join(Object array, CharSequence conjunction) {
if (isArray(array)) { if(null == array){
final Class<?> componentType = array.getClass().getComponentType(); throw new NullPointerException("Array must be not null!");
if (componentType.isPrimitive()) { }
final String componentTypeName = componentType.getName(); if (false == isArray(array)) {
switch (componentTypeName) { throw new IllegalArgumentException(StrUtil.format("[{}] is not a Array!", array.getClass()));
case "long": }
return join((long[]) array, conjunction);
case "int": final Class<?> componentType = array.getClass().getComponentType();
return join((int[]) array, conjunction); if (componentType.isPrimitive()) {
case "short": final String componentTypeName = componentType.getName();
return join((short[]) array, conjunction); switch (componentTypeName) {
case "char": case "long":
return join((char[]) array, conjunction); return join((long[]) array, conjunction);
case "byte": case "int":
return join((byte[]) array, conjunction); return join((int[]) array, conjunction);
case "boolean": case "short":
return join((boolean[]) array, conjunction); return join((short[]) array, conjunction);
case "float": case "char":
return join((float[]) array, conjunction); return join((char[]) array, conjunction);
case "double": case "byte":
return join((double[]) array, conjunction); return join((byte[]) array, conjunction);
default: case "boolean":
throw new UtilException("Unknown primitive type: [{}]", componentTypeName); return join((boolean[]) array, conjunction);
} case "float":
} else { return join((float[]) array, conjunction);
return join((Object[]) array, conjunction); case "double":
} return join((double[]) array, conjunction);
default:
throw new UtilException("Unknown primitive type: [{}]", componentTypeName);
}
} else {
return join((Object[]) array, conjunction);
} }
throw new UtilException(StrUtil.format("[{}] is not a Array!", array.getClass()));
} }
/** /**
@ -1266,7 +1266,9 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.0.1 * @since 3.0.1
*/ */
public static byte[] toArray(ByteBuffer bytebuffer) { public static byte[] toArray(ByteBuffer bytebuffer) {
if (false == bytebuffer.hasArray()) { if (bytebuffer.hasArray()) {
return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
} else {
int oldPosition = bytebuffer.position(); int oldPosition = bytebuffer.position();
bytebuffer.position(0); bytebuffer.position(0);
int size = bytebuffer.limit(); int size = bytebuffer.limit();
@ -1274,8 +1276,6 @@ public class ArrayUtil extends PrimitiveArrayUtil {
bytebuffer.get(buffers); bytebuffer.get(buffers);
bytebuffer.position(oldPosition); bytebuffer.position(oldPosition);
return buffers; return buffers;
} else {
return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
} }
} }

View File

@ -53,6 +53,12 @@ public class ArrayUtilTest {
public void isNotEmptyTest() { public void isNotEmptyTest() {
int[] a = {1, 2}; int[] a = {1, 2};
Assert.assertTrue(ArrayUtil.isNotEmpty(a)); Assert.assertTrue(ArrayUtil.isNotEmpty(a));
String[] b = {"a", "b", "c"};
Assert.assertTrue(ArrayUtil.isNotEmpty(b));
Object c = new Object[]{"1", "2", 3, 4D};
Assert.assertTrue(ArrayUtil.isNotEmpty(c));
} }
@Test @Test
@ -253,15 +259,19 @@ public class ArrayUtilTest {
String[] array = {"aa", "bb", "cc", "dd"}; String[] array = {"aa", "bb", "cc", "dd"};
String join = ArrayUtil.join(array, ",", "[", "]"); String join = ArrayUtil.join(array, ",", "[", "]");
Assert.assertEquals("[aa],[bb],[cc],[dd]", join); Assert.assertEquals("[aa],[bb],[cc],[dd]", join);
Object array2 = new String[]{"aa", "bb", "cc", "dd"};
String join2 = ArrayUtil.join(array2, ",");
Assert.assertEquals("aa,bb,cc,dd", join2);
} }
@Test @Test
public void getArrayTypeTest() { public void getArrayTypeTest() {
Class<?> arrayType = ArrayUtil.getArrayType(int.class); Class<?> arrayType = ArrayUtil.getArrayType(int.class);
Assert.assertEquals(int[].class, arrayType); Assert.assertSame(int[].class, arrayType);
arrayType = ArrayUtil.getArrayType(String.class); arrayType = ArrayUtil.getArrayType(String.class);
Assert.assertEquals(String[].class, arrayType); Assert.assertSame(String[].class, arrayType);
} }
@Test @Test
@ -384,4 +394,34 @@ public class ArrayUtilTest {
final int[] reverse = ArrayUtil.reverse(a); 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 removeEmptyTest() {
String[] a = {"a", "b", "", null, " ", "c"};
String[] resultA = {"a", "b", " ", "c"};
Assert.assertArrayEquals(ArrayUtil.removeEmpty(a), resultA);
}
@Test
public void removeBlankTest() {
String[] a = {"a", "b", "", null, " ", "c"};
String[] resultA = {"a", "b", "c"};
Assert.assertArrayEquals(ArrayUtil.removeBlank(a), resultA);
}
@Test
public void nullToEmptyTest() {
String[] a = {"a", "b", "", null, " ", "c"};
String[] resultA = {"a", "b", "", "", " ", "c"};
Assert.assertArrayEquals(ArrayUtil.nullToEmpty(a), resultA);
}
@Test
public void wrapTest() {
Object a = new int[]{1, 2, 3, 4};
Object[] wrapA = ArrayUtil.wrap(a);
for (Object o : wrapA) {
Assert.assertTrue(o instanceof Integer);
}
}
} }