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

Add support from Greek References #117 #156

Merged
merged 3 commits into from
Jan 3, 2025
Merged
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 @@ -20,6 +20,10 @@
import pl.allegro.finance.tradukisto.internal.languages.german.GermanIntegerToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.german.GermanThousandToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.german.GermanValues;
import pl.allegro.finance.tradukisto.internal.languages.greek.GreekBigNumberToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.greek.GreekIntegerToWordsConverter;
import pl.allegro.finance.tradukisto.internal.languages.greek.GreekValues;
import pl.allegro.finance.tradukisto.internal.languages.greek.GreekValuesForSmallNumbers;
import pl.allegro.finance.tradukisto.internal.languages.hindi.HindiBigDecimalToBankingMoneyConverter;
import pl.allegro.finance.tradukisto.internal.languages.hindi.HindiValues;
import pl.allegro.finance.tradukisto.internal.languages.hindi.IndianNumberToWordsConverter;
Expand Down Expand Up @@ -131,6 +135,23 @@ public static Container czechContainer() {
return new Container(integerConverter, null, bigDecimalBankingMoneyValueConverter);
}

public static Container greekContainer() {
GreekValues values = new GreekValues();
GreekBigNumberToWordsConverter containerForBigNumbers = new GreekBigNumberToWordsConverter(values.baseNumbers());
Container containerForSmallNumbers = new Container(new GreekValuesForSmallNumbers());

IntegerToStringConverter integerConverter = new GreekIntegerToWordsConverter(
new NumberToWordsConverter(containerForBigNumbers, values.pluralForms()),
containerForSmallNumbers.getIntegerConverter(),
values.exceptions()
);

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

return new Container(integerConverter, null, bigDecimalBankingMoneyValueConverter);
}

