add method

This commit is contained in:
Looly
2020-08-07 17:52:57 +08:00
parent 4d9b19ab26
commit 594fc16960
2 changed files with 247 additions and 250 deletions

View File

@@ -14,6 +14,7 @@
* 【core 】 修改UUID正则更加严谨issue#I1Q1IW@Gitee * 【core 】 修改UUID正则更加严谨issue#I1Q1IW@Gitee
* 【core 】 ArrayUtil增加isAllNull方法issue#1004@Github * 【core 】 ArrayUtil增加isAllNull方法issue#1004@Github
* 【core 】 CollUtil增加contains方法pr#152@Gitee * 【core 】 CollUtil增加contains方法pr#152@Gitee
* 【core 】 ArrayUtil增加isAllNotNull方法pr#1008@Github
### Bug修复# ### Bug修复#
* 【core 】 修复原始类型转换时,转换失败没有抛出异常的问题 * 【core 】 修复原始类型转换时,转换失败没有抛出异常的问题

View File

@@ -16,14 +16,16 @@ import java.util.*;
* 数组工具类 * 数组工具类
* *
* @author Looly * @author Looly
*
*/ */
public class ArrayUtil { public class ArrayUtil {
/** 数组中元素未找到的下标,值为-1 */ /**
* 数组中元素未找到的下标,值为-1
*/
public static final int INDEX_NOT_FOUND = -1; public static final int INDEX_NOT_FOUND = -1;
// ---------------------------------------------------------------------- isEmpty // ---------------------------------------------------------------------- isEmpty
/** /**
* 数组是否为空 * 数组是否为空
* *
@@ -44,7 +46,7 @@ public class ArrayUtil {
* @return 非空empty的原数组或默认数组 * @return 非空empty的原数组或默认数组
* @since 4.6.9 * @since 4.6.9
*/ */
public static <T> T[] defaultIfEmpty(T[] array, T[] defaultArray){ public static <T> T[] defaultIfEmpty(T[] array, T[] defaultArray) {
return isEmpty(array) ? defaultArray : array; return isEmpty(array) ? defaultArray : array;
} }
@@ -148,6 +150,7 @@ public class ArrayUtil {
} }
// ---------------------------------------------------------------------- isNotEmpty // ---------------------------------------------------------------------- isNotEmpty
/** /**
* 数组是否为非空 * 数组是否为非空
* *
@@ -155,7 +158,7 @@ public class ArrayUtil {
* @param array 数组 * @param array 数组
* @return 是否为非空 * @return 是否为非空
*/ */
public static <T> boolean isNotEmpty( T[] array) { public static <T> boolean isNotEmpty(T[] array) {
return (array != null && array.length != 0); return (array != null && array.length != 0);
} }
@@ -278,8 +281,8 @@ public class ArrayUtil {
* @param <T> 数组元素类型 * @param <T> 数组元素类型
* @param array 被检查的数组 * @param array 被检查的数组
* @return 多个字段是否全为null * @return 多个字段是否全为null
* @since 5.4.0
* @author dahuoyzs * @author dahuoyzs
* @since 5.4.0
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> boolean isAllNull(T... array) { public static <T> boolean isAllNull(T... array) {
@@ -405,7 +408,7 @@ public class ArrayUtil {
*/ */
@SafeVarargs @SafeVarargs
public static <T> T[] append(T[] buffer, T... newElements) { public static <T> T[] append(T[] buffer, T... newElements) {
if(isEmpty(buffer)) { if (isEmpty(buffer)) {
return newElements; return newElements;
} }
return insert(buffer, buffer.length, newElements); return insert(buffer, buffer.length, newElements);
@@ -422,7 +425,7 @@ public class ArrayUtil {
*/ */
@SafeVarargs @SafeVarargs
public static <T> Object append(Object array, T... newElements) { public static <T> Object append(Object array, T... newElements) {
if(isEmpty(array)) { if (isEmpty(array)) {
return newElements; return newElements;
} }
return insert(array, length(array), newElements); return insert(array, length(array), newElements);
@@ -439,10 +442,10 @@ public class ArrayUtil {
* @since 4.1.2 * @since 4.1.2
*/ */
public static <T> T[] setOrAppend(T[] buffer, int index, T value) { public static <T> T[] setOrAppend(T[] buffer, int index, T value) {
if(index < buffer.length) { if (index < buffer.length) {
Array.set(buffer, index, value); Array.set(buffer, index, value);
return buffer; return buffer;
}else { } else {
return append(buffer, value); return append(buffer, value);
} }
} }
@@ -457,10 +460,10 @@ public class ArrayUtil {
* @since 4.1.2 * @since 4.1.2
*/ */
public static Object setOrAppend(Object array, int index, Object value) { public static Object setOrAppend(Object array, int index, Object value) {
if(index < length(array)) { if (index < length(array)) {
Array.set(array, index, value); Array.set(array, index, value);
return array; return array;
}else { } else {
return append(array, value); return append(array, value);
} }
} }
@@ -479,7 +482,7 @@ public class ArrayUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T[] insert(T[] buffer, int index, T... newElements) { public static <T> T[] insert(T[] buffer, int index, T... newElements) {
return (T[]) insert((Object)buffer, index, newElements); return (T[]) insert((Object) buffer, index, newElements);
} }
/** /**
@@ -499,7 +502,7 @@ public class ArrayUtil {
if (isEmpty(newElements)) { if (isEmpty(newElements)) {
return array; return array;
} }
if(isEmpty(array)) { if (isEmpty(array)) {
return newElements; return newElements;
} }
@@ -528,7 +531,7 @@ public class ArrayUtil {
* @return 调整后的新数组 * @return 调整后的新数组
*/ */
public static <T> T[] resize(T[] data, int newSize, Class<?> componentType) { public static <T> T[] resize(T[] data, int newSize, Class<?> componentType) {
if(newSize < 0){ if (newSize < 0) {
return data; return data;
} }
@@ -549,7 +552,7 @@ public class ArrayUtil {
* @since 4.6.7 * @since 4.6.7
*/ */
public static Object resize(Object array, int newSize) { public static Object resize(Object array, int newSize) {
if(newSize < 0){ if (newSize < 0) {
return array; return array;
} }
if (null == array) { if (null == array) {
@@ -574,7 +577,7 @@ public class ArrayUtil {
* @since 4.6.7 * @since 4.6.7
*/ */
public static byte[] resize(byte[] bytes, int newSize) { public static byte[] resize(byte[] bytes, int newSize) {
if(newSize < 0){ if (newSize < 0) {
return bytes; return bytes;
} }
final byte[] newArray = new byte[newSize]; final byte[] newArray = new byte[newSize];
@@ -1078,7 +1081,7 @@ public class ArrayUtil {
* <pre> * <pre>
* 1、修改元素对象返回集合中为修改后的对象 * 1、修改元素对象返回集合中为修改后的对象
* </pre> * </pre>
* * <p>
* 注意:此方法会修改原数组! * 注意:此方法会修改原数组!
* *
* @param <T> 数组元素类型 * @param <T> 数组元素类型
@@ -1087,7 +1090,7 @@ public class ArrayUtil {
* @since 5.3.3 * @since 5.3.3
*/ */
public static <T> void edit(T[] array, Editor<T> editor) { public static <T> void edit(T[] array, Editor<T> editor) {
for(int i = 0; i < array.length; i++){ for (int i = 0; i < array.length; i++) {
array[i] = editor.edit(array[i]); array[i] = editor.edit(array[i]);
} }
} }
@@ -1107,7 +1110,7 @@ public class ArrayUtil {
* @since 3.2.1 * @since 3.2.1
*/ */
public static <T> T[] filter(T[] array, Filter<T> filter) { public static <T> T[] filter(T[] array, Filter<T> filter) {
if(null == filter) { if (null == filter) {
return array; return array;
} }
@@ -1220,6 +1223,7 @@ public class ArrayUtil {
} }
// ------------------------------------------------------------------- indexOf and lastIndexOf and contains // ------------------------------------------------------------------- indexOf and lastIndexOf and contains
/** /**
* 返回数组中指定元素所在位置,未找到返回{@link #INDEX_NOT_FOUND} * 返回数组中指定元素所在位置,未找到返回{@link #INDEX_NOT_FOUND}
* *
@@ -1283,7 +1287,6 @@ public class ArrayUtil {
* 数组中是否包含元素 * 数组中是否包含元素
* *
* @param <T> 数组元素类型 * @param <T> 数组元素类型
*
* @param array 数组 * @param array 数组
* @param value 被检查的元素 * @param value 被检查的元素
* @return 是否包含 * @return 是否包含
@@ -1296,7 +1299,6 @@ public class ArrayUtil {
* 数组中是否包含指定元素中的任意一个 * 数组中是否包含指定元素中的任意一个
* *
* @param <T> 数组元素类型 * @param <T> 数组元素类型
*
* @param array 数组 * @param array 数组
* @param values 被检查的多个元素 * @param values 被检查的多个元素
* @return 是否包含指定元素中的任意一个 * @return 是否包含指定元素中的任意一个
@@ -1305,7 +1307,7 @@ public class ArrayUtil {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> boolean containsAny(T[] array, T... values) { public static <T> boolean containsAny(T[] array, T... values) {
for (T value : values) { for (T value : values) {
if(contains(array, value)) { if (contains(array, value)) {
return true; return true;
} }
} }
@@ -1725,6 +1727,7 @@ public class ArrayUtil {
} }
// ------------------------------------------------------------------- Wrap and unwrap // ------------------------------------------------------------------- Wrap and unwrap
/** /**
* 将原始类型数组包装为包装类型 * 将原始类型数组包装为包装类型
* *
@@ -1896,7 +1899,7 @@ public class ArrayUtil {
final byte[] array = new byte[length]; final byte[] array = new byte[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = ObjectUtil.defaultIfNull(values[i], (byte)0); array[i] = ObjectUtil.defaultIfNull(values[i], (byte) 0);
} }
return array; return array;
} }
@@ -1940,7 +1943,7 @@ public class ArrayUtil {
final short[] array = new short[length]; final short[] array = new short[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
array[i] = ObjectUtil.defaultIfNull(values[i], (short)0); array[i] = ObjectUtil.defaultIfNull(values[i], (short) 0);
} }
return array; return array;
} }
@@ -2144,7 +2147,7 @@ public class ArrayUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T get(Object array, int index) { public static <T> T get(Object array, int index) {
if(null == array) { if (null == array) {
return null; return null;
} }
@@ -2167,7 +2170,7 @@ public class ArrayUtil {
* @return 结果 * @return 结果
*/ */
public static <T> T[] getAny(Object array, int... indexes) { public static <T> T[] getAny(Object array, int... indexes) {
if(null == array) { if (null == array) {
return null; return null;
} }
@@ -2186,8 +2189,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.2.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.2.2
*/ */
public static <T> T[] sub(T[] array, int start, int end) { public static <T> T[] sub(T[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2221,8 +2224,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.5.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.5.2
*/ */
public static byte[] sub(byte[] array, int start, int end) { public static byte[] sub(byte[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2256,8 +2259,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.5.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.5.2
*/ */
public static int[] sub(int[] array, int start, int end) { public static int[] sub(int[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2291,8 +2294,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.5.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.5.2
*/ */
public static long[] sub(long[] array, int start, int end) { public static long[] sub(long[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2326,8 +2329,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.5.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.5.2
*/ */
public static short[] sub(short[] array, int start, int end) { public static short[] sub(short[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2361,8 +2364,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.5.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.5.2
*/ */
public static char[] sub(char[] array, int start, int end) { public static char[] sub(char[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2396,8 +2399,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.5.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.5.2
*/ */
public static double[] sub(double[] array, int start, int end) { public static double[] sub(double[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2431,8 +2434,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.5.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.5.2
*/ */
public static float[] sub(float[] array, int start, int end) { public static float[] sub(float[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2466,8 +2469,8 @@ public class ArrayUtil {
* @param start 开始位置(包括) * @param start 开始位置(包括)
* @param end 结束位置(不包括) * @param end 结束位置(不包括)
* @return 新的数组 * @return 新的数组
* @since 4.5.2
* @see Arrays#copyOfRange(Object[], int, int) * @see Arrays#copyOfRange(Object[], int, int)
* @since 4.5.2
*/ */
public static boolean[] sub(boolean[] array, int start, int end) { public static boolean[] sub(boolean[] array, int start, int end) {
int length = length(array); int length = length(array);
@@ -2563,21 +2566,21 @@ public class ArrayUtil {
return null; return null;
} }
if(obj instanceof long[]){ if (obj instanceof long[]) {
return Arrays.toString((long[]) obj); return Arrays.toString((long[]) obj);
} else if(obj instanceof int[]){ } else if (obj instanceof int[]) {
return Arrays.toString((int[]) obj); return Arrays.toString((int[]) obj);
} else if(obj instanceof short[]){ } else if (obj instanceof short[]) {
return Arrays.toString((short[]) obj); return Arrays.toString((short[]) obj);
} else if(obj instanceof char[]){ } else if (obj instanceof char[]) {
return Arrays.toString((char[]) obj); return Arrays.toString((char[]) obj);
} else if(obj instanceof byte[]){ } else if (obj instanceof byte[]) {
return Arrays.toString((byte[]) obj); return Arrays.toString((byte[]) obj);
} else if(obj instanceof boolean[]){ } else if (obj instanceof boolean[]) {
return Arrays.toString((boolean[]) obj); return Arrays.toString((boolean[]) obj);
} else if(obj instanceof float[]){ } else if (obj instanceof float[]) {
return Arrays.toString((float[]) obj); return Arrays.toString((float[]) obj);
} else if(obj instanceof double[]){ } else if (obj instanceof double[]) {
return Arrays.toString((double[]) obj); return Arrays.toString((double[]) obj);
} else if (ArrayUtil.isArray(obj)) { } else if (ArrayUtil.isArray(obj)) {
// 对象数组 // 对象数组
@@ -2607,8 +2610,8 @@ public class ArrayUtil {
* @param array 数组对象 * @param array 数组对象
* @return 数组长度 * @return 数组长度
* @throws IllegalArgumentException 如果参数不为数组,抛出此异常 * @throws IllegalArgumentException 如果参数不为数组,抛出此异常
* @since 3.0.8
* @see Array#getLength(Object) * @see Array#getLength(Object)
* @since 3.0.8
*/ */
public static int length(Object array) throws IllegalArgumentException { public static int length(Object array) throws IllegalArgumentException {
if (null == array) { if (null == array) {
@@ -2689,10 +2692,10 @@ public class ArrayUtil {
} else { } else {
sb.append(conjunction); sb.append(conjunction);
} }
if(null != editor){ if (null != editor) {
item = editor.edit(item); item = editor.edit(item);
} }
if(null != item){ if (null != item) {
sb.append(StrUtil.toString(item)); sb.append(StrUtil.toString(item));
} }
} }
@@ -2999,12 +3002,12 @@ public class ArrayUtil {
} }
// ---------------------------------------------------------------------- remove // ---------------------------------------------------------------------- remove
/** /**
* 移除数组中对应位置的元素<br> * 移除数组中对应位置的元素<br>
* copy from commons-lang * copy from commons-lang
* *
* @param <T> 数组元素类型 * @param <T> 数组元素类型
*
* @param array 数组对象,可以是对象数组,也可以原始类型数组 * @param array 数组对象,可以是对象数组,也可以原始类型数组
* @param index 位置如果位置小于0或者大于长度返回原数组 * @param index 位置如果位置小于0或者大于长度返回原数组
* @return 去掉指定元素后的新数组或原数组 * @return 去掉指定元素后的新数组或原数组
@@ -3159,6 +3162,7 @@ public class ArrayUtil {
} }
// ---------------------------------------------------------------------- remove // ---------------------------------------------------------------------- remove
/** /**
* 移除数组中指定的元素<br> * 移除数组中指定的元素<br>
* 只会移除匹配到的第一个元素 copy from commons-lang * 只会移除匹配到的第一个元素 copy from commons-lang
@@ -3624,6 +3628,7 @@ public class ArrayUtil {
} }
// ------------------------------------------------------------------------------------------------------------ min and max // ------------------------------------------------------------------------------------------------------------ min and max
/** /**
* 取最小值 * 取最小值
* *
@@ -4232,35 +4237,26 @@ public class ArrayUtil {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> T[] distinct(T[] array) { public static <T> T[] distinct(T[] array) {
if(isEmpty(array)) { if (isEmpty(array)) {
return array; return array;
} }
final Set<T> set = new LinkedHashSet<>(array.length, 1); final Set<T> set = new LinkedHashSet<>(array.length, 1);
Collections.addAll(set, array); Collections.addAll(set, array);
return toArray(set, (Class<T>)getComponentType(array)); return toArray(set, (Class<T>) getComponentType(array));
} }
/** /**
* 多个字段是否全部不为null * 多个字段是否全部不为null
* *
* @param <T> 数组元素类型 * @param <T> 数组元素类型
* @param array 被检查的数组 * @param array 被检查的数组
* @return 多个字段是否全部不为null * @return 多个字段是否全部不为null
* @since 5.3.11 * @since 5.4.0
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T> boolean isAllNotNull(T... array) { public static <T> boolean isAllNotNull(T... array) {
if (isNotEmpty(array)) { return false == hasNull(array);
for (T element : array) {
if (null == element) {
return false;
}
}
}
return true;
} }
} }