mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
change style for ArrayUtil
This commit is contained in:
parent
e503824e9c
commit
70ac62660d
@ -11,6 +11,7 @@
|
||||
* 【extra 】 增加Wit模板引擎支持
|
||||
* 【core 】 增加DesensitizedUtil(pr#282@Gitee)
|
||||
* 【core 】 增加DateTime字符串构造(issue#I3CQZG@Gitee)
|
||||
* 【core 】 修改ArrayUtil代码风格(pr#287@Gitee)
|
||||
|
||||
### Bug修复
|
||||
* 【core 】 修复FileTypeUtil中OFD格式判断问题(pr#1489@Github)
|
||||
|
@ -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,7 +1223,13 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
* @return 连接后的字符串
|
||||
*/
|
||||
public static String join(Object array, CharSequence conjunction) {
|
||||
if (isArray(array)) {
|
||||
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();
|
||||
@ -1255,8 +1257,6 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
||||
return join((Object[]) array, conjunction);
|
||||
}
|
||||
}
|
||||
throw new UtilException(StrUtil.format("[{}] is not a Array!", array.getClass()));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link ByteBuffer} 转byte数组
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user