public CategoryTree create(final List<Category> allCategoriesAsFlatList, final List<Category> roots, final List<Category> subtreeRoots) { final List<Category> allAsFlatList = allCategoriesAsFlatList; final Map<LocalizedStringEntry, Category> categoriesByLocaleAndSlug = getLocalizedStringEntryCategoryMap(allCategoriesAsFlatList); final Map<String, Category> categoriesById = allCategoriesAsFlatList.stream().collect(toMap(Category::getId, Function.<Category>identity())); final Map<String, Category> categoriesByKey = allCategoriesAsFlatList.stream().filter(category -> category.getKey() != null).collect(toMap(Category::getKey, Function.<Category>identity())); final Map<String, List<Category>> childrenByParentId = getChildrenByParentIdMap(allCategoriesAsFlatList); return new CategoryTreeImpl(roots, allAsFlatList, categoriesByLocaleAndSlug, categoriesById, categoriesByKey ,childrenByParentId, subtreeRoots); }
@Override public Optional<Category> findByExternalId(final String externalId) { return getAllAsFlatList().parallelStream() .filter(cat -> Optional.ofNullable(cat.getExternalId()).map(extIdElement -> extIdElement.equals(externalId)).orElse(false)) .findAny();//should be okay, since the externalId should be unique }
@Override public Category getRootAncestor(final Identifiable<Category> category) { requireNonNull(category); final Category theCategory = CategoryTreeUtils.getCategoryOrThrow(category, this); return theCategory .getAncestors().stream().findFirst() .flatMap(root -> findById(root.getId())) .orElse(theCategory); }
@Override public List<Category> findSiblings(final Collection<? extends Identifiable<Category>> categoryIdentifiables) { return categoryIdentifiables.stream() .flatMap(category -> getSiblings(CategoryTreeUtils.getCategoryOrThrow(category, this)).stream()) .distinct() .filter(sibling -> !categoryIdentifiables.stream().anyMatch(c -> sibling.getId().equals(c.getId()))) .collect(toList()); }
@Override public List<Category> findSiblings(final Collection<? extends Identifiable<Category>> categoryIdentifiables) { return categoryIdentifiables.stream() .flatMap(category -> getSiblings(CategoryTreeUtils.getCategoryOrThrow(category, this)).stream()) .distinct() .filter(sibling -> !categoryIdentifiables.stream().anyMatch(c -> sibling.getId().equals(c.getId()))) .collect(toList()); }
public CategoryTree create(final List<Category> allCategoriesAsFlatList, final List<Category> roots, final List<Category> subtreeRoots) { final List<Category> allAsFlatList = allCategoriesAsFlatList; final Map<LocalizedStringEntry, Category> categoriesByLocaleAndSlug = getLocalizedStringEntryCategoryMap(allCategoriesAsFlatList); final Map<String, Category> categoriesById = allCategoriesAsFlatList.stream().collect(toMap(Category::getId, Function.<Category>identity())); final Map<String, Category> categoriesByKey = allCategoriesAsFlatList.stream().filter(category -> category.getKey() != null).collect(toMap(Category::getKey, Function.<Category>identity())); final Map<String, List<Category>> childrenByParentId = getChildrenByParentIdMap(allCategoriesAsFlatList); return new CategoryTreeImpl(roots, allAsFlatList, categoriesByLocaleAndSlug, categoriesById, categoriesByKey ,childrenByParentId, subtreeRoots); }
@Override public Optional<Category> findByExternalId(final String externalId) { return getAllAsFlatList().parallelStream() .filter(cat -> Optional.ofNullable(cat.getExternalId()).map(extIdElement -> extIdElement.equals(externalId)).orElse(false)) .findAny();//should be okay, since the externalId should be unique }
@Override public Category getRootAncestor(final Identifiable<Category> category) { requireNonNull(category); final Category theCategory = CategoryTreeUtils.getCategoryOrThrow(category, this); return theCategory .getAncestors().stream().findFirst() .flatMap(root -> findById(root.getId())) .orElse(theCategory); }
public CategoryTree create(final List<Category> allCategoriesAsFlatList) { final Predicate<Category> isRootCategory = c -> c.getParent() == null; final List<Category> roots = allCategoriesAsFlatList.parallelStream().filter(isRootCategory).collect(toList()); final List<Category> allAsFlatList = allCategoriesAsFlatList; final Map<LocalizedStringEntry, Category> categoriesByLocaleAndSlug = new HashMap<>(); allCategoriesAsFlatList.forEach(category -> { final Set<Locale> localesForTheCategory = category.getSlug().getLocales(); localesForTheCategory.forEach(locale -> { final LocalizedStringEntry stringsEntry = LocalizedStringEntry.of(locale, category.getSlug().get(locale)); categoriesByLocaleAndSlug.put(stringsEntry, category); }); }); final Map<String, Category> categoriesById = allCategoriesAsFlatList.stream().collect(toMap(Category::getId, Function.<Category>identity())); final Map<String, List<Category>> childrenByParentId = new HashMap<>(); allCategoriesAsFlatList.forEach(category -> Optional.ofNullable(category.getParent()).ifPresent(parentReference -> { final String parentId = parentReference.getId(); final List<Category> entries = childrenByParentId.getOrDefault(parentId, new LinkedList<>()); entries.add(category); childrenByParentId.put(parentId, entries); }) ); return new CategoryTreeImpl(roots, allAsFlatList, categoriesByLocaleAndSlug, categoriesById, childrenByParentId); } }
@Override public Optional<Category> findByExternalId(final String externalId) { return getAllAsFlatList().parallelStream() .filter(cat -> Optional.ofNullable(cat.getExternalId()).map(extIdElement -> extIdElement.equals(externalId)).orElse(false)) .findAny();//should be okay, since the externalId should be unique }
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 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 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); }