Java Object Compare areEqualsDirect(Object bean, Object other, Object... properties)

Here you can find the source of areEqualsDirect(Object bean, Object other, Object... properties)

Description

Test if the two objects are equals.

License

Apache License

Parameter

Parameter Description
bean The bean to test.
other The other bean to test for equality with the first one.
properties The properties to compare one by one. The properties n is compared to the property n + (properties.length / 2). This array must be pair.

Return

A boolean indicating if the two objects are equals or not.

Declaration

public static boolean areEqualsDirect(Object bean, Object other, Object... properties) 

Method Source Code

//package com.java2s;
/*//from w  w w. j a v a 2  s.co m
 * Copyright JTheque (Baptiste Wicht)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Test if the two objects are equals.
     *
     * @param bean       The bean to test.
     * @param other      The other bean to test for equality with the first one.
     * @param properties The properties to compare one by one. The properties n is compared to the property n +
     *                   (properties.length / 2). This array must be pair.
     *
     * @return A boolean indicating if the two objects are equals or not.
     */
    public static boolean areEqualsDirect(Object bean, Object other, Object... properties) {
        if (bean == other) {
            return true;
        }

        if (areObjectIncompatible(bean, other)) {
            return false;
        }

        int numberOfProperties = properties.length / 2;

        for (int i = 0; i < numberOfProperties; i++) {
            Object propertyBean = properties[i];
            Object propertyOther = properties[i + numberOfProperties];

            if (propertyBean == null) {
                if (propertyOther != null) {
                    return false;
                }
            } else if (!propertyBean.equals(propertyOther)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Test if 2 objects are incompatible for equality test.
     *
     * @param object The first object to test.
     * @param other  The second object to test.
     *
     * @return true if the objects are incompatibles else false.
     */
    public static boolean areObjectIncompatible(Object object, Object other) {
        if (other == null) {
            return true;
        }

        return object.getClass() != other.getClass();
    }
}

Related

  1. areEqualEvenIfBothNull(Object first, Object second)
  2. areEquals(final Object o1, final Object o2)
  3. areEquals(Object a, Object b)
  4. areEquals(Object arg0, Object arg1)
  5. areEquals(Object... objs)
  6. equivalent(Object thisObj, Object thatObj)