CheckUtils.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package com.caimei.utils;
  2. import java.lang.reflect.Array;
  3. import java.util.Collection;
  4. import java.util.Map;
  5. public class CheckUtils {
  6. public static final String COMMON_FIELD = "flowID,initiator,";
  7. /**
  8. * 验证对象是否为NULL,空字符串,空数组,空的Collection或Map(只有空格的字符串也认为是空串)
  9. * @param obj 被验证的对象
  10. * @param message 异常信息
  11. */
  12. @SuppressWarnings("rawtypes")
  13. public static void notEmpty(Object obj, String message) {
  14. if (obj == null){
  15. throw new IllegalArgumentException(message + " must be specified");
  16. }
  17. if (obj instanceof String && obj.toString().trim().length()==0){
  18. throw new IllegalArgumentException(message + " must be specified");
  19. }
  20. if (obj.getClass().isArray() && Array.getLength(obj)==0){
  21. throw new IllegalArgumentException(message + " must be specified");
  22. }
  23. if (obj instanceof Collection && ((Collection)obj).isEmpty()){
  24. throw new IllegalArgumentException(message + " must be specified");
  25. }
  26. if (obj instanceof Map && ((Map)obj).isEmpty()){
  27. throw new IllegalArgumentException(message + " must be specified");
  28. }
  29. }
  30. }