Java Object Equal nullSafeEquals(Object o1, Object o2)

Here you can find the source of nullSafeEquals(Object o1, Object o2)

Description

null Safe Equals

License

Apache License

Declaration

public static boolean nullSafeEquals(Object o1, Object o2) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public static boolean nullSafeEquals(Object o1, Object o2) {
        if (o1 == o2) {
            return true;
        }/*from   ww  w.j  av a  2  s.  c  o  m*/
        if (o1 == null || o2 == null) {
            return false;
        }
        if (o1.equals(o2)) {
            return true;
        }
        if (o1.getClass().isArray() && o2.getClass().isArray()) {
            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;
    }

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

Related

  1. equals(Object... objects)
  2. internalEquals(Object[] o1, Object[] o2)
  3. isEquals(final Object lhs, final Object rhs)
  4. isEquals(Object object1, Object object2)
  5. nullSafeEquals(Object o1, Object o2)
  6. objectsEqual(final Object objectA, final Object objectB)
  7. objectsEqual(Object a, Object b)
  8. objectsEqual(Object lhs, Object rhs)
  9. objectsEqual(Object o1, Object o2)