Commit b67f4812 authored by Vincent Calame's avatar Vincent Calame

Création d'un paquet Money

parent 6da752cf
......@@ -10,10 +10,10 @@
package net.mapeadores.opendocument.io;
import java.io.IOException;
import net.mapeadores.util.primitives.Amount;
import net.mapeadores.util.money.Amount;
import net.mapeadores.util.primitives.CurrencyInteger;
import net.mapeadores.util.primitives.FuzzyDate;
import net.mapeadores.util.primitives.MoneyLong;
import net.mapeadores.util.money.MoneyLong;
import net.mapeadores.util.text.StringUtils;
import net.mapeadores.util.xml.XMLWriter;
......@@ -272,10 +272,11 @@ public final class OdXML {
}
public static void addCurrencyTableCell(XMLWriter xmlWriter, long montant, int currencyInteger, String cellStyleName, int colSpan) throws IOException {
int fractionDigits = CurrencyInteger.getSubunitLength(currencyInteger);
xmlWriter.startOpenTag("table:table-cell");
xmlWriter.addAttribute("office:value-type", "currency");
xmlWriter.addAttribute("office:currency", CurrencyInteger.toString(currencyInteger));
xmlWriter.addAttribute("office:value", MoneyLong.toDecimal(montant, currencyInteger, false).toString());
xmlWriter.addAttribute("office:value", MoneyLong.toDecimal(montant, fractionDigits, false).toString());
xmlWriter.addAttribute("table:style-name", cellStyleName);
if (colSpan > 1) {
xmlWriter.addAttribute("table:number-columns-spanned", colSpan);
......
......@@ -13,7 +13,7 @@ import java.math.BigDecimal;
import net.mapeadores.util.primitives.CurrencyInteger;
import net.mapeadores.util.primitives.CurrencyIntegerException;
import net.mapeadores.util.primitives.Decimal;
import net.mapeadores.util.primitives.MoneyUtils;
import net.mapeadores.util.money.MoneyUtils;
import net.mapeadores.util.text.StringUtils;
......
/* UtilLib - Copyright (c) 2016 Vincent Calame - Exemole
/* UtilLib - Copyright (c) 2016-2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
......@@ -7,10 +7,15 @@
*/
package net.mapeadores.util.primitives;
package net.mapeadores.util.money;
import net.mapeadores.util.money.MoneyUtils;
import net.mapeadores.util.money.MoneyLong;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import net.mapeadores.util.primitives.CurrencyInteger;
import net.mapeadores.util.primitives.CurrencyIntegerException;
import net.mapeadores.util.primitives.Decimal;
import net.mapeadores.util.text.StringUtils;
......@@ -22,10 +27,18 @@ public final class Amount {
private final long moneyLong;
private final int currencyInteger;
private final int fractionDigits;
public Amount(long moneyLong, int currencyInteger) {
this.moneyLong = moneyLong;
this.currencyInteger = currencyInteger;
this.fractionDigits = CurrencyInteger.getSubunitLength(currencyInteger);
}
private Amount(long moneyLong, int currencyInteger, int fractionDigits) {
this.moneyLong = moneyLong;
this.currencyInteger = currencyInteger;
this.fractionDigits = fractionDigits;
}
public long getMoneyLong() {
......@@ -41,12 +54,12 @@ public final class Amount {
}
public Decimal toDecimal(boolean avoidDecimal) {
return MoneyLong.toDecimal(moneyLong, currencyInteger, avoidDecimal);
return MoneyLong.toDecimal(moneyLong, fractionDigits, avoidDecimal);
}
@Override
public String toString() {
return MoneyLong.toString(moneyLong, currencyInteger);
return MoneyLong.toString(moneyLong, fractionDigits);
}
public String toLitteralString(DecimalFormatSymbols symbols, boolean avoidDecimal) {
......@@ -54,7 +67,8 @@ public final class Amount {
}
public static Amount fromDecimal(Decimal decimal, int currencyInteger) {
return new Amount(MoneyLong.toMoneyLong(decimal, currencyInteger), currencyInteger);
int fractionDigits = CurrencyInteger.getSubunitLength(currencyInteger);
return new Amount(MoneyLong.toMoneyLong(decimal, fractionDigits), currencyInteger, fractionDigits);
}
public static Amount parse(String s) throws ParseException {
......
......@@ -7,7 +7,9 @@
*/
package net.mapeadores.util.primitives;
package net.mapeadores.util.money;
import net.mapeadores.util.primitives.Decimal;
/**
......
/* UtilLib - Copyright (c) 2012-2016 Vincent Calame - Exemole
/* UtilLib - Copyright (c) 2012-2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
......@@ -7,10 +7,12 @@
*/
package net.mapeadores.util.primitives;
package net.mapeadores.util.money;
import java.text.DecimalFormatSymbols;
import net.mapeadores.util.exceptions.ShouldNotOccurException;
import net.mapeadores.util.primitives.CurrencyInteger;
import net.mapeadores.util.primitives.Decimal;
/**
......@@ -22,16 +24,15 @@ public class MoneyLong {
private MoneyLong() {
}
public static long toMoneyLong(Decimal decimal, int currencyInteger) {
int multiplicator = CurrencyInteger.getSubunitMultiplicator(currencyInteger);
public static long toMoneyLong(Decimal decimal, int fractionDigits) {
int multiplicator = MoneyUtils.toMultiplicator(fractionDigits);
long lg = decimal.getPartieEntiere() * multiplicator;
int partieDecimale = decimal.getPartieDecimale();
if (partieDecimale == 0) {
return lg;
}
int subunitLength = CurrencyInteger.getSubunitLength(currencyInteger);
byte zeroLength = decimal.getZeroLength();
if (zeroLength > subunitLength) {
if (zeroLength > fractionDigits) {
return lg;
}
boolean neg = false;
......@@ -43,38 +44,36 @@ public class MoneyLong {
partieDecimale = -partieDecimale;
neg = true;
}
lg = lg + getDecimalPart(zeroLength, partieDecimale, subunitLength);
lg = lg + getDecimalPart(zeroLength, partieDecimale, fractionDigits);
if (neg) {
lg = -lg;
}
return lg;
}
public static Decimal toDecimal(long l, int currencyInteger, boolean avoidDecimal) {
int multiplicator = CurrencyInteger.getSubunitMultiplicator(currencyInteger);
int length = CurrencyInteger.getSubunitLength(currencyInteger);
public static Decimal toDecimal(long l, int fractionDigits, boolean avoidDecimal) {
int multiplicator = CurrencyInteger.getSubunitMultiplicator(fractionDigits);
long partieEntiere = l / multiplicator;
int partieDecimale = (int) (l % multiplicator);
byte zeroLength = 0;
if (partieDecimale == 0) {
if (!avoidDecimal) {
zeroLength = (byte) length;
zeroLength = (byte) fractionDigits;
}
} else if (Math.abs(partieDecimale) < multiplicator) {
String s = String.valueOf(Math.abs(partieDecimale));
zeroLength = (byte) (length - s.length());
zeroLength = (byte) (fractionDigits - s.length());
}
return new Decimal(partieEntiere, zeroLength, partieDecimale);
}
public static String toString(long moneyLong, int currencyInteger) {
int subunitLength = CurrencyInteger.getSubunitLength(currencyInteger);
public static String toString(long moneyLong, int fractionDigits) {
StringBuilder buf = new StringBuilder();
if (moneyLong == 0) {
buf.append('0');
if (subunitLength > 0) {
if (fractionDigits > 0) {
buf.append('.');
for (int i = 0; i < subunitLength; i++) {
for (int i = 0; i < fractionDigits; i++) {
buf.append('0');
}
}
......@@ -86,17 +85,17 @@ public class MoneyLong {
}
String s = Long.toString(moneyLong);
int longLength = s.length();
if (longLength <= subunitLength) {
if (longLength <= fractionDigits) {
buf.append('0');
} else {
int entierLength = longLength - subunitLength;
int entierLength = longLength - fractionDigits;
for (int i = 0; i < entierLength; i++) {
buf.append(s.charAt(i));
}
}
if (subunitLength > 0) {
if (fractionDigits > 0) {
buf.append('.');
int depart = (longLength - subunitLength);
int depart = (longLength - fractionDigits);
if (depart < 0) {
depart = -depart;
for (int i = 0; i < depart; i++) {
......@@ -111,22 +110,23 @@ public class MoneyLong {
return buf.toString();
}
public static String toLitteralString(long moneyLong, int currencyInteger, DecimalFormatSymbols symbols, boolean avoidDecimal) {
String result = toString(moneyLong, currencyInteger, symbols.getDecimalSeparator(), symbols.getGroupingSeparator(), avoidDecimal);
int fractionDigits = CurrencyInteger.getSubunitLength(currencyInteger);
String result = toString(moneyLong, fractionDigits, symbols.getDecimalSeparator(), symbols.getGroupingSeparator(), avoidDecimal);
result = CurrencyInteger.appendCurrencySymboltoString(result, currencyInteger);
result = result.replace(' ', '\u00A0');
return result;
}
public static String toString(long moneyLong, int currencyInteger, char decimalSeparator, char groupingSeparator, boolean avoidDecimal) {
int subunitLength = CurrencyInteger.getSubunitLength(currencyInteger);
public static String toString(long moneyLong, int fractionDigits, char decimalSeparator, char groupingSeparator, boolean avoidDecimal) {
StringBuilder buf = new StringBuilder();
if (moneyLong == 0) {
buf.append('0');
if (!avoidDecimal) {
if (subunitLength > 0) {
if (fractionDigits > 0) {
buf.append(decimalSeparator);
for (int i = 0; i < subunitLength; i++) {
for (int i = 0; i < fractionDigits; i++) {
buf.append('0');
}
}
......@@ -139,10 +139,10 @@ public class MoneyLong {
}
String s = Long.toString(moneyLong);
int longLength = s.length();
if (longLength <= subunitLength) {
if (longLength <= fractionDigits) {
buf.append('0');
} else {
int entierLength = longLength - subunitLength;
int entierLength = longLength - fractionDigits;
if (entierLength <= 3) {
for (int i = 0; i < entierLength; i++) {
buf.append(s.charAt(i));
......@@ -169,9 +169,9 @@ public class MoneyLong {
}
}
}
if (subunitLength > 0) {
if (fractionDigits > 0) {
boolean append = true;
int depart = (longLength - subunitLength);
int depart = (longLength - fractionDigits);
if (depart < 0) {
depart = -depart;
buf.append(decimalSeparator);
......
/* UtilLib - Copyright (c) 2013 Vincent Calame - Exemole
/* UtilLib - Copyright (c) 2013-2018 Vincent Calame - Exemole
* Logiciel libre donné sous triple licence :
* 1) selon les termes de la CeCILL V2
* 2) selon les termes de l’EUPL V.1.1
......@@ -7,9 +7,11 @@
*/
package net.mapeadores.util.primitives;
package net.mapeadores.util.money;
import net.mapeadores.util.exceptions.ShouldNotOccurException;
import net.mapeadores.util.primitives.CurrencyInteger;
import net.mapeadores.util.primitives.Decimal;
import net.mapeadores.util.text.StringUtils;
......@@ -136,6 +138,14 @@ public class MoneyUtils {
}
}
public static int toMultiplicator(int fractionDigits) {
int result = 1;
for (int i = 0; i < fractionDigits; i++) {
result = result * 10;
}
return result;
}
private static class SameMoneyConversion implements MoneyConversion {
......
......@@ -9,7 +9,7 @@
package net.mapeadores.util.table;
import net.mapeadores.util.primitives.Amount;
import net.mapeadores.util.money.Amount;
import net.mapeadores.util.primitives.Decimal;
import net.mapeadores.util.primitives.FuzzyDate;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment