Java Object Compare areEqual(Object value1, Object value2)

Here you can find the source of areEqual(Object value1, Object value2)

Description

Compares the two given values by taking null and NaN values into account.
Two null values will be considered equal.

License

Open Source License

Parameter

Parameter Description
value1 First value.
value2 Second value.

Return

True if both values are equal or if both are null.

Declaration

public static boolean areEqual(Object value1, Object value2) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w w w.  ja v  a  2 s . c o m
     * Compares the two given values by taking null and NaN values into account.<br>Two null values will be considered
     * equal. Two NaN values (either Double or Float) will be considered equal.
     *
     * @param value1 First value.
     * @param value2 Second value.
     *
     * @return True if both values are equal or if both are null.
     */
    public static boolean areEqual(Object value1, Object value2) {
        return ((value1 == null) && (value2 == null)) || (isNaN(value1) && isNaN(value2))
                || ((value1 != null) && value1.equals(value2));
    }

    /**
     * Checks if the specified value is either NaN.
     *
     * @param value Value to be checked.
     *
     * @return True if the value is NaN, false otherwise.
     */
    private static boolean isNaN(Object value) {
        return ((value instanceof Float) && Float.isNaN((Float) value))
                || ((value instanceof Double) && Double.isNaN((Double) value));
    }
}

Related

  1. areEqual(Object obj1, Object obj2)
  2. areEqual(Object object1, Object object2)
  3. areEqual(Object one, Object another)
  4. areEqual(Object parameter, Object otherParameter)
  5. areEqual(Object value, Object otherValue)
  6. areEqual(T obj1, T obj2)
  7. areEqual(T obj1, V obj2)
  8. areEqualComparables(T object1, T object2)
  9. areEqualEvenIfBothNull(Object first, Object second)