All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.optionfactory.spring.marshaling.jaxb.money.XsdDecimalToLongCents Maven / Gradle / Ivy

There is a newer version: 19.3
Show newest version
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);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy