net.optionfactory.spring.marshaling.jaxb.money.XsdDecimalToLongCents Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of marshaling-jaxb Show documentation
Show all versions of marshaling-jaxb Show documentation
optionfactory-spring jaxb time adapters
package net.optionfactory.spring.marshaling.jaxb.money;
import jakarta.xml.bind.annotation.adapters.XmlAdapter;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
public class XsdDecimalToLongCents extends XmlAdapter {
private static final DecimalFormatSymbols XSD_DECIMAL_SYMBOLS = new DecimalFormatSymbols();
static {
XSD_DECIMAL_SYMBOLS.setDecimalSeparator('.');
}
@Override
public Long unmarshal(String value) {
if (value == null) {
return null;
}
final var decimalFormat = new DecimalFormat("0.##", XSD_DECIMAL_SYMBOLS);
decimalFormat.setParseBigDecimal(true);
try {
final BigDecimal parsed = (BigDecimal) decimalFormat.parse(value);
return parsed.movePointRight(2).longValue();
} catch (ParseException ex) {
throw new IllegalArgumentException(String.format("Unparseable decimal value: %s", value));
}
}
@Override
public String marshal(Long cents) {
if (cents == null) {
return null;
}
final var bd = new BigDecimal(cents).movePointLeft(2);
return new DecimalFormat("0.##", XSD_DECIMAL_SYMBOLS).format(bd);
}
}