Commit c58a4b13 authored by liaozan's avatar liaozan 🏀

Update ValidateUtils

parent f0741e83
...@@ -37,24 +37,25 @@ public class ValidateUtils { ...@@ -37,24 +37,25 @@ public class ValidateUtils {
isFalse(expression, () -> new ParamInvalidException(message)); isFalse(expression, () -> new ParamInvalidException(message));
} }
public static <T extends BaseException> void isFalse(boolean expression, Supplier<T> errorSupplier) { public static <E extends BaseException> void isFalse(boolean expression, Supplier<E> errorSupplier) {
if (expression) { if (expression) {
throw errorSupplier.get(); throw errorSupplier.get();
} }
} }
public static void notNull(Object object) { public static <T> T notNull(T object) {
notNull(object, "The validated object is null"); return notNull(object, "The validated object is null");
} }
public static void notNull(Object object, String message) { public static <T> T notNull(T object, String message) {
notNull(object, () -> new ParamInvalidException(message)); return notNull(object, () -> new ParamInvalidException(message));
} }
public static <T extends BaseException> void notNull(Object object, Supplier<T> errorSupplier) { public static <T, E extends BaseException> T notNull(T object, Supplier<E> errorSupplier) {
if (object == null) { if (object == null) {
throw errorSupplier.get(); throw errorSupplier.get();
} }
return object;
} }
public static void isNull(Object object) { public static void isNull(Object object) {
...@@ -71,18 +72,19 @@ public class ValidateUtils { ...@@ -71,18 +72,19 @@ public class ValidateUtils {
} }
} }
public static void notEmpty(String value) { public static String notEmpty(String value) {
notEmpty(value, "The validated string is empty"); return notEmpty(value, "The validated string is empty");
} }
public static void notEmpty(String value, String message) { public static String notEmpty(String value, String message) {
notEmpty(value, () -> new ParamInvalidException(message)); return notEmpty(value, () -> new ParamInvalidException(message));
} }
public static <T extends BaseException> void notEmpty(String value, Supplier<T> errorSupplier) { public static <E extends BaseException> String notEmpty(String value, Supplier<E> errorSupplier) {
if (value == null || value.isBlank()) { if (value == null || value.isBlank()) {
throw errorSupplier.get(); throw errorSupplier.get();
} }
return value;
} }
public static void isEmpty(String value) { public static void isEmpty(String value) {
...@@ -99,18 +101,19 @@ public class ValidateUtils { ...@@ -99,18 +101,19 @@ public class ValidateUtils {
} }
} }
public static void notEmpty(Object[] array) { public static <T> T[] notEmpty(T[] array) {
notEmpty(array, "The validated array is empty"); return notEmpty(array, "The validated array is empty");
} }
public static void notEmpty(Object[] array, String message) { public static <T> T[] notEmpty(T[] array, String message) {
notEmpty(array, () -> new ParamInvalidException(message)); return notEmpty(array, () -> new ParamInvalidException(message));
} }
public static <T extends BaseException> void notEmpty(Object[] array, Supplier<T> errorSupplier) { public static <T, E extends BaseException> T[] notEmpty(T[] array, Supplier<E> errorSupplier) {
if (array == null || array.length == 0) { if (array == null || array.length == 0) {
throw errorSupplier.get(); throw errorSupplier.get();
} }
return array;
} }
public static void isEmpty(Object[] array) { public static void isEmpty(Object[] array) {
...@@ -121,24 +124,25 @@ public class ValidateUtils { ...@@ -121,24 +124,25 @@ public class ValidateUtils {
isEmpty(array, () -> new ParamInvalidException(message)); isEmpty(array, () -> new ParamInvalidException(message));
} }
public static <T extends BaseException> void isEmpty(Object[] array, Supplier<T> errorSupplier) { public static <E extends BaseException> void isEmpty(Object[] array, Supplier<E> errorSupplier) {
if (array != null && array.length != 0) { if (array != null && array.length != 0) {
throw errorSupplier.get(); throw errorSupplier.get();
} }
} }
public static void notEmpty(Collection<?> collection) { public static <T, C extends Collection<T>> C notEmpty(C collection) {
notEmpty(collection, "The validated collection is empty"); return notEmpty(collection, "The validated collection is empty");
} }
public static void notEmpty(Collection<?> collection, String message) { public static <T, C extends Collection<T>> C notEmpty(C collection, String message) {
notEmpty(collection, () -> new ParamInvalidException(message)); return notEmpty(collection, () -> new ParamInvalidException(message));
} }
public static <T extends BaseException> void notEmpty(Collection<?> collection, Supplier<T> errorSupplier) { public static <T, C extends Collection<T>, E extends BaseException> C notEmpty(C collection, Supplier<E> errorSupplier) {
if (collection == null || collection.isEmpty()) { if (collection == null || collection.isEmpty()) {
throw errorSupplier.get(); throw errorSupplier.get();
} }
return collection;
} }
public static void isEmpty(Collection<?> collection) { public static void isEmpty(Collection<?> collection) {
...@@ -155,18 +159,19 @@ public class ValidateUtils { ...@@ -155,18 +159,19 @@ public class ValidateUtils {
} }
} }
public static void notEmpty(Map<?, ?> map) { public static <K, V> Map<K, V> notEmpty(Map<K, V> map) {
notEmpty(map, "The validated map is empty"); return notEmpty(map, "The validated map is empty");
} }
public static void notEmpty(Map<?, ?> map, String message) { public static <K, V> Map<K, V> notEmpty(Map<K, V> map, String message) {
notEmpty(map, () -> new ParamInvalidException(message)); return notEmpty(map, () -> new ParamInvalidException(message));
} }
public static <T extends BaseException> void notEmpty(Map<?, ?> map, Supplier<T> errorSupplier) { public static <K, V, E extends BaseException> Map<K, V> notEmpty(Map<K, V> map, Supplier<E> errorSupplier) {
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
throw errorSupplier.get(); throw errorSupplier.get();
} }
return map;
} }
public static void isEmpty(Map<?, ?> map) { public static void isEmpty(Map<?, ?> map) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment