添加工具类。

feature/net-util
ZhouXY108 2023-04-29 18:38:14 +08:00
parent bf9d2fc45b
commit bf082b56d9
3 changed files with 80 additions and 34 deletions

View File

@ -113,71 +113,56 @@ public class Assert {
}
// isEmpty - Collection
private static <T> boolean isEmpty(@Nullable Collection<T> collection) {
return collection == null || collection.isEmpty();
public static <E extends Throwable> void isEmpty(@Nullable Collection<?> collection, Supplier<E> e) throws E {
Assert.isTrue(MoreCollections.isEmpty(collection), e);
}
public static <T, E extends Throwable> void isEmpty(@Nullable Collection<T> collection, Supplier<E> e) throws E {
Assert.isTrue(isEmpty(collection), e);
public static void isEmpty(@Nullable Collection<?> collection, String errorMessage) {
Assert.isTrue(MoreCollections.isEmpty(collection), errorMessage);
}
public static <T> void isEmpty(@Nullable Collection<T> collection, String errorMessage) {
Assert.isTrue(isEmpty(collection), errorMessage);
}
public static <T> void isEmpty(@Nullable Collection<T> collection, String errorMessageTemplate, Object... args) {
Assert.isTrue(isEmpty(collection), errorMessageTemplate, args);
public static void isEmpty(@Nullable Collection<?> collection, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreCollections.isEmpty(collection), errorMessageTemplate, args);
}
// isNotEmpty - Collection
private static <T> boolean isNotEmpty(@Nullable Collection<T> collection) {
return collection != null && !collection.isEmpty();
public static <E extends Throwable> void isNotEmpty(@Nullable Collection<?> collection, Supplier<E> e) throws E {
Assert.isTrue(MoreCollections.isNotEmpty(collection), e);
}
public static <T, E extends Throwable> void isNotEmpty(@Nullable Collection<T> collection, Supplier<E> e) throws E {
Assert.isTrue(isNotEmpty(collection), e);
public static void isNotEmpty(@Nullable Collection<?> collection, String errorMessage) {
Assert.isTrue(MoreCollections.isNotEmpty(collection), errorMessage);
}
public static <T> void isNotEmpty(@Nullable Collection<T> collection, String errorMessage) {
Assert.isTrue(isNotEmpty(collection), errorMessage);
}
public static <T> void isNotEmpty(@Nullable Collection<T> collection, String errorMessageTemplate, Object... args) {
Assert.isTrue(isNotEmpty(collection), errorMessageTemplate, args);
public static void isNotEmpty(@Nullable Collection<?> collection, String errorMessageTemplate, Object... args) {
Assert.isTrue(MoreCollections.isNotEmpty(collection), errorMessageTemplate, args);
}
// isEmpty - Array
private static <T> boolean isEmpty(@Nullable T[] arr) {
return arr == null || arr.length == 0;
}
public static <T, E extends Throwable> void isEmpty(@Nullable T[] arr, Supplier<E> e) throws E {
Assert.isTrue(isEmpty(arr), e);
Assert.isTrue(MoreArrays.isEmpty(arr), e);
}
public static <T> void isEmpty(@Nullable T[] arr, String errorMessage) {
Assert.isTrue(isEmpty(arr), errorMessage);
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessage);
}
public static <T> void isEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) {
Assert.isTrue(isEmpty(arr), errorMessageTemplate, args);
Assert.isTrue(MoreArrays.isEmpty(arr), errorMessageTemplate, args);
}
// isNotEmpty - Array
private static <T> boolean isNotEmpty(@Nullable T[] arr) {
return arr != null && arr.length > 0;
}
// TODO int[] long[] double[]
public static <T, E extends Throwable> void isNotEmpty(@Nullable T[] arr, Supplier<E> e) throws E {
Assert.isTrue(isNotEmpty(arr), e);
Assert.isTrue(MoreArrays.isNotEmpty(arr), e);
}
public static <T> void isNotEmpty(@Nullable T[] arr, String errorMessage) {
Assert.isTrue(isNotEmpty(arr), errorMessage);
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessage);
}
public static <T> void isNotEmpty(@Nullable T[] arr, String errorMessageTemplate, Object... args) {
Assert.isTrue(isNotEmpty(arr), errorMessageTemplate, args);
Assert.isTrue(MoreArrays.isNotEmpty(arr), errorMessageTemplate, args);
}
// isEmpty - String

View File

@ -0,0 +1,42 @@
package xyz.zhouxy.plusone.commons.util;
import javax.annotation.Nullable;
public class MoreArrays {
public static boolean isEmpty(@Nullable int[] arr) {
return arr == null || arr.length == 0;
}
public static boolean isNotEmpty(@Nullable int[] arr) {
return arr != null && arr.length > 0;
}
public static boolean isEmpty(@Nullable long[] arr) {
return arr == null || arr.length == 0;
}
public static boolean isNotEmpty(@Nullable long[] arr) {
return arr != null && arr.length > 0;
}
public static boolean isEmpty(@Nullable double[] arr) {
return arr == null || arr.length == 0;
}
public static boolean isNotEmpty(@Nullable double[] arr) {
return arr != null && arr.length > 0;
}
public static <T> boolean isEmpty(@Nullable T[] arr) {
return arr == null || arr.length == 0;
}
public static <T> boolean isNotEmpty(@Nullable T[] arr) {
return arr != null && arr.length > 0;
}
private MoreArrays() {
throw new IllegalStateException("Utility class");
}
}

View File

@ -0,0 +1,19 @@
package xyz.zhouxy.plusone.commons.util;
import java.util.Collection;
import javax.annotation.Nullable;
public class MoreCollections {
public static boolean isEmpty(@Nullable Collection<?> collection) {
return collection == null || collection.isEmpty();
}
public static boolean isNotEmpty(@Nullable Collection<?> collection) {
return collection != null && !collection.isEmpty();
}
private MoreCollections() {
throw new IllegalStateException("Utility class");
}
}