/** * Creates a category tree from a flat list of categories. * * @param allCategoriesAsFlatList all categories as flat list. * @return the created category tree. */ static CategoryTree of(final List<Category> allCategoriesAsFlatList) { requireNonNull(allCategoriesAsFlatList); return CategoryTreeFactory.of().create(allCategoriesAsFlatList); } }
public static CategoryTree create(final List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { final List<Category> categoryTreeList = categoriesOrganizedAsTree(allCategoriesAsFlatListWithoutChildrenSettings); final List<Category> allAsFlatList = getAllRecursive(categoryTreeList); final Map<LocaleSlugPair, Category> categoriesByLocaleAndSlug = buildBySlugMap(allAsFlatList); final Map<String, Category> categoriesById = buildByIdMap(allAsFlatList); return new CategoryTreeImpl(categoryTreeList, allAsFlatList, categoriesByLocaleAndSlug, categoriesById); }
public static CategoryTreeFactory of() { return new CategoryTreeFactory(); }
private static List<Category> categoriesOrganizedAsTree(List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { final List<Category> categoriesOrganizedInTrees; if (allCategoriesAsFlatListWithoutChildrenSettings == null) { categoriesOrganizedInTrees = Collections.emptyList(); LOGGER.warn(() -> "null passed for categories."); } else if (allCategoriesAsFlatListWithoutChildrenSettings.size() == 0) { categoriesOrganizedInTrees = Collections.emptyList(); } else { final Pair<List<Category>, List<Category>> partition = partition(allCategoriesAsFlatListWithoutChildrenSettings, new CategoryHasParentPredicate()); final List<Category> rootCategories = partition.getLeft(); final List<Category> categoriesWithoutParents = partition.getRight(); final Multimap<String, Category> categoriesByParentId = buildParentMultiMap(rootCategories); categoriesOrganizedInTrees = buildTreeRecursive(Optional.<Category>empty(), categoriesWithoutParents, new ArrayList<>(), categoriesByParentId); } return categoriesOrganizedInTrees; }
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); } }
/** * Creates a category tree from a flat list of categories. * * @param allCategoriesAsFlatListWithoutChildrenSettings all categories as flat list, {@code element.getChildren()} must result in an empty list. * @return the created category tree. */ static CategoryTree of(final List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { requireNonNull(allCategoriesAsFlatListWithoutChildrenSettings); return CategoryTreeFactory.create(allCategoriesAsFlatListWithoutChildrenSettings); } }
private static List<Category> getAllRecursive(final Iterable<Category> categories) { final List<Category> result = new ArrayList<>(); for (Category c: categories) { result.add(c); result.addAll(getAllRecursive(c.getChildren()).stream().collect(Collectors.toList())); } return result; }
final List<Category> childrenForCategory = immutableCopyOf(buildTreeRecursive(Optional.of(child), categoriesByParent.get(child.getId()), pathInTree, categoriesByParent)); final List<Category> pathInTreeForCategory = immutableCopyOf(pathInTree); final Optional<Reference<Category>> parentForCategory = parent.map(p -> p.toReference());
@Override public CategoryTree getSubtree(final Collection<? extends Identifiable<Category>> parentCategories) { requireNonNull(parentCategories); return CategoryTreeFactory.of().createSubtree(this, parentCategories); }
private static List<Category> categoriesOrganizedAsTree(List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { final List<Category> categoriesOrganizedInTrees; if (allCategoriesAsFlatListWithoutChildrenSettings == null) { categoriesOrganizedInTrees = Collections.emptyList(); LOGGER.warn(() -> "null passed for categories."); } else if (allCategoriesAsFlatListWithoutChildrenSettings.size() == 0) { categoriesOrganizedInTrees = Collections.emptyList(); } else { final Pair<List<Category>, List<Category>> partition = partition(allCategoriesAsFlatListWithoutChildrenSettings, new CategoryHasParentPredicate()); final List<Category> rootCategories = partition.getLeft(); final List<Category> categoriesWithoutParents = partition.getRight(); final Multimap<String, Category> categoriesByParentId = buildParentMultiMap(rootCategories); categoriesOrganizedInTrees = buildTreeRecursive(Optional.<Category>empty(), categoriesWithoutParents, new ArrayList<>(), categoriesByParentId); } return categoriesOrganizedInTrees; }
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); } }
/** * Creates a category tree from a flat list of categories. * * @param allCategoriesAsFlatListWithoutChildrenSettings all categories as flat list, {@code element.getChildren()} must result in an empty list. * @return the created category tree. */ static CategoryTree of(final List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { requireNonNull(allCategoriesAsFlatListWithoutChildrenSettings); return CategoryTreeFactory.create(allCategoriesAsFlatListWithoutChildrenSettings); } }
private static List<Category> getAllRecursive(final Iterable<Category> categories) { final List<Category> result = new ArrayList<>(); for (Category c: categories) { result.add(c); result.addAll(getAllRecursive(c.getChildren()).stream().collect(Collectors.toList())); } return result; }
final List<Category> childrenForCategory = immutableCopyOf(buildTreeRecursive(Optional.of(child), categoriesByParent.get(child.getId()), pathInTree, categoriesByParent)); final List<Category> pathInTreeForCategory = immutableCopyOf(pathInTree); final Optional<Reference<Category>> parentForCategory = parent.map(p -> p.toReference());
@Override public CategoryTree getSubtree(final Collection<? extends Identifiable<Category>> parentCategories) { requireNonNull(parentCategories); return CategoryTreeFactory.of().createSubtree(this, parentCategories); }
public static CategoryTree create(final List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { final List<Category> categoryTreeList = categoriesOrganizedAsTree(allCategoriesAsFlatListWithoutChildrenSettings); final List<Category> allAsFlatList = getAllRecursive(categoryTreeList); final Map<LocaleSlugPair, Category> categoriesByLocaleAndSlug = buildBySlugMap(allAsFlatList); final Map<String, Category> categoriesById = buildByIdMap(allAsFlatList); return new CategoryTreeImpl(categoryTreeList, allAsFlatList, categoriesByLocaleAndSlug, categoriesById); }
/** * Creates a category tree from a flat list of categories. * * @param allCategoriesAsFlatList all categories as flat list. * @return the created category tree. */ static CategoryTree of(final List<Category> allCategoriesAsFlatList) { requireNonNull(allCategoriesAsFlatList); return CategoryTreeFactory.of().create(allCategoriesAsFlatList); } }
private static List<Category> categoriesOrganizedAsTree(List<Category> allCategoriesAsFlatListWithoutChildrenSettings) { final List<Category> categoriesOrganizedInTrees; if (allCategoriesAsFlatListWithoutChildrenSettings == null) { categoriesOrganizedInTrees = Collections.emptyList(); LOGGER.warn(() -> "null passed for categories."); } else if (allCategoriesAsFlatListWithoutChildrenSettings.size() == 0) { categoriesOrganizedInTrees = Collections.emptyList(); } else { final Pair<List<Category>, List<Category>> partition = partition(allCategoriesAsFlatListWithoutChildrenSettings, new CategoryHasParentPredicate()); final List<Category> rootCategories = partition.getLeft(); final List<Category> categoriesWithoutParents = partition.getRight(); final Multimap<String, Category> categoriesByParentId = buildParentMultiMap(rootCategories); categoriesOrganizedInTrees = buildTreeRecursive(Optional.<Category>empty(), categoriesWithoutParents, new ArrayList<>(), categoriesByParentId); } return categoriesOrganizedInTrees; }