### Question:
DocumentReferenceComparator implements Comparator<DocumentReference> { @Override public int compare(DocumentReference alice, DocumentReference bob) { Iterator<EntityReference> aliceIterator = alice.getReversedReferenceChain().iterator(); Iterator<EntityReference> bobIterator = bob.getReversedReferenceChain().iterator(); while (aliceIterator.hasNext() && bobIterator.hasNext()) { int diff = aliceIterator.next().getName().compareTo(bobIterator.next().getName()); if (diff != 0) { return diff; } } if (aliceIterator.hasNext()) { return 1; } else if (bobIterator.hasNext()) { return -1; } String aliceLocale = alice.getLocale() != null ? alice.getLocale().toString() : ""; String bobLocale = bob.getLocale() != null ? bob.getLocale().toString() : ""; return aliceLocale.compareTo(bobLocale); }  @Override int compare(DocumentReference alice, DocumentReference bob);  }### Answer:
@Test public void compare() { DocumentReference reference = new DocumentReference("wiki", "Space", "Page"); assertEquals(0, comparator.compare(reference, new DocumentReference(reference))); assertEquals(0, comparator.compare(new DocumentReference(reference, Locale.FRENCH), new DocumentReference(reference, Locale.FRENCH))); assertTrue(comparator.compare(new DocumentReference(reference, Locale.ROOT), new DocumentReference(reference, Locale.GERMAN)) < 0); assertTrue(comparator.compare(new DocumentReference(reference, Locale.ITALIAN), reference) > 0); assertTrue(comparator.compare(reference, new DocumentReference("wiki", "Space", "APage")) > 0); assertTrue(comparator.compare(reference, new DocumentReference("wiki", "XSpace", "APage")) < 0); assertTrue(comparator.compare(reference, new DocumentReference("aWiki", "ASpace", "APage")) > 0); }
@Test public void compareNestedPages() { assertTrue(compare(Arrays.asList("math", "Path", "To", "Page"), Arrays.asList("math", "Path", "Page")) > 0); assertTrue(compare(Arrays.asList("math", "Path", "Alice"), Arrays.asList("math", "Path", "To", "Bob")) < 0); assertTrue( compare(Arrays.asList("math", "Users", "Alice", "Files"), Arrays.asList("math", "Users", "Alice")) > 0); assertTrue( compare(Arrays.asList("math", "Users", "Alice"), Arrays.asList("math", "Users", "Alice", "Files")) < 0); assertEquals(0, compare(Arrays.asList("math", "Users", "Alice", "Files"), Arrays.asList("math", "Users", "Alice", "Files"))); }