比较java中的两个可能为null的值

时间:2015-04-23 13:26:07

标签: java null

我有一个类似这样的java类:

class MyClass {

    MyClass(Object obj) {

        m_Obj = obj
    }

    private Object m_Obj;
}

因为m_Obj可以为null,所以我的equals方法必须如下所示:

@Override
public boolean equals(
        Object obj) {

    if (!(obj instanceof MyObject))
        return false;

    MyObject other = (MyObject)obj;

    if ((m_Obj == null) &&
        (other.m_Obj == null)
        return true;

    if ((m_Obj == null) &&
        (other.m_Obj != null)
        return false;

    if ((m_Obj != null) &&
        (other.m_Obj == null)
        return false;


    return m_Obj.equals(other.m_Obj);
}

当我有多个类的成员可能是null或者可能不是null时,equals函数就变成了一个很长的障碍。我可以编写自己的函数:

public boolean equals(
        Object a,
        Object b) {

    if ((a == null) &&
        (b == null)
        return true;

    if ((a == null) &&
        (b != null)
        return false;

    if ((a != null) &&
        (b == null)
        return false;


    return a.equals(b);
}

我想知道某人是否已在其中一个java / apache库中完成此操作?

2 个答案:

答案 0 :(得分:4)

是的,因为Java SE 7有一个标准的API:Objects.equals

@Override
public boolean equals(Object obj) {

    if (obj == this){
       return true;
    }

    if (!(obj instanceof MyObject))
        return false;

    MyObject other = (MyObject)obj;

    return Objects.equals(m_ObjA, other.m_ObjA)
           && Objects.equals(m_ObjB, other.m_ObjB)
           && (myPrimitive == other.myPrimitive);
}

答案 1 :(得分:0)

以下是我对此的回答:(http://sourceforge.net/p/tus/code/HEAD/tree/tjacobs/util/PrimaryKey.java

package tjacobs.util;

/**
 * The reason d'etre for PrimaryKey is to make overriding equals, hashCode, and compareTo simpler and easier to
 * implement so that they are consistent with one another in data object classes.
 * @author tom_jacobs
 *
 * @param <T>
 */
public interface PrimaryKey<T> {
    public Object[] getPrimaryKey();
    public boolean equals(T obj);
    public int hashCode();

    public interface ComparablePrimaryKey<T> extends Comparable<T>, PrimaryKey<T> {
        @SuppressWarnings("unchecked")
        public Comparable[] getPrimaryKey();

    }

    public static class Impl {
        @SuppressWarnings("unchecked")
        public static final boolean equalsImpl(PrimaryKey key1, PrimaryKey key2) {
            Object[] data1 = key1.getPrimaryKey();
            Object[] data2 = key2.getPrimaryKey();
            //don't check length equal - let that get thrown as an exception
            try {
                for (int i = 0; i < data1.length; i++) {
                    if (data1[i] == null && data2[i] == null) continue;
                    if (data1[i] == null || data2[i] == null) return false;
                    if (!data1[i].equals(data2[i])) return false;
                }
            }
            catch (ArrayIndexOutOfBoundsException ex) {
                //throw new Error("Class: " + key1.getClass() + " has invalid implementation of getPrimaryKey");
                ex.printStackTrace();
                return false;
            }
            return true;
        }

        @SuppressWarnings("unchecked")
        public static final int compareTo(ComparablePrimaryKey key1, ComparablePrimaryKey key2) {
            Comparable[] data1 = key1.getPrimaryKey();
            Comparable[] data2 = key2.getPrimaryKey();
            for (int i = 0; i < data1.length; i++) {
                System.out.println("comparing: " + data1[i] + " to " + data2[i]);
                int compare = data1[i].compareTo(data2[i]);
                if (compare != 0) {
                    System.out.println("returning: " + compare);
                    return compare;
                }
            }
            return 0;
        }

        @SuppressWarnings("unchecked")
        public static final int hashCode(PrimaryKey key) {
            Object[] data1 = key.getPrimaryKey();
            long hashCode = 0;
            for (int i= 0; i < data1.length; i++) {
                hashCode += data1[i].hashCode();
            }
            return (int) hashCode;
        }

        /*
         * model equals, hashCode, compareTo methods

        public boolean equals(Object o) {
            if (o.getClass() != getClass()) {
                return false;
            }
            return PrimaryKey.Impl.equalsImpl( this, o);
        }

        public int hashCode() {
            return PrimaryKey.Impl.hashCode(this);
        }

         public int compareTo(PrimaryKey obj) {
            return PrimaryKey.Impl.compareTo(this, obj);
         }

        */

    }


}
相关问题