SCC { public SCC(Map<StatusCode, List<StatusCode>> graph) { this.graph = graph; } SCC(Map<StatusCode, List<StatusCode>> graph); List<StatusCode[]> getStronglyConnectedComponents(); void visit(StatusCode statusCode);  }
@Test public void testScc() { Map<StatusCode, List<StatusCode>> graph = new HashMap<StatusCode, List<StatusCode>>(); StatusCode[] codes = new StatusCode[] { new StatusCode(new UUID(0, 1)), new StatusCode(new UUID(0, 2)), new StatusCode(new UUID(0, 3)), new StatusCode(new UUID(0, 4)), new StatusCode(new UUID(0, 5)), new StatusCode(new UUID(0, 6)), new StatusCode(new UUID(0, 7)), new StatusCode(new UUID(0, 8)), new StatusCode(new UUID(0, 9)) }; graph.put(codes[0], asList(codes[1])); graph.put(codes[1], asList(codes[2])); graph.put(codes[2], asList(codes[0], codes[6])); graph.put(codes[3], asList(codes[4])); graph.put(codes[4], asList(codes[5], codes[6])); graph.put(codes[5], asList(codes[3])); graph.put(codes[6], asList(codes[7])); graph.put(codes[7], asList(codes[8])); graph.put(codes[8], asList(codes[6])); List<StatusCode[]> sccs = new SCC(graph).getStronglyConnectedComponents(); assertNotNull(sccs); assertEquals(3, sccs.size()); Comparator<StatusCode> scComparator = new Comparator<StatusCode>() { @Override public int compare(StatusCode o1, StatusCode o2) { return o1.getId() .compareTo(o2.getId()); } }; StatusCode[] expected; sccs = sccs.stream() .peek(g -> Arrays.sort(g, scComparator)) .sorted((a, b) -> a[0].getId() .compareTo(b[0].getId())) .collect(Collectors.toList()); expected = new StatusCode[] { codes[0], codes[1], codes[2] }; Arrays.sort(expected, scComparator); StatusCode[] got = sccs.get(0); assertArrayEquals(expected, got); expected = new StatusCode[] { codes[4], codes[5], codes[3] }; Arrays.sort(expected, scComparator); got = sccs.get(1); assertArrayEquals(expected, got); expected = new StatusCode[] { codes[6], codes[7], codes[8] }; Arrays.sort(expected, scComparator); got = sccs.get(2); assertArrayEquals(expected, got); }