SubstitutionUtils { public static <T extends HasInheritance> Optional<List<Subtypes<T>>> collectTypeHierarchy(Map<QName, T> types, QName parent) { T type = types.get(parent); if (Objects.nonNull(type) && type.getAbstract().equals(TBoolean.YES)) { List<Subtypes<T>> subtypes = new ArrayList<>(); types.forEach((key, current) -> { if (Objects.nonNull(current.getDerivedFrom()) && current.getDerivedFrom().getTypeAsQName().equals(parent)) { Subtypes<T> child = new Subtypes<>(current); subtypes.add(child); collectTypeHierarchy(types, key) .ifPresent(child::addChildren); } }); return Optional.of(subtypes); } return Optional.empty(); }  static Map<R, List<Subtypes<T>>> collectSubstitutableTemplates(List<R> templates, Map<QName, T> types); static Optional<List<Subtypes<T>>> collectTypeHierarchy(Map<QName, T> types, QName parent); static boolean containsPatterns(List<TNodeTemplate> topologyNodes, Map<QName, TNodeType> nodeTypes);  }
@Test public void retrieveTypeHierarchy() { Optional<List<Subtypes<TNodeType>>> tNodeTypeSubtypes = SubstitutionUtils.collectTypeHierarchy(nodeTypes, grandFather); assertTrue(tNodeTypeSubtypes.isPresent()); List<Subtypes<TNodeType>> subtypes = tNodeTypeSubtypes.get(); assertEquals(3, subtypes.size()); assertTrue(subtypes.removeIf(nodeType -> PARENT.equals(nodeType.getElement().getName()))); assertTrue(subtypes.removeIf(nodeType -> MOTHER.equals(nodeType.getElement().getName()))); assertTrue(subtypes.removeIf(nodeType -> AUNT.equals(nodeType.getElement().getName()))); }
@Test public void retrieveTypeHierarchy2() { Optional<List<Subtypes<TNodeType>>> tNodeTypeSubtypes = SubstitutionUtils.collectTypeHierarchy(nodeTypes, parent); assertTrue(tNodeTypeSubtypes.isPresent()); List<Subtypes<TNodeType>> subtypes = tNodeTypeSubtypes.get(); assertEquals(1, subtypes.size()); Subtypes<TNodeType> firstChild = subtypes.get(0); assertEquals(CHILD, firstChild.getElement().getName()); assertEquals(1, firstChild.getChildren().size()); Subtypes<TNodeType> childOfFirstChild = firstChild.getChildren().get(0); assertEquals(GRANDCHILD, childOfFirstChild.getElement().getName()); assertNull(childOfFirstChild.getChildren()); }