@Test public void testDeepEquals() { Node n0 = new Node(1, new Node(2)); Node n1 = new Node(1, new Node(2, new Node(3))); Node n2 = new Node(1, new Node(2, new Node(3))); Node n3 = new Node(1, new Node(2, new Node(4))); Node n4 = new Node(1, new Node(2, new Node(4))); Node n5 = new Node(1, new Node(2, new Node(4, new Node(5)))); assertTrue(DeepEquals.deepEquals(n1, n1)); assertTrue(DeepEquals.deepEquals(n1, n2)); assertTrue(DeepEquals.deepEquals(n3, n4)); assertFalse(DeepEquals.deepEquals(n0, n1)); assertFalse(DeepEquals.deepEquals(n0, n3)); assertFalse(DeepEquals.deepEquals(n2, n4)); assertFalse(DeepEquals.deepEquals(n4, n5)); }
public static boolean deepEquals(Object a, Object b) { Set visited = new HashSet<DualKey>(); LinkedList<DualKey> stack = new LinkedList<DualKey>(); stack.addFirst(new DualKey(a, b)); while (!stack.isEmpty()) { DualKey dualKey = stack.removeFirst(); visited.add(dualKey); if (dualKey._key1 == dualKey._key2) { continue; } if (dualKey._key1 == null || dualKey._key2 == null) { return false; } if (!dualKey._key1.getClass().equals(dualKey._key2.getClass())) { return false; } if (dualKey._key1.getClass().isArray()) { if (!compareArrays(dualKey._key1, dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedSet) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Set) { if (!compareUnorderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Collection) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedMap) { if (!compareSortedMap((SortedMap) dualKey._key1, (SortedMap) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Map) { if (!compareUnorderedMap((Map) dualKey._key1, (Map) dualKey._key2, stack, visited)) { return false; } continue; } if (hasCustomEquals(dualKey._key1.getClass())) { if (!dualKey._key1.equals(dualKey._key2)) { return false; } continue; } Collection<Field> fields = getDeepDeclaredFields(dualKey._key1.getClass()); for (Field field : fields) { try { DualKey dk = new DualKey(field.get(dualKey._key1), field.get(dualKey._key2)); if (!visited.contains(dk)) { stack.addFirst(dk); } } catch (Exception ignored) { } } } return true; }
DeepEquals { public static boolean deepEquals(Object a, Object b) { Set visited = new HashSet<DualKey>(); LinkedList<DualKey> stack = new LinkedList<DualKey>(); stack.addFirst(new DualKey(a, b)); while (!stack.isEmpty()) { DualKey dualKey = stack.removeFirst(); visited.add(dualKey); if (dualKey._key1 == dualKey._key2) { continue; } if (dualKey._key1 == null || dualKey._key2 == null) { return false; } if (!dualKey._key1.getClass().equals(dualKey._key2.getClass())) { return false; } if (dualKey._key1.getClass().isArray()) { if (!compareArrays(dualKey._key1, dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedSet) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Set) { if (!compareUnorderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Collection) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedMap) { if (!compareSortedMap((SortedMap) dualKey._key1, (SortedMap) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Map) { if (!compareUnorderedMap((Map) dualKey._key1, (Map) dualKey._key2, stack, visited)) { return false; } continue; } if (hasCustomEquals(dualKey._key1.getClass())) { if (!dualKey._key1.equals(dualKey._key2)) { return false; } continue; } Collection<Field> fields = getDeepDeclaredFields(dualKey._key1.getClass()); for (Field field : fields) { try { DualKey dk = new DualKey(field.get(dualKey._key1), field.get(dualKey._key2)); if (!visited.contains(dk)) { stack.addFirst(dk); } } catch (Exception ignored) { } } } return true; } }
DeepEquals { public static boolean deepEquals(Object a, Object b) { Set visited = new HashSet<DualKey>(); LinkedList<DualKey> stack = new LinkedList<DualKey>(); stack.addFirst(new DualKey(a, b)); while (!stack.isEmpty()) { DualKey dualKey = stack.removeFirst(); visited.add(dualKey); if (dualKey._key1 == dualKey._key2) { continue; } if (dualKey._key1 == null || dualKey._key2 == null) { return false; } if (!dualKey._key1.getClass().equals(dualKey._key2.getClass())) { return false; } if (dualKey._key1.getClass().isArray()) { if (!compareArrays(dualKey._key1, dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedSet) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Set) { if (!compareUnorderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Collection) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedMap) { if (!compareSortedMap((SortedMap) dualKey._key1, (SortedMap) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Map) { if (!compareUnorderedMap((Map) dualKey._key1, (Map) dualKey._key2, stack, visited)) { return false; } continue; } if (hasCustomEquals(dualKey._key1.getClass())) { if (!dualKey._key1.equals(dualKey._key2)) { return false; } continue; } Collection<Field> fields = getDeepDeclaredFields(dualKey._key1.getClass()); for (Field field : fields) { try { DualKey dk = new DualKey(field.get(dualKey._key1), field.get(dualKey._key2)); if (!visited.contains(dk)) { stack.addFirst(dk); } } catch (Exception ignored) { } } } return true; }  }
DeepEquals { public static boolean deepEquals(Object a, Object b) { Set visited = new HashSet<DualKey>(); LinkedList<DualKey> stack = new LinkedList<DualKey>(); stack.addFirst(new DualKey(a, b)); while (!stack.isEmpty()) { DualKey dualKey = stack.removeFirst(); visited.add(dualKey); if (dualKey._key1 == dualKey._key2) { continue; } if (dualKey._key1 == null || dualKey._key2 == null) { return false; } if (!dualKey._key1.getClass().equals(dualKey._key2.getClass())) { return false; } if (dualKey._key1.getClass().isArray()) { if (!compareArrays(dualKey._key1, dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedSet) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Set) { if (!compareUnorderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Collection) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedMap) { if (!compareSortedMap((SortedMap) dualKey._key1, (SortedMap) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Map) { if (!compareUnorderedMap((Map) dualKey._key1, (Map) dualKey._key2, stack, visited)) { return false; } continue; } if (hasCustomEquals(dualKey._key1.getClass())) { if (!dualKey._key1.equals(dualKey._key2)) { return false; } continue; } Collection<Field> fields = getDeepDeclaredFields(dualKey._key1.getClass()); for (Field field : fields) { try { DualKey dk = new DualKey(field.get(dualKey._key1), field.get(dualKey._key2)); if (!visited.contains(dk)) { stack.addFirst(dk); } } catch (Exception ignored) { } } } return true; }  static boolean deepEquals(Object a, Object b); static boolean hasCustomEquals(Class c); static int deepHashCode(Object obj); static boolean hasCustomHashCode(Class c); static Collection<Field> getDeepDeclaredFields(Class c); }
DeepEquals { public static boolean deepEquals(Object a, Object b) { Set visited = new HashSet<DualKey>(); LinkedList<DualKey> stack = new LinkedList<DualKey>(); stack.addFirst(new DualKey(a, b)); while (!stack.isEmpty()) { DualKey dualKey = stack.removeFirst(); visited.add(dualKey); if (dualKey._key1 == dualKey._key2) { continue; } if (dualKey._key1 == null || dualKey._key2 == null) { return false; } if (!dualKey._key1.getClass().equals(dualKey._key2.getClass())) { return false; } if (dualKey._key1.getClass().isArray()) { if (!compareArrays(dualKey._key1, dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedSet) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Set) { if (!compareUnorderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Collection) { if (!compareOrderedCollection((Collection) dualKey._key1, (Collection) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof SortedMap) { if (!compareSortedMap((SortedMap) dualKey._key1, (SortedMap) dualKey._key2, stack, visited)) { return false; } continue; } if (dualKey._key1 instanceof Map) { if (!compareUnorderedMap((Map) dualKey._key1, (Map) dualKey._key2, stack, visited)) { return false; } continue; } if (hasCustomEquals(dualKey._key1.getClass())) { if (!dualKey._key1.equals(dualKey._key2)) { return false; } continue; } Collection<Field> fields = getDeepDeclaredFields(dualKey._key1.getClass()); for (Field field : fields) { try { DualKey dk = new DualKey(field.get(dualKey._key1), field.get(dualKey._key2)); if (!visited.contains(dk)) { stack.addFirst(dk); } } catch (Exception ignored) { } } } return true; }  static boolean deepEquals(Object a, Object b); static boolean hasCustomEquals(Class c); static int deepHashCode(Object obj); static boolean hasCustomHashCode(Class c); static Collection<Field> getDeepDeclaredFields(Class c);  }