Java Object Compare areEqual(Object value, Object otherValue)

Here you can find the source of areEqual(Object value, Object otherValue)

Description

A comprehensive isEqual method that handles nulls and arrays safely.

License

BSD License

Parameter

Parameter Description
value Object
otherValue Object

Return

boolean

Declaration

@SuppressWarnings("PMD.CompareObjectsWithEquals")
public static boolean areEqual(Object value, Object otherValue) 

Method Source Code

//package com.java2s;
/**/* w  ww  .j  a  va 2  s  . c  o  m*/
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

public class Main {
    /**
     * A comprehensive isEqual method that handles nulls and arrays safely.
     *
     * @param value
     *            Object
     * @param otherValue
     *            Object
     * @return boolean
     */
    @SuppressWarnings("PMD.CompareObjectsWithEquals")
    public static boolean areEqual(Object value, Object otherValue) {
        if (value == otherValue) {
            return true;
        }
        if (value == null) {
            return false;
        }
        if (otherValue == null) {
            return false;
        }

        if (value.getClass().getComponentType() != null) {
            return arraysAreEqual(value, otherValue);
        }
        return value.equals(otherValue);
    }

    /**
     * Returns true if the objects are array instances and each of their
     * elements compares via equals as well.
     *
     * @param value
     *            Object
     * @param otherValue
     *            Object
     * @return boolean
     */
    public static boolean arraysAreEqual(Object value, Object otherValue) {
        if (value instanceof Object[]) {
            if (otherValue instanceof Object[]) {
                return valuesAreTransitivelyEqual((Object[]) value, (Object[]) otherValue);
            }
            return false;
        }
        return false;
    }

    /**
     * Returns whether the arrays are equal by examining each of their elements,
     * even if they are arrays themselves.
     *
     * @param thisArray
     *            Object[]
     * @param thatArray
     *            Object[]
     * @return boolean
     */
    public static boolean valuesAreTransitivelyEqual(Object[] thisArray, Object[] thatArray) {
        if (thisArray == thatArray) {
            return true;
        }
        if (thisArray == null || thatArray == null) {
            return false;
        }
        if (thisArray.length != thatArray.length) {
            return false;
        }
        for (int i = 0; i < thisArray.length; i++) {
            if (!areEqual(thisArray[i], thatArray[i])) {
                return false; // recurse if req'd
            }
        }
        return true;
    }
}

Related

  1. areEqual(Object obj1, Object obj2)
  2. areEqual(Object obj1, Object obj2)
  3. areEqual(Object object1, Object object2)
  4. areEqual(Object one, Object another)
  5. areEqual(Object parameter, Object otherParameter)
  6. areEqual(Object value1, Object value2)
  7. areEqual(T obj1, T obj2)
  8. areEqual(T obj1, V obj2)
  9. areEqualComparables(T object1, T object2)