Commit 4bdeb70e authored by liaozan's avatar liaozan 🏀

Improve ValidateUtils

parent 45db497f
package com.schbrain.common.util;
import com.schbrain.common.exception.BaseException;
import com.schbrain.common.exception.ParamInvalidException;
import java.util.*;
import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;
public class ValidateUtils {
......@@ -12,181 +15,123 @@ public class ValidateUtils {
private ValidateUtils() {
}
public static void isTrue(boolean expression) {
isTrue(expression, "操作有误");
public static void isTrue(boolean expression, String message) {
isTrue(expression, () -> new ParamInvalidException(message));
}
public static void isTrue(boolean expression, String message) {
public static <T extends BaseException> void isTrue(boolean expression, Supplier<T> errorSupplier) {
if (!expression) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void isFalse(boolean expression) {
isFalse(expression, "操作有误");
public static void isFalse(boolean expression, String message) {
isFalse(expression, () -> new ParamInvalidException(message));
}
public static void isFalse(boolean expression, String message) {
public static <T extends BaseException> void isFalse(boolean expression, Supplier<T> errorSupplier) {
if (expression) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void notNull(Object object) {
notNull(object, "The validated object is null");
public static void notNull(Object object, String message) {
notNull(object, () -> new ParamInvalidException(message));
}
public static void notNull(Object object, String message) {
public static <T extends BaseException> void notNull(Object object, Supplier<T> errorSupplier) {
if (object == null) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void isNull(Object object) {
isNull(object, "The validated object is not null");
public static void isNull(Object object, String message) {
isNull(object, () -> new ParamInvalidException(message));
}
public static void isNull(Object object, String message) {
public static <T extends BaseException> void isNull(Object object, Supplier<T> errorSupplier) {
if (object != null) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void notEmpty(String value) {
notEmpty(value, "The validated string is empty");
public static void notEmpty(String value, String message) {
notEmpty(value, () -> new ParamInvalidException(message));
}
public static void notEmpty(String value, String message) {
if (null == value || value.isBlank()) {
throw new ParamInvalidException(message);
public static <T extends BaseException> void notEmpty(String value, Supplier<T> errorSupplier) {
if (value == null || value.isBlank()) {
throw errorSupplier.get();
}
}
public static void isEmpty(String value) {
isEmpty(value, "The validated string is not empty");
public static void isEmpty(String value, String message) {
isEmpty(value, () -> new ParamInvalidException(message));
}
public static void isEmpty(String value, String message) {
public static <T extends BaseException> void isEmpty(String value, Supplier<T> errorSupplier) {
if (value != null && !value.isEmpty()) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void notEmpty(Object[] array) {
notEmpty(array, "The validated array is empty");
public static void notEmpty(Object[] array, String message) {
notEmpty(array, () -> new ParamInvalidException(message));
}
public static void notEmpty(Object[] array, String message) {
public static <T extends BaseException> void notEmpty(Object[] array, Supplier<T> errorSupplier) {
if (array == null || array.length == 0) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void isEmpty(Object[] array) {
isEmpty(array, "The validated array is not empty");
public static void isEmpty(Object[] array, String message) {
isEmpty(array, () -> new ParamInvalidException(message));
}
public static void isEmpty(Object[] array, String message) {
public static <T extends BaseException> void isEmpty(Object[] array, Supplier<T> errorSupplier) {
if (array != null && array.length != 0) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void notEmpty(Collection<?> collection) {
notEmpty(collection, "The validated collection is empty");
public static void notEmpty(Collection<?> collection, String message) {
notEmpty(collection, () -> new ParamInvalidException(message));
}
public static void notEmpty(Collection<?> collection, String message) {
public static <T extends BaseException> void notEmpty(Collection<?> collection, Supplier<T> errorSupplier) {
if (collection == null || collection.isEmpty()) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void isEmpty(Collection<?> collection) {
isEmpty(collection, "The validated collection is not empty");
public static void isEmpty(Collection<?> collection, String message) {
isEmpty(collection, () -> new ParamInvalidException(message));
}
public static void isEmpty(Collection<?> collection, String message) {
public static <T extends BaseException> void isEmpty(Collection<?> collection, Supplier<T> errorSupplier) {
if (collection != null && !collection.isEmpty()) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void notEmpty(Map<?, ?> map) {
notEmpty(map, "The validated map is empty");
public static void notEmpty(Map<?, ?> map, String message) {
notEmpty(map, () -> new ParamInvalidException(message));
}
public static void notEmpty(Map<?, ?> map, String message) {
public static <T extends BaseException> void notEmpty(Map<?, ?> map, Supplier<T> errorSupplier) {
if (map == null || map.isEmpty()) {
throw new ParamInvalidException(message);
throw errorSupplier.get();
}
}
public static void isEmpty(Map<?, ?> map) {
isEmpty(map, "The validated map is not empty");
}
public static void isEmpty(Map<?, ?> map, String message) {
if (map != null && !map.isEmpty()) {
throw new ParamInvalidException(message);
}
}
public static void noNullElements(Object[] array) {
notNull(array);
for (int i = 0; i < array.length; i++) {
if (array[i] == null) {
throw new ParamInvalidException("The validated array contains null element at index: " + i);
}
}
}
public static void noNullElements(Object[] array, String message) {
notNull(array);
for (Object item : array) {
if (item == null) {
throw new ParamInvalidException(message);
}
}
isEmpty(map, () -> new ParamInvalidException(message));
}
public static void noNullElements(Collection<?> collection, String message) {
notNull(collection);
for (Object item : collection) {
if (item == null) {
throw new ParamInvalidException(message);
}
}
}
public static void noNullElements(Collection<?> collection) {
notNull(collection);
int i = 0;
for (Iterator<?> it = collection.iterator(); it.hasNext(); i++) {
if (it.next() == null) {
throw new ParamInvalidException("The validated collection contains null element at index: " + i);
}
}
}
public static void allElementsOfType(Collection<?> collection, Class<?> clazz, String message) {
notNull(collection);
notNull(clazz);
for (Object item : collection) {
if (!clazz.isInstance(item)) {
throw new ParamInvalidException(message);
}
}
}
public static void allElementsOfType(Collection<?> collection, Class<?> clazz) {
notNull(collection);
notNull(clazz);
int i = 0;
for (Iterator<?> it = collection.iterator(); it.hasNext(); i++) {
if (!clazz.isInstance(it.next())) {
throw new ParamInvalidException("The validated collection contains an element not of type " + clazz.getName() + " at index: " + i);
}
public static <T extends BaseException> void isEmpty(Map<?, ?> map, Supplier<T> errorSupplier) {
if (map != null && !map.isEmpty()) {
throw errorSupplier.get();
}
}
......
......@@ -35,6 +35,11 @@
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
......@@ -57,20 +62,6 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>display-info</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
......
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