Java Object Compare areEqual(Object aThis, Object aThat)

Here you can find the source of areEqual(Object aThis, Object aThat)

Description

Equals for possibly-null object field.

License

Open Source License

Declaration

static boolean areEqual(Object aThis, Object aThat) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w ww .  j  a va2 s .  c om
     * Equals for possibly-<tt>null</tt> object field.
     * <p/>
     * <P><em>Does not include arrays</em>. (This restriction will likely be removed in a future version.)
     */
    static boolean areEqual(Object aThis, Object aThat) {
        if (isArray(aThis) || isArray(aThat)) {
            throw new IllegalArgumentException("This method does not currently support arrays.");
        }
        return aThis == null ? aThat == null : aThis.equals(aThat);
    }

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

    /**
     * Quick checks for <em>possibly</em> determining equality of two objects.
     * <p/>
     * <P>This method exists to make <tt>equals</tt> implementations readIndexes more legibly, and to avoid multiple
     * <tt>return</tt> statements.
     * <p/>
     * <P><em>It cannot be used by itself to fully implement <tt>equals</tt>. </em> It uses <tt>==</tt> and
     * <tt>instanceof</tt> to determine if equality can be found cheaply, without the need to examine field values in
     * detail. It is <em>always</em> paired with some other method (usually {@link #equalsFields(Object[], Object[])}),
     * as in the following example : <PRE> public boolean equals(Object aThat){ Boolean result = Utils.quickEquals(this,
     * aThat); <b>if ( result == null ){</b> //quick checks not sufficient to determine equality, //so a full
     * field-by-field check is needed : This this = (This) aThat; //will not fail result =
     * Utils.equalsFor(this.getSignificantFields(), that.getSignificantFields()); } return result; } </PRE>
     * <p/>
     * <P>This method is unusual since it returns a <tt>Boolean</tt> that takes <em>3</em> values : <tt>true</tt>,
     * <tt>false</tt>, and <tt>null</tt>. Here, <tt>true</tt> and <tt>false</tt> mean that a simple quick check was able
     * to determine equality. <span class='highlight'>The <tt>null</tt> case means that the quick checks were not able
     * to determine if the objects are equal or not, and that further field-by-field examination is necessary. The
     * caller must always perform a check-for-null on the return value.</span>
     */
    static Boolean equals(Object aThis, Object aThat) {
        Boolean result = null;
        if (aThis == aThat) {
            result = Boolean.TRUE;
        } else {
            Class<?> thisClass = aThis.getClass();
            if (!thisClass.isInstance(aThat)) {
                result = Boolean.FALSE;
            }
        }
        return result;
    }
}

Related

  1. areEqual(final Object first, final Object second)
  2. areEqual(final Object o1, final Object o2)
  3. areEqual(final Object object0, final Object object1)
  4. areEqual(final Object x, final Object y)
  5. areEqual(final T object1, final T object2)
  6. areEqual(Object first, Object second)
  7. areEqual(Object first, Object second, boolean equalEvenIfBothNull)
  8. areEqual(Object o1, Object o2)
  9. areEqual(Object o1, Object o2)