io.github.osamabmaq.tafqeetj.converters.IntegerToWordsConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TafqeetJ Show documentation
Show all versions of TafqeetJ Show documentation
A Java library to convert numbers from digital format to words format in Arabic language.
The newest version!
package io.github.osamabmaq.tafqeetj.converters;
import io.github.osamabmaq.tafqeetj.exceptions.NumberOutOfRangeException;
import io.github.osamabmaq.tafqeetj.numbers.ThreeDigitsNumber;
import io.github.osamabmaq.tafqeetj.numbers.NumberSign;
import java.math.BigInteger;
import java.util.Map;
class IntegerToWordsConverter {
private final NumberToThreeDigitsNumberGroupsDivider divider = new NumberToThreeDigitsNumberGroupsDivider();
private static IntegerToWordsConverter instance;
private IntegerToWordsConverter() {
}
public synchronized static IntegerToWordsConverter getInstance() {
if (instance == null)
instance = new IntegerToWordsConverter();
return instance;
}
public IntegerInWords convert(int number) {
return convert((long) number);
}
public IntegerInWords convert(byte number) {
return convert((long) number);
}
public IntegerInWords convert(short number) {
return convert((long) number);
}
public IntegerInWords convert(BigInteger number) {
if (TafqeetRangeChecker.isOutOfRange(number))
throw new NumberOutOfRangeException(number);
return convert(number.longValue());
}
public IntegerInWords convert(long number) {
if (TafqeetRangeChecker.isOutOfRange(number))
throw new NumberOutOfRangeException(number);
if (number == 0)
return IntegerInWords.createZero();
IntegerInWords convertedNumber = convertNumber(divider.divide(number));
if (number < 0)
convertedNumber.setSign(NumberSign.NEGATIVE);
return convertedNumber;
}
private IntegerInWords convertNumber(Map threeDigitsNumberMap) {
IntegerInWords integerInWords = new IntegerInWords();
integerInWords.setTrillions(convertThreeDigitsNumberIfPresent("trillions", threeDigitsNumberMap));
integerInWords.setBillions(convertThreeDigitsNumberIfPresent("billions", threeDigitsNumberMap));
integerInWords.setMillions(convertThreeDigitsNumberIfPresent("millions", threeDigitsNumberMap));
integerInWords.setThousands(convertThreeDigitsNumberIfPresent("thousands", threeDigitsNumberMap));
integerInWords.setBasicNumberGroup(convertBasicNumberIfPresent(threeDigitsNumberMap));
return integerInWords;
}
private String convertBasicNumberIfPresent(Map threeDigitsNumberMap) {
if (threeDigitsNumberMap.containsKey("basicNumberGroup")) {
ThreeDigitsNumber number = threeDigitsNumberMap.get("basicNumberGroup");
return number.getSign()
.getBasicNumberConverter()
.convert(number);
}
return "";
}
private String convertThreeDigitsNumberIfPresent(String numberGroupName,
Map threeDigitsNumberMap) {
if (threeDigitsNumberMap.containsKey(numberGroupName)) {
ThreeDigitsNumber number = threeDigitsNumberMap.get(numberGroupName);
return number.getSign()
.getQuantitiveNumberConverterFactory()
.getConverter(numberGroupName)
.convert(number);
}
return "";
}
}