import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
/**
* @description: <br/>
* 参数校验工具类
* <p>
* <br/>
* @author: Qz1997
* @create 2021/7/28 15:56
*/
@SuppressWarnings("unused")
public final class ObjUtils {
/**
* 对象转boolean
* <p>
* Map<Object, Object> map = new HashMap<>(16);
* map.put("1", "1");
* isBoolean(map); => true
* isBoolean(new HashMap<>()); => false
* isBoolean(""); => false
* isBoolean("12331"); => true
* isBoolean(null); => false
* isBoolean(new String[]{}); => false
* isBoolean(new String[]{""}); => true
* isBoolean(Arrays.asList()); => false
* isBoolean(Collections.singletonList("1")); => true
* isBoolean(new Object()); => true
*
* @param obj obj
* @return boolean
*/
public static boolean toBoolean(Object obj) {
if (obj == null) {
return false;
}
if (obj instanceof CharSequence) {
return ((CharSequence) obj).length() != 0;
}
if (obj.getClass().isArray()) {
return Array.getLength(obj) != 0;
}
if (obj instanceof Collection<?>) {
return !((Collection<?>) obj).isEmpty();
}
if (obj instanceof Map<?, ?>) {
return !((Map<?, ?>) obj).isEmpty();
}
return true;
}
/**
* 校验参数必须全部为空 返回 true
* <p>
* checkParamAllNull(); => true
* checkParamAllNull(null); => true
* checkParamAllNull(null, null); => true
* checkParamAllNull(new Object(), null); => false
* checkParamAllNull(new Object(), new Object()); => false
*
* @param obj bj
* @return boolean
*/
public static boolean checkParamAllNull(Object... obj) {
if (ArrayUtils.isEmpty(obj)) {
return true;
}
long count = Arrays.stream(obj).filter(ObjectUtils::isEmpty).count();
return count == obj.length;
}
/**
* 校验参数有一个为空 返回 true
* checkParamOrNull(); => true
* checkParamOrNull(null); => true
* checkParamOrNull(null, null); => true
* checkParamOrNull(new Object(), null); => true
* checkParamOrNull(new Object(), new Object()); => false
*
* @param obj obj
* @return boolean
*/
public static boolean checkParamOrNull(Object... obj) {
if (ArrayUtils.isEmpty(obj)) {
return true;
}
long count = Arrays.stream(obj).filter(ObjectUtils::isEmpty).count();
return count != 0;
}
}
参数校验工具类
最新推荐文章于 2023-05-23 16:58:23 发布