Java Object Compare areEqual(final T object1, final T object2)

Here you can find the source of areEqual(final T object1, final T object2)

Description

Null safe comparison of instances, especially numerics like Integer.

License

Open Source License

Parameter

Parameter Description
object1 1st object to compare
object2 2ned object to compare

Return

true if both null, same instance or both values equal

Declaration

public static <T> boolean areEqual(final T object1, final T object2) 

Method Source Code

//package com.java2s;
/** License based on "The MIT License (MIT)":
    /*from   w w w  .j a  va 2 s . c o  m*/
   Copyright (c) 2014, "Michael H?nnig" <michael.hoennig@javagil.de>
       
   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to deal
   in the Software without restriction, including without limitation the rights
   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
   copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:
       
   The above copyright notice and this permission notice shall be included in all
   copies or substantial portions of the Software.
       
   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.
       
   This license becomes void immediately in case of the licensee opening any 
   law suit against the licensor concerning patent infringement issues. 
*/

public class Main {
    /**
     * Null safe comparison of instances, especially numerics like Integer.
     * 
     * @param object1 1st object to compare
     * @param object2 2ned object to compare
     * @return true if both null, same instance or both values equal
     */
    public static <T> boolean areEqual(final T object1, final T object2) {
        if (object1 == object2) {
            return true;
        }
        if (object1 == null || object2 == null) {
            return false;
        }
        if (object1.equals(object2)) {
            return true;
        }
        return false;
    }
}

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(Object aThis, Object aThat)
  6. areEqual(Object first, Object second)
  7. areEqual(Object first, Object second, boolean equalEvenIfBothNull)
  8. areEqual(Object o1, Object o2)