添加判空

dev
ZhouXY108 2024-05-28 09:37:51 +08:00
parent 78ed0c8ed1
commit 2297536307
1 changed files with 38 additions and 41 deletions

View File

@ -30,74 +30,74 @@ public class ArrayTools {
// isNullOrEmpty // isNullOrEmpty
public static <T> boolean isNullOrEmpty(T[] arr) { public static <T> boolean isNullOrEmpty(@Nullable T[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - float // isNullOrEmpty - float
public static boolean isNullOrEmpty(float[] arr) { public static boolean isNullOrEmpty(@Nullable float[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - double // isNullOrEmpty - double
public static boolean isNullOrEmpty(double[] arr) { public static boolean isNullOrEmpty(@Nullable double[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - byte // isNullOrEmpty - byte
public static boolean isNullOrEmpty(byte[] arr) { public static boolean isNullOrEmpty(@Nullable byte[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - long // isNullOrEmpty - long
public static boolean isNullOrEmpty(long[] arr) { public static boolean isNullOrEmpty(@Nullable long[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNullOrEmpty - int // isNullOrEmpty - int
public static boolean isNullOrEmpty(int[] arr) { public static boolean isNullOrEmpty(@Nullable int[] arr) {
return arr == null || arr.length == 0; return arr == null || arr.length == 0;
} }
// isNotEmpty // isNotEmpty
public static <T> boolean isNotEmpty(T[] arr) { public static <T> boolean isNotEmpty(@Nullable T[] arr) {
return arr != null && arr.length > 0; return arr != null && arr.length > 0;
} }
// isNotEmpty - float // isNotEmpty - float
public static boolean isNotEmpty(float[] arr) { public static boolean isNotEmpty(@Nullable float[] arr) {
return arr != null && arr.length > 0; return arr != null && arr.length > 0;
} }
// isNotEmpty - double // isNotEmpty - double
public static boolean isNotEmpty(double[] arr) { public static boolean isNotEmpty(@Nullable double[] arr) {
return arr != null && arr.length > 0; return arr != null && arr.length > 0;
} }
// isNotEmpty - byte // isNotEmpty - byte
public static boolean isNotEmpty(byte[] arr) { public static boolean isNotEmpty(@Nullable byte[] arr) {
return arr != null && arr.length > 0; return arr != null && arr.length > 0;
} }
// isNotEmpty - long // isNotEmpty - long
public static boolean isNotEmpty(long[] arr) { public static boolean isNotEmpty(@Nullable long[] arr) {
return arr != null && arr.length > 0; return arr != null && arr.length > 0;
} }
// isNotEmpty - int // isNotEmpty - int
public static boolean isNotEmpty(int[] arr) { public static boolean isNotEmpty(@Nullable int[] arr) {
return arr != null && arr.length > 0; return arr != null && arr.length > 0;
} }
// concat // concat
public static float[] concatFloatArray(Collection<float[]> arrays) { public static float[] concatFloatArray(@Nullable Collection<float[]> arrays) {
int length = 0; if (arrays == null || arrays.isEmpty()) {
for (float[] arr : arrays) { return new float[0];
length += arr.length;
} }
float[] result = new float[length]; final int length = arrays.stream().mapToInt(a -> a.length).sum();
final float[] result = new float[length];
int i = 0; int i = 0;
for (float[] arr : arrays) { for (float[] arr : arrays) {
System.arraycopy(arr, 0, result, i, arr.length); System.arraycopy(arr, 0, result, i, arr.length);
@ -106,12 +106,12 @@ public class ArrayTools {
return result; return result;
} }
public static double[] concatDoubleArray(Collection<double[]> arrays) { public static double[] concatDoubleArray(@Nullable Collection<double[]> arrays) {
int length = 0; if (arrays == null || arrays.isEmpty()) {
for (double[] arr : arrays) { return new double[0];
length += arr.length;
} }
double[] result = new double[length]; final int length = arrays.stream().mapToInt(a -> a.length).sum();
final double[] result = new double[length];
int i = 0; int i = 0;
for (double[] arr : arrays) { for (double[] arr : arrays) {
System.arraycopy(arr, 0, result, i, arr.length); System.arraycopy(arr, 0, result, i, arr.length);
@ -120,12 +120,12 @@ public class ArrayTools {
return result; return result;
} }
public static byte[] concatByteArray(Collection<byte[]> arrays) { public static byte[] concatByteArray(@Nullable Collection<byte[]> arrays) {
int length = 0; if (arrays == null || arrays.isEmpty()) {
for (byte[] arr : arrays) { return new byte[0];
length += arr.length;
} }
byte[] result = new byte[length]; final int length = arrays.stream().mapToInt(a -> a.length).sum();
final byte[] result = new byte[length];
int i = 0; int i = 0;
for (byte[] arr : arrays) { for (byte[] arr : arrays) {
System.arraycopy(arr, 0, result, i, arr.length); System.arraycopy(arr, 0, result, i, arr.length);
@ -134,12 +134,12 @@ public class ArrayTools {
return result; return result;
} }
public static long[] concatLongArray(Collection<long[]> arrays) { public static long[] concatLongArray(@Nullable Collection<long[]> arrays) {
int length = 0; if (arrays == null || arrays.isEmpty()) {
for (long[] arr : arrays) { return new long[0];
length += arr.length;
} }
long[] result = new long[length]; final int length = arrays.stream().mapToInt(a -> a.length).sum();
final long[] result = new long[length];
int i = 0; int i = 0;
for (long[] arr : arrays) { for (long[] arr : arrays) {
System.arraycopy(arr, 0, result, i, arr.length); System.arraycopy(arr, 0, result, i, arr.length);
@ -148,12 +148,12 @@ public class ArrayTools {
return result; return result;
} }
public static int[] concatIntArray(Collection<int[]> arrays) { public static int[] concatIntArray(@Nullable Collection<int[]> arrays) {
int length = 0; if (arrays == null || arrays.isEmpty()) {
for (int[] arr : arrays) { return new int[0];
length += arr.length;
} }
int[] result = new int[length]; final int length = arrays.stream().mapToInt(a -> a.length).sum();
final int[] result = new int[length];
int i = 0; int i = 0;
for (int[] arr : arrays) { for (int[] arr : arrays) {
System.arraycopy(arr, 0, result, i, arr.length); System.arraycopy(arr, 0, result, i, arr.length);
@ -166,10 +166,7 @@ public class ArrayTools {
if (arrays == null || arrays.isEmpty()) { if (arrays == null || arrays.isEmpty()) {
return Collections.emptyList(); return Collections.emptyList();
} }
int length = 0; final int length = arrays.stream().mapToInt(a -> a.length).sum();
for (T[] arr : arrays) {
length += arr.length;
}
final List<T> result = new ArrayList<>(length); final List<T> result = new ArrayList<>(length);
for (T[] arr : arrays) { for (T[] arr : arrays) {
Collections.addAll(result, arr); Collections.addAll(result, arr);