|
@@ -7,14 +7,13 @@ import java.util.Collection;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @author Quiet
|
|
|
|
|
- *
|
|
|
|
|
- * @CreateTime 2019年10月29日 下午4:56:48
|
|
|
|
|
- *
|
|
|
|
|
- * @Description 这个是参数校验的类
|
|
|
|
|
|
|
+ * <p>参数检查类</p>
|
|
|
*
|
|
*
|
|
|
|
|
+ * @author: linft
|
|
|
|
|
+ * @date: 2023/4/14
|
|
|
|
|
+ * @since:
|
|
|
*/
|
|
*/
|
|
|
public class ParamCheckUtils {
|
|
public class ParamCheckUtils {
|
|
|
|
|
|
|
@@ -37,9 +36,12 @@ public class ParamCheckUtils {
|
|
|
private static void throwParamException(String message) {
|
|
private static void throwParamException(String message) {
|
|
|
throw new ParamException(message);
|
|
throw new ParamException(message);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- * # null == obj throw ParamException(1001,message);
|
|
|
|
|
|
|
+ * 检查Object非空
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param obj
|
|
|
|
|
+ * @param message
|
|
|
*/
|
|
*/
|
|
|
public static void notNull(Object obj, String message) {
|
|
public static void notNull(Object obj, String message) {
|
|
|
if(null == obj)
|
|
if(null == obj)
|
|
@@ -59,51 +61,60 @@ public class ParamCheckUtils {
|
|
|
public void throwException(String message) {
|
|
public void throwException(String message) {
|
|
|
throwParamException(message);
|
|
throwParamException(message);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null == obj throw ParamException
|
|
|
|
|
|
|
+ * 检查Object不为null,指定提示message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param obj
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck notNull(Object obj, String message) {
|
|
public ParamCheck notNull(Object obj, String message) {
|
|
|
if(null == obj)
|
|
if(null == obj)
|
|
|
throwParamException(message);
|
|
throwParamException(message);
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null == obj throw ParamException
|
|
|
|
|
|
|
+ * 检查Object不为null
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param obj
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck notNull(Object obj) {
|
|
public ParamCheck notNull(Object obj) {
|
|
|
return notNull(obj,msg);
|
|
return notNull(obj,msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null==str || "".equals(str) throw ParamException
|
|
|
|
|
|
|
+ * 检查字符串不为空,指定提示msg
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param str
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck strNotEmpty(String str, String message) {
|
|
public ParamCheck strNotEmpty(String str, String message) {
|
|
|
if(null == str || "".equals(str))
|
|
if(null == str || "".equals(str))
|
|
|
throwParamException(message);
|
|
throwParamException(message);
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null==str || "".equals(str) throw ParamException
|
|
|
|
|
|
|
+ * 检查字符串不为空,指定提示msg
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param str
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck strNotEmpty(String str) {
|
|
public ParamCheck strNotEmpty(String str) {
|
|
|
return strNotEmpty(str,msg);
|
|
return strNotEmpty(str,msg);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description param == null || param <= 0 throw ParamException
|
|
|
|
|
|
|
+ * 检查参数int大于0,指定提示message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param param
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck greaterThanZero(Integer param, String message) {
|
|
public ParamCheck greaterThanZero(Integer param, String message) {
|
|
|
notNull(param, message);
|
|
notNull(param, message);
|
|
@@ -112,64 +123,23 @@ public class ParamCheckUtils {
|
|
|
}
|
|
}
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description param == null || param <= 0 throw ParamException
|
|
|
|
|
|
|
+ * 检查参数int大于0,指定提示message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param param
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck greaterThanZero(Integer param) {
|
|
public ParamCheck greaterThanZero(Integer param) {
|
|
|
return greaterThanZero(param,msg);
|
|
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
|
|
|
|
|
|
|
+ * 检查数字类不为0,指定提示语message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param number
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck notZero(Number number, String message) {
|
|
public ParamCheck notZero(Number number, String message) {
|
|
|
notNull(number, message);
|
|
notNull(number, message);
|
|
@@ -203,109 +173,127 @@ public class ParamCheckUtils {
|
|
|
}
|
|
}
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description 0 == zero throw ParamException
|
|
|
|
|
|
|
+ * 检查数字类不为0
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param number
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck notZero(Number number) {
|
|
public ParamCheck notZero(Number number) {
|
|
|
return notZero(number,msg);
|
|
return notZero(number,msg);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null == arr || arr.length == 0 throw ParamException
|
|
|
|
|
|
|
+ * 检查Object[]不为空,指定提示语message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param arr
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck arrNotEmpty(Object[] arr, String message) {
|
|
public ParamCheck arrNotEmpty(Object[] arr, String message) {
|
|
|
if(null == arr || arr.length == 0)
|
|
if(null == arr || arr.length == 0)
|
|
|
throwParamException(message);
|
|
throwParamException(message);
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null == arr || arr.length == 0 throw ParamException
|
|
|
|
|
|
|
+ * 检查Object[]不为空,指定提示语message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param arr
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck arrNotEmpty(Object[] arr) {
|
|
public ParamCheck arrNotEmpty(Object[] arr) {
|
|
|
return arrNotEmpty(arr,msg);
|
|
return arrNotEmpty(arr,msg);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null == collections || collections.size() == 0 throw ParamException
|
|
|
|
|
|
|
+ * 检查Collection不为空,指定提示语message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param collections
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck collectionNotEmpty(Collection<?> collections, String message) {
|
|
public ParamCheck collectionNotEmpty(Collection<?> collections, String message) {
|
|
|
if(null == collections || collections.size() == 0)
|
|
if(null == collections || collections.size() == 0)
|
|
|
throwParamException(message);
|
|
throwParamException(message);
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null == collections || collections.size() == 0 throw ParamException
|
|
|
|
|
|
|
+ * 检查Collection不为空
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param collections
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck collectionNotEmpty(Collection<?> collections) {
|
|
public ParamCheck collectionNotEmpty(Collection<?> collections) {
|
|
|
return collectionNotEmpty(collections,msg);
|
|
return collectionNotEmpty(collections,msg);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null == map || map.size() == 0 throw ParamException
|
|
|
|
|
|
|
+ * 检查Map不为空,指定提示语message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param map
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck mapNotEmpty(Map<?, ?> map, String message) {
|
|
public ParamCheck mapNotEmpty(Map<?, ?> map, String message) {
|
|
|
if(null == map || map.size() == 0)
|
|
if(null == map || map.size() == 0)
|
|
|
throwParamException(message);
|
|
throwParamException(message);
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description null == map || map.size() == 0 throw ParamException
|
|
|
|
|
|
|
+ * 检查Map不为空
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param map
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck mapNotEmpty(Map<?, ?> map) {
|
|
public ParamCheck mapNotEmpty(Map<?, ?> map) {
|
|
|
return mapNotEmpty(map,msg);
|
|
return mapNotEmpty(map,msg);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description !Pattern.matches(regex, str) throw ParamException
|
|
|
|
|
|
|
+ * 检查regex和str是否一致,指定提示语message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param regex
|
|
|
|
|
+ * @param str
|
|
|
|
|
+ * @param message
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck strNotRegex(String regex, String str, String message) {
|
|
public ParamCheck strNotRegex(String regex, String str, String message) {
|
|
|
if(!Pattern.matches(regex, str))
|
|
if(!Pattern.matches(regex, str))
|
|
|
throwParamException(message);
|
|
throwParamException(message);
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
- *
|
|
|
|
|
- * @Description !Pattern.matches(regex, str) throw ParamException
|
|
|
|
|
|
|
+ * 检查regex和str是否一致,指定提示语message
|
|
|
*
|
|
*
|
|
|
|
|
+ * @param regex
|
|
|
|
|
+ * @param str
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck strNotRegex(String regex, String str) {
|
|
public ParamCheck strNotRegex(String regex, String str) {
|
|
|
return strNotRegex(regex,str,msg);
|
|
return strNotRegex(regex,str,msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * 检查是否为Mail地址
|
|
|
*
|
|
*
|
|
|
- * @Description !Pattern.matches(email, msg) throw ParamException
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * @param email
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck isNotMail(String email) {
|
|
public ParamCheck isNotMail(String email) {
|
|
|
return isNotMail(email, this.msg);
|
|
return isNotMail(email, this.msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * 检查是否为Mail地址,指定提示msg
|
|
|
*
|
|
*
|
|
|
- * @Description !Pattern.matches(email, msg) throw ParamException
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * @param email
|
|
|
|
|
+ * @param msg
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck isNotMail(String email, String msg) {
|
|
public ParamCheck isNotMail(String email, String msg) {
|
|
|
Pattern p = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
|
|
Pattern p = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
|
|
@@ -315,18 +303,21 @@ public class ParamCheckUtils {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * 检查是否为手机号
|
|
|
*
|
|
*
|
|
|
- * @Description !Pattern.matches(mobile, msg) throw ParamException
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * @param mobile
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck isNotMobile(String mobile) {
|
|
public ParamCheck isNotMobile(String mobile) {
|
|
|
return isNotMobile(mobile, this.msg);
|
|
return isNotMobile(mobile, this.msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * 检查是否为手机号,指定提示msg
|
|
|
*
|
|
*
|
|
|
- * @Description !Pattern.matches(mobile, msg) throw ParamException
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * @param mobile
|
|
|
|
|
+ * @param msg
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck isNotMobile(String mobile,String msg) {
|
|
public ParamCheck isNotMobile(String mobile,String msg) {
|
|
|
Pattern p = Pattern.compile("^1[3|4|5|6|7|8|9][0-9]\\d{8}$");
|
|
Pattern p = Pattern.compile("^1[3|4|5|6|7|8|9][0-9]\\d{8}$");
|
|
@@ -334,40 +325,24 @@ public class ParamCheckUtils {
|
|
|
throwParamException(msg);
|
|
throwParamException(msg);
|
|
|
return this;
|
|
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
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * @param number
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck isNotNumber(String number) {
|
|
public ParamCheck isNotNumber(String number) {
|
|
|
return isNotNumber(number, this.msg);
|
|
return isNotNumber(number, this.msg);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
|
|
+ * 检查参数是否为数字,指定提示msg
|
|
|
*
|
|
*
|
|
|
- * @Description !Pattern.matches(number, msg) throw ParamException
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * @param number
|
|
|
|
|
+ * @param msg
|
|
|
|
|
+ * @return
|
|
|
*/
|
|
*/
|
|
|
public ParamCheck isNotNumber(String number,String msg) {
|
|
public ParamCheck isNotNumber(String number,String msg) {
|
|
|
Pattern p = Pattern.compile("^-?\\d+(\\.\\d+)?$");
|
|
Pattern p = Pattern.compile("^-?\\d+(\\.\\d+)?$");
|
|
@@ -376,13 +351,17 @@ public class ParamCheckUtils {
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 检查参数是否包含特殊字符
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param str
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
public ParamCheck isNotSpecialChar(String str) {
|
|
public ParamCheck isNotSpecialChar(String str) {
|
|
|
Pattern p = Pattern.compile("[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t");
|
|
Pattern p = Pattern.compile("[ _`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t");
|
|
|
if(p.matcher(str).find())
|
|
if(p.matcher(str).find())
|
|
|
throwParamException(msg);
|
|
throwParamException(msg);
|
|
|
return this;
|
|
return this;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
}
|
|
}
|