package com.gschen.common.util;
import com.google.common.base.Preconditions;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Pattern;
public final class CheckUtil {
public static final Pattern PHONE = Pattern.compile("(13\\d|14[579]|15[^4\\D]|17[^49\\D]|18\\d)\\d{8}");
public static void checkArgument(boolean expr, String msg) {
Preconditions.checkArgument(expr, msg);
}
public static void checkNotBlank(String str, String msg) {
checkArgument(isNotBlank(str), msg);
}
public static void checkPattern(String s, Pattern p, String msg) {
checkArgument(p.matcher(s).matches(), msg);
}
public static void checkNotNull(Object o, String msg) {
checkArgument(o != null, msg);
}
public static void checkNotNull(Object o) {
checkNotNull(o, "参数不能为空");
}
public static void checkPositive(Number o, String msg) {
checkArgument(isPositive(o), msg);
}
public static void checkPositive(String o, String msg) {
checkArgument(isPositive(o), msg);
}
public static void checkBetween(Number target, Number from, Number to, String msg) {
checkNotNull(target, msg);
if (from != null) {
checkArgument(target.doubleValue() >= from.doubleValue(), msg);
}
if (to != null) {
checkArgument(target.doubleValue() <= to.doubleValue(), msg);
}
}
public static void checkNonNegtive(Number o, String msg) {
checkArgument(isNonNegtive(o), msg);
}
public static void checkNonNegtive(String o, String msg) {
checkArgument(isNonNegtive(o), msg);
}
public static void checkNotEmpty(Collection<?> collection, String msg) {
checkArgument(isNotEmpty(collection), msg);
}
public static void checkCollectionMaxLength(Collection<?> collection, int maxLength, String msg) {
checkArgument(CollectionUtils.size(collection) <= maxLength, msg);
}
public static void checkNotEmpty(Map<?, ?> map, String msg) {
checkArgument(isNotEmpty(map), msg);
}
public static void checkNotEmpty(Object[] arr, String msg) {
checkArgument(isNotEmpty(arr), msg);
}
public static void checkInvokerClass(String... clsNames) {
if (hasInvokedBy(clsNames)) {
return;
}
throw new UnsupportedOperationException("当前方法只允许被: " + Arrays.toString(clsNames) + " 调用");
}
public static void checkInvokerClass(Class<?> cls) {
checkInvokerClass(cls.getCanonicalName());
}
public static void checkMaxLen(String field, int maxLen, String errMsg) {
if (StringUtils.isBlank(field) || maxLen < 0) {
return;
}
checkArgument(field.length() <= maxLen, errMsg);
}
public static boolean isBlank(String str) {
return !isNotBlank(str);
}
public static boolean isNotBlank(String str) {
return str != null && str.trim().length() > 0;
}
public static boolean isPositive(Number n) {
return n != null && n.doubleValue() > 0;
}
public static boolean isPositive(String n) {
if (!NumberUtils.isCreatable(n)) {
return false;
}
return isPositive(Double.valueOf(n));
}
public static boolean isNonNegtive(Number n) {
return n != null && n.doubleValue() >= 0;
}
public static boolean isNonNegtive(String n) {
if (!NumberUtils.isCreatable(n)) {
return false;
}
return isNonNegtive(Double.valueOf(n));
}
public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}
public static boolean isEmpty(Object[] arr) {
return arr == null || arr.length == 0;
}
public static boolean isEmpty(Map<?, ?> map) {
return map == null || map.isEmpty();
}
public static boolean isNotEmpty(Collection<?> collection) {
return !isEmpty(collection);
}
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
}
public static boolean isNotEmpty(Object[] arr) {
return !isEmpty(arr);
}
public static boolean isExceedLength(String s, int maxLen) {
return s != null && maxLen > -1 && s.length() > maxLen;
}
public static boolean isEqual(Object o1, Object o2) {
return (o1 == null && o2 == null) || (o1 != null && o1.equals(o2));
}
public static void checkEqual(Object o1, Object o2, String msg) {
checkArgument(isEqual(o1, o2), msg);
}
public static boolean hasInvokedBy(String... clsNames) {
StackTraceElement[] st = Thread.currentThread().getStackTrace();
for (StackTraceElement s : st) {
for (String clsName : clsNames) {
if (clsName.equals(s.getClassName())) {
return true;
}
}
}
return false;
}
public static boolean hasInvokedBy(Class<?> cls) {
return hasInvokedBy(cls.getCanonicalName());
}
}