public static Money sum()

in online_bontique_demo/common/src/main/java/org/apache/dubbo/shop/common/utils/MoneyUtils.java [24:47]


    public static Money sum(Money a, Money b) {
        if (!isValid(a) || !isValid(b)) {
            throw new IllegalArgumentException("Invalid money value");
        } else if (!a.getCurrencyCode().equals(b.getCurrencyCode())) {
            throw new IllegalArgumentException("Mismatching currency codes");
        }

        long units = a.getUnits() + b.getUnits();
        int nanos = a.getNanos() + b.getNanos();

        if ((units >= 0 && nanos >= 0) || (units < 0 && nanos <= 0)) {
            units += nanos / 1_000_000_000;
            nanos %= 1_000_000_000;
        } else {
            if (units > 0) {
                units--;
                nanos += 1_000_000_000;
            } else {
                units++;
                nanos -= 1_000_000_000;
            }
        }
        return new Money(a.getCurrencyCode(), units, nanos);
    }