Skip to content

Commit

Permalink
46 Added getValueConverters(Locale locale) and getByLanguageCode(Stri…
Browse files Browse the repository at this point in the history
…ng languageCode) methods (#47)

Co-authored-by: altro3 <[email protected]>
  • Loading branch information
altro3 and altro3 authored Oct 18, 2021
1 parent 9678fdd commit ea58f86
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 25 deletions.
78 changes: 61 additions & 17 deletions src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package pl.allegro.finance.tradukisto;

import com.google.common.base.Strings;
import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import static com.google.common.base.Verify.verify;
import static com.google.common.base.Verify.verifyNotNull;
import static pl.allegro.finance.tradukisto.internal.Container.brazilianPortugueseContainer;
import static pl.allegro.finance.tradukisto.internal.Container.czechContainer;
Expand All @@ -13,39 +20,76 @@
import static pl.allegro.finance.tradukisto.internal.Container.latvianContainer;
import static pl.allegro.finance.tradukisto.internal.Container.polishContainer;
import static pl.allegro.finance.tradukisto.internal.Container.russianContainer;
import static pl.allegro.finance.tradukisto.internal.Container.serbianCyrillicContainer;
import static pl.allegro.finance.tradukisto.internal.Container.serbianContainer;
import static pl.allegro.finance.tradukisto.internal.Container.serbianCyrillicContainer;
import static pl.allegro.finance.tradukisto.internal.Container.slovakContainer;
import static pl.allegro.finance.tradukisto.internal.Container.turkishContainer;
import static pl.allegro.finance.tradukisto.internal.Container.ukrainianContainer;

public enum ValueConverters {

BRAZILIAN_PORTUGUESE_INTEGER(brazilianPortugueseContainer().getNumbersConverter()),
ENGLISH_INTEGER(englishContainer().getNumbersConverter()),
GERMAN_INTEGER(germanContainer().getNumbersConverter()),
RUSSIAN_INTEGER(russianContainer().getNumbersConverter()),
ITALIAN_INTEGER(italianContainer().getNumbersConverter()),
POLISH_INTEGER(polishContainer().getNumbersConverter()),
CZECH_INTEGER(czechContainer().getNumbersConverter()),
SLOVAK_INTEGER(slovakContainer().getNumbersConverter()),
LATVIAN_INTEGER(latvianContainer().getNumbersConverter()),
KAZAKH_INTEGER(kazakhContainer().getNumbersConverter()),
UKRAINIAN_INTEGER(ukrainianContainer().getNumbersConverter()),
SERBIAN_INTEGER(serbianContainer().getNumbersConverter()),
SERBIAN_CYRILLIC_INTEGER(serbianCyrillicContainer().getNumbersConverter()),
FRENCH_INTEGER(frenchContainer().getNumbersConverter()),
TURKISH_INTEGER(turkishContainer().getNumbersConverter());
BRAZILIAN_PORTUGUESE_INTEGER(brazilianPortugueseContainer().getNumbersConverter(), Arrays.asList("pt", "pt-br")),
ENGLISH_INTEGER(englishContainer().getNumbersConverter(), "en"),
GERMAN_INTEGER(germanContainer().getNumbersConverter(), "de"),
RUSSIAN_INTEGER(russianContainer().getNumbersConverter(), "ru"),
ITALIAN_INTEGER(italianContainer().getNumbersConverter(), "it"),
POLISH_INTEGER(polishContainer().getNumbersConverter(), "pl"),
CZECH_INTEGER(czechContainer().getNumbersConverter(), "cs"),
SLOVAK_INTEGER(slovakContainer().getNumbersConverter(), "sk"),
LATVIAN_INTEGER(latvianContainer().getNumbersConverter(), "lv"),
KAZAKH_INTEGER(kazakhContainer().getNumbersConverter(), "kk"),
UKRAINIAN_INTEGER(ukrainianContainer().getNumbersConverter(), "uk"),
SERBIAN_INTEGER(serbianContainer().getNumbersConverter(), Arrays.asList("sr", getLanguageCodeFor("sr", "Latn"))),
SERBIAN_CYRILLIC_INTEGER(serbianCyrillicContainer().getNumbersConverter(), getLanguageCodeFor("sr", "Cyrl")),
FRENCH_INTEGER(frenchContainer().getNumbersConverter(), "fr"),
TURKISH_INTEGER(turkishContainer().getNumbersConverter(), "tr");

private final IntegerToStringConverter converter;
private final List<String> languageCodes;

ValueConverters(IntegerToStringConverter converter, String languageCodes) {
this(converter, Collections.singletonList(languageCodes));
}

ValueConverters(IntegerToStringConverter converter) {
ValueConverters(IntegerToStringConverter converter, List<String> languageCodes) {
this.converter = converter;
this.languageCodes = languageCodes;
}

public String asWords(Integer value) {
verifyNotNull(value);

return converter.asWords(value);
}

public static ValueConverters getByLocaleOrDefault(Locale locale, ValueConverters defaultConverter) {
verifyNotNull(locale);

String languageCode;
if (hasSpecifiedScript(locale)) {
languageCode = getLanguageCodeFor(locale.getLanguage(), locale.getScript());
} else {
languageCode = locale.getLanguage();
}

return getByLanguageCodeOrDefault(languageCode, defaultConverter);
}

private static boolean hasSpecifiedScript(Locale locale) {
return !Strings.isNullOrEmpty(locale.getScript());
}

private static String getLanguageCodeFor(String language, String script) {
return new Locale.Builder().setLanguage(language).setScript(script).build().toString();
}

public static ValueConverters getByLanguageCodeOrDefault(String languageCode, ValueConverters defaultConverter) {
verifyNotNull(languageCode);
verify(!languageCode.isEmpty());

return Arrays.stream(values())
.filter(it -> it.languageCodes.contains(languageCode))
.findFirst()
.orElse(defaultConverter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ class ValueConvertersTest extends Specification {
BRAZILIAN_PORTUGUESE_INTEGER.asWords(1_234) == "mil duzentos e trinta e quatro"
}

def "should convert numbers in German"() {
def "should convert numbers in English"() {
expect:
GERMAN_INTEGER.asWords(1_234) == "eintausendzweihundertvierunddreißig"
ENGLISH_INTEGER.asWords(1_234) == "one thousand two hundred thirty-four"
}

def "should convert numbers in Italian"() {
def "should convert numbers in German"() {
expect:
ITALIAN_INTEGER.asWords(1_234) == "milleduecentotrentaquattro"
GERMAN_INTEGER.asWords(1_234) == "eintausendzweihundertvierunddreißig"
}

def "should convert numbers in Russian"() {
expect:
RUSSIAN_INTEGER.asWords(1_234) == "одна тысяча двести тридцать четыре"
}

def "should convert numbers in Italian"() {
expect:
ITALIAN_INTEGER.asWords(1_234) == "milleduecentotrentaquattro"
}

def "should convert numbers in Polish"() {
expect:
POLISH_INTEGER.asWords(1_234) == "jeden tysiąc dwieście trzydzieści cztery"
Expand All @@ -37,9 +42,9 @@ class ValueConvertersTest extends Specification {
CZECH_INTEGER.asWords(1_234) == "jeden tisíc dvě stě třicet čtyři"
}

def "should convert numbers in English"() {
def "should convert numbers in Slovak"() {
expect:
ENGLISH_INTEGER.asWords(1_234) == "one thousand two hundred thirty-four"
SLOVAK_INTEGER.asWords(1_234) == "jeden tisíc dvesto tridsať štyri"
}

def "should convert numbers in Latvian"() {
Expand All @@ -52,21 +57,151 @@ class ValueConvertersTest extends Specification {
KAZAKH_INTEGER.asWords(1_234) == "бір мың екі жүз отыз төрт"
}

def "should convert numbers in Turkish"() {
def "should convert numbers in Ukrainian"() {
expect:
TURKISH_INTEGER.asWords(1_234) == "Bin İki Yüz Otuz Dört"
UKRAINIAN_INTEGER.asWords(1_234) == "одна тисяча двісті тридцять чотири"
}

def "should convert numbers in Serbian"() {
expect:
SERBIAN_INTEGER.asWords(1_234) == "jedna hiljada dvesta trideset četiri"
}

def "should convert numbers in Serbian Cyrillic"() {
expect:
SERBIAN_CYRILLIC_INTEGER.asWords(1_234) == "једна хиљада двеста тридесет четири"
}

def "should convert numbers in French"() {
expect:
FRENCH_INTEGER.asWords(1_234) == "mille deux cent trente-quatre"
}

def "should convert numbers in Turkish"() {
expect:
TURKISH_INTEGER.asWords(1_234) == "Bin İki Yüz Otuz Dört"
}

def "should throw exception when null given"() {
when:
POLISH_INTEGER.asWords(null)

then:
thrown(VerifyException)
}

def "getByLocaleOrDefault for Brazilian Portuguese"() {
expect:
getByLocaleOrDefault(new Locale("pt-br"), null).asWords(1_234) == "mil duzentos e trinta e quatro"
}

def "getByLocaleOrDefault for Portuguese"() {
expect:
getByLocaleOrDefault(new Locale("pt"), null).asWords(1_234) == "mil duzentos e trinta e quatro"
}

def "getByLocaleOrDefault for English"() {
expect:
getByLocaleOrDefault(Locale.ENGLISH, null).asWords(1_234) == "one thousand two hundred thirty-four"
}

def "getByLocaleOrDefault for German"() {
expect:
getByLocaleOrDefault(new Locale("de"), null).asWords(1_234) == "eintausendzweihundertvierunddreißig"
}

def "getByLocaleOrDefault for Russian"() {
expect:
getByLocaleOrDefault(new Locale("ru"), null).asWords(1_234) == "одна тысяча двести тридцать четыре"
}

def "getByLocaleOrDefault for Italian"() {
expect:
getByLocaleOrDefault(new Locale("it"), null).asWords(1_234) == "milleduecentotrentaquattro"
}

def "getByLocaleOrDefault for Polish"() {
expect:
getByLocaleOrDefault(new Locale("pl"), null).asWords(1_234) == "jeden tysiąc dwieście trzydzieści cztery"
}

def "getByLocaleOrDefault for Czech"() {
expect:
getByLocaleOrDefault(new Locale("cs"), null).asWords(1_234) == "jeden tisíc dvě stě třicet čtyři"
}

def "getByLocaleOrDefault for Slovak"() {
expect:
getByLocaleOrDefault(new Locale("sk"), null).asWords(1_234) == "jeden tisíc dvesto tridsať štyri"
}

def "getByLocaleOrDefault for Latvian"() {
expect:
getByLocaleOrDefault(new Locale("lv"), null).asWords(1_234) == "viens tūkstotis divi simti trīsdesmit četri"
}

def "getByLocaleOrDefault for Kazakh"() {
expect:
getByLocaleOrDefault(new Locale("kk"), null).asWords(1_234) == "бір мың екі жүз отыз төрт"
}

def "getByLocaleOrDefault for Ukrainian"() {
expect:
getByLocaleOrDefault(new Locale("uk"), null).asWords(1_234) == "одна тисяча двісті тридцять чотири"
}

def "getByLocaleOrDefault for Serbian"() {
expect:
getByLocaleOrDefault(new Locale("sr"), null).asWords(1_234) == "jedna hiljada dvesta trideset četiri"
}

def "getByLocaleOrDefault for Serbian Latin"() {
expect:
getByLocaleOrDefault(new Locale.Builder().setLanguage("sr").setScript("Latn").build(), null).asWords(1_234) == "jedna hiljada dvesta trideset četiri"
}

def "getByLocaleOrDefault for Serbian Cyrillic"() {
expect:
getByLocaleOrDefault(new Locale.Builder().setLanguage("sr").setScript("Cyrl").build(), null).asWords(1_234) == "једна хиљада двеста тридесет четири"
}

def "getByLocaleOrDefault for French"() {
expect:
getByLocaleOrDefault(Locale.FRENCH, null).asWords(1_234) == "mille deux cent trente-quatre"
}

def "getByLocaleOrDefault for Turkish"() {
expect:
getByLocaleOrDefault(new Locale("tr"), null).asWords(1_234) == "Bin İki Yüz Otuz Dört"
}

def "getByLocaleOrDefault: should throw exception when null given"() {
when:
getByLocaleOrDefault(null, null).asWords(null)

then:
thrown(VerifyException)
}

def "getByLanguageCodeOrDefault: should throw exception when null language code given"() {
when:
getByLanguageCodeOrDefault(null, null).asWords(null)

then:
thrown(VerifyException)
}

def "getByLocaleOrDefault: should throw exception when empty language code given"() {
when:
getByLocaleOrDefault(new Locale(""), null).asWords(null)

then:
thrown(VerifyException)
}

def "getByLocaleOrDefault: test for default converter"() {
expect:
getByLocaleOrDefault(new Locale("zh"), FRENCH_INTEGER) == FRENCH_INTEGER
}

}

0 comments on commit ea58f86

Please sign in to comment.