-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Julia Glaszka <[email protected]>
- Loading branch information
1 parent
8e6d7b9
commit 15d5182
Showing
5 changed files
with
114 additions
and
19 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
81 changes: 81 additions & 0 deletions
81
...ava/pl/allegro/finance/tradukisto/internal/languages/dutch/DutchLongToWordsConverter.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,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(); | ||
} | ||
} |
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