private Map<LocalizedStringEntry, Category> getLocalizedStringEntryCategoryMap(final List<Category> 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); }); }); return categoriesByLocaleAndSlug; }
private Map<LocalizedStringEntry, Category> getLocalizedStringEntryCategoryMap(final List<Category> 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); }); }); return categoriesByLocaleAndSlug; }
private static Map<LocaleSlugPair, Category> buildBySlugMap(final Collection<Category> categories) { final Map<LocaleSlugPair, Category> map = new HashMap<>(); for (final Category category : categories) { for (final Locale locale : category.getSlug().getLocales()) { map.put(new LocaleSlugPair(locale, category.getSlug().get(locale).get()), category); } } return map; }
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); } }
@Test public void returnPresentLocales() throws Exception { assertThat(LOCALIZED_STRING.getLocales()).isEqualTo(new HashSet<>(asList(Locale.GERMAN, Locale.ENGLISH))); }
@Test public void getLocales() { final LocalizedString ls = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food"); final Set<Locale> locales = ls.getLocales(); assertThat(locales) .isEqualTo(new HashSet<>(asList(Locale.GERMAN, Locale.ENGLISH))); }
@Test public void createByMap() { final Map<Locale, String> map = new HashMap<>(); map.put(Locale.GERMAN, "Hundefutter"); map.put(Locale.ENGLISH, "dog food"); final LocalizedString ls = LocalizedString.of(map); assertThat(ls.getLocales()).containsExactly(Locale.GERMAN, Locale.ENGLISH); assertThat(ls).isEqualTo(LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food")); map.remove(Locale.GERMAN); map.put(Locale.forLanguageTag("es"), "Comida para perro"); assertThat(ls.getLocales()) .overridingErrorMessage("changes in the creation map do not change the created object") .containsExactly(Locale.GERMAN, Locale.ENGLISH); assertThat(ls).isEqualTo(LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food")); assertThat(ls.get(Locale.GERMAN)).isEqualTo("Hundefutter"); }
@Test public void createFromTwoValues() throws Exception { final LocalizedString ls = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food"); assertThat(ls.getLocales()).containsExactly(Locale.GERMAN, Locale.ENGLISH); assertThat(ls.get(Locale.GERMAN)).isEqualTo("Hundefutter"); assertThat(ls.get(Locale.ENGLISH)).isEqualTo("dog food"); }
@Test public void defaultUseCases() { final LocalizedString ls = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food"); assertThat(ls.getTranslation(singletonList(Locale.US))).isEqualTo("dog food");//fuzzy search assertThat(ls.getTranslation(singletonList(Locale.ENGLISH))).isEqualTo("dog food");//strict search assertThat(ls.getLocales()).isEqualTo(new HashSet<>(asList(Locale.GERMAN, Locale.ENGLISH)));//inspecting locales assertThat(ls.slugified())//slugified values for urls .isEqualTo(LocalizedString.of(Locale.GERMAN, "hundefutter", Locale.ENGLISH, "dog-food")); final LocalizedString slugifiedUnique = ls.slugifiedUnique(); assertThat(slugifiedUnique.get(Locale.GERMAN)).matches("hundefutter-\\d+");//example: hundefutter-62899407 assertThat(slugifiedUnique.get(Locale.ENGLISH)).matches("dog-food-\\d+"); }
@Test public void createFromOneValue() throws Exception { final LocalizedString ls = LocalizedString.of(Locale.GERMAN, "Hundefutter"); assertThat(ls.getLocales()).containsExactly(Locale.GERMAN); assertThat(ls.get(Locale.GERMAN)).isEqualTo("Hundefutter"); assertThat(ls.get(Locale.ENGLISH)).isNull(); }