001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.util.common.string;
018
019import com.echothree.model.control.accounting.common.transfer.CurrencyTransfer;
020import com.google.common.math.IntMath;
021import java.util.Formatter;
022
023public class AmountUtils {
024
025    private AmountUtils() {
026        super();
027    }
028
029    private static class AmountUtilsHolder {
030        static AmountUtils instance = new AmountUtils();
031    }
032
033    public static AmountUtils getInstance() {
034        return AmountUtilsHolder.instance;
035    }
036
037    public String formatAmount(CurrencyTransfer currency, Integer amount) {
038        String result = null;
039
040        if(amount != null) {
041            int fractionDigits = currency.getDefaultFractionDigits();
042
043            if(fractionDigits == 0) {
044                result = amount.toString();
045            } else {
046                int rawAmount = amount;
047                int i = IntMath.checkedPow(10, fractionDigits);
048                StringBuilder builtResult = new StringBuilder().append(rawAmount / i).append(currency.getFractionSeparator());
049                StringBuilder formatString = new StringBuilder().append("%0").append(fractionDigits).append("d");
050
051                new Formatter(builtResult).format(formatString.toString(), Math.abs(rawAmount) % i);
052                result = builtResult.toString();
053            }
054        }
055
056        return result;
057    }
058
059    public String formatPriceUnit(CurrencyTransfer currency, Integer priceUnit) {
060        String result = null;
061
062        if(priceUnit != null) {
063            Integer priceUnitFractionDigits = currency.getPriceUnitFractionDigits();
064            int fractionDigits = priceUnitFractionDigits == null ? 0 : priceUnitFractionDigits;
065
066            if(fractionDigits == 0) {
067                result = priceUnit.toString();
068            } else {
069                int rawPriceUnit = priceUnit;
070                int i = IntMath.checkedPow(10, fractionDigits);
071                StringBuilder builtResult = new StringBuilder().append(rawPriceUnit / i).append(currency.getFractionSeparator());
072                StringBuilder formatString = new StringBuilder().append("%0").append(fractionDigits).append("d");
073
074                new Formatter(builtResult).format(formatString.toString(), Math.abs(rawPriceUnit) % i);
075                result = builtResult.toString();
076            }
077        }
078
079        return result;
080    }
081
082    public String formatPriceLine(CurrencyTransfer currency, Integer priceLine) {
083        String result = null;
084
085        if(priceLine != null) {
086            Integer priceLineFractionDigits = currency.getPriceLineFractionDigits();
087            int fractionDigits = priceLineFractionDigits == null ? 0 : priceLineFractionDigits;
088
089            if(fractionDigits == 0) {
090                result = priceLine.toString();
091            } else {
092                int rawPriceLine = priceLine;
093                int i = IntMath.checkedPow(10, fractionDigits);
094                StringBuilder builtResult = new StringBuilder().append(rawPriceLine / i).append(currency.getFractionSeparator());
095                StringBuilder formatString = new StringBuilder().append("%0").append(fractionDigits).append('d');
096
097                new Formatter(builtResult).format(formatString.toString(), Math.abs(rawPriceLine) % i);
098                result = builtResult.toString();
099            }
100        }
101
102        return result;
103    }
104
105    public String formatCostUnit(CurrencyTransfer currency, Integer costUnit) {
106        String result = null;
107
108        if(costUnit != null) {
109            Integer costUnitFractionDigits = currency.getCostUnitFractionDigits();
110            int fractionDigits = costUnitFractionDigits == null ? 0 : costUnitFractionDigits;
111
112            if(fractionDigits == 0) {
113                result = costUnit.toString();
114            } else {
115                int rawCostUnit = costUnit;
116                int i = IntMath.checkedPow(10, fractionDigits);
117                StringBuilder builtResult = new StringBuilder().append(rawCostUnit / i).append(currency.getFractionSeparator());
118                StringBuilder formatString = new StringBuilder().append("%0").append(fractionDigits).append('d');
119
120                new Formatter(builtResult).format(formatString.toString(), Math.abs(rawCostUnit) % i);
121                result = builtResult.toString();
122            }
123        }
124
125        return result;
126    }
127
128    public String formatCostLine(CurrencyTransfer currency, Integer costLine) {
129        String result = null;
130
131        if(costLine != null) {
132            Integer costLineFractionDigits = currency.getCostLineFractionDigits();
133            int fractionDigits = costLineFractionDigits == null ? 0 : costLineFractionDigits;
134
135            if(fractionDigits == 0) {
136                result = costLine.toString();
137            } else {
138                int rawCostLine = costLine;
139                int i = IntMath.checkedPow(10, fractionDigits);
140                StringBuilder builtResult = new StringBuilder().append(rawCostLine / i).append(currency.getFractionSeparator());
141                StringBuilder formatString = new StringBuilder().append("%0").append(fractionDigits).append('d');
142
143                new Formatter(builtResult).format(formatString.toString(), Math.abs(rawCostLine) % i);
144                result = builtResult.toString();
145            }
146        }
147
148        return result;
149    }
150
151    public AmountFormatter getAmountFormatter(CurrencyTransfer currency, AmountFormatType amountFormatType) {
152        return new AmountFormatter(currency, amountFormatType);
153    }
154    
155}