/** * 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 slugifyUnique() throws Exception { final LocalizedString actual = LocalizedString.of(Locale.GERMAN, "Aa -A_", Locale.ENGLISH, "dog food").slugifiedUnique(); assertThat(actual.stream().allMatch(entry -> entry.getValue().matches("[\\w-]+-\\d{6,14}"))).isTrue(); }
@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 queryByName() throws Exception { withCategory(client(), category -> { final LocalizedStringEntry name = category.getName().stream().findAny().get(); final Query<Category> query = CategoryQuery.of().byName(name.getLocale(), name.getValue()); assertThat(client().executeBlocking(query).head().get().getId()).isEqualTo(category.getId()); }); }
@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")); }