Class DeepEquals


  • public class DeepEquals
    extends java.lang.Object
    Test two objects for equivalence with a 'deep' comparison. This will traverse the Object graph and perform either a field-by-field comparison on each object (if no .equals() method has been overridden from Object), or it will call the customized .equals() method if it exists. This method will allow object graphs loaded at different times (with different object ids) to be reliably compared. Object.equals() / Object.hashCode() rely on the object's identity, which would not consider two equivalent objects necessarily equals. This allows graphs containing instances of Classes that did not overide .equals() / .hashCode() to be compared. For example, testing for existence in a cache. Relying on an object's identity will not locate an equivalent object in a cache.

    This method will handle cycles correctly, for example A->B->C->A. Suppose a and a' are two separate instances of A with the same values for all fields on A, B, and C. Then a.deepEquals(a') will return true. It uses cycle detection storing visited objects in a Set to prevent endless loops.
    Author:
    John DeRegnaucourt (jdereg@gmail.com)
    Copyright [2010] John DeRegnaucourt

    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.
    • Constructor Summary

      Constructors 
      Constructor Description
      DeepEquals()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean deepEquals​(java.lang.Object a, java.lang.Object b)
      Compare two objects with a 'deep' comparison.
      static int deepHashCode​(java.lang.Object obj)
      Get a deterministic hashCode (int) value for an Object, regardless of when it was created or where it was loaded into memory.
      static java.util.Collection<java.lang.reflect.Field> getDeepDeclaredFields​(java.lang.Class<?> c)
      Get all non static, non transient, fields of the passed in class.
      static boolean hasCustomEquals​(java.lang.Class<?> c)
      Determine if the passed in class has a non-Object.equals() method.
      static boolean hasCustomHashCode​(java.lang.Class<?> c)
      Determine if the passed in class has a non-Object.hashCode() method.
      • Methods inherited from class java.lang.Object

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

      • DeepEquals

        public DeepEquals()
    • Method Detail

      • deepEquals

        public static boolean deepEquals​(java.lang.Object a,
                                         java.lang.Object b)
        Compare two objects with a 'deep' comparison. This will traverse the Object graph and perform either a field-by-field comparison on each object (if no .equals() method has been overridden from Object), or it will call the customized .equals() method if it exists. This method will allow object graphs loaded at different times (with different object ids) to be reliably compared. Object.equals() / Object.hashCode() rely on the object's identity, which would not consider to equivalent objects necessarily equals. This allows graphs containing instances of Classes that did no overide .equals() / .hashCode() to be compared. For example, testing for existence in a cache. Relying on an objects identity will not locate an object in cache, yet relying on it being equivalent will.

        This method will handle cycles correctly, for example A->B->C->A. Suppose a and a' are two separate instances of the A with the same values for all fields on A, B, and C. Then a.deepEquals(a') will return true. It uses cycle detection storing visited objects in a Set to prevent endless loops.
        Parameters:
        a - Object one to compare
        b - Object two to compare
        Returns:
        true if a is equivalent to b, false otherwise. Equivalent means that all field values of both subgraphs are the same, either at the field level or via the respectively encountered overridden .equals() methods during traversal.
      • hasCustomEquals

        public static boolean hasCustomEquals​(java.lang.Class<?> c)
        Determine if the passed in class has a non-Object.equals() method. This method caches its results in static ConcurrentHashMap to benefit execution performance.
        Parameters:
        c - Class to check.
        Returns:
        true, if the passed in Class has a .equals() method somewhere between itself and just below Object in it's inheritance.
      • deepHashCode

        public static int deepHashCode​(java.lang.Object obj)
        Get a deterministic hashCode (int) value for an Object, regardless of when it was created or where it was loaded into memory. The problem with java.lang.Object.hashCode() is that it essentially relies on memory location of an object (what identity it was assigned), whereas this method will produce the same hashCode for any object graph, regardless of how many times it is created.

        This method will handle cycles correctly (A->B->C->A). In this case, Starting with object A, B, or C would yield the same hashCode. If an object encountered (root, suboject, etc.) has a hashCode() method on it (that is not Object.hashCode()), that hashCode() method will be called and it will stop traversal on that branch.
        Parameters:
        obj - Object who hashCode is desired.
        Returns:
        the 'deep' hashCode value for the passed in object.
      • hasCustomHashCode

        public static boolean hasCustomHashCode​(java.lang.Class<?> c)
        Determine if the passed in class has a non-Object.hashCode() method. This method caches its results in static ConcurrentHashMap to benefit execution performance.
        Parameters:
        c - Class to check.
        Returns:
        true, if the passed in Class has a .hashCode() method somewhere between itself and just below Object in it's inheritance.
      • getDeepDeclaredFields

        public static java.util.Collection<java.lang.reflect.Field> getDeepDeclaredFields​(java.lang.Class<?> c)
        Get all non static, non transient, fields of the passed in class. The special this$ field is also not returned. The result is cached in a static ConcurrentHashMap to benefit execution performance.
        Parameters:
        c - Class instance
        Returns:
        Collection of only the fields in the passed in class that would need further processing (reference fields). This makes field traversal on a class faster as it does not need to continually process known fields like primitives.