Java Object Equal equalObjects(Object object1, Object object2)

Here you can find the source of equalObjects(Object object1, Object object2)

Description

Indicates whether the given objects are "equal to" each other.

License

Open Source License

Parameter

Parameter Description
object1 the first object, can be <code>null</code>
object2 the second object, can also be <code>null</code>

Return

true if this first object equals the second; false otherwise.

Declaration

public static boolean equalObjects(Object object1, Object object2) 

Method Source Code


//package com.java2s;
/*//w w w .j a v a2 s.c o  m
 * Copyright (C) 2010 Brockmann Consult GmbH (info@brockmann-consult.de)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, see http://www.gnu.org/licenses/
 */

import java.util.Arrays;

public class Main {
    /**
     * Indicates whether the given objects are "equal to" each other.
     * <p> This method should be used in place of the <code>Object.equals</code> if one ore both arguments can be
     * <code>null</code>.
     * <p> If both objects are arrays of the same primitive types the comparision is delegated to the corresponding
     * <code>java.util.Arrays.equals</code> method.
     * <p> If both objects are object arrays with equal lengths, the method is recursively called for each array element
     * pair. If the first pair is not equal, the method immediately returns <code>false</code>.
     *
     * @param object1 the first object, can be <code>null</code>
     * @param object2 the second object, can also be <code>null</code>
     *
     * @return <code>true</code> if this first object equals the second; <code>false</code> otherwise.
     *
     * @see java.util.Arrays#equals
     */
    public static boolean equalObjects(Object object1, Object object2) {
        if (object1 == object2) {
            return true;
        }
        if (object1 == null) {
            return false;
        }
        if (object1.getClass().isArray()) {
            if ((object1 instanceof byte[]) && (object2 instanceof byte[])) {
                return Arrays.equals((byte[]) object1, (byte[]) object2);
            } else if ((object1 instanceof short[]) && (object2 instanceof short[])) {
                return Arrays.equals((short[]) object1, (short[]) object2);
            } else if ((object1 instanceof int[]) && (object2 instanceof int[])) {
                return Arrays.equals((int[]) object1, (int[]) object2);
            } else if ((object1 instanceof long[]) && (object2 instanceof long[])) {
                return Arrays.equals((long[]) object1, (long[]) object2);
            } else if ((object1 instanceof float[]) && (object2 instanceof float[])) {
                return Arrays.equals((float[]) object1, (float[]) object2);
            } else if ((object1 instanceof double[]) && (object2 instanceof double[])) {
                return Arrays.equals((double[]) object1, (double[]) object2);
            } else if ((object1 instanceof boolean[]) && (object2 instanceof boolean[])) {
                return Arrays.equals((boolean[]) object1, (boolean[]) object2);
            } else if ((object1 instanceof Object[]) && (object2 instanceof Object[])) {
                Object[] array1 = (Object[]) object1;
                Object[] array2 = (Object[]) object2;
                if (array1.length != array2.length) {
                    return false;
                }
                for (int i = 0; i < array1.length; i++) {
                    if (!equalObjects(array1[i], array2[i])) {
                        return false;
                    }
                }
                return true;
            }
        }
        return object1.equals(object2);
    }
}

Related

  1. areEqual(final Object x, final Object y)
  2. areEqual(Object o1, Object o2)
  3. areEqual(Object o1, Object o2)
  4. areEqualStr(Object o1, Object o2)
  5. deepEquals(Object o1, Object o2)
  6. equals(BSONObject a, BSONObject b)
  7. equals(final Object a, final Object b)
  8. equals(Object a, Object b)
  9. Equals(Object in1, Object in2)