|
@@ -404,6 +404,84 @@ public class ValidationUtil {
|
|
|
return checkRangeAndAssignInt(obj,Integer.MIN_VALUE,Integer.MAX_VALUE,patterns);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @Title: checkBlankIntegerAndAssignNullIfIsBlank
|
|
|
+ * @Description: 针对传入的参数进行校验--->Integer类型
|
|
|
+ * @Detail: 参数类型虽然是Object,但是要求数据类型是String或者是Integer类型的数据,而且String类型还必须是能被解析成整数的,否则会抛出异常.
|
|
|
+ * 这个针对Integer范围内类型数据的校验和赋值
|
|
|
+ * 如果传入的参数为null,空串""或空长串" "均返回null
|
|
|
+ * 如果一切都解析正常,说明解析没有问题,结果将解析后的数值以Integer类型返回
|
|
|
+ * @AddFuntion:增加多格式校验功能[2018/1/27 10:48AM]
|
|
|
+ * @Usage: 适用于数据判断的校验和赋值同时进行
|
|
|
+ * @return int
|
|
|
+ * @author ylx
|
|
|
+ * @date 2017年12月26日 下午3:48:40
|
|
|
+ */
|
|
|
+ public static Integer checkBlankIntegerAndAssignNullIfIsBlank(final Object obj,String...patterns) {
|
|
|
+ String number = null;
|
|
|
+ try {
|
|
|
+ if (obj == null) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ number = String.class.cast(obj);
|
|
|
+ if("".equals(number.trim())) {
|
|
|
+ throw new NullPointerException();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ Integer num = Integer.parseInt(number);
|
|
|
+ if (num < Integer.MIN_VALUE) {
|
|
|
+ throw new OutOfRangeException("最小值应该大于等于"+Integer.MIN_VALUE);
|
|
|
+ }
|
|
|
+ if(num > Integer.MAX_VALUE){
|
|
|
+ throw new OutOfRangeException("最大值应该小于等于"+Integer.MAX_VALUE);
|
|
|
+ }
|
|
|
+ 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();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof NullPointerException) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (e instanceof NumberFormatException) {
|
|
|
+ throw new RuntimeException("数据类型解析异常");
|
|
|
+ }
|
|
|
+ if (e instanceof OutOfRangeException) {
|
|
|
+ throw new OutOfRangeException(e.getMessage());
|
|
|
+ }
|
|
|
+ Integer num = null;
|
|
|
+ try {
|
|
|
+ num = Integer.class.cast(obj);
|
|
|
+ } catch (Exception e1) {
|
|
|
+ throw new RuntimeException("数字解析异常");
|
|
|
+ }
|
|
|
+ if (num < Integer.MIN_VALUE) {
|
|
|
+ throw new OutOfRangeException("最小值应该大于等于"+Integer.MIN_VALUE);
|
|
|
+ }
|
|
|
+ if(num > Integer.MAX_VALUE){
|
|
|
+ throw new OutOfRangeException("最大值应该小于等于"+Integer.MAX_VALUE);
|
|
|
+ }
|
|
|
+ if(patterns.length!=0){//格式校验
|
|
|
+ for (String pattern : patterns) {
|
|
|
+ if(String.valueOf(obj).matches(pattern)){
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ throw new RuntimeException("与指定格式不符");
|
|
|
+ }
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* @Title: checkRangeAndAssignDouble
|
|
|
* @Description: 针对传入的参数进行校验--->Double类型
|