/src/main/java/cn/eyeblue/blog/util/ValidationUtil.java

https://github.com/eyebluecn/blog · Java · 89 lines · 55 code · 18 blank · 16 comment · 17 complexity · 335bfb61b629a47c0022e82d8af6f1f4 MD5 · raw file

  1. package cn.eyeblue.blog.util;
  2. import java.util.Collection;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5. /**
  6. * 和参数处理有关的通用方法
  7. */
  8. public class ValidationUtil {
  9. /**
  10. * 检查参数是否为空
  11. *
  12. * @param list the list
  13. * @return true, if successful
  14. */
  15. public static boolean checkParam(Object... list) {
  16. boolean res = true;
  17. for (Object obj : list) {
  18. //普通对象我们看看是不是为空,或者空字符串
  19. if (obj == null || "".equals(obj)) {
  20. res = false;
  21. break;
  22. }
  23. //如果是容器类的话,那么我们看看是否大小为0
  24. //String.class.isAssignableFrom(Object.class); false
  25. //Object.class.isAssignableFrom(String.class); true
  26. //Class1.isAssignableFrom(Class2);表示判断Class1是否是Class2的父类。
  27. else if (Collection.class.isAssignableFrom(obj.getClass())) {
  28. Collection c = (Collection) obj;
  29. if (c.size() == 0) {
  30. res = false;
  31. }
  32. break;
  33. }
  34. }
  35. return res;
  36. }
  37. public static boolean isPhone(String str) {
  38. return str != null && str.matches("\\d{11}");
  39. }
  40. public static boolean isLocationWord(String str) {
  41. return str != null && str.matches("^[A-Za-z0-9_-]+$");
  42. }
  43. public static final Pattern VALID_EMAIL_ADDRESS_REGEX =
  44. Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
  45. public static boolean isEmail(String emailStr) {
  46. Matcher matcher = VALID_EMAIL_ADDRESS_REGEX.matcher(emailStr);
  47. return matcher.find();
  48. }
  49. //判断是不是整形数组构成的。[from,to] 空字符串返回正确
  50. public static boolean isIntArrayString(String str, int from, int to) {
  51. if (str.trim().equals("")) {
  52. return true;
  53. }
  54. String[] strArr = str.split(" ");
  55. for (String s : strArr) {
  56. try {
  57. Integer i = Integer.parseInt(s);
  58. if (i < from || i > to) {
  59. return false;
  60. }
  61. } catch (Exception e) {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. //判断是否在某个范围内.[from,to]
  68. public static boolean inRange(int target, int from, int to) {
  69. return (target >= from && target <= to);
  70. }
  71. }