You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
luoo_parent/luoo_common/src/main/java/util/AssertUtil.java

147 lines
4.0 KiB

package util;
import exception.BizException;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.Collection;
public class AssertUtil {
public static void mustTrue(boolean flag, String tips) {
if (!flag) {
assertError(tips);
}
}
public static void mustFalse(boolean flag, String tips) {
mustTrue(!flag, tips);
}
public static void valueLimit(Number value, Number min, Number max, String tips) {
if (null == value || value.longValue() < min.longValue() || value.longValue() > max.longValue()) {
assertError(tips);
}
}
public static void valueMinLimit(Number value, Number min, String tips) {
if (null == value || value.longValue() < min.longValue()) {
assertError(tips);
}
}
public static void valueMaxLimit(Number value, Number max, String tips) {
if (null == value || value.longValue() > max.longValue()) {
assertError(tips);
}
}
public static void lengthLimit(String text, int min, int max, String tips) {
if (null == text || text.length() < min || text.length() > max) {
assertError(tips);
}
}
public static void sizeLimit(Collection<?> array, int min, int max, String tips) {
if (null == array || array.size() < min || array.size() > max) {
assertError(tips);
}
}
public static void sizeLimit(Object[] array, int min, int max, String tips) {
if (null == array || array.length < min || array.length > max) {
assertError(tips);
}
}
public static void mustNotIn(Object source, Object[] arrays, String tips) {
Object find = CollectionUtil.findAny(Arrays.asList(arrays), source::equals);
if (null != find) {
assertError(tips);
}
}
public static void mustIn(Object source, Object[] arrays, String tips) {
Object find = CollectionUtil.findAny(Arrays.asList(arrays), source::equals);
if (null == find) {
assertError(tips);
}
}
public static void mustNotEq(Object source, Object unexpect, String tips) {
if (unexpect.equals(source)) {
assertError(tips);
}
}
public static void mustEq(Object source, Object expect, String tips) {
if (!expect.equals(source)) {
assertError(tips);
}
}
public static void mustNull(Object source, String tips) {
if (null != source) {
assertError(tips);
}
}
public static void mustNotNull(Object source, String tips) {
if (null == source) {
assertError(tips);
}
}
public static void mustNotBlank(String text, String tips) {
if (StringUtils.isBlank(text)) {
assertError(tips);
}
}
public static void mustEmpty(String text, String tips) {
if (!StringUtils.isEmpty(text)) {
assertError(tips);
}
}
public static void mustEmpty(Collection<?> collection, String tips) {
if (null != collection && collection.size() > 0) {
assertError(tips);
}
}
public static void mustEmpty(Object[] arrays, String tips) {
if (null != arrays && arrays.length > 0) {
assertError(tips);
}
}
public static void mustNotEmpty(String text, String tips) {
if (StringUtils.isEmpty(text)) {
assertError(tips);
}
}
public static void mustNotEmpty(Collection<?> collection, String tips) {
if (null == collection || collection.size() == 0) {
assertError(tips);
}
}
public static void mustNotEmpty(Object[] arrays, String tips) {
if (null == arrays || arrays.length == 0) {
assertError(tips);
}
}
public static void mustGtZero(Long value, String tips) {
if (value <= 0) {
assertError(tips);
}
}
public static void assertError(String tips) {
throw new BizException(tips);
}
}