### Question:
LinkedOrderedNodes { @SuppressWarnings("unchecked") public static <T extends ResourceNode> List<T> sortResources(Collection<T> nodes) { if (nodes.size() < 2) { return new ArrayList<T>(nodes); } final ResourceNode initial = nodes.iterator().next(); ResourceNode predecessor = getPredecessor(initial); ResourceNode successor = getSuccessor(initial); if (predecessor == null && successor == null) { return new ArrayList<T>(nodes); } else if (predecessor == null) { return (List<T>) sortBySuccessors(nodes); } else if (successor == null) { return (List<T>) sortByPredecessors(nodes); } else { final LinkedList<ResourceNode> result = new LinkedList<ResourceNode>(); result.add(initial); while(predecessor != null) { result.addFirst(predecessor); predecessor = getPredecessor(predecessor); } while(successor != null) { result.addLast(successor); successor = getSuccessor(successor); } return (List<T>) result; } }  @SuppressWarnings("unchecked") static List<T> sortResources(Collection<T> nodes); static List<ResourceNode> sortByPredecessors(Collection<? extends ResourceNode> nodes); static List<ResourceNode> sortBySuccessors(Collection<? extends ResourceNode> nodes);  }### Answer:
@Test public void testPredecessorSort() { final Set<ResourceNode> all = new HashSet<ResourceNode>(); ResourceNode current = create(0); all.add(current); for(int i=1; i <= SIZE; i++) { ResourceNode next = create(i); all.add(next); current.addAssociation(Aras.IS_PREDECESSOR_OF, next); current = next; } List<? extends ResourceNode> sorted = LinkedOrderedNodes.sortResources(all); for(int i=0; i < SIZE; i++) { int id = getID(sorted.get(i)); Assert.assertEquals("expected id to be " + i + " but was " + id, i, id); } }
@Test public void testSuccessorSort() { final Set<ResourceNode> all = new HashSet<ResourceNode>(); ResourceNode current = create(0); all.add(current); for(int i=1; i <= SIZE; i++) { ResourceNode next = create(i); all.add(next); next.addAssociation(Aras.IS_SUCCESSOR_OF, current); current = next; } List<? extends ResourceNode> sorted = LinkedOrderedNodes.sortResources(all); for(int i=0; i < SIZE; i++) { int id = getID(sorted.get(i)); Assert.assertEquals("expected id to be " + i + " but was " + id, i, id); } }