如果大量变量需要非空校验,如果变量为空返回对应的提示信息 package com.util;import java.util.Collection;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;/** * 参数校验 * 实例: * CheckP
package com.util; import java.util.Collection; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; /** * 参数校验 * 实例: * CheckParametersUtil.getInstance() * .put(loginUserId, "loginUserId") * .put(branId, "branId") * .put(shelfNo, "shelfNo") * .put(newShelfNo, "newShelfNo") * .checkParameter(); * @ClassName: CheckParametersUtil * @Description:TODO * @author: XuWei * @date: 2017年11月28日 * * */ public class CheckParametersUtil { Mapmap = new HashMap<>(); /** * 添加需要校验的参数 * @param object 实参 * @param parameterName 参数名称 * @return CheckParametersUtil * @author: XuWei */ public CheckParametersUtil put(Object object, String parameterName) { map.put(parameterName, object); return this; } /** * 获取CheckParametersUtil实例 * @return CheckParametersUtil * @author: XuWei */ public static CheckParametersUtil getInstance(){ return new CheckParametersUtil(); } /** * 校验 * @return DataMessage * @author: XuWei * @throws Exception */ public void checkParameter() throws Exception { for(Entry entry : map.entrySet()) { if(isEmpty(entry.getValue())){ throw new Exception("参数【" + entry.getKey() + "】为空" ); } } } public String toString(Object object) { return object == null ? "" : object.toString(); } public boolean isEmpty(Collection collection) { return collection == null || collection.isEmpty(); } public boolean isEmpty(Map map) { return map == null || map.isEmpty(); } public boolean isEmpty(String string) { return toString(string).isEmpty(); } public boolean isEmptyTrim(String string) { return toString(string).trim().isEmpty(); } public boolean isEmpty(Object object) { return toString(object).isEmpty(); } public boolean isEmptyTrim(Object object) { return toString(object).trim().isEmpty(); } public boolean isEmpty(T[] array) { return array == null || array.length == 0; } }