private List<Category> getSubtreeAsFlatList(final CategoryTree categoryTree, final Collection<? extends Identifiable<Category>> parentCategories) { final List<Category> categories = new ArrayList<>(); parentCategories.forEach(parent -> { categories.add(getCategoryOrThrow(parent, categoryTree)); final List<Category> children = categoryTree.findChildren(parent); categories.addAll(getSubtreeAsFlatList(categoryTree, children)); }); return categories; }
private List<Category> getSubtreeAsFlatList(final CategoryTree categoryTree, final Collection<? extends Identifiable<Category>> parentCategories) { final List<Category> categories = new ArrayList<>(); parentCategories.forEach(parent -> { categories.add(getCategoryOrThrow(parent, categoryTree)); final List<Category> children = categoryTree.findChildren(parent); categories.addAll(getSubtreeAsFlatList(categoryTree, children)); }); return categories; }
public CategoryTree createSubtree(final CategoryTree categoryTree, final Collection<? extends Identifiable<Category>> subtreeRoots) { final List<Category> subtreeRootsCategories = subtreeRoots.stream() .map(identifiable -> getCategoryOrThrow(identifiable, categoryTree)) .collect(toList()); final List<String> subtreeRootIds = subtreeRoots.parallelStream().map(Identifiable::getId).collect(toList()); subtreeRootsCategories.forEach(subtreeRoot -> { final Optional<String> rootAncestorOptional = subtreeRoot.getAncestors() .stream() .map(x -> x.getId()) .filter(ancestorId -> subtreeRootIds.contains(ancestorId)) .findFirst(); if (rootAncestorOptional.isPresent()) { throw new IllegalArgumentException(String.format("category of ID [%s] cannot be subtree root and descendant of [%s]", subtreeRoot.getId(), rootAncestorOptional.get())); } }); final List<Category> includedCategories = new LinkedList<>(); subtreeRootsCategories.forEach(parentCategory -> { includedCategories.add(parentCategory); final List<Category> children = categoryTree.findChildren(parentCategory); includedCategories.addAll(getSubtreeAsFlatList(categoryTree, children)); }); final List<Category> roots = findRoots(includedCategories); return create(includedCategories, roots, subtreeRootsCategories); } }
public CategoryTree createSubtree(final CategoryTree categoryTree, final Collection<? extends Identifiable<Category>> subtreeRoots) { final List<Category> subtreeRootsCategories = subtreeRoots.stream() .map(identifiable -> getCategoryOrThrow(identifiable, categoryTree)) .collect(toList()); final List<String> subtreeRootIds = subtreeRoots.parallelStream().map(Identifiable::getId).collect(toList()); subtreeRootsCategories.forEach(subtreeRoot -> { final Optional<String> rootAncestorOptional = subtreeRoot.getAncestors() .stream() .map(x -> x.getId()) .filter(ancestorId -> subtreeRootIds.contains(ancestorId)) .findFirst(); if (rootAncestorOptional.isPresent()) { throw new IllegalArgumentException(String.format("category of ID [%s] cannot be subtree root and descendant of [%s]", subtreeRoot.getId(), rootAncestorOptional.get())); } }); final List<Category> includedCategories = new LinkedList<>(); subtreeRootsCategories.forEach(parentCategory -> { includedCategories.add(parentCategory); final List<Category> children = categoryTree.findChildren(parentCategory); includedCategories.addAll(getSubtreeAsFlatList(categoryTree, children)); }); final List<Category> roots = findRoots(includedCategories); return create(includedCategories, roots, subtreeRootsCategories); } }