|
@@ -939,6 +939,92 @@ public class ValidationUtil {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: checkNullString
|
|
|
+ * @Description: TODO(检测字符串是否为null,否则就报错,如果传入格式要求还要符合传入的格式要求,不然也报错)
|
|
|
+ * @param @param obj
|
|
|
+ * @param @param patterns 设定文件
|
|
|
+ * @return void 返回类型
|
|
|
+ * @author ylx
|
|
|
+ * @date 2018年1月29日 上午11:43:30
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static final void checkNullString(Object obj,String...patterns){
|
|
|
+ try {
|
|
|
+ String str=String.class.cast(obj);
|
|
|
+ if(null==str) throw new RuntimeException("字符串不能为null");
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(str.matches(pattern)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: checkEmptyString
|
|
|
+ * @Description: TODO(检测字符串是否为null/"",否则就报错,如果传入格式要求还要符合传入的格式要求,不然也报错)
|
|
|
+ * @param @param obj
|
|
|
+ * @param @param patterns 设定文件
|
|
|
+ * @return void 返回类型
|
|
|
+ * @author ylx
|
|
|
+ * @date 2018年1月29日 上午11:43:30
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static final void checkEmptyString(Object obj,String...patterns){
|
|
|
+ try {
|
|
|
+ String str=String.class.cast(obj);
|
|
|
+ if(null==str) throw new RuntimeException("字符串不能为null");
|
|
|
+ if("".equals(str)) throw new RuntimeException("字符串内容不能为空");
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(str.matches(pattern)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @Title: checkBlankString
|
|
|
+ * @Description: TODO(检测字符串是否为null/""/" ",否则就报错,如果传入格式要求还要符合传入的格式要求,不然也报错)
|
|
|
+ * @param @param obj
|
|
|
+ * @param @param patterns 设定文件
|
|
|
+ * @return void 返回类型
|
|
|
+ * @author ylx
|
|
|
+ * @date 2018年1月29日 上午11:43:30
|
|
|
+ * @throws
|
|
|
+ */
|
|
|
+ public static final void checkBlankString(Object obj,String...patterns){
|
|
|
+ try {
|
|
|
+ String str=String.class.cast(obj);
|
|
|
+ if(null==str) throw new RuntimeException("字符串不能为null");
|
|
|
+ if("".equals(str.trim())) throw new RuntimeException("字符串内容不能为空");
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(str.matches(pattern)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
System.out.println(checkAndAssignNullIntegerIfIsBlank(" "));
|