Class EqualObjectGraphs


  • final class EqualObjectGraphs
    extends java.lang.Object
    An object that implements deep equality test of objects, including their reference graph topology, that is in addition to establishing by-value equality of objects, it also establishes that their reachable object graphs have identical shape. It is capable of custom-comparing a wide range of various objects, including various Rhino Scriptables, Java arrays, Java Lists, and to some degree Java Maps and Sets (sorted Maps are okay, as well as Sets with elements that can be sorted using their Comparable implementation, and Maps whose keysets work the same). The requirement for sortable maps and sets is to ensure deterministic order of traversal, which is necessary for establishing structural equality of object graphs.

    An instance of this object is stateful in that it memoizes pairs of objects that already compared equal, so reusing an instance for repeated equality tests of potentially overlapping object graph is beneficial for performance as long as all equality test invocations returns true. Reuse is not advised after an equality test returned false since there is a heuristic in comparing cyclic data structures that can memoize false equalities if two cyclic data structures end up being unequal.

    • Field Detail

      • valueClasses

        private static final java.util.Set<java.lang.Class<?>> valueClasses
      • knownEquals

        private final java.util.Map<java.lang.Object,​java.lang.Object> knownEquals
      • currentlyCompared

        private final java.util.Map<java.lang.Object,​java.lang.Object> currentlyCompared
    • Constructor Detail

      • EqualObjectGraphs

        EqualObjectGraphs()
    • Method Detail

      • withThreadLocal

        static <T> T withThreadLocal​(java.util.function.Function<EqualObjectGraphs,​T> action)
      • equalGraphs

        boolean equalGraphs​(java.lang.Object o1,
                            java.lang.Object o2)
      • equalGraphsNoMemo

        private boolean equalGraphsNoMemo​(java.lang.Object o1,
                                          java.lang.Object o2)
      • equalObjectArrays

        private boolean equalObjectArrays​(java.lang.Object[] a1,
                                          java.lang.Object[] a2)
      • equalLists

        private boolean equalLists​(java.util.List<?> l1,
                                   java.util.List<?> l2)
      • equalMaps

        private boolean equalMaps​(java.util.Map<?,​?> m1,
                                  java.util.Map<?,​?> m2)
      • sortedEntries

        private static java.util.Iterator<java.util.Map.Entry> sortedEntries​(java.util.Map m)
      • equalSets

        private boolean equalSets​(java.util.Set<?> s1,
                                  java.util.Set<?> s2)
      • sortedSet

        private static java.lang.Object[] sortedSet​(java.util.Set<?> s)
      • getSortedIds

        private static java.lang.Object[] getSortedIds​(Scriptable s)
      • getSymbolName

        private static java.lang.String getSymbolName​(Symbol s)
      • getIds

        private static java.lang.Object[] getIds​(Scriptable s)
      • getValue

        private static java.lang.Object getValue​(Scriptable s,
                                                 java.lang.Object id)