LEFT | RIGHT |
1 // © 2017 and later: Unicode, Inc. and others. | 1 // © 2017 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 package com.ibm.icu.impl.number.formatters; | 3 package com.ibm.icu.impl.number.formatters; |
4 | 4 |
5 import java.math.BigDecimal; | 5 import java.math.BigDecimal; |
6 | 6 |
7 import com.ibm.icu.impl.StandardPlural; | 7 import com.ibm.icu.impl.StandardPlural; |
8 import com.ibm.icu.impl.number.AffixPatternUtils; | 8 import com.ibm.icu.impl.number.AffixPatternUtils; |
9 import com.ibm.icu.impl.number.PNAffixGenerator; | 9 import com.ibm.icu.impl.number.PNAffixGenerator; |
10 import com.ibm.icu.impl.number.PatternString; | 10 import com.ibm.icu.impl.number.PatternString; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 // If the user asked for ISO Code, return the ISO Code instead of the symbol | 128 // If the user asked for ISO Code, return the ISO Code instead of the symbol |
129 CurrencyStyle style = properties.getCurrencyStyle(); | 129 CurrencyStyle style = properties.getCurrencyStyle(); |
130 if (style == CurrencyStyle.ISO_CODE) { | 130 if (style == CurrencyStyle.ISO_CODE) { |
131 return getCurrencyIsoCode(symbols, properties); | 131 return getCurrencyIsoCode(symbols, properties); |
132 } | 132 } |
133 | 133 |
134 // Get the currency symbol | 134 // Get the currency symbol |
135 Currency currency = properties.getCurrency(); | 135 Currency currency = properties.getCurrency(); |
136 if (currency == null) { | 136 if (currency == null) { |
137 return symbols.getCurrencySymbol(); | 137 return symbols.getCurrencySymbol(); |
138 } | 138 } else if (currency.equals(symbols.getCurrency())) { |
139 return currency.getName(symbols.getULocale(), Currency.SYMBOL_NAME, null); | 139 // The user may have set a custom currency symbol in DecimalFormatSymbols. |
| 140 return symbols.getCurrencySymbol(); |
| 141 } else { |
| 142 // Use the canonical symbol. |
| 143 return currency.getName(symbols.getULocale(), Currency.SYMBOL_NAME, null); |
| 144 } |
140 } | 145 } |
141 | 146 |
142 /** | 147 /** |
143 * Returns the currency ISO code based on the input, like "USD". | 148 * Returns the currency ISO code based on the input, like "USD". |
144 * | 149 * |
145 * @param symbols The current {@link DecimalFormatSymbols} instance | 150 * @param symbols The current {@link DecimalFormatSymbols} instance |
146 * @param properties The current property bag | 151 * @param properties The current property bag |
147 * @return The currency ISO code string, e.g., to substitute '¤¤' in a decimal
pattern string. | 152 * @return The currency ISO code string, e.g., to substitute '¤¤' in a decimal
pattern string. |
148 */ | 153 */ |
149 public static String getCurrencyIsoCode( | 154 public static String getCurrencyIsoCode( |
150 DecimalFormatSymbols symbols, ICurrencyProperties properties) { | 155 DecimalFormatSymbols symbols, ICurrencyProperties properties) { |
151 Currency currency = properties.getCurrency(); | 156 Currency currency = properties.getCurrency(); |
152 if (currency == null) { | 157 if (currency == null) { |
153 // If a currency object was not provided, use the string from symbols | 158 // If a currency object was not provided, use the string from symbols |
154 // Note: symbols.getCurrency().getCurrencyCode() won't work here because | 159 // Note: symbols.getCurrency().getCurrencyCode() won't work here because |
155 // DecimalFormatSymbols#setInternationalCurrencySymbol() does not update t
he | 160 // DecimalFormatSymbols#setInternationalCurrencySymbol() does not update t
he |
156 // immutable internal currency instance. | 161 // immutable internal currency instance. |
157 return symbols.getInternationalCurrencySymbol(); | 162 return symbols.getInternationalCurrencySymbol(); |
158 } else { | 163 } else if (currency.equals(symbols.getCurrency())) { |
159 // If a currency object was provided, use it | 164 // The user may have set a custom currency symbol in DecimalFormatSymbols. |
| 165 return symbols.getInternationalCurrencySymbol(); |
| 166 } else { |
| 167 // Use the canonical currency code. |
160 return currency.getCurrencyCode(); | 168 return currency.getCurrencyCode(); |
161 } | 169 } |
162 } | 170 } |
163 | 171 |
164 /** | 172 /** |
165 * Returns the currency long name on the input, like "US dollars". | 173 * Returns the currency long name on the input, like "US dollars". |
166 * | 174 * |
167 * @param symbols The current {@link DecimalFormatSymbols} instance | 175 * @param symbols The current {@link DecimalFormatSymbols} instance |
168 * @param properties The current property bag | 176 * @param properties The current property bag |
169 * @param plural The plural form | 177 * @param plural The plural form |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 // CurrencyPluralInfo is available. Use it to generate affixes for long
name support. | 220 // CurrencyPluralInfo is available. Use it to generate affixes for long
name support. |
213 String pluralPattern = info.getCurrencyPluralPattern(plural.getKeyword()
); | 221 String pluralPattern = info.getCurrencyPluralPattern(plural.getKeyword()
); |
214 PatternString.parseToExistingProperties(pluralPattern, temp, true); | 222 PatternString.parseToExistingProperties(pluralPattern, temp, true); |
215 result = pnag.getModifiers(symbols, sym, iso, longName, temp); | 223 result = pnag.getModifiers(symbols, sym, iso, longName, temp); |
216 } | 224 } |
217 mod.put(plural, result.positive, result.negative); | 225 mod.put(plural, result.positive, result.negative); |
218 } | 226 } |
219 return mod; | 227 return mod; |
220 } | 228 } |
221 | 229 |
222 private static final ThreadLocal<Properties> threadLocalProperties = | |
223 new ThreadLocal<Properties>() { | |
224 @Override | |
225 protected Properties initialValue() { | |
226 return new Properties(); | |
227 } | |
228 }; | |
229 | |
230 private static final Currency DEFAULT_CURRENCY = Currency.getInstance("XXX"); | 230 private static final Currency DEFAULT_CURRENCY = Currency.getInstance("XXX"); |
231 | 231 |
232 public static Rounder getCurrencyRounder(DecimalFormatSymbols symbols, IProper
ties properties) { | 232 public static void populateCurrencyRounderProperties( |
| 233 Properties destination, DecimalFormatSymbols symbols, IProperties properti
es) { |
| 234 |
233 Currency currency = properties.getCurrency(); | 235 Currency currency = properties.getCurrency(); |
234 if (currency == null) { | 236 if (currency == null) { |
235 // Fall back to the DecimalFormatSymbols currency instance. | 237 // Fall back to the DecimalFormatSymbols currency instance. |
236 currency = symbols.getCurrency(); | 238 currency = symbols.getCurrency(); |
237 } | 239 } |
238 if (currency == null) { | 240 if (currency == null) { |
239 // There is a currency symbol in the pattern, but we have no currency avai
lable to use. | 241 // There is a currency symbol in the pattern, but we have no currency avai
lable to use. |
240 // Use the default currency instead so that we can still apply currency us
age rules. | 242 // Use the default currency instead so that we can still apply currency us
age rules. |
241 currency = DEFAULT_CURRENCY; | 243 currency = DEFAULT_CURRENCY; |
242 } | 244 } |
243 | 245 |
244 Currency.CurrencyUsage currencyUsage = properties.getCurrencyUsage(); | 246 Currency.CurrencyUsage currencyUsage = properties.getCurrencyUsage(); |
245 if (currencyUsage == null) { | 247 if (currencyUsage == null) { |
246 currencyUsage = CurrencyUsage.STANDARD; | 248 currencyUsage = CurrencyUsage.STANDARD; |
247 } | 249 } |
248 | 250 |
249 double incrementDouble = currency.getRoundingIncrement(currencyUsage); | 251 double incrementDouble = currency.getRoundingIncrement(currencyUsage); |
250 int fractionDigits = currency.getDefaultFractionDigits(currencyUsage); | 252 int fractionDigits = currency.getDefaultFractionDigits(currencyUsage); |
251 | 253 |
252 Properties cprops = threadLocalProperties.get().clear(); | 254 destination.setRoundingMode(properties.getRoundingMode()); |
253 cprops.setRoundingMode(properties.getRoundingMode()); | 255 destination.setMinimumIntegerDigits(properties.getMinimumIntegerDigits()); |
254 cprops.setMinimumIntegerDigits(properties.getMinimumIntegerDigits()); | 256 destination.setMaximumIntegerDigits(properties.getMaximumIntegerDigits()); |
255 cprops.setMaximumIntegerDigits(properties.getMaximumIntegerDigits()); | |
256 | 257 |
257 int _minFrac = properties.getMinimumFractionDigits(); | 258 int _minFrac = properties.getMinimumFractionDigits(); |
258 if (_minFrac < 0) { | |
259 cprops.setMinimumFractionDigits(fractionDigits); | |
260 } else { | |
261 cprops.setMinimumFractionDigits(_minFrac); | |
262 } | |
263 int _maxFrac = properties.getMaximumFractionDigits(); | 259 int _maxFrac = properties.getMaximumFractionDigits(); |
264 if (_maxFrac < 0) { | 260 if (_minFrac >= 0 || _maxFrac >= 0) { |
265 cprops.setMaximumFractionDigits(fractionDigits); | 261 // User override |
266 } else { | 262 destination.setMinimumFractionDigits(_minFrac); |
267 cprops.setMaximumFractionDigits(_maxFrac); | 263 destination.setMaximumFractionDigits(_maxFrac); |
| 264 } else { |
| 265 destination.setMinimumFractionDigits(fractionDigits); |
| 266 destination.setMaximumFractionDigits(fractionDigits); |
268 } | 267 } |
269 | 268 |
270 if (incrementDouble > 0.0) { | 269 if (incrementDouble > 0.0) { |
271 BigDecimal incrementBigDecimal; | 270 BigDecimal incrementBigDecimal; |
272 BigDecimal _roundingIncrement = properties.getRoundingIncrement(); | 271 BigDecimal _roundingIncrement = properties.getRoundingIncrement(); |
273 if (_roundingIncrement != null) { | 272 if (_roundingIncrement != null) { |
274 incrementBigDecimal = _roundingIncrement; | 273 incrementBigDecimal = _roundingIncrement; |
275 } else { | 274 } else { |
276 incrementBigDecimal = BigDecimal.valueOf(incrementDouble); | 275 incrementBigDecimal = BigDecimal.valueOf(incrementDouble); |
277 } | 276 } |
278 cprops.setRoundingIncrement(incrementBigDecimal); | 277 destination.setRoundingIncrement(incrementBigDecimal); |
| 278 } else { |
| 279 } |
| 280 } |
| 281 |
| 282 private static final ThreadLocal<Properties> threadLocalProperties = |
| 283 new ThreadLocal<Properties>() { |
| 284 @Override |
| 285 protected Properties initialValue() { |
| 286 return new Properties(); |
| 287 } |
| 288 }; |
| 289 |
| 290 public static Rounder getCurrencyRounder(DecimalFormatSymbols symbols, IProper
ties properties) { |
| 291 Properties cprops = threadLocalProperties.get().clear(); |
| 292 populateCurrencyRounderProperties(cprops, symbols, properties); |
| 293 if (cprops.getRoundingIncrement() != null) { |
279 return IncrementRounder.getInstance(cprops); | 294 return IncrementRounder.getInstance(cprops); |
280 } else { | 295 } else { |
281 return MagnitudeRounder.getInstance(cprops); | 296 return MagnitudeRounder.getInstance(cprops); |
282 } | 297 } |
283 } | 298 } |
284 } | 299 } |
LEFT | RIGHT |