class SortingHierarchicalCollections {

    public List<T> sortFromRootToLeaf(Collection<T> elements) throws CycleDetectedException {

        Map<IdT, T> mapOfElementsById = indexElementsById(elements);

        DependencyGraph<T> graph = new DependencyGraph<>(m ->
                parentId.apply(m).map(mapOfElementsById::get));

        elements.forEach(graph::registerItem);

        return graph.getBuildChain().collect(Collectors.toList());
    }

    public  SortingHierarchicalCollections(Function<T, IdT> index,
                                  Function<T, Optional<IdT>> parentId);

}