Skip to content

Commit

Permalink
#99 | add longConverter for Dutch
Browse files Browse the repository at this point in the history
  • Loading branch information
aszumowska committed Oct 16, 2024
1 parent 32c13f7 commit 374f551
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Objects;

import static pl.allegro.finance.tradukisto.internal.Container.croatianContainer;
import static pl.allegro.finance.tradukisto.internal.Container.dutchContainer;
import static pl.allegro.finance.tradukisto.internal.Container.englishContainer;
import static pl.allegro.finance.tradukisto.internal.Container.polishContainer;
import static pl.allegro.finance.tradukisto.internal.Container.hindiContainer;
Expand All @@ -17,7 +18,8 @@ public enum LongValueConverters {
POLISH_LONG(polishContainer().getLongConverter()),
HINDI_LONG(hindiContainer().getLongConverter()),
SWEDISH_LONG(swedishContainer().getLongConverter()),
JAPANESE_KANJI_LONG(japaneseKanjiContainer().getLongConverter());
JAPANESE_KANJI_LONG(japaneseKanjiContainer().getLongConverter()),
DUTCH_LONG(dutchContainer().getLongConverter());

private final LongToStringConverter converter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pl.allegro.finance.tradukisto.internal.languages.czech.CzechValues;
import pl.allegro.finance.tradukisto.internal.languages.czech.CzechValuesForSmallNumbers;
import pl.allegro.finance.tradukisto.internal.languages.dutch.DutchIntegerToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.dutch.DutchLongToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.dutch.DutchThousandToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.dutch.DutchValues;
import pl.allegro.finance.tradukisto.internal.languages.english.AmericanEnglishValues;
Expand Down Expand Up @@ -103,12 +104,12 @@ public static Container sloveneContainer() {
SloveneValues values = new SloveneValues();

SloveneThousandToWordsConverter sloveneThousandToWordsConverter = new SloveneThousandToWordsConverter(
values.baseNumbers());
values.baseNumbers());

IntegerToStringConverter converter = new NumberToWordsConverter(sloveneThousandToWordsConverter, values.pluralForms());

BigDecimalToStringConverter bigDecimalBankingMoneyValueConverter = new BigDecimalToBankingMoneyConverter(
converter, values.currency());
converter, values.currency());

return new Container(converter, null, bigDecimalBankingMoneyValueConverter);
}
Expand Down Expand Up @@ -189,21 +190,26 @@ public static Container germanContainer() {
}

public static Container dutchContainer() {

DutchValues values = new DutchValues();

DutchThousandToWordsConverter dutchThousandToWordsConverter =
new DutchThousandToWordsConverter(values.baseNumbers());

IntegerToStringConverter converter = new DutchIntegerToWordsConverter(
new NumberToWordsConverter(dutchThousandToWordsConverter, values.pluralForms()), values.exceptions(),
new NumberToWordsConverter(dutchThousandToWordsConverter, values.pluralForms()),
values.exceptions(),
dutchThousandToWordsConverter
);

LongToStringConverter dutchLongToWordsConverter = new DutchLongToWordsConverter(
dutchThousandToWordsConverter,
values.pluralForms()
);

BigDecimalToStringConverter bigDecimalBankingMoneyValueConverter =
new BigDecimalToBankingMoneyConverter(converter, values.currency());

return new Container(converter, null, bigDecimalBankingMoneyValueConverter);
return new Container(converter, dutchLongToWordsConverter, bigDecimalBankingMoneyValueConverter);
}

public static Container italianContainer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package pl.allegro.finance.tradukisto.internal.languages.dutch;

import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter;
import pl.allegro.finance.tradukisto.internal.LongToStringConverter;
import pl.allegro.finance.tradukisto.internal.languages.GenderType;
import pl.allegro.finance.tradukisto.internal.languages.PluralForms;
import pl.allegro.finance.tradukisto.internal.support.Assert;
import pl.allegro.finance.tradukisto.internal.support.NumberChunking;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static java.util.Collections.reverse;

