EntityReference implements Serializable, Cloneable, Comparable<EntityReference> { public int compareTo(EntityReference reference) { return new CompareToBuilder().append(toString(), reference.toString()).toComparison(); } EntityReference(String name, EntityType type);  EntityReference(String name, EntityType type, EntityReference parent); void setName(String name); String getName(); void setParent(EntityReference parent); EntityReference getParent(); void setChild(EntityReference child); EntityReference getChild(); void setType(EntityType type); EntityType getType(); EntityReference getRoot(); EntityReference extractReference(EntityType type); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode(); @Override EntityReference clone(); int compareTo(EntityReference reference);  }
@Test public void testCompareTo() { EntityReference reference = new EntityReference("f", EntityType.DOCUMENT, new EntityReference("e", EntityType.SPACE, new EntityReference("d", EntityType.WIKI))); EntityReference reference2 = new EntityReference("c", EntityType.DOCUMENT, new EntityReference("b", EntityType.SPACE, new EntityReference("a", EntityType.WIKI))); EntityReference reference3 = new EntityReference("c", EntityType.DOCUMENT, new EntityReference("a", EntityType.SPACE, new EntityReference("a", EntityType.WIKI))); Assert.assertEquals(0, reference.compareTo(reference)); List<EntityReference> list = new ArrayList<EntityReference>(); list.add(reference); list.add(reference2); list.add(reference3); Collections.sort(list); Assert.assertSame(reference, list.get(2)); Assert.assertSame(reference2, list.get(1)); Assert.assertSame(reference3, list.get(0)); }