public static Container slovakContainer() {
SlovakValues slovakValues = new SlovakValues();
Container containerForBigNumbers = new Container(slovakValues);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package pl.allegro.finance.tradukisto.internal.languages.greek;

import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter;
import pl.allegro.finance.tradukisto.internal.languages.GenderForms;
import pl.allegro.finance.tradukisto.internal.languages.GenderType;
import pl.allegro.finance.tradukisto.internal.support.Range;

import java.util.Map;

import static java.lang.String.format;

public class GreekBigNumberToWordsConverter implements GenderAwareIntegerToStringConverter {

private final Map<Integer, GenderForms> baseValues;

public GreekBigNumberToWordsConverter(Map<Integer, GenderForms> baseValues) {
this.baseValues = baseValues;
}

@Override
public String asWords(Integer value, GenderType genderType) {
if (baseValues.containsKey(value)) {
return baseValues.get(value).formFor(genderType);
}
if (Range.closed(21, 99).contains(value)) {
return twoDigitsNumberAsString(value, genderType);
}
if (Range.closed(101, 999).contains(value)) {
return threeDigitsNumberAsString(value, genderType);
}
if (Range.closed(1000, 999999).contains(value)) {
return thousandsAsString(value, genderType);
}

throw new IllegalArgumentException(format("Can't convert %d", value));
}

private String twoDigitsNumberAsString(Integer value, GenderType genderType) {
Integer units = value % 10;
Integer tens = value - units;
return format("%s %s", asWords(tens, genderType), asWords(units, genderType));
}

private String threeDigitsNumberAsString(Integer value, GenderType genderType) {
Integer tensWithUnits = value % 100;
Integer hundreds = value - tensWithUnits;
GenderType thousandsGenderType = genderType;
if (hundreds / 100 == 1 && tensWithUnits > 0)
thousandsGenderType = GenderType.FEMININE;
return format("%s %s", asWords(hundreds, thousandsGenderType), asWords(tensWithUnits, genderType));
}

private String thousandsAsString(Integer value, GenderType genderType) {
Integer thousands = value / 1000;
Integer other = value % 1000;

if (other == 0) {
return format("%s", asWords(thousands, genderType));
}
return format("%s %s", asWords(thousands, genderType), asWords(other, genderType));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package pl.allegro.finance.tradukisto.internal.languages.greek;

import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter;
import pl.allegro.finance.tradukisto.internal.LongToStringConverter;
import pl.allegro.finance.tradukisto.internal.NumberProcessor;

import java.util.Map;

public class GreekIntegerToWordsConverter implements IntegerToStringConverter, LongToStringConverter {
private final Map<Integer, String> exceptions;

private final NumberProcessor numberProcessor;

public GreekIntegerToWordsConverter(
IntegerToStringConverter bigNumbersConverter,
IntegerToStringConverter smallNumbersConverter,
Map<Integer, String> exceptions
) {
this.numberProcessor = new NumberProcessor(bigNumbersConverter, smallNumbersConverter);
this.exceptions = exceptions;
}

@Override
public String asWords(Integer value) {
if (exceptions.containsKey(value)) {
return exceptions.get(value);
}

Integer bigNumber = value / 1000;
Integer smallNumber = value % 1000;

return numberProcessor.process(bigNumber, smallNumber);
}

@Override
public String asWords(Long value) {
if (exceptions.containsKey(value)) {
return exceptions.get(value);
}

Integer bigNumber = value.intValue() / 1000;
Integer smallNumber = value.intValue() % 1000;

return numberProcessor.process(bigNumber, smallNumber);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pl.allegro.finance.tradukisto.internal.languages.greek;

import pl.allegro.finance.tradukisto.internal.languages.GenderType;
import pl.allegro.finance.tradukisto.internal.languages.PluralForms;
import pl.allegro.finance.tradukisto.internal.support.Range;

public class GreekPluralForms implements PluralForms {

private final String singularForm;
private final String pluralForm;
private final String genitivePluralForm;
private final GenderType genderType;

public GreekPluralForms(String singularForm, String pluralForm, String genitivePluralForm) {
this(singularForm, pluralForm, genitivePluralForm, GenderType.NON_APPLICABLE);
}

public GreekPluralForms(String singularForm, String pluralForm, String genitivePluralForm, GenderType genderType) {
this.singularForm = singularForm;
this.pluralForm = pluralForm;
this.genitivePluralForm = genitivePluralForm;
this.genderType = genderType;
}

@Override
public String formFor(Integer value) {
if (value == 1) {
return singularForm;
}
if (Range.closed(3, 4).contains(value)) {
return genitivePluralForm;
}
return pluralForm;
}

@Override
public GenderType genderType() {
return genderType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package pl.allegro.finance.tradukisto.internal.languages.greek;

import pl.allegro.finance.tradukisto.internal.BaseValues;
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 java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForms;
import static pl.allegro.finance.tradukisto.internal.support.BaseNumbersBuilder.baseNumbersBuilder;

public class GreekValues implements BaseValues {

@Override
public Map<Integer, GenderForms> baseNumbers() {
return baseNumbersBuilder()
.put(0, "μηδέν")
.put(1, genderForms("ένα", "μία", "ένας", "μία"))
.put(2, "δύο")
.put(3, genderForms("τρία", "τρεις", "τρεις", "τρεις"))
.put(4, genderForms("τέσσερα", "τέσσερεις", "τέσσερεις", "τέσσερεις"))
.put(5, "πέντε")
.put(6, "έξι")
.put(7, "επτά")
.put(8, "οκτώ")
.put(9, "εννέα")
.put(10, "δέκα")
.put(11, "έντεκα")
.put(12, "δώδεκα")
.put(13, "δεκατρία")
.put(14, "δεκατέσσερα")
.put(15, "δεκαπέντε")
.put(16, "δεκαέξι")
.put(17, "δεκαεπτά")
.put(18, "δεκαοκτώ")
.put(19, "δεκαεννέα")
.put(20, "είκοσι")
.put(30, "τριάντα")
.put(40, "σαράντα")
.put(50, "πενήντα")
.put(60, "εξήντα")
.put(70, "εβδομήντα")
.put(80, "ογδόντα")
.put(90, "ενενήντα")
.put(100, genderForms("εκατό", "εκατόν", "εκατόν", "εκατόν"))
.put(200, genderForms("διακόσια", "διακόσιες", "διακόσιες", "διακόσια"))
.put(300, genderForms("τριακόσια", "τριακόσιες", "τριακόσιες", "τριακόσια"))
.put(400, genderForms("τετρακόσια", "τετρακόσιες", "τετρακόσιες", "τετρακόσια"))
.put(500, genderForms("πεντακόσια", "πεντακόσιες", "πεντακόσιες", "πεντακόσια"))
.put(600, genderForms("εξακόσια", "εξακόσιες", "εξακόσιες", "εξακόσια"))
.put(700, genderForms("επτακόσια", "επτακόσιες", "επτακόσιες", "επτακόσια"))
.put(800, genderForms("οκτακόσια", "οκτακόσιες", "οκτακόσιες", "οκτακόσια"))
.put(900, genderForms("εννιακόσια", "εννιακόσιες", "εννιακόσιες", "εννιακόσια"))
.build();
}

@Override
public List<PluralForms> pluralForms() {
return Arrays.asList(
new GreekPluralForms("χίλια", "χιλιάδες", "χιλιάδες", GenderType.FEMININE),
new GreekPluralForms("εκατομμύριο", "εκατομμύρια", "εκατομμύρια", GenderType.MASCULINE),
new GreekPluralForms("δισεκατομμύριο", "δισεκατομμύρια", "εκατομμύρια", GenderType.MASCULINE),
new GreekPluralForms("τρισεκατομμύριο", "τρισεκατομμύρια", "εκατομμύρια", GenderType.MASCULINE),
new GreekPluralForms("τετράκις εκατομμύριο", "τετράκις εκατομμύρια", "τετράκις εκατομμύρια", GenderType.MASCULINE),
new GreekPluralForms("πεντάκις εκατομμύριο", "πεντάκις εκατομμύρια", "πεντάκις εκατομμύρια", GenderType.MASCULINE)
);
}

@Override
public String currency() {
return "€";
}

@Override
public char twoDigitsNumberSeparator() {
return ' ';
}

public Map<Integer, String> exceptions() {
Map<Integer, String> result = new HashMap<>();
result.put(100, "εκατό");
result.put(1000, "χίλια");
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package pl.allegro.finance.tradukisto.internal.languages.greek;

import pl.allegro.finance.tradukisto.internal.BaseValues;
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.czech.CzechPluralForms;
import pl.allegro.finance.tradukisto.internal.languages.czech.CzechValues;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForm;

public class GreekValuesForSmallNumbers implements BaseValues {
private final GreekValues originalValues = new GreekValues();

@Override
public Map<Integer, GenderForms> baseNumbers() {
Map<Integer, GenderForms> baseNumbers = originalValues.baseNumbers();
baseNumbers.put(1, genderForm(baseNumbers.get(1).formFor(GenderType.MASCULINE)));
baseNumbers.put(3, genderForm(baseNumbers.get(3).formFor(GenderType.MASCULINE)));
baseNumbers.put(4, genderForm(baseNumbers.get(4).formFor(GenderType.MASCULINE)));
return baseNumbers;
}

@Override
public List<PluralForms> pluralForms() {
return Arrays.asList(new GreekPluralForms("", "", "", GenderType.NON_APPLICABLE));
}

@Override
public String currency() {
return originalValues.currency();
}

@Override
public char twoDigitsNumberSeparator() {
return ' ';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.allegro.finance.tradukisto.internal.languages.greek

import pl.allegro.finance.tradukisto.internal.languages.GenderType
import spock.lang.Specification
import spock.lang.Unroll

class GreekPluralFormsTest extends Specification {

def pluralForms = new GreekPluralForms("χίλια", "χιλιάδες", "χιλιάδες", GenderType.FEMININE)

def "should provide value for singular form"() {
expect:
pluralForms.formFor(1) == "χίλια"
}

@Unroll
def "should provide value for plural form: #value"() {
expect:
pluralForms.formFor(value) == "χιλιάδες"

where:
value << (2..4)
}
}
Loading
Loading