-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add japanese test * Add Japanese integer support * Add Japanese long support * Add Japanese long support * Add JapaneseNumberChunkingTest * Add Japanese BigDecimal support * Fix JapaneseNumberChunkingTest * Fix README for Japanese support * Fix Container name
- Loading branch information
Showing
15 changed files
with
413 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...llegro/finance/tradukisto/internal/languages/japanese/JapaneseNumberToWordsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.japanese; | ||
|
||
import pl.allegro.finance.tradukisto.internal.converters.NumberToWordsConverter; | ||
import pl.allegro.finance.tradukisto.internal.languages.PluralForms; | ||
import pl.allegro.finance.tradukisto.internal.support.Assert; | ||
import pl.allegro.finance.tradukisto.internal.support.JapaneseNumberChunking; | ||
import pl.allegro.finance.tradukisto.internal.support.NumberChunking; | ||
|
||
import java.util.List; | ||
|
||
public class JapaneseNumberToWordsConverter extends NumberToWordsConverter { | ||
|
||
private final NumberChunking numberChunking = new JapaneseNumberChunking(); | ||
|
||
private final List<PluralForms> pluralForms; | ||
|
||
public JapaneseNumberToWordsConverter(JapaneseThousandToWordsConverter japaneseThousandToWordsConverter, List<PluralForms> pluralForms) { | ||
super(japaneseThousandToWordsConverter, pluralForms); | ||
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<Integer> valueChunks = numberChunking.chunk(value); | ||
List<PluralForms> formsToUse = getRequiredFormsInReversedOrder(valueChunks.size()); | ||
|
||
return joinValueChunksWithForms(valueChunks.iterator(), formsToUse.iterator()); | ||
} | ||
|
||
@Override | ||
protected String joinParts(List<String> result) { | ||
return result.isEmpty() | ||
? hundredsToWordsConverter.asWords(0, pluralForms.get(0).genderType()) | ||
: String.join("", result).trim(); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...n/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapanesePluralForms.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.japanese; | ||
|
||
import pl.allegro.finance.tradukisto.internal.languages.GenderType; | ||
import pl.allegro.finance.tradukisto.internal.languages.PluralForms; | ||
|
||
public class JapanesePluralForms implements PluralForms { | ||
|
||
private final String form; | ||
|
||
public JapanesePluralForms(String form) { | ||
this.form = form; | ||
} | ||
|
||
@Override | ||
public String formFor(Integer value) { | ||
return form; | ||
} | ||
|
||
@Override | ||
public GenderType genderType() { | ||
return GenderType.NON_APPLICABLE; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...egro/finance/tradukisto/internal/languages/japanese/JapaneseThousandToWordsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.japanese; | ||
|
||
import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter; | ||
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 JapaneseThousandToWordsConverter implements IntegerToStringConverter { | ||
|
||
private final Map<Integer, GenderForms> baseValues; | ||
|
||
public JapaneseThousandToWordsConverter(Map<Integer, GenderForms> baseValues) { | ||
this.baseValues = baseValues; | ||
} | ||
|
||
@Override | ||
public String asWords(Integer value) { | ||
if (baseValues.containsKey(value)) { | ||
return baseValues.get(value).formFor(GenderType.NON_APPLICABLE); | ||
} | ||
if (Range.closed(11, 99).contains(value)) { | ||
return twoDigitsNumberAsString(value); | ||
} | ||
if (Range.closed(101, 999).contains(value)) { | ||
return threeDigitsNumberAsString(value); | ||
} | ||
if (Range.closed(1001, 9999).contains(value)) { | ||
return fourDigitsNumberAsString(value); | ||
} | ||
throw new IllegalArgumentException(format("Can't convert %d", value)); | ||
} | ||
|
||
private String twoDigitsNumberAsString(Integer value) { | ||
Integer units = value % 10; | ||
Integer tens = value - units; | ||
return format("%s%s", asWords(tens), asWords(units)); | ||
} | ||
|
||
private String threeDigitsNumberAsString(Integer value) { | ||
Integer units = value % 100; | ||
Integer hundreds = value - units; | ||
return format("%s%s", asWords(hundreds), asWords(units)); | ||
} | ||
|
||
private String fourDigitsNumberAsString(Integer value) { | ||
Integer units = value % 1000; | ||
Integer thousands = value - units; | ||
return format("%s%s", asWords(thousands), asWords(units)); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package pl.allegro.finance.tradukisto.internal.languages.japanese; | ||
|
||
import pl.allegro.finance.tradukisto.internal.BaseValues; | ||
import pl.allegro.finance.tradukisto.internal.languages.GenderForms; | ||
import pl.allegro.finance.tradukisto.internal.languages.PluralForms; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static pl.allegro.finance.tradukisto.internal.support.BaseNumbersBuilder.baseNumbersBuilder; | ||
|
||
public class JapaneseValues implements BaseValues { | ||
|
||
@Override | ||
public Map<Integer, GenderForms> baseNumbers() { | ||
return baseNumbersBuilder() | ||
.put(0, "零") | ||
.put(1, "一") | ||
.put(2, "二") | ||
.put(3, "三") | ||
.put(4, "四") | ||
.put(5, "五") | ||
.put(6, "六") | ||
.put(7, "七") | ||
.put(8, "八") | ||
.put(9, "九") | ||
.put(10, "十") | ||
.put(20, "二十") | ||
.put(30, "三十") | ||
.put(40, "四十") | ||
.put(50, "五十") | ||
.put(60, "六十") | ||
.put(70, "七十") | ||
.put(80, "八十") | ||
.put(90, "九十") | ||
.put(100, "百") | ||
.put(200, "二百") | ||
.put(300, "三百") | ||
.put(400, "四百") | ||
.put(500, "五百") | ||
.put(600, "六百") | ||
.put(700, "七百") | ||
.put(800, "八百") | ||
.put(900, "九百") | ||
.put(1000, "千") | ||
.put(2000, "二千") | ||
.put(3000, "三千") | ||
.put(4000, "四千") | ||
.put(5000, "五千") | ||
.put(6000, "六千") | ||
.put(7000, "七千") | ||
.put(8000, "八千") | ||
.put(9000, "九千") | ||
.build(); | ||
} | ||
|
||
@Override | ||
public List<PluralForms> pluralForms() { | ||
return Arrays.asList( | ||
new JapanesePluralForms(""), | ||
new JapanesePluralForms("万"), | ||
new JapanesePluralForms("億"), | ||
new JapanesePluralForms("兆"), | ||
new JapanesePluralForms("京") | ||
); | ||
} | ||
|
||
@Override | ||
public String currency() { | ||
return "円"; | ||
} | ||
|
||
@Override | ||
public char twoDigitsNumberSeparator() { | ||
return ' '; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunking.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pl.allegro.finance.tradukisto.internal.support; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class JapaneseNumberChunking extends NumberChunking { | ||
|
||
private static final int JAPANESE_SPLIT_FACTOR = 1_0000; | ||
|
||
@Override | ||
public List<Integer> chunk(Long value) { | ||
LinkedList<Integer> result = new LinkedList<>(); | ||
|
||
while (value > 0) { | ||
result.addFirst((int) (value % JAPANESE_SPLIT_FACTOR)); | ||
value /= JAPANESE_SPLIT_FACTOR; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.