HierarchicalTypeDependencySorter { public static <T extends HierarchicalType> List<T> sortTypes(List<T> types) { Map<String, T> typesByName = new HashMap<>(); for (T type : types) { typesByName.put(type.name, type); } List<T> result = new ArrayList<>(types.size()); Set<T> processed = new HashSet<>(); for (T type : types) { addToResult(type, result, processed, typesByName); } return result; }  static List<T> sortTypes(List<T> types);  }
@SuppressWarnings("rawtypes") @Test public void testSimpleModel() throws AtlasException { TypeSystem ts = TypeSystem.getInstance(); HierarchicalType a = new ClassType(ts, "a", null, ImmutableSet.<String>of(), 0); HierarchicalType b = new ClassType(ts, "B", null, ImmutableSet.of("a"), 0); HierarchicalType c = new ClassType(ts, "C", null, ImmutableSet.of("B"), 0); List<HierarchicalType> unsortedTypes = Arrays.asList(c, a, b); List<HierarchicalType> sortedTypes = HierarchicalTypeDependencySorter.sortTypes(unsortedTypes); Assert.assertEquals(sortedTypes.size(), 3); Assert.assertEquals(sortedTypes.indexOf(a), 0); Assert.assertEquals(sortedTypes.indexOf(b), 1); Assert.assertEquals(sortedTypes.indexOf(c), 2); }
@SuppressWarnings("rawtypes") @Test public void testLargerModel() throws Exception { TypeSystem ts = TypeSystem.getInstance(); HierarchicalType testObjectType = new ClassType(ts, "TestObject", null, ImmutableSet.<String>of(), 0); HierarchicalType testDataSetType = new ClassType(ts, "TestDataSet", null, ImmutableSet.of("TestObject"), 0); HierarchicalType testColumnType = new ClassType(ts, "TestColumn", null, ImmutableSet.of("TestObject"), 0); HierarchicalType testRelationalDataSetType = new ClassType(ts, "TestRelationalDataSet", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testTableType = new ClassType(ts, "Table", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testDataFileType = new ClassType(ts, "TestDataFile", null, ImmutableSet.of("TestRelationalDataSet"), 0); HierarchicalType testDocumentType = new ClassType(ts, "TestDocument", null, ImmutableSet.of("TestDataSet"), 0); HierarchicalType testAnnotationType = new ClassType(ts, "TestAnnotation", null, ImmutableSet.<String>of(), 0); HierarchicalType myNewAnnotationType = new ClassType(ts, "MyNewAnnotation", null, ImmutableSet.of("TestAnnotation"), 0); List<HierarchicalType> unsortedTypes = Arrays.asList(testTableType, testColumnType, myNewAnnotationType, testDataSetType, testDataFileType, testAnnotationType, testRelationalDataSetType, testObjectType, testDocumentType); List<HierarchicalType> sortedTypes = HierarchicalTypeDependencySorter.sortTypes(unsortedTypes); Assert.assertTrue(sortedTypes.indexOf(testObjectType) < sortedTypes.indexOf(testDataSetType)); Assert.assertTrue(sortedTypes.indexOf(testObjectType) < sortedTypes.indexOf(testColumnType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testRelationalDataSetType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testDocumentType)); Assert.assertTrue(sortedTypes.indexOf(testDataSetType) < sortedTypes.indexOf(testTableType)); Assert.assertTrue(sortedTypes.indexOf(testRelationalDataSetType) < sortedTypes.indexOf(testDataFileType)); Assert.assertTrue(sortedTypes.indexOf(testAnnotationType) < sortedTypes.indexOf(myNewAnnotationType)); }