判断参数是否为空,是因为在controller层会写很多if else 这样代码很冗余,封装一种方法可以判断参数是否为空,判断集合,是否为0,因为现在业务使用需要判断类型不是很多,如果多种类型可以考虑新增方法去判断,最好不要在一个方法中写太多代码,这样不方便阅读,希望帮助到大家。
/**
* 判断参数是否为空
*
* @param keys
* 1、代表需要判断是否小于或等于0
* 2、判断集合
* @param strs
* 具体值
* @return
*/
public static boolean isNotEmptyBatch(String[] keys, Object... strs) {
for (int i = 0; i < strs.length; i++) {
Object string = strs[i];
if (string == null || "".equals(string)) {
return false;
}
if (null != keys[i] ) {
if (keys[i].equals("1")) {
String strValue = strs[i].toString();
if (!isDigit(strValue) || Integer.valueOf(strValue) <= 0) {
return false;
}
}
if (keys[i].equals("2")) {
List<?> list = (List<?>)strs[i];
if (list.size() <=0) {
return false;
}
}
}
}
return true;
}
/**
* 判断是否是数字
*
* @param str
* @return
*/
public static boolean isDigit(String str) {
if (isNull(str))
return false;
for (int i = 0, size = str.length(); i < size; i++) {
if (!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
public static boolean isNull(String str) {
return (str == null || str.equals("null") || str.trim().length() == 0);
}
public static void main(String[] args) {
System.out.println(isNotEmptyBatch(new String[]{"1","2"}, "1",new ArrayList()));
}