|
@@ -0,0 +1,388 @@
|
|
|
|
|
+package com.zd.common.core.utils;
|
|
|
|
|
+
|
|
|
|
|
+import com.zd.common.core.exception.ParamException;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author Quiet
|
|
|
|
|
+ *
|
|
|
|
|
+ * @CreateTime 2019年10月29日 下午4:56:48
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description 这个是参数校验的类
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+public class ParamCheckUtils {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(ParamCheckUtils.class);
|
|
|
|
|
+
|
|
|
|
|
+ private static class LoadParamCheck{
|
|
|
|
|
+ private static final ParamCheckUtils PARAM_CHECK_UTILS = new ParamCheckUtils();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private ParamCheckUtils() {}
|
|
|
|
|
+
|
|
|
|
|
+ public static ParamCheck build() {
|
|
|
|
|
+ return LoadParamCheck.PARAM_CHECK_UTILS.new ParamCheck();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static ParamCheck build(String msg) {
|
|
|
|
|
+ return LoadParamCheck.PARAM_CHECK_UTILS.new ParamCheck(msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static void throwParamException(String message) {
|
|
|
|
|
+ throw new ParamException(message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * # null == obj throw ParamException(1001,message);
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void notNull(Object obj, String message) {
|
|
|
|
|
+ if(null == obj)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public class ParamCheck{
|
|
|
|
|
+
|
|
|
|
|
+ private String msg;
|
|
|
|
|
+
|
|
|
|
|
+ public ParamCheck() {}
|
|
|
|
|
+
|
|
|
|
|
+ public ParamCheck(String msg) {
|
|
|
|
|
+ this.msg = msg;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void throwException(String message) {
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null == obj throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck notNull(Object obj, String message) {
|
|
|
|
|
+ if(null == obj)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null == obj throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck notNull(Object obj) {
|
|
|
|
|
+ return notNull(obj,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null==str || "".equals(str) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck strNotEmpty(String str, String message) {
|
|
|
|
|
+ if(null == str || "".equals(str))
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null==str || "".equals(str) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck strNotEmpty(String str) {
|
|
|
|
|
+ return strNotEmpty(str,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description param == null || param <= 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck greaterThanZero(Integer param, String message) {
|
|
|
|
|
+ notNull(param, message);
|
|
|
|
|
+ if(param <= 0) {
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ }
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description param == null || param <= 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck greaterThanZero(Integer param) {
|
|
|
|
|
+ return greaterThanZero(param,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description param == null || param <= 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck greaterThanZero(Byte param, String message) {
|
|
|
|
|
+ notNull(param, message);
|
|
|
|
|
+ if(param <= 0) {
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ }
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description param == null || param <= 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck greaterThanZero(Byte param) {
|
|
|
|
|
+ return greaterThanZero(param,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description param == null || param <= 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck greaterThanZero(Short param, String message) {
|
|
|
|
|
+ notNull(param, message);
|
|
|
|
|
+ if(param <= 0) {
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ }
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description param == null || param <= 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck greaterThanZero(Short param) {
|
|
|
|
|
+ return greaterThanZero(param,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description 0 == zero throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck notZero(Number number, String message) {
|
|
|
|
|
+ notNull(number, message);
|
|
|
|
|
+ if(number instanceof Byte) {
|
|
|
|
|
+ Byte bt = (Byte) number;
|
|
|
|
|
+ if(bt == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ } else if(number instanceof Short) {
|
|
|
|
|
+ Short st = (Short) number;
|
|
|
|
|
+ if(st == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ } else if(number instanceof Integer) {
|
|
|
|
|
+ Integer it = (Integer) number;
|
|
|
|
|
+ if(it == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ } else if(number instanceof Long) {
|
|
|
|
|
+ Long lg = (Long) number;
|
|
|
|
|
+ if(lg == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ } else if(number instanceof Double) {
|
|
|
|
|
+ Double de = (Double) number;
|
|
|
|
|
+ if(de == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ } else if(number instanceof Float) {
|
|
|
|
|
+ Float ft = (Float) number;
|
|
|
|
|
+ if(ft == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ LOGGER.error("该数据不在: Byte,Short,Integer,Long,Fload,Double中");
|
|
|
|
|
+ throwParamException("该数据不在: Byte,Short,Integer,Long,Fload,Double中");
|
|
|
|
|
+ }
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description 0 == zero throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck notZero(Number number) {
|
|
|
|
|
+ return notZero(number,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null == arr || arr.length == 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck arrNotEmpty(Object[] arr, String message) {
|
|
|
|
|
+ if(null == arr || arr.length == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null == arr || arr.length == 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck arrNotEmpty(Object[] arr) {
|
|
|
|
|
+ return arrNotEmpty(arr,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null == collections || collections.size() == 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck collectionNotEmpty(Collection<?> collections, String message) {
|
|
|
|
|
+ if(null == collections || collections.size() == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null == collections || collections.size() == 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck collectionNotEmpty(Collection<?> collections) {
|
|
|
|
|
+ return collectionNotEmpty(collections,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null == map || map.size() == 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck mapNotEmpty(Map<?, ?> map, String message) {
|
|
|
|
|
+ if(null == map || map.size() == 0)
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description null == map || map.size() == 0 throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck mapNotEmpty(Map<?, ?> map) {
|
|
|
|
|
+ return mapNotEmpty(map,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !Pattern.matches(regex, str) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck strNotRegex(String regex, String str, String message) {
|
|
|
|
|
+ if(!Pattern.matches(regex, str))
|
|
|
|
|
+ throwParamException(message);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !Pattern.matches(regex, str) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck strNotRegex(String regex, String str) {
|
|
|
|
|
+ return strNotRegex(regex,str,msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !Pattern.matches(email, msg) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck isNotMail(String email) {
|
|
|
|
|
+ return isNotMail(email, this.msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !Pattern.matches(email, msg) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck isNotMail(String email, String msg) {
|
|
|
|
|
+ Pattern p = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
|
|
|
|
|
+ if(!p.matcher(email).matches())
|
|
|
|
|
+ throwParamException(msg);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !Pattern.matches(mobile, msg) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck isNotMobile(String mobile) {
|
|
|
|
|
+ return isNotMobile(mobile, this.msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !Pattern.matches(mobile, msg) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck isNotMobile(String mobile,String msg) {
|
|
|
|
|
+ Pattern p = Pattern.compile("^1[3|4|5|6|7|8|9][0-9]\\d{8}$");
|
|
|
|
|
+ if(!p.matcher(mobile).matches())
|
|
|
|
|
+ throwParamException(msg);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !condition throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck isTrue(boolean condition, String msg) {
|
|
|
|
|
+ if(!condition)
|
|
|
|
|
+ throwParamException(msg);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !condition throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck isTrue(boolean condition) {
|
|
|
|
|
+ return isTrue(condition, this.msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !Pattern.matches(number, msg) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck isNotNumber(String number) {
|
|
|
|
|
+ return isNotNumber(number, this.msg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @Description !Pattern.matches(number, msg) throw ParamException
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ public ParamCheck isNotNumber(String number,String msg) {
|
|
|
|
|
+ Pattern p = Pattern.compile("^-?\\d+(\\.\\d+)?$");
|
|
|
|
|
+ if(!p.matcher(number).matches())
|
|
|
|
|
+ throwParamException(msg);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public ParamCheck isNotSpecialChar(String str) {
|
|
|
|
|
+ Pattern p = Pattern.compile("[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t");
|
|
|
|
|
+ if(p.matcher(str).find())
|
|
|
|
|
+ throwParamException(msg);
|
|
|
|
|
+ return this;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|