public class DutchLongToWordsConverter implements LongToStringConverter {

private final NumberChunking numberChunking = new NumberChunking();

private final GenderAwareIntegerToStringConverter hundredsToWordsConverter;
private final List<PluralForms> pluralForms;

public DutchLongToWordsConverter(
GenderAwareIntegerToStringConverter hundredsToWordsConverter,
List<PluralForms> pluralForms
) {
this.hundredsToWordsConverter = hundredsToWordsConverter;
this.pluralForms = pluralForms;
}

@Override
public String asWords(Long value) {
Assert.isTrue(value >= 0, () -> String.format("can't convert negative numbers for value %d", value));

List<String> result = new ArrayList<>();

long bigNumber = value / 1000000;
long smallNumber = value % 1000000;

if (bigNumber > 0) {
List<Integer> valueChunks = numberChunking.chunk(bigNumber);
List<PluralForms> formsToUse = getRequiredFormsInReversedOrder(valueChunks.size());
result.add(joinValueChunksWithForms(valueChunks.iterator(), formsToUse.iterator()));
}

if (smallNumber > 0) {
result.add(hundredsToWordsConverter.asWords((int) smallNumber, GenderType.NON_APPLICABLE));
}

return joinParts(result);
}

protected List<PluralForms> getRequiredFormsInReversedOrder(int chunks) {
List<PluralForms> formsToUse = new ArrayList<>(pluralForms.subList(0, chunks));
reverse(formsToUse);
return formsToUse;
}

protected String joinValueChunksWithForms(Iterator<Integer> chunks, Iterator<PluralForms> formsToUse) {
List<String> result = new ArrayList<>();

while (chunks.hasNext() && formsToUse.hasNext()) {
Integer currentChunkValue = chunks.next();
PluralForms currentForms = formsToUse.next();

if (currentChunkValue > 0) {
String words = hundredsToWordsConverter.asWords(currentChunkValue, currentForms.genderType());
result.add(words);
result.add(currentForms.formFor(currentChunkValue));
}
}

return joinParts(result);
}

protected String joinParts(List<String> result) {
return result.isEmpty()
? hundredsToWordsConverter.asWords(0, pluralForms.get(0).genderType())
: String.join(" ", result).trim();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.util.Map;

import pl.allegro.finance.tradukisto.internal.languages.GenderForms;
import pl.allegro.finance.tradukisto.internal.languages.GenderType;
import pl.allegro.finance.tradukisto.internal.languages.PluralForms;
import pl.allegro.finance.tradukisto.internal.languages.RegularPluralForms;

import static java.util.Collections.singletonMap;

Expand Down Expand Up @@ -63,7 +65,10 @@ public Map<Integer, String> exceptions() {
public List<PluralForms> pluralForms() {
return Arrays.asList(
new DutchPluralForms("miljoen"),
new DutchPluralForms("miljard")
new DutchPluralForms("miljard"),
new DutchPluralForms("biljoen"),
new DutchPluralForms("biljard"),
new DutchPluralForms("triljoen")
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.allegro.finance.tradukisto.internal.languages.dutch

import spock.lang.Ignore

import spock.lang.Specification
import spock.lang.Unroll

Expand Down Expand Up @@ -109,26 +109,27 @@ class DutchValuesTest extends Specification {
2147483647 | "twee miljard honderdzevenenveertig miljoen vierhonderddrieëntachtigduizendzeshonderdzevenenveertig"
}

@Ignore("Needs Dutch long converter and values for trillion, quadrillion, quintillion")
@Unroll
def "should convert long #value to '#words' in Dutch"() {
expect:
longConverter.asWords(value) == words

where:
value | words
5_000_000_000 | ""
5_000_000_000 | "vijf miljard"

1_000_000_000_000 | ""
2_000_000_000_000 | ""
5_000_000_000_000 | ""
1_000_000_000_000 | "één biljoen"
2_000_000_000_000 | "twee biljoen"
5_000_000_000_000 | "vijf biljoen"

1_000_000_000_000_000 | ""
2_000_000_000_000_000 | ""
5_000_000_000_000_000 | ""
1_000_000_000_000_000 | "één biljard"
2_000_000_000_000_000 | "twee biljard"
5_000_000_000_000_000 | "vijf biljard"

1_000_000_000_000_000_000 | ""
2_000_000_000_000_000_000 | ""
Long.MAX_VALUE | ""
1_000_000_000_000_000_000 | "één triljoen"
2_000_000_000_000_000_000 | "twee triljoen"
Long.MAX_VALUE | "negen triljoen tweehonderddrieëntwintig biljard " +
"driehonderdtweeënzeventig biljoen zesendertig miljard " +
"achthonderdvierenvijftig miljoen zevenhonderdvijfenzeventigduizendachthonderdzeven"
}
}

0 comments on commit 374f551

Please sign in to comment.