Class CompareUtil


  • public final class CompareUtil
    extends java.lang.Object
    A utility class that provides helper methods for implementing equals and hashCode.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.Object TIE_LOCK  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CompareUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean equal​(double n1, double n2)
      Compares two numbers for equality.
      static boolean equal​(java.lang.Object o1, java.lang.Object o2)
      Compares two objects for equality.
      static int getHashCode​(double number)
      Returns a hash code for the given number.
      static int getHashCode​(java.lang.Object object)
      Returns the hash code of the given object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • TIE_LOCK

        private static final java.lang.Object TIE_LOCK
    • Constructor Detail

      • CompareUtil

        private CompareUtil()
    • Method Detail

      • equal

        public static boolean equal​(java.lang.Object o1,
                                    java.lang.Object o2)
        Compares two objects for equality. In order to prevent lock-ordering deadlocks the following strategy is used: when two non null objects are passed to the method, the comparison is done by calling the Object.equals(Object) method of the object with the lower hash code (System.identityHashCode(Object)); in the rare case that two different objects have the same hash code, a lock is used.
        Parameters:
        o1 - an object
        o2 - another object
        Returns:
        true if either o1 and o2 are null or if o1.equals(o2)
      • getHashCode

        public static int getHashCode​(java.lang.Object object)
        Returns the hash code of the given object.
        Parameters:
        object - an object
        Returns:
        object.hashCode(), or 0 if object is null
      • equal

        public static boolean equal​(double n1,
                                    double n2)
        Compares two numbers for equality. Uses the same comparison algorithm as the Double.equals(Object) method.
        Parameters:
        n1 - a number
        n2 - another number
        Returns:
        true if the two numbers are equal, false otherwise
      • getHashCode

        public static int getHashCode​(double number)
        Returns a hash code for the given number. Applies the same algorithm as the Double.hashCode() method.
        Parameters:
        number - a number
        Returns:
        a hash code for that number