Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#99 | add longConverter for Dutch #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
Loading