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

View File

@ -89,7 +89,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @return 是否为非空
*/
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
*/
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
*/
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
*/
public static boolean isArray(Object obj) {
if (null == obj) {
// throw new NullPointerException("Object check for isArray is null");
return false;
}
return obj.getClass().isArray();
return null != obj && obj.getClass().isArray();
}
/**
@ -1227,35 +1223,39 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @return 连接后的字符串
*/
public static String join(Object array, CharSequence conjunction) {
if (isArray(array)) {
final Class<?> componentType = array.getClass().getComponentType();
if (componentType.isPrimitive()) {
final String componentTypeName = componentType.getName();
switch (componentTypeName) {
case "long":
return join((long[]) array, conjunction);
case "int":
return join((int[]) array, conjunction);
case "short":
return join((short[]) array, conjunction);
case "char":
return join((char[]) array, conjunction);
case "byte":
return join((byte[]) array, conjunction);
case "boolean":
return join((boolean[]) array, conjunction);
case "float":
return join((float[]) array, conjunction);
case "double":
return join((double[]) array, conjunction);
default:
throw new UtilException("Unknown primitive type: [{}]", componentTypeName);
}
} else {
return join((Object[]) array, conjunction);
}
if(null == array){
throw new NullPointerException("Array must be not null!");
}
if (false == isArray(array)) {
throw new IllegalArgumentException(StrUtil.format("[{}] is not a Array!", array.getClass()));
}
final Class<?> componentType = array.getClass().getComponentType();
if (componentType.isPrimitive()) {
final String componentTypeName = componentType.getName();
switch (componentTypeName) {
case "long":
return join((long[]) array, conjunction);
case "int":
return join((int[]) array, conjunction);
case "short":
return join((short[]) array, conjunction);
case "char":
return join((char[]) array, conjunction);
case "byte":
return join((byte[]) array, conjunction);
case "boolean":
return join((boolean[]) array, conjunction);
case "float":
return join((float[]) 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
*/
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();
bytebuffer.position(0);
int size = bytebuffer.limit();
@ -1274,8 +1276,6 @@ public class ArrayUtil extends PrimitiveArrayUtil {
bytebuffer.get(buffers);
bytebuffer.position(oldPosition);
return buffers;
} else {
return Arrays.copyOfRange(bytebuffer.array(), bytebuffer.position(), bytebuffer.limit());
}
}

View File

@ -53,6 +53,12 @@ public class ArrayUtilTest {
public void isNotEmptyTest() {
int[] a = {1, 2};
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
@ -253,15 +259,19 @@ public class ArrayUtilTest {
String[] array = {"aa", "bb", "cc", "dd"};
String join = ArrayUtil.join(array, ",", "[", "]");
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
public void getArrayTypeTest() {
Class<?> arrayType = ArrayUtil.getArrayType(int.class);
Assert.assertEquals(int[].class, arrayType);
Assert.assertSame(int[].class, arrayType);
arrayType = ArrayUtil.getArrayType(String.class);
Assert.assertEquals(String[].class, arrayType);
Assert.assertSame(String[].class, arrayType);
}
@Test
@ -384,4 +394,34 @@ public class ArrayUtilTest {
final int[] reverse = ArrayUtil.reverse(a);
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);
}
}
}