1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.oceanus.decimal;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusLocale;
20
21 import java.text.DecimalFormat;
22 import java.text.DecimalFormatSymbols;
23 import java.text.NumberFormat;
24 import java.util.Currency;
25 import java.util.HashMap;
26 import java.util.Locale;
27 import java.util.Map;
28
29
30
31
32 public class OceanusDecimalLocale {
33
34
35
36 private static final String DOLLAR = "$";
37
38
39
40
41 private static final String POUND = "£";
42
43
44
45
46 private final Locale theLocale;
47
48
49
50
51 private final Map<String, Currency> theCurrencyMap;
52
53
54
55
56 private final Map<String, String> theSymbolMap;
57
58
59
60
61 private final int theGroupingSize;
62
63
64
65
66 private final String theGrouping;
67
68
69
70
71 private final char theMinusSign;
72
73
74
75
76 private final char thePerCent;
77
78
79
80
81 private final char thePerMille;
82
83
84
85
86 private final String theDecimal;
87
88
89
90
91 private final String theMoneyDecimal;
92
93
94
95
96 private final Currency theCurrency;
97
98
99
100
101 protected OceanusDecimalLocale() {
102
103 this(OceanusLocale.getDefaultLocale());
104 }
105
106
107
108
109
110
111 protected OceanusDecimalLocale(final Locale pLocale) {
112
113 DecimalFormatSymbols mySymbols = DecimalFormatSymbols.getInstance(pLocale);
114 final Currency myCurrency = mySymbols.getCurrency();
115 theLocale = myCurrency.getDefaultFractionDigits() == -1
116 ? Locale.UK
117 : pLocale;
118
119
120 theCurrencyMap = new HashMap<>();
121 theSymbolMap = new HashMap<>();
122
123
124 mySymbols = DecimalFormatSymbols.getInstance(theLocale);
125 final DecimalFormat myFormat = (DecimalFormat) NumberFormat.getInstance(theLocale);
126 theGroupingSize = myFormat.getGroupingSize();
127
128
129 theMinusSign = mySymbols.getMinusSign();
130 thePerCent = mySymbols.getPercent();
131 thePerMille = mySymbols.getPerMill();
132 theGrouping = Character.toString(mySymbols.getGroupingSeparator());
133 theDecimal = Character.toString(mySymbols.getDecimalSeparator());
134 theMoneyDecimal = Character.toString(mySymbols.getMonetaryDecimalSeparator());
135
136
137 theCurrency = mySymbols.getCurrency();
138 final String myCurrSymbol = theCurrency.getSymbol(theLocale);
139 declareSymbol(myCurrSymbol, theCurrency);
140
141
142 if (!DOLLAR.equals(myCurrSymbol)) {
143 declareSymbol(DOLLAR, Currency.getInstance(Locale.US));
144 }
145 if (!POUND.equals(myCurrSymbol)) {
146 declareSymbol(POUND, Currency.getInstance(Locale.UK));
147 }
148 }
149
150
151
152
153
154
155 protected int getGroupingSize() {
156 return theGroupingSize;
157 }
158
159
160
161
162
163
164 protected String getGrouping() {
165 return theGrouping;
166 }
167
168
169
170
171
172
173 protected char getMinusSign() {
174 return theMinusSign;
175 }
176
177
178
179
180
181
182 protected char getPerCent() {
183 return thePerCent;
184 }
185
186
187
188
189
190
191 protected char getPerMille() {
192 return thePerMille;
193 }
194
195
196
197
198
199
200 protected String getDecimal() {
201 return theDecimal;
202 }
203
204
205
206
207
208
209 protected String getMoneyDecimal() {
210 return theMoneyDecimal;
211 }
212
213
214
215
216
217
218 protected Currency getDefaultCurrency() {
219 return theCurrency;
220 }
221
222
223
224
225
226
227
228
229 protected Currency parseCurrencySymbol(final String pSymbol) {
230
231 Currency myCurrency = theCurrencyMap.get(pSymbol);
232
233
234 if (myCurrency == null) {
235
236 for (Currency myCurr : Currency.getAvailableCurrencies()) {
237
238 if (pSymbol.equals(myCurr.getSymbol(theLocale))) {
239
240 myCurrency = myCurr;
241 declareSymbol(pSymbol, myCurrency);
242 break;
243 }
244 }
245
246
247 if (myCurrency == null) {
248
249 throw new IllegalArgumentException("Invalid currency: "
250 + pSymbol);
251 }
252 }
253
254
255 return myCurrency;
256 }
257
258
259
260
261
262
263
264 private void declareSymbol(final String pSymbol,
265 final Currency pCurrency) {
266
267 theCurrencyMap.put(pSymbol, pCurrency);
268
269
270 final String myCode = pCurrency.getCurrencyCode();
271 theSymbolMap.computeIfAbsent(myCode, c -> pSymbol);
272 }
273
274
275
276
277
278
279
280 protected String getSymbol(final Currency pCurrency) {
281
282 final String mySymbol = theSymbolMap.get(pCurrency.getCurrencyCode());
283 return mySymbol == null
284 ? pCurrency.getSymbol(theLocale)
285 : mySymbol;
286 }
287 }