常规校验工具类

支持Object、List、Map、String判空;手机号、邮箱校验

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 校验类
 *
 * @author hanmw
 */
public class Validater
{
   private static Logger logger = LogManager.getLogger(Validater.class);
   

   /**
    * @param object
    * @return
    * @Description 对象是否为null, 为null true
    */
   public static boolean objectIsNull(Object object)
   {
      boolean flag = false;
      if (null == object)
      {
         flag = true;
      }
      else
      {
         flag = StringUtils.isBlank(String.valueOf(object));
      }
      return flag;
   }

   /**
    * @param collection
    * @return 为空返回true
    * @Description 集合判空
    */
   public static boolean isEmptyList(Collection<?> collection)
   {
      return null == collection || collection.isEmpty();
   }

   /**
    * @param map
    * @return 为空返回true
    * @Description Map判空
    */
   public static boolean isEmptyMap(Map<?, ?> map)
   {
      return null == map || isEmptyList(map.keySet());
   }

   /**
    * @param str
    * @return 为空返回true
    * @Description String判空
    */
   public static boolean isEmptyString(String str)
   {

      return null == str || ("").equals(str);
   }
   
   public static boolean isPhone(String phone)
   {
      String regex =
         "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,2,3,5,6,7,8])|(18[0-9])|(19[1|5|8|9]))\\d{8}$";
      if (phone.length() != 11)
      {
         return false;
      }
      else
      {
         Pattern p = Pattern.compile(regex);
         Matcher m = p.matcher(phone);
         boolean isMatch = m.matches();
         if (!isMatch)
         {
            return false;
         }
         return isMatch;
      }
   }

   public static boolean isEmail(String email)
   {
      if (null == email || "".equals(email))
      {
         return false;
      }
      String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
      Pattern p = Pattern.compile(regEx1);
      Matcher m = p.matcher(email);
      if (m.matches())
      {
         return true;
      }
      else
      {
         return false;
      }
   }

   public static boolean checkAllFieldsIsNull(Object object)
   {
      try
      {
         for (Field f : object.getClass().getDeclaredFields())
         {
            f.setAccessible(true);
            if (!Modifier.isStatic(f.getModifiers()))
            {
               if (f.get(object) != null && !"".equals(f.get(object).toString()))
               {
                  return false;
               }
            }
         }
      }
      catch (IllegalAccessException e)
      {
         e.printStackTrace();
      }
      return true;
   }

   /**
    * 判断两个日期是否相等
    *
    * @param d1
    * @param d2
    * @return
    */
   public static boolean checkDate(Date d1, Date d2)
   {
      if (d1 != null && d2 != null)
      {
         if (d1.getTime() != d2.getTime())
         {
            return false;
         }
      }
      else if (d1 == null && d2 == null)
      {
         return true;
      }
      else
      {
         return false;
      }
      return true;
   }
   
}
posted @ 2022-11-03 10:39  云村的王子  阅读(22)  评论(0编辑  收藏  举报  来源