stringutils 判断对象以及对象内的map list是否为空

本文介绍了一个Java实用工具类ArrayIsNotNull,该类提供了多种方法来检查对象及其内部结构如字符串、数组、集合和映射等是否为空或未指定。通过抛出异常的方式确保程序的健壮性和数据的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());	
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值