stringutils 判断对象以及对象内的map list是否为空
public class ArrayIsNotNull {
public static void notEmpty(String message,Object obj) {
if (obj == null){
throw new IllegalArgumentException(message + " must be specified,obj is null");
}
if (obj instanceof String && obj.toString().trim().length()==0){
throw new IllegalArgumentException(message + " must be specified,String is empty");
}
if (obj.getClass().isArray() && Array.getLength(obj)==0){
throw new IllegalArgumentException(message + " must be specified,Array is empty");
}
if (obj instanceof Collection && ((Collection)obj).isEmpty()){
throw new IllegalArgumentException(message + " must be specified, Collection is empty");
}
if (obj instanceof Map && ((Map)obj).isEmpty()){
throw new IllegalArgumentException(message + " must be specified,Map is empty");
}
}
public static boolean isNull(Object obj) {
boolean result=false;
if (obj == null){
result=true;
return result;
}
if (obj instanceof String && obj.toString().trim().length()==0){
result=true;
return result;
}
if (obj.getClass().isArray() && Array.getLength(obj)==0){
result=true;
return result;
}
if (obj instanceof Collection && ((Collection)obj).isEmpty()){
result=true;
return result;
}
if (obj instanceof Map && ((Map)obj).isEmpty()){
result=true;
return result;
}
return result;
}
public static void isNullAndThrowExp(String[] msg, Object... o) {
StringBuffer buffer = new StringBuffer();
if(msg.length+1 != o.length)
throw new IllegalArgumentException(
"strs's length is not equlas checkNames's length");
for (int i = 1; i < o.length; i++) {
//student.getmap() 的值为null
if (isNull(o[i])) {
buffer.append(msg[i-1]+",");
}
}
if(buffer.toString().endsWith(",")){
buffer.deleteCharAt(buffer.length() - 1);
buffer.append(" must be specified");
throw new IllegalArgumentException(buffer.toString());
}
}
public static void main(String[] args) throws Exception {
Student student=new Student();
student.setAge(11);
student.setNameString("");
student.setPeople(true);
ArrayList<Object> list=new ArrayList<Object>();
list.add("");
student.setList(list);
Map<Object, Object> map=new HashMap<Object, Object>();
//map.put("", "");
student.setMap(map);
isNullAndThrowExp(new String[]{"age","nameString","isPeople","list","map"},student,student.getAge(),student.getNameString(),student.isPeople(),student.getList(),student.getMap());
}
}