|
@@ -1,6 +1,7 @@
|
|
|
package com.fuzamei.utils;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
@@ -76,7 +77,13 @@ public class ValidationUtil {
|
|
|
}
|
|
|
number = String.class.cast(obj);
|
|
|
try {
|
|
|
- Integer.parseInt(number);
|
|
|
+ Integer num = Integer.parseInt(number);
|
|
|
+ if (num < min) {
|
|
|
+ throw new OutOfRangeException("最小值应该大于等于"+min);
|
|
|
+ }
|
|
|
+ if(num > max){
|
|
|
+ throw new OutOfRangeException("最大值应该小于等于"+max);
|
|
|
+ }
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
|
}
|
|
@@ -87,6 +94,9 @@ public class ValidationUtil {
|
|
|
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);
|
|
@@ -168,7 +178,13 @@ public class ValidationUtil {
|
|
|
}
|
|
|
number = String.class.cast(obj);
|
|
|
try {
|
|
|
- Long.parseLong(number);
|
|
|
+ Long num = Long.parseLong(number);
|
|
|
+ if (num < min) {
|
|
|
+ throw new OutOfRangeException("最小值应该大于等于"+min);
|
|
|
+ }
|
|
|
+ if(num > max){
|
|
|
+ throw new OutOfRangeException("最大值应该小于等于"+max);
|
|
|
+ }
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
|
}
|
|
@@ -179,9 +195,12 @@ public class ValidationUtil {
|
|
|
if (e instanceof NumberFormatException) {
|
|
|
throw new RuntimeException("数据类型解析异常");
|
|
|
}
|
|
|
+ if (e instanceof OutOfRangeException) {
|
|
|
+ throw new OutOfRangeException(e.getMessage());
|
|
|
+ }
|
|
|
Long num = null;
|
|
|
try {
|
|
|
- num=Long.parseLong(""+obj);
|
|
|
+ num=Long.parseLong(String.valueOf(obj));
|
|
|
} catch (Exception e1) {
|
|
|
throw new RuntimeException("数字解析异常");
|
|
|
}
|
|
@@ -259,7 +278,14 @@ public class ValidationUtil {
|
|
|
}
|
|
|
number = String.class.cast(obj);
|
|
|
try {
|
|
|
- return Integer.parseInt(number);
|
|
|
+ Integer num = Integer.parseInt(number);
|
|
|
+ if (num < min) {
|
|
|
+ throw new OutOfRangeException("最小值应该大于等于"+min);
|
|
|
+ }
|
|
|
+ if(num > max){
|
|
|
+ throw new OutOfRangeException("最大值应该小于等于"+max);
|
|
|
+ }
|
|
|
+ return num;
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
|
}
|
|
@@ -270,6 +296,9 @@ public class ValidationUtil {
|
|
|
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);
|
|
@@ -300,7 +329,10 @@ public class ValidationUtil {
|
|
|
public static int checkMinAndAssignInt(final Object obj, int min) {
|
|
|
return checkRangeAndAssignInt(obj,min,Integer.MAX_VALUE);
|
|
|
}
|
|
|
-
|
|
|
+ public static void main(String[] args) {
|
|
|
+ int checkMinAndAssignInt = checkMinAndAssignInt(-1,1);
|
|
|
+ System.out.println(checkMinAndAssignInt);
|
|
|
+ }
|
|
|
|
|
|
* @Title: checkMaxAndAssignInt
|
|
|
* @Description: 针对传入的参数进行校验--->Integer类型
|
|
@@ -350,7 +382,14 @@ public class ValidationUtil {
|
|
|
}
|
|
|
number = String.class.cast(obj);
|
|
|
try {
|
|
|
- return Long.parseLong(number);
|
|
|
+ Long num = Long.parseLong(number);
|
|
|
+ if (num < min) {
|
|
|
+ throw new OutOfRangeException("最小值应该大于等于"+min);
|
|
|
+ }
|
|
|
+ if(num > max){
|
|
|
+ throw new OutOfRangeException("最大值应该小于等于"+max);
|
|
|
+ }
|
|
|
+ return num;
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
|
}
|
|
@@ -361,9 +400,12 @@ public class ValidationUtil {
|
|
|
if (e instanceof NumberFormatException) {
|
|
|
throw new RuntimeException("数据类型解析异常");
|
|
|
}
|
|
|
+ if (e instanceof OutOfRangeException) {
|
|
|
+ throw new OutOfRangeException(e.getMessage());
|
|
|
+ }
|
|
|
Long num = null;
|
|
|
try {
|
|
|
- num=Long.parseLong(""+obj);
|
|
|
+ num=Long.parseLong(String.valueOf(obj));
|
|
|
} catch (Exception e1) {
|
|
|
throw new RuntimeException("数字解析异常");
|
|
|
}
|
|
@@ -575,7 +617,7 @@ public class ValidationUtil {
|
|
|
return defaultInt;
|
|
|
}
|
|
|
try {
|
|
|
- return Integer.parseInt(number);
|
|
|
+ return Integer.parseInt(number.trim());
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
|
}
|
|
@@ -615,7 +657,7 @@ public class ValidationUtil {
|
|
|
return defaultLong;
|
|
|
}
|
|
|
try {
|
|
|
- return Long.parseLong(number);
|
|
|
+ return Long.parseLong(number.trim());
|
|
|
} catch (NumberFormatException e) {
|
|
|
throw new NumberFormatException();
|
|
|
}
|
|
@@ -625,15 +667,13 @@ public class ValidationUtil {
|
|
|
}
|
|
|
Long num = null;
|
|
|
try {
|
|
|
- num=Long.parseLong(""+obj);
|
|
|
+ num=Long.parseLong(String.valueOf(obj));
|
|
|
} catch (Exception e1) {
|
|
|
throw new RuntimeException("Long解析异常");
|
|
|
}
|
|
|
return num;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
|
|
|
String json="{'KEY1':'12','KEY2':23,'KEY3':null}";
|
|
|
Map parseObject = JSON.parseObject(json, Map.class);
|
|
@@ -644,5 +684,15 @@ public class ValidationUtil {
|
|
|
|
|
|
|
|
|
}*/
|
|
|
+}
|
|
|
|
|
|
+class OutOfRangeException extends RuntimeException{
|
|
|
+ public OutOfRangeException() {}
|
|
|
+ public OutOfRangeException(String message) {
|
|
|
+ super(message);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public String getMessage() {
|
|
|
+ return super.getMessage();
|
|
|
+ }
|
|
|
}
|