/** * Creates an entry for language tag and a value. * * <p>The language tag can be just a language code</p> * {@include.example io.sphere.sdk.models.LocalizedStringEntryTest#ofLanguageTagAndValueForLanguage()} * <p>or language and a country</p> * {@include.example io.sphere.sdk.models.LocalizedStringEntryTest#ofLanguageTagAndValueForLanguageTag()} * * @param languageTag language tag as in {@link Locale#forLanguageTag(String)} * @param value value of this entry * @return entry */ public static LocalizedStringEntry of(final String languageTag, final String value) { final Locale locale = Locale.forLanguageTag(languageTag); return of(locale, value); }
@Override public Optional<Category> findBySlug(final Locale locale, final String slug) { return Optional.ofNullable(categoriesByLocaleAndSlug.get(LocalizedStringEntry.of(locale, slug))); }
@Override public Optional<Category> findBySlug(final Locale locale, final String slug) { return Optional.ofNullable(categoriesByLocaleAndSlug.get(LocalizedStringEntry.of(locale, slug))); }
@Override public Optional<Category> findBySlug(final Locale locale, final String slug) { return Optional.ofNullable(categoriesByLocaleAndSlug.get(LocalizedStringEntry.of(locale, slug))); }
/** * Creates a new Stream of entries. * * {@include.example io.sphere.sdk.models.LocalizedStringTest#streamAndCollector()} * * @return stream of all entries */ public Stream<LocalizedStringEntry> stream() { return translations.entrySet().stream().map(entry -> LocalizedStringEntry.of(entry.getKey(), entry.getValue())); }
@Override public C withText(final Locale locale, final String text) { final LocalizedStringEntry locStringEntry = LocalizedStringEntry.of(requireNonNull(locale), requireNonNull(text)); return withText(locStringEntry); }
/** * Creates an instance by supplying a map of {@link String} the language tag and {@link String}. Changes to the map won't affect the instance. * * {@include.example io.sphere.sdk.models.LocalizedStringTest#ofStringToStringMap()} * * @param translations the key value pairs for the translation * @return a new instance which has the same key value pairs as {@code translation} at creation time */ @JsonIgnore public static LocalizedString ofStringToStringMap(final Map<String, String> translations) { requireNonNull(translations); return translations.entrySet().stream() .map(localeEntry -> { final Locale locale = Locale.forLanguageTag(localeEntry.getKey()); return LocalizedStringEntry.of(locale, localeEntry.getValue()); }) .collect(LocalizedString.streamCollector()); }
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; }
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); } }
/** * Creates a new instance where each translation value is transformed with {@code function}. * * {@include.example io.sphere.sdk.models.LocalizedStringTest#mapValue()} * * @param function transforms a value for a locale into a new value * @return a new {@link LocalizedString} which consist all elements for this transformed with {@code function} */ public LocalizedString mapValue(final BiFunction<Locale, String, String> function) { return stream().map(entry -> { final String newValue = function.apply(entry.getLocale(), entry.getValue()); return LocalizedStringEntry.of(entry.getLocale(), newValue); }).collect(streamCollector()); }
@Test public void ofLanguageTagAndValueForLanguage() { final LocalizedStringEntry entry = LocalizedStringEntry.of("de", "foo"); assertThat(entry.getLocale()).isEqualTo(Locale.GERMAN); assertThat(entry.getValue()).isEqualTo("foo"); }
@Test public void ofLanguageTagAndValueForLanguageTag() { final LocalizedStringEntry entry = LocalizedStringEntry.of("de-DE", "foo"); assertThat(entry.getLocale()).isEqualTo(Locale.GERMANY); assertThat(entry.getValue()).isEqualTo("foo"); } }
@Test public void buildsText() throws Exception { final TestableSearchDsl dsl = new TestableSearchDsl().withText(ENGLISH, "foo"); assertThat(dsl.text()).isEqualTo(LocalizedStringEntry.of(ENGLISH, "foo")); }
@Test public void stream() throws Exception { final LocalizedString upperCased = LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food") .stream() .map(e -> LocalizedStringEntry.of(e.getLocale(), e.getValue().toUpperCase())) .collect(LocalizedString.streamCollector()); assertThat(upperCased).isEqualTo(LocalizedString.of(Locale.GERMAN, "HUNDEFUTTER", Locale.ENGLISH, "DOG FOOD")); }
@Test public void streamAndCollector() { final LocalizedString ls = LocalizedString.of(Locale.GERMAN, "Company Hundefutter", Locale.ENGLISH, "Company dog food"); final Stream<LocalizedStringEntry> stream = ls.stream(); final LocalizedString updatedLs = stream .map(entry -> { final Locale locale = entry.getLocale(); final String value = entry.getValue(); final String newValue = value.replace("Company ", ""); return LocalizedStringEntry.of(locale, newValue); }) .collect(LocalizedString.streamCollector()); assertThat(updatedLs).isEqualTo(LocalizedString.of(Locale.GERMAN, "Hundefutter", Locale.ENGLISH, "dog food")); }
@Test public void suggestionForMultipleLanguages() { withSuggestProduct(client(), product -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); final List<LocalizedStringEntry> keywords = asList( LocalizedStringEntry.of(Locale.GERMAN, "offiz"), LocalizedStringEntry.of(Locale.ENGLISH, "multi") ); final SuggestQuery suggestQuery = SuggestQuery.of(keywords) .withStaged(true); assertEventually(() -> { final SuggestionResult suggestionResult = client().executeBlocking(suggestQuery); assertThat(suggestionResult.getSuggestionsForLocale(Locale.GERMAN)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Schweizer Messer"))); assertThat(suggestionResult.getSuggestionsForLocale(Locale.ENGLISH)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Multi tool"))); }); }); }
@Test public void executionWithCustomTokenizer() { withSuggestProduct(client(), product -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); final SuggestQuery suggestQuery = SuggestQuery.of(LocalizedStringEntry.of(Locale.GERMAN, "offiz")) .withStaged(true); assertEventually(() -> { final SuggestionResult suggestionResult = client().executeBlocking(suggestQuery); assertThat(suggestionResult.getSuggestionsForLocale(Locale.GERMAN)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Schweizer Messer"))); }); }); }
@Test public void execution() { withSuggestProduct(client(), product -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); final SuggestQuery suggestQuery = SuggestQuery.of(LocalizedStringEntry.of(Locale.ENGLISH, "knife")) .withStaged(true); assertEventually(() -> { final SuggestionResult suggestionResult = client().executeBlocking(suggestQuery); assertThat(suggestionResult.getSuggestionsForLocale(Locale.ENGLISH)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Swiss Army Knife"))); }); }); }
@Test public void fuzzy() { withSuggestProduct(client(), product -> { final SearchKeywords searchKeywords = SearchKeywords.of( Locale.ENGLISH, asList(SearchKeyword.of("Multi tool"), SearchKeyword.of("Swiss Army Knife", WhiteSpaceSuggestTokenizer.of())), Locale.GERMAN, singletonList(SearchKeyword.of("Schweizer Messer", CustomSuggestTokenizer.of(asList("schweizer messer", "offiziersmesser", "sackmesser")))) ); assertThat(product.getMasterData().getStaged().getSearchKeywords()).isEqualTo(searchKeywords); final SuggestQuery suggestQuery = SuggestQuery.of(LocalizedStringEntry.of(Locale.ENGLISH, "knive")) .withStaged(true).withFuzzy(true); assertEventually(() -> { final SuggestionResult suggestionResult = client().executeBlocking(suggestQuery); assertThat(suggestionResult.getSuggestionsForLocale(Locale.ENGLISH)) .matches(suggestionsList -> suggestionsList.stream() .anyMatch(suggestion -> suggestion.getText().equals("Swiss Army Knife"))); }); }); } }