java Object工具类 ObjectUtil

package com.xj.hhjk.common.util;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;


public abstract class ObjectUtil {
     public static final String EMPTY_STRING = "";
     public static final String NULL_STRING  = "null";

  
     public static boolean isArray(Object obj) {
          return ((obj != null) && obj.getClass().isArray());
     }

     /**
      * 对象是否为 空,Null
      * 
      * @Author JiangHan
      * @param obj
      * @return
      */
     @SuppressWarnings("rawtypes")
     public static boolean isNullOrEmpty(Object obj) {
          if (obj == null)
               return true;
          if (obj instanceof String) {
               return ((String) obj).isEmpty();
          } else if (obj instanceof Collection) {
               return ((Collection) obj).isEmpty();
          } else if (obj instanceof Array) {
               return Array.getLength((Array) obj) < 1;
          } else if (obj instanceof Map) {
               return ((Map) obj).isEmpty();
          }
          return false;
     }

     public static boolean isNotEmpty(Object obj) {
          return !isNullOrEmpty(obj);
     }


     public static Boolean isNullOrEmpty(String target) {
          return target == null || target.trim().length() < 1;
     }

     public static Boolean notEmpty(String target) {
          return !isNullOrEmpty(target);
     }


     public static boolean isNullOrEmpty(Object[] array) {
          return ((array == null) || (array.length == 0));
     }

     public static boolean isNotEmpty(Object[] array) {
          return !isNullOrEmpty(array);
     }

    
     @SuppressWarnings("rawtypes")
     public static boolean isNullOrEmpty(Collection collection) {
          return ((collection == null) || collection.isEmpty());
     }

     @SuppressWarnings("rawtypes")
     public static boolean isNotEmpty(Collection collection) {
          return !isNullOrEmpty(collection);
     }

     
     @SuppressWarnings("rawtypes")
     public static boolean isNullOrEmpty(Map map) {
          return ((map == null) || map.isEmpty());
     }

     @SuppressWarnings("rawtypes")
     public static boolean isNotEmpty(Map map) {
          return !isNullOrEmpty(map);
     }

     /*
      * public static boolean equals(Object x, Object y) { return (x == y) || ((x != null) && (y != null) && x.equals(y)); }
      */
     /**
      * Determine if the given objects are equal, returning <code>true</code> if both are <code>null</code> or <code>false</code> if only one is <code>null</code>.
      * <p>
      * Compares arrays with <code>Arrays.equals</code>, performing an equality check based on the array elements rather than the array reference.
      * 
      * @param o1
      *             first Object to compare
      * @param o2
      *             second Object to compare
      * @return whether the given objects are equal
      * @see java.util.Arrays#equals
      */
     public static boolean equals(Object o1, Object o2) {
          if (o1 == o2) {
               return true;
          }
          if ((o1 == null) || (o2 == null)) {
               return false;
          }
          if (o1.equals(o2)) {
               return true;
          }
          if (isArray(o1) && isArray(o2)) {
               if ((o1 instanceof Object[]) && (o2 instanceof Object[])) {
                    return Arrays.equals((Object[]) o1, (Object[]) o2);
               }
               if ((o1 instanceof boolean[]) && (o2 instanceof boolean[])) {
                    return Arrays.equals((boolean[]) o1, (boolean[]) o2);
               }
               if ((o1 instanceof byte[]) && (o2 instanceof byte[])) {
                    return Arrays.equals((byte[]) o1, (byte[]) o2);
               }
               if ((o1 instanceof char[]) && (o2 instanceof char[])) {
                    return Arrays.equals((char[]) o1, (char[]) o2);
               }
               if ((o1 instanceof double[]) && (o2 instanceof double[])) {
                    return Arrays.equals((double[]) o1, (double[]) o2);
               }
               if ((o1 instanceof float[]) && (o2 instanceof float[])) {
                    return Arrays.equals((float[]) o1, (float[]) o2);
               }
               if ((o1 instanceof int[]) && (o2 instanceof int[])) {
                    return Arrays.equals((int[]) o1, (int[]) o2);
               }
               if ((o1 instanceof long[]) && (o2 instanceof long[])) {
                    return Arrays.equals((long[]) o1, (long[]) o2);
               }
               if ((o1 instanceof short[]) && (o2 instanceof short[])) {
                    return Arrays.equals((short[]) o1, (short[]) o2);
               }
          }
          return false;
     }

     /**
      * Determine the class name for the given object.
      * <p>
      * Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
      * 
      * @param obj
      *             the object to introspect (may be <code>null</code>)
      * @return the corresponding class name
      */
     public static String getClassName(Object obj) {
          return (obj != null ? obj.getClass().getName() : NULL_STRING);
     }

     /**
      * Return a String representation of an object's overall identity.
      * 
      * @param obj
      *             the object (may be <code>null</code>)
      * @return the object's identity as String representation, or an empty String if the object was <code>null</code>
      */
     public static String getIdentity(Object obj) {
          return obj == null ? EMPTY_STRING : obj.getClass().getName() + "@" + getHexIdentity(obj);
     }

     /**
      * Return a hex String form of an object's identity hash code.
      * 
      * @param obj
      *             the object
      * @return the object's identity code in hex notation
      */
     public static String getHexIdentity(Object obj) {
          return Integer.toHexString(System.identityHashCode(obj));
     }

     /**
      * Return the same value as <code>{@link Boolean#hashCode()}</code>.
      * 
      * @see Boolean#hashCode()
      */
     public static int hashCode(boolean bool) {
          return bool ? 1231 : 1237;
     }

     /**
      * Return the same value as <code>{@link Long#hashCode()}</code>.
      * 
      * @see Byte#hashCode()
      */
     public static int hashCode(byte value) {
          return (int) value;
     }

     /**
      * Return the same value as <code>{@link Long#hashCode()}</code>.
      * 
      * @see Character#hashCode()
      */
     public static int hashCode(char value) {
          return (int) value;
     }

     // ---------------------------------------------------------------------
     // Convenience methods for content-based equality/hash-code handling
     // ---------------------------------------------------------------------
     /**
      * Return the same value as <code>{@link Double#hashCode()}</code>.
      * 
      * @see Double#hashCode()
      */
     public static int hashCode(double dbl) {
          long bits = Double.doubleToLongBits(dbl);
          return hashCode(bits);
     }

     /**
      * Return the same value as <code>{@link Float#hashCode()}</code>.
      * 
      * @see Float#hashCode()
      */
     public static int hashCode(float flt) {
          return Float.floatToIntBits(flt);
     }

     /**
      * Return the same value as <code>{@link Long#hashCode()}</code>.
      * 
      * @see Long#hashCode()
      */
     public static int hashCode(long lng) {
          return (int) (lng ^ (lng >>> 32));
     }

     /**
      * Return the same value as <code>{@link Long#hashCode()}</code>.
      * 
      * @see Short#hashCode()
      */
     public static int hashCode(short value) {
          return (int) value;
     }

     /**
      * Return the same value as <code>{@link Long#hashCode()}</code>.
      * 
      * @see Integer#hashCode()
      */
     public static int hashCode(int value) {
          return value;
     }

     /**
      * Return the same value as <code>{@link Long#hashCode()}</code>.
      * 
      * @see Integer#hashCode()
      */
     public static Object ifNullValue(Object value, Object convertValue) {
          if (value == null) {
               return convertValue;
          }
          return value;
     }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长青风

赏一块,发大财!赏两块,惹人爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值