|
@@ -25,7 +25,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
public class ValidationUtil {
|
|
|
|
|
|
private ValidationUtil() {
|
|
|
- throw new AssertionError("不能实例化 ValidationUtil");
|
|
|
+ throw new AssertionError("instaniation is not permitted");
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -64,12 +64,13 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的范围,超出范围的也会报异常.
|
|
|
* 如果一切都解析正常,返回一个true的布尔类型
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验
|
|
|
* @return boolean
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static boolean checkRangeOfInt(final Object obj, int min, int max) {
|
|
|
+ public static boolean checkRangeOfInt(final Object obj, int min, int max, String... patterns) {
|
|
|
String number = null;
|
|
|
try {
|
|
|
if (obj == null) {
|
|
@@ -110,6 +111,14 @@ public class ValidationUtil {
|
|
|
throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(String.valueOf(obj).matches(pattern)){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -119,13 +128,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最小值,小于最小值的也会报异常.
|
|
|
* 如果一切都解析正常,返回一个true的布尔类型
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验
|
|
|
* @return boolean
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static boolean checkMinOfInt(final Object obj, int min) {
|
|
|
- return checkRangeOfInt(obj, min, Integer.MAX_VALUE);
|
|
|
+ public static boolean checkMinOfInt(final Object obj, int min,String...patterns) {
|
|
|
+ return checkRangeOfInt(obj, min, Integer.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -134,13 +144,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最大值,大于最大值的也会报异常.
|
|
|
* 如果一切都解析正常,返回一个true的布尔类型
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验
|
|
|
* @return boolean
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static boolean checkMaxOfInt(final Object obj, int max) {
|
|
|
- return checkRangeOfInt(obj, Integer.MIN_VALUE, max);
|
|
|
+ public static boolean checkMaxOfInt(final Object obj, int max,String...patterns) {
|
|
|
+ return checkRangeOfInt(obj, Integer.MIN_VALUE, max,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -149,13 +160,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 针对整个Integer范围内的数据校验(也就是能被解析成Integer的String还是Integer就能校验成功返回true)
|
|
|
* 如果一切都解析正常,返回一个true的布尔类型
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验
|
|
|
* @return boolean
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static boolean checkOfInt(final Object obj) {
|
|
|
- return checkRangeOfInt(obj, Integer.MIN_VALUE, Integer.MAX_VALUE);
|
|
|
+ public static boolean checkOfInt(final Object obj,String...patterns) {
|
|
|
+ return checkRangeOfInt(obj, Integer.MIN_VALUE, Integer.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -165,12 +177,13 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Long类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的范围,超出范围的也会报异常.
|
|
|
* 如果一切都解析正常,返回一个true的布尔类型
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验
|
|
|
* @return boolean
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static boolean checkRangeOfLong(final Object obj, long min, long max) {
|
|
|
+ public static boolean checkRangeOfLong(final Object obj, long min, long max,String...patterns) {
|
|
|
String number = null;
|
|
|
try {
|
|
|
if (obj == null) {
|
|
@@ -211,6 +224,14 @@ public class ValidationUtil {
|
|
|
throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(String.valueOf(obj).matches(pattern)){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -220,13 +241,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Long类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最小值,小于最小值的也会报异常.
|
|
|
* 如果一切都解析正常,返回一个true的布尔类型
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验
|
|
|
* @return boolean
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static boolean checkMinOfLong(final Object obj, long min) {
|
|
|
- return checkRangeOfLong(obj, min, Long.MAX_VALUE);
|
|
|
+ public static boolean checkMinOfLong(final Object obj, long min,String...patterns) {
|
|
|
+ return checkRangeOfLong(obj, min, Long.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -235,13 +257,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Long类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最大值,大于最大值的也会报异常.
|
|
|
* 如果一切都解析正常,返回一个true的布尔类型
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验
|
|
|
* @return boolean
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static boolean checkMaxOfLong(final Object obj, long max) {
|
|
|
- return checkRangeOfLong(obj, Long.MIN_VALUE, max);
|
|
|
+ public static boolean checkMaxOfLong(final Object obj, long max,String...patterns) {
|
|
|
+ return checkRangeOfLong(obj, Long.MIN_VALUE, max,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -250,13 +273,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Long类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 这个针对Long范围内类型数据的校验
|
|
|
* 如果一切都解析正常,返回一个true的布尔类型
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验
|
|
|
* @return boolean
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static boolean checkOfLong(final Object obj) {
|
|
|
- return checkRangeOfLong(obj, Long.MIN_VALUE, Long.MAX_VALUE);
|
|
|
+ public static boolean checkOfLong(final Object obj,String...patterns) {
|
|
|
+ return checkRangeOfLong(obj, Long.MIN_VALUE, Long.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -265,12 +289,13 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的范围,超出范围的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以int类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return int
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static int checkRangeAndAssignInt(final Object obj, int min, int max) {
|
|
|
+ public static int checkRangeAndAssignInt(final Object obj, int min, int max,String...patterns) {
|
|
|
String number = null;
|
|
|
try {
|
|
|
if (obj == null) {
|
|
@@ -285,6 +310,14 @@ public class ValidationUtil {
|
|
|
if(num > max){
|
|
|
throw new OutOfRangeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(number.matches(pattern)){
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return num;
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
@@ -311,6 +344,14 @@ public class ValidationUtil {
|
|
|
if(num > max){
|
|
|
throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(String.valueOf(obj).matches(pattern)){
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return num;
|
|
|
}
|
|
|
}
|
|
@@ -321,13 +362,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最小值,小于最小值的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以int类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return int
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static int checkMinAndAssignInt(final Object obj, int min) {
|
|
|
- return checkRangeAndAssignInt(obj,min,Integer.MAX_VALUE);
|
|
|
+ public static int checkMinAndAssignInt(final Object obj, int min,String...patterns) {
|
|
|
+ return checkRangeAndAssignInt(obj,min,Integer.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -336,13 +378,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最大值,大于最大值的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以int类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return int
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static int checkMaxAndAssignInt(final Object obj, int max) {
|
|
|
- return checkRangeAndAssignInt(obj,Integer.MIN_VALUE,max);
|
|
|
+ public static int checkMaxAndAssignInt(final Object obj, int max,String...patterns) {
|
|
|
+ return checkRangeAndAssignInt(obj,Integer.MIN_VALUE,max,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -351,13 +394,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 这个针对Integer范围内类型数据的校验和赋值
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以int类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return int
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static int checkAndAssignInt(final Object obj) {
|
|
|
- return checkRangeAndAssignInt(obj,Integer.MIN_VALUE,Integer.MAX_VALUE);
|
|
|
+ public static int checkAndAssignInt(final Object obj,String...patterns) {
|
|
|
+ return checkRangeAndAssignInt(obj,Integer.MIN_VALUE,Integer.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -366,12 +410,13 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Double类型的数据,而且String类型还必须是能被解析成double的,否则会抛出异常.
|
|
|
* 而且还要限定数值的范围,超出范围的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以double类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return double
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static double checkRangeAndAssignDouble(final Object obj, double min, double max) {
|
|
|
+ public static double checkRangeAndAssignDouble(final Object obj, double min, double max,String...patterns) {
|
|
|
String number = null;
|
|
|
try {
|
|
|
if (obj == null) {
|
|
@@ -386,6 +431,14 @@ public class ValidationUtil {
|
|
|
if(num > max){
|
|
|
throw new OutOfRangeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(number.matches(pattern)){
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return num;
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
@@ -412,6 +465,14 @@ public class ValidationUtil {
|
|
|
if(num > max){
|
|
|
throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(String.valueOf(obj).matches(pattern)){
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return num;
|
|
|
}
|
|
|
}
|
|
@@ -422,13 +483,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Double类型的数据,而且String类型还必须是能被解析成double的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最大值,超出最大值的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以double类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return double
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static double checkMinAndAssignDouble(final Object obj, double min) {
|
|
|
- return checkRangeAndAssignDouble(obj,min,Double.MAX_VALUE);
|
|
|
+ public static double checkMinAndAssignDouble(final Object obj, double min,String...patterns) {
|
|
|
+ return checkRangeAndAssignDouble(obj,min,Double.MAX_VALUE,patterns);
|
|
|
}
|
|
|
/**
|
|
|
* @Title: checkRangeAndAssignDouble
|
|
@@ -436,13 +498,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Double类型的数据,而且String类型还必须是能被解析成double的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最大值,超出最大值的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以double类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return double
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static double checkMaxAndAssignDouble(final Object obj,double max) {
|
|
|
- return checkRangeAndAssignDouble(obj,Double.MIN_VALUE,max);
|
|
|
+ public static double checkMaxAndAssignDouble(final Object obj,double max,String...patterns) {
|
|
|
+ return checkRangeAndAssignDouble(obj,Double.MIN_VALUE,max,patterns);
|
|
|
}
|
|
|
/**
|
|
|
* @Title: checkRangeAndAssignDouble
|
|
@@ -450,13 +513,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Double类型的数据,而且String类型还必须是能被解析成double的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最大值,超出最大值的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以double类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return double
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static double checkAndAssignDouble(final Object obj) {
|
|
|
- return checkRangeAndAssignDouble(obj,Double.MIN_VALUE,Double.MAX_VALUE);
|
|
|
+ public static double checkAndAssignDouble(final Object obj,String...patterns) {
|
|
|
+ return checkRangeAndAssignDouble(obj,Double.MIN_VALUE,Double.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -465,12 +529,13 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Long类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的范围,超出范围的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以long类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return long
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static long checkRangeAndAssignLong(final Object obj, long min, long max) {
|
|
|
+ public static long checkRangeAndAssignLong(final Object obj, long min, long max,String...patterns) {
|
|
|
String number = null;
|
|
|
try {
|
|
|
if (obj == null) {
|
|
@@ -485,6 +550,14 @@ public class ValidationUtil {
|
|
|
if(num > max){
|
|
|
throw new OutOfRangeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(number.matches(pattern)){
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return num;
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
@@ -511,6 +584,14 @@ public class ValidationUtil {
|
|
|
if(num > max){
|
|
|
throw new RuntimeException("最大值应该小于等于"+max);
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(String.valueOf(obj).matches(pattern)){
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return num;
|
|
|
}
|
|
|
}
|
|
@@ -521,13 +602,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Long类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最小值,小于最小值的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以long类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return long
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static long checkMinAndAssignLong(final Object obj, long min) {
|
|
|
- return checkRangeAndAssignLong(obj,min,Long.MAX_VALUE);
|
|
|
+ public static long checkMinAndAssignLong(final Object obj, long min,String...patterns) {
|
|
|
+ return checkRangeAndAssignLong(obj,min,Long.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -536,13 +618,14 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Long类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 而且还要限定数值的最大值,大于最大值的也会报异常.
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以long类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return long
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static long checkMaxAndAssignLong(final Object obj, long max) {
|
|
|
- return checkRangeAndAssignLong(obj,Long.MIN_VALUE,max);
|
|
|
+ public static long checkMaxAndAssignLong(final Object obj, long max,String...patterns) {
|
|
|
+ return checkRangeAndAssignLong(obj,Long.MIN_VALUE,max,patterns);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -551,29 +634,39 @@ public class ValidationUtil {
|
|
|
* @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Long类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
* 这个针对Long范围内类型数据的校验和赋值
|
|
|
* 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以long类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
* @return long
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static long checkAndAssignLong(final Object obj) {
|
|
|
- return checkRangeAndAssignLong(obj,Long.MIN_VALUE,Long.MAX_VALUE);
|
|
|
+ public static long checkAndAssignLong(final Object obj,String...patterns) {
|
|
|
+ return checkRangeAndAssignLong(obj,Long.MIN_VALUE,Long.MAX_VALUE,patterns);
|
|
|
}
|
|
|
/**
|
|
|
* @Title: range
|
|
|
* @Target: 针对空(null)
|
|
|
* @Description: TODO(针对传入的参数进行非空(null)校验并返回string数据类型--->只针对String类型,校验和赋值同时进行,同时返回真实的String值)
|
|
|
* @Attention: 保证传入的数据是字符串,且不能为空(null),否则报异常,正常就返回一个正常的String类型的参数
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @return String
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static String checkNullAndAssignString(final Object obj){
|
|
|
+ public static String checkNullAndAssignString(final Object obj,String...patterns){
|
|
|
try {
|
|
|
if (obj == null) {
|
|
|
throw new NullPointerException();
|
|
|
}
|
|
|
String str=String.class.cast(obj);
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(str.matches(pattern)){
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return str;
|
|
|
} catch (Exception e) {
|
|
|
if(e instanceof NullPointerException){
|
|
@@ -591,11 +684,12 @@ public class ValidationUtil {
|
|
|
* @Target: 针对空(null)和空字符串("")
|
|
|
* @Description: TODO(针对传入的参数进行非空(null)和空字符串("")校验并返回string数据类型--->只针对String类型,校验和赋值同时进行,同时返回真实的String值)
|
|
|
* @Attention: 保证传入的数据是字符串,且不能为空(null)和空字符串(""),否则报异常,正常就返回一个正常的String类型的参数
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @return String
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static String checkEmptyAndAssignString(final Object obj){
|
|
|
+ public static String checkEmptyAndAssignString(final Object obj,String...patterns){
|
|
|
try {
|
|
|
if (obj == null) {
|
|
|
throw new NullPointerException();
|
|
@@ -604,6 +698,14 @@ public class ValidationUtil {
|
|
|
if("".equals(str)){
|
|
|
throw new RuntimeException("字符串不能为空");
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(str.matches(pattern)){
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return str;
|
|
|
} catch (Exception e) {
|
|
|
if(e instanceof NullPointerException){
|
|
@@ -621,11 +723,12 @@ public class ValidationUtil {
|
|
|
* @target: 针对空(null)和空字符串("")和空内容的字符串(" ")
|
|
|
* @Description: TODO(针对传入的参数进行非空(null)和空字符串("")和空内容的字符串(" ")校验并返回string数据类型--->只针对String类型,校验和赋值同时进行,同时返回真实的String值)
|
|
|
* @Attention: 保证传入的数据是字符串,且不能为空(null)和空字符串("")和空内容的字符串(" "),否则报异常,正常就返回一个正常的String类型的参数
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @return String
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static String checkBlankAndAssignString(final Object obj){
|
|
|
+ public static String checkBlankAndAssignString(final Object obj,String...patterns){
|
|
|
try {
|
|
|
if (obj == null) {
|
|
|
throw new NullPointerException();
|
|
@@ -634,6 +737,14 @@ public class ValidationUtil {
|
|
|
if("".equals(str.trim())){
|
|
|
throw new RuntimeException("字符串不能无任何内容");
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(str.matches(pattern)){
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return str;
|
|
|
} catch (Exception e) {
|
|
|
if(e instanceof NullPointerException){
|
|
@@ -651,11 +762,12 @@ public class ValidationUtil {
|
|
|
* @Title: checkBlankStringAndAssignNullIfIsBlank
|
|
|
* @Description: TODO(针对传入的Object类型参数进行非空(null)和空字符串("")和空内容的字符串(" ")校验并返回string数据类型,只要是null,""或" "全部返回null,不会抛出异常,除非无法强转为String,其它情况正常转String)
|
|
|
* @Attention: 默认返回null
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @return String
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static String checkBlankStringAndAssignNullIfIsBlank(final Object obj){
|
|
|
+ public static String checkBlankStringAndAssignNullIfIsBlank(final Object obj,String...patterns){
|
|
|
try {
|
|
|
if (obj == null) {
|
|
|
return null;
|
|
@@ -664,6 +776,14 @@ public class ValidationUtil {
|
|
|
if("".equals(str.trim())){
|
|
|
return null;
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(str.matches(pattern)){
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return str;
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException("String类型转换异常");
|
|
@@ -674,11 +794,12 @@ public class ValidationUtil {
|
|
|
* @Title: checkBlankStringAndAssignEmptyIfIsBlank
|
|
|
* @Description: TODO(针对传入的参数进行非空(null)和空字符串("")和空内容的字符串(" ")校验并返回string数据类型,只要是null,""或" "全部返回""空串,不会抛出异常,除非无法强转为String,其它情况正常转String)
|
|
|
* @Attention: 默认返回""
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
* @return String
|
|
|
* @author ylx
|
|
|
* @date 2017年12月26日 下午3:48:40
|
|
|
*/
|
|
|
- public static String checkBlankStringAndAssignEmptyIfIsBlank(final Object obj){
|
|
|
+ public static String checkBlankStringAndAssignEmptyIfIsBlank(final Object obj,String...patterns){
|
|
|
try {
|
|
|
if (obj == null) {
|
|
|
return "";
|
|
@@ -687,6 +808,14 @@ public class ValidationUtil {
|
|
|
if("".equals(str.trim())){
|
|
|
return "";
|
|
|
}
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(str.matches(pattern)){
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
return str;
|
|
|
} catch (Exception e) {
|
|
|
throw new RuntimeException("String类型转换异常");
|