@Test public void shouldSortEntitiesForComplexTopologyWithLoop() throws Exception { setupComplexTopologyWithLoop(); List<Entity> entities = asList(entityE, entityD, entityA, entityF, entityC, entityB); relationshipSorter.sort(entities); assertEntitiesOrder(entities, asSet( order(entityA, entityD), order(entityB, entityD), order(entityC, entityD), order(entityE, entityD), order(entityF, entityA), order(entityF, entityB), order(entityF, entityC), order(entityF, entityE) )); }
public void sort(List<Entity> entities) { final Map<Entity, Integer> entitiesOrder = new HashMap<>(); EntitiesRetriever entitiesRetriever = new EntitiesRetriever(entities); for (Entity entity : entities) { traverse(entity, new ArrayList<Entity>(), entitiesOrder, entitiesRetriever); } Collections.sort(entities, new Comparator<Entity>() { @Override public int compare(Entity entity1, Entity entity2) { return Integer.compare(entitiesOrder.get(entity2), entitiesOrder.get(entity1)); } }); }
RelationshipSorter { public void sort(List<Entity> entities) { final Map<Entity, Integer> entitiesOrder = new HashMap<>(); EntitiesRetriever entitiesRetriever = new EntitiesRetriever(entities); for (Entity entity : entities) { traverse(entity, new ArrayList<Entity>(), entitiesOrder, entitiesRetriever); } Collections.sort(entities, new Comparator<Entity>() { @Override public int compare(Entity entity1, Entity entity2) { return Integer.compare(entitiesOrder.get(entity2), entitiesOrder.get(entity1)); } }); } }
RelationshipSorter { public void sort(List<Entity> entities) { final Map<Entity, Integer> entitiesOrder = new HashMap<>(); EntitiesRetriever entitiesRetriever = new EntitiesRetriever(entities); for (Entity entity : entities) { traverse(entity, new ArrayList<Entity>(), entitiesOrder, entitiesRetriever); } Collections.sort(entities, new Comparator<Entity>() { @Override public int compare(Entity entity1, Entity entity2) { return Integer.compare(entitiesOrder.get(entity2), entitiesOrder.get(entity1)); } }); }  }
RelationshipSorter { public void sort(List<Entity> entities) { final Map<Entity, Integer> entitiesOrder = new HashMap<>(); EntitiesRetriever entitiesRetriever = new EntitiesRetriever(entities); for (Entity entity : entities) { traverse(entity, new ArrayList<Entity>(), entitiesOrder, entitiesRetriever); } Collections.sort(entities, new Comparator<Entity>() { @Override public int compare(Entity entity1, Entity entity2) { return Integer.compare(entitiesOrder.get(entity2), entitiesOrder.get(entity1)); } }); }  void sort(List<Entity> entities); }
RelationshipSorter { public void sort(List<Entity> entities) { final Map<Entity, Integer> entitiesOrder = new HashMap<>(); EntitiesRetriever entitiesRetriever = new EntitiesRetriever(entities); for (Entity entity : entities) { traverse(entity, new ArrayList<Entity>(), entitiesOrder, entitiesRetriever); } Collections.sort(entities, new Comparator<Entity>() { @Override public int compare(Entity entity1, Entity entity2) { return Integer.compare(entitiesOrder.get(entity2), entitiesOrder.get(entity1)); } }); }  void sort(List<Entity> entities);  }