Print this page
rev 5615 : 6336885: RFE: Locale Data Deployment Enhancements
4609153: Provide locale data for Indic locales
5104387: Support for gl_ES locale (galician language)
6337471: desktop/system locale preferences support
7056139: (cal) SPI support for locale-dependent Calendar parameters
7058206: Provide CalendarData SPI for week params and display field value names
7073852: Support multiple scripts for digits and decimal symbols per locale
7079560: [Fmt-Da] Context dependent month names support in SimpleDateFormat
7171324: getAvailableLocales() of locale sensitive services should return the actual availability of locales
7151414: (cal) Support calendar type identification
7168528: LocaleServiceProvider needs to be aware of Locale extensions
7171372: (cal) locale's default Calendar should be created if unknown calendar is specified
Summary: JEP 127: Improve Locale Data Packaging and Adopt Unicode CLDR Data (part 1 w/o Jigsaw. by Naoto Sato and Masayoshi Okutsu)
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/classes/java/util/Currency.java
+++ new/src/share/classes/java/util/Currency.java
1 1 /*
2 2 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package java.util;
27 27
28 28 import java.io.BufferedInputStream;
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
29 29 import java.io.DataInputStream;
30 30 import java.io.File;
31 31 import java.io.FileInputStream;
32 32 import java.io.FileReader;
33 33 import java.io.IOException;
34 34 import java.io.Serializable;
35 35 import java.security.AccessController;
36 36 import java.security.PrivilegedAction;
37 37 import java.util.concurrent.ConcurrentHashMap;
38 38 import java.util.concurrent.ConcurrentMap;
39 -import java.util.logging.Level;
40 39 import java.util.regex.Pattern;
41 40 import java.util.regex.Matcher;
42 41 import java.util.spi.CurrencyNameProvider;
43 -import java.util.spi.LocaleServiceProvider;
44 -import sun.util.LocaleServiceProviderPool;
42 +import sun.util.locale.provider.LocaleServiceProviderPool;
45 43 import sun.util.logging.PlatformLogger;
46 -import sun.util.resources.LocaleData;
47 -import sun.util.resources.OpenListResourceBundle;
48 44
49 45
50 46 /**
51 47 * Represents a currency. Currencies are identified by their ISO 4217 currency
52 48 * codes. Visit the <a href="http://www.iso.org/iso/en/prods-services/popstds/currencycodes.html">
53 49 * ISO web site</a> for more information, including a table of
54 50 * currency codes.
55 51 * <p>
56 52 * The class is designed so that there's never more than one
57 53 * <code>Currency</code> instance for any given currency. Therefore, there's
58 54 * no public constructor. You obtain a <code>Currency</code> instance using
59 55 * the <code>getInstance</code> methods.
60 56 * <p>
61 57 * Users can supersede the Java runtime currency data by creating a properties
62 58 * file named <code><JAVA_HOME>/lib/currency.properties</code>. The contents
63 59 * of the properties file are key/value pairs of the ISO 3166 country codes
64 60 * and the ISO 4217 currency data respectively. The value part consists of
65 61 * three ISO 4217 values of a currency, i.e., an alphabetic code, a numeric
66 62 * code, and a minor unit. Those three ISO 4217 values are separated by commas.
67 63 * The lines which start with '#'s are considered comment lines. For example,
68 64 * <p>
69 65 * <code>
70 66 * #Sample currency properties<br>
71 67 * JP=JPZ,999,0
72 68 * </code>
73 69 * <p>
74 70 * will supersede the currency data for Japan.
75 71 *
76 72 * @since 1.4
77 73 */
78 74 public final class Currency implements Serializable {
79 75
80 76 private static final long serialVersionUID = -158308464356906721L;
81 77
82 78 /**
83 79 * ISO 4217 currency code for this currency.
84 80 *
85 81 * @serial
86 82 */
87 83 private final String currencyCode;
88 84
89 85 /**
90 86 * Default fraction digits for this currency.
91 87 * Set from currency data tables.
92 88 */
93 89 transient private final int defaultFractionDigits;
94 90
95 91 /**
96 92 * ISO 4217 numeric code for this currency.
97 93 * Set from currency data tables.
98 94 */
99 95 transient private final int numericCode;
100 96
101 97
102 98 // class data: instance map
103 99
104 100 private static ConcurrentMap<String, Currency> instances = new ConcurrentHashMap<>(7);
105 101 private static HashSet<Currency> available;
106 102
107 103
108 104 // Class data: currency data obtained from currency.data file.
109 105 // Purpose:
110 106 // - determine valid country codes
111 107 // - determine valid currency codes
112 108 // - map country codes to currency codes
113 109 // - obtain default fraction digits for currency codes
114 110 //
115 111 // sc = special case; dfd = default fraction digits
116 112 // Simple countries are those where the country code is a prefix of the
117 113 // currency code, and there are no known plans to change the currency.
118 114 //
119 115 // table formats:
120 116 // - mainTable:
121 117 // - maps country code to 32-bit int
122 118 // - 26*26 entries, corresponding to [A-Z]*[A-Z]
123 119 // - \u007F -> not valid country
124 120 // - bits 18-31: unused
125 121 // - bits 8-17: numeric code (0 to 1023)
126 122 // - bit 7: 1 - special case, bits 0-4 indicate which one
127 123 // 0 - simple country, bits 0-4 indicate final char of currency code
128 124 // - bits 5-6: fraction digits for simple countries, 0 for special cases
129 125 // - bits 0-4: final char for currency code for simple country, or ID of special case
130 126 // - special case IDs:
131 127 // - 0: country has no currency
132 128 // - other: index into sc* arrays + 1
133 129 // - scCutOverTimes: cut-over time in millis as returned by
134 130 // System.currentTimeMillis for special case countries that are changing
135 131 // currencies; Long.MAX_VALUE for countries that are not changing currencies
136 132 // - scOldCurrencies: old currencies for special case countries
137 133 // - scNewCurrencies: new currencies for special case countries that are
138 134 // changing currencies; null for others
139 135 // - scOldCurrenciesDFD: default fraction digits for old currencies
140 136 // - scNewCurrenciesDFD: default fraction digits for new currencies, 0 for
141 137 // countries that are not changing currencies
142 138 // - otherCurrencies: concatenation of all currency codes that are not the
143 139 // main currency of a simple country, separated by "-"
144 140 // - otherCurrenciesDFD: decimal format digits for currencies in otherCurrencies, same order
145 141
146 142 static int formatVersion;
147 143 static int dataVersion;
148 144 static int[] mainTable;
149 145 static long[] scCutOverTimes;
150 146 static String[] scOldCurrencies;
151 147 static String[] scNewCurrencies;
152 148 static int[] scOldCurrenciesDFD;
153 149 static int[] scNewCurrenciesDFD;
154 150 static int[] scOldCurrenciesNumericCode;
155 151 static int[] scNewCurrenciesNumericCode;
156 152 static String otherCurrencies;
157 153 static int[] otherCurrenciesDFD;
158 154 static int[] otherCurrenciesNumericCode;
159 155
160 156 // handy constants - must match definitions in GenerateCurrencyData
161 157 // magic number
162 158 private static final int MAGIC_NUMBER = 0x43757244;
163 159 // number of characters from A to Z
164 160 private static final int A_TO_Z = ('Z' - 'A') + 1;
165 161 // entry for invalid country codes
166 162 private static final int INVALID_COUNTRY_ENTRY = 0x007F;
167 163 // entry for countries without currency
168 164 private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x0080;
169 165 // mask for simple case country entries
170 166 private static final int SIMPLE_CASE_COUNTRY_MASK = 0x0000;
171 167 // mask for simple case country entry final character
172 168 private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x001F;
173 169 // mask for simple case country entry default currency digits
174 170 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x0060;
175 171 // shift count for simple case country entry default currency digits
176 172 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT = 5;
177 173 // mask for special case country entries
178 174 private static final int SPECIAL_CASE_COUNTRY_MASK = 0x0080;
179 175 // mask for special case country index
180 176 private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x001F;
181 177 // delta from entry index component in main table to index into special case tables
182 178 private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1;
183 179 // mask for distinguishing simple and special case countries
↓ open down ↓ |
126 lines elided |
↑ open up ↑ |
184 180 private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK;
185 181 // mask for the numeric code of the currency
186 182 private static final int NUMERIC_CODE_MASK = 0x0003FF00;
187 183 // shift count for the numeric code of the currency
188 184 private static final int NUMERIC_CODE_SHIFT = 8;
189 185
190 186 // Currency data format version
191 187 private static final int VALID_FORMAT_VERSION = 1;
192 188
193 189 static {
194 - AccessController.doPrivileged(new PrivilegedAction<Object>() {
195 - public Object run() {
190 + AccessController.doPrivileged(new PrivilegedAction<Void>() {
191 + @Override
192 + public Void run() {
196 193 String homeDir = System.getProperty("java.home");
197 194 try {
198 195 String dataFile = homeDir + File.separator +
199 196 "lib" + File.separator + "currency.data";
200 - DataInputStream dis = new DataInputStream(
201 - new BufferedInputStream(
202 - new FileInputStream(dataFile)));
203 - if (dis.readInt() != MAGIC_NUMBER) {
204 - throw new InternalError("Currency data is possibly corrupted");
197 + try (DataInputStream dis = new DataInputStream(
198 + new BufferedInputStream(
199 + new FileInputStream(dataFile)))) {
200 + if (dis.readInt() != MAGIC_NUMBER) {
201 + throw new InternalError("Currency data is possibly corrupted");
202 + }
203 + formatVersion = dis.readInt();
204 + if (formatVersion != VALID_FORMAT_VERSION) {
205 + throw new InternalError("Currency data format is incorrect");
206 + }
207 + dataVersion = dis.readInt();
208 + mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
209 + int scCount = dis.readInt();
210 + scCutOverTimes = readLongArray(dis, scCount);
211 + scOldCurrencies = readStringArray(dis, scCount);
212 + scNewCurrencies = readStringArray(dis, scCount);
213 + scOldCurrenciesDFD = readIntArray(dis, scCount);
214 + scNewCurrenciesDFD = readIntArray(dis, scCount);
215 + scOldCurrenciesNumericCode = readIntArray(dis, scCount);
216 + scNewCurrenciesNumericCode = readIntArray(dis, scCount);
217 + int ocCount = dis.readInt();
218 + otherCurrencies = dis.readUTF();
219 + otherCurrenciesDFD = readIntArray(dis, ocCount);
220 + otherCurrenciesNumericCode = readIntArray(dis, ocCount);
205 221 }
206 - formatVersion = dis.readInt();
207 - if (formatVersion != VALID_FORMAT_VERSION) {
208 - throw new InternalError("Currency data format is incorrect");
209 - }
210 - dataVersion = dis.readInt();
211 - mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
212 - int scCount = dis.readInt();
213 - scCutOverTimes = readLongArray(dis, scCount);
214 - scOldCurrencies = readStringArray(dis, scCount);
215 - scNewCurrencies = readStringArray(dis, scCount);
216 - scOldCurrenciesDFD = readIntArray(dis, scCount);
217 - scNewCurrenciesDFD = readIntArray(dis, scCount);
218 - scOldCurrenciesNumericCode = readIntArray(dis, scCount);
219 - scNewCurrenciesNumericCode = readIntArray(dis, scCount);
220 - int ocCount = dis.readInt();
221 - otherCurrencies = dis.readUTF();
222 - otherCurrenciesDFD = readIntArray(dis, ocCount);
223 - otherCurrenciesNumericCode = readIntArray(dis, ocCount);
224 - dis.close();
225 222 } catch (IOException e) {
226 223 throw new InternalError(e);
227 224 }
228 225
229 226 // look for the properties file for overrides
230 227 try {
231 228 File propFile = new File(homeDir + File.separator +
232 229 "lib" + File.separator +
233 230 "currency.properties");
234 231 if (propFile.exists()) {
235 232 Properties props = new Properties();
236 233 try (FileReader fr = new FileReader(propFile)) {
237 234 props.load(fr);
238 235 }
239 236 Set<String> keys = props.stringPropertyNames();
240 237 Pattern propertiesPattern =
241 238 Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*([0-3])");
242 239 for (String key : keys) {
243 240 replaceCurrencyData(propertiesPattern,
244 241 key.toUpperCase(Locale.ROOT),
245 242 props.getProperty(key).toUpperCase(Locale.ROOT));
246 243 }
247 244 }
248 245 } catch (IOException e) {
249 246 info("currency.properties is ignored because of an IOException", e);
250 247 }
251 248 return null;
252 249 }
253 250 });
254 251 }
255 252
256 253 /**
257 254 * Constants for retrieving localized names from the name providers.
258 255 */
259 256 private static final int SYMBOL = 0;
260 257 private static final int DISPLAYNAME = 1;
261 258
262 259
263 260 /**
264 261 * Constructs a <code>Currency</code> instance. The constructor is private
265 262 * so that we can insure that there's never more than one instance for a
266 263 * given currency.
267 264 */
268 265 private Currency(String currencyCode, int defaultFractionDigits, int numericCode) {
269 266 this.currencyCode = currencyCode;
270 267 this.defaultFractionDigits = defaultFractionDigits;
271 268 this.numericCode = numericCode;
272 269 }
273 270
274 271 /**
275 272 * Returns the <code>Currency</code> instance for the given currency code.
276 273 *
277 274 * @param currencyCode the ISO 4217 code of the currency
278 275 * @return the <code>Currency</code> instance for the given currency code
279 276 * @exception NullPointerException if <code>currencyCode</code> is null
280 277 * @exception IllegalArgumentException if <code>currencyCode</code> is not
281 278 * a supported ISO 4217 code.
282 279 */
283 280 public static Currency getInstance(String currencyCode) {
284 281 return getInstance(currencyCode, Integer.MIN_VALUE, 0);
285 282 }
286 283
287 284 private static Currency getInstance(String currencyCode, int defaultFractionDigits,
288 285 int numericCode) {
289 286 // Try to look up the currency code in the instances table.
290 287 // This does the null pointer check as a side effect.
291 288 // Also, if there already is an entry, the currencyCode must be valid.
292 289 Currency instance = instances.get(currencyCode);
293 290 if (instance != null) {
294 291 return instance;
295 292 }
296 293
297 294 if (defaultFractionDigits == Integer.MIN_VALUE) {
298 295 // Currency code not internally generated, need to verify first
299 296 // A currency code must have 3 characters and exist in the main table
300 297 // or in the list of other currencies.
301 298 if (currencyCode.length() != 3) {
302 299 throw new IllegalArgumentException();
303 300 }
304 301 char char1 = currencyCode.charAt(0);
305 302 char char2 = currencyCode.charAt(1);
306 303 int tableEntry = getMainTableEntry(char1, char2);
307 304 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
308 305 && tableEntry != INVALID_COUNTRY_ENTRY
309 306 && currencyCode.charAt(2) - 'A' == (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) {
310 307 defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
311 308 numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
312 309 } else {
313 310 // Check for '-' separately so we don't get false hits in the table.
314 311 if (currencyCode.charAt(2) == '-') {
315 312 throw new IllegalArgumentException();
316 313 }
317 314 int index = otherCurrencies.indexOf(currencyCode);
318 315 if (index == -1) {
319 316 throw new IllegalArgumentException();
320 317 }
321 318 defaultFractionDigits = otherCurrenciesDFD[index / 4];
322 319 numericCode = otherCurrenciesNumericCode[index / 4];
323 320 }
324 321 }
325 322
326 323 Currency currencyVal =
327 324 new Currency(currencyCode, defaultFractionDigits, numericCode);
328 325 instance = instances.putIfAbsent(currencyCode, currencyVal);
329 326 return (instance != null ? instance : currencyVal);
330 327 }
331 328
332 329 /**
333 330 * Returns the <code>Currency</code> instance for the country of the
334 331 * given locale. The language and variant components of the locale
335 332 * are ignored. The result may vary over time, as countries change their
336 333 * currencies. For example, for the original member countries of the
↓ open down ↓ |
102 lines elided |
↑ open up ↑ |
337 334 * European Monetary Union, the method returns the old national currencies
338 335 * until December 31, 2001, and the Euro from January 1, 2002, local time
339 336 * of the respective countries.
340 337 * <p>
341 338 * The method returns <code>null</code> for territories that don't
342 339 * have a currency, such as Antarctica.
343 340 *
344 341 * @param locale the locale for whose country a <code>Currency</code>
345 342 * instance is needed
346 343 * @return the <code>Currency</code> instance for the country of the given
347 - * locale, or null
344 + * locale, or {@code null}
348 345 * @exception NullPointerException if <code>locale</code> or its country
349 - * code is null
350 - * @exception IllegalArgumentException if the country of the given locale
346 + * code is {@code null}
347 + * @exception IllegalArgumentException if the country of the given {@code locale}
351 348 * is not a supported ISO 3166 country code.
352 349 */
353 350 public static Currency getInstance(Locale locale) {
354 351 String country = locale.getCountry();
355 352 if (country == null) {
356 353 throw new NullPointerException();
357 354 }
358 355
359 356 if (country.length() != 2) {
360 357 throw new IllegalArgumentException();
361 358 }
362 359
363 360 char char1 = country.charAt(0);
364 361 char char2 = country.charAt(1);
365 362 int tableEntry = getMainTableEntry(char1, char2);
366 363 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
367 364 && tableEntry != INVALID_COUNTRY_ENTRY) {
368 365 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');
369 366 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
370 367 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
371 - StringBuffer sb = new StringBuffer(country);
368 + StringBuilder sb = new StringBuilder(country);
372 369 sb.append(finalChar);
373 370 return getInstance(sb.toString(), defaultFractionDigits, numericCode);
374 371 } else {
375 372 // special cases
376 373 if (tableEntry == INVALID_COUNTRY_ENTRY) {
377 374 throw new IllegalArgumentException();
378 375 }
379 376 if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) {
380 377 return null;
381 378 } else {
382 379 int index = (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA;
383 380 if (scCutOverTimes[index] == Long.MAX_VALUE || System.currentTimeMillis() < scCutOverTimes[index]) {
384 381 return getInstance(scOldCurrencies[index], scOldCurrenciesDFD[index],
385 382 scOldCurrenciesNumericCode[index]);
386 383 } else {
387 384 return getInstance(scNewCurrencies[index], scNewCurrenciesDFD[index],
388 385 scNewCurrenciesNumericCode[index]);
389 386 }
390 387 }
391 388 }
392 389 }
393 390
394 391 /**
395 392 * Gets the set of available currencies. The returned set of currencies
396 393 * contains all of the available currencies, which may include currencies
397 394 * that represent obsolete ISO 4217 codes. The set can be modified
398 395 * without affecting the available currencies in the runtime.
399 396 *
400 397 * @return the set of available currencies. If there is no currency
401 398 * available in the runtime, the returned set is empty.
402 399 * @since 1.7
403 400 */
404 401 public static Set<Currency> getAvailableCurrencies() {
405 402 synchronized(Currency.class) {
406 403 if (available == null) {
407 404 available = new HashSet<>(256);
408 405
409 406 // Add simple currencies first
410 407 for (char c1 = 'A'; c1 <= 'Z'; c1 ++) {
411 408 for (char c2 = 'A'; c2 <= 'Z'; c2 ++) {
412 409 int tableEntry = getMainTableEntry(c1, c2);
413 410 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
414 411 && tableEntry != INVALID_COUNTRY_ENTRY) {
415 412 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');
416 413 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
417 414 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
418 415 StringBuilder sb = new StringBuilder();
419 416 sb.append(c1);
420 417 sb.append(c2);
421 418 sb.append(finalChar);
422 419 available.add(getInstance(sb.toString(), defaultFractionDigits, numericCode));
423 420 }
424 421 }
425 422 }
426 423
427 424 // Now add other currencies
428 425 StringTokenizer st = new StringTokenizer(otherCurrencies, "-");
429 426 while (st.hasMoreElements()) {
430 427 available.add(getInstance((String)st.nextElement()));
431 428 }
432 429 }
433 430 }
434 431
435 432 @SuppressWarnings("unchecked")
436 433 Set<Currency> result = (Set<Currency>) available.clone();
437 434 return result;
438 435 }
439 436
440 437 /**
441 438 * Gets the ISO 4217 currency code of this currency.
442 439 *
443 440 * @return the ISO 4217 currency code of this currency.
444 441 */
445 442 public String getCurrencyCode() {
446 443 return currencyCode;
447 444 }
448 445
449 446 /**
450 447 * Gets the symbol of this currency for the default locale.
451 448 * For example, for the US Dollar, the symbol is "$" if the default
452 449 * locale is the US, while for other locales it may be "US$". If no
453 450 * symbol can be determined, the ISO 4217 currency code is returned.
454 451 *
455 452 * @return the symbol of this currency for the default locale
456 453 */
457 454 public String getSymbol() {
458 455 return getSymbol(Locale.getDefault(Locale.Category.DISPLAY));
459 456 }
460 457
461 458 /**
462 459 * Gets the symbol of this currency for the specified locale.
↓ open down ↓ |
81 lines elided |
↑ open up ↑ |
463 460 * For example, for the US Dollar, the symbol is "$" if the specified
464 461 * locale is the US, while for other locales it may be "US$". If no
465 462 * symbol can be determined, the ISO 4217 currency code is returned.
466 463 *
467 464 * @param locale the locale for which a display name for this currency is
468 465 * needed
469 466 * @return the symbol of this currency for the specified locale
470 467 * @exception NullPointerException if <code>locale</code> is null
471 468 */
472 469 public String getSymbol(Locale locale) {
473 - try {
474 - // Check whether a provider can provide an implementation that's closer
475 - // to the requested locale than what the Java runtime itself can provide.
476 - LocaleServiceProviderPool pool =
477 - LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
478 -
479 - if (pool.hasProviders()) {
480 - // Assuming that all the country locales include necessary currency
481 - // symbols in the Java runtime's resources, so there is no need to
482 - // examine whether Java runtime's currency resource bundle is missing
483 - // names. Therefore, no resource bundle is provided for calling this
484 - // method.
485 - String symbol = pool.getLocalizedObject(
486 - CurrencyNameGetter.INSTANCE,
487 - locale, (OpenListResourceBundle)null,
488 - currencyCode, SYMBOL);
489 - if (symbol != null) {
490 - return symbol;
491 - }
492 - }
493 -
494 - ResourceBundle bundle = LocaleData.getCurrencyNames(locale);
495 - return bundle.getString(currencyCode);
496 - } catch (MissingResourceException e) {
497 - // use currency code as symbol of last resort
498 - return currencyCode;
470 + LocaleServiceProviderPool pool =
471 + LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
472 + String symbol = pool.getLocalizedObject(
473 + CurrencyNameGetter.INSTANCE,
474 + locale, currencyCode, SYMBOL);
475 + if (symbol != null) {
476 + return symbol;
499 477 }
478 +
479 + // use currency code as symbol of last resort
480 + return currencyCode;
500 481 }
501 482
502 483 /**
503 484 * Gets the default number of fraction digits used with this currency.
504 485 * For example, the default number of fraction digits for the Euro is 2,
505 486 * while for the Japanese Yen it's 0.
506 487 * In the case of pseudo-currencies, such as IMF Special Drawing Rights,
507 488 * -1 is returned.
508 489 *
509 490 * @return the default number of fraction digits used with this currency
510 491 */
511 492 public int getDefaultFractionDigits() {
512 493 return defaultFractionDigits;
513 494 }
514 495
515 496 /**
516 497 * Returns the ISO 4217 numeric code of this currency.
517 498 *
518 499 * @return the ISO 4217 numeric code of this currency
519 500 * @since 1.7
520 501 */
521 502 public int getNumericCode() {
522 503 return numericCode;
523 504 }
524 505
525 506 /**
526 507 * Gets the name that is suitable for displaying this currency for
527 508 * the default locale. If there is no suitable display name found
528 509 * for the default locale, the ISO 4217 currency code is returned.
529 510 *
530 511 * @return the display name of this currency for the default locale
531 512 * @since 1.7
532 513 */
533 514 public String getDisplayName() {
534 515 return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY));
535 516 }
536 517
537 518 /**
538 519 * Gets the name that is suitable for displaying this currency for
↓ open down ↓ |
29 lines elided |
↑ open up ↑ |
539 520 * the specified locale. If there is no suitable display name found
540 521 * for the specified locale, the ISO 4217 currency code is returned.
541 522 *
542 523 * @param locale the locale for which a display name for this currency is
543 524 * needed
544 525 * @return the display name of this currency for the specified locale
545 526 * @exception NullPointerException if <code>locale</code> is null
546 527 * @since 1.7
547 528 */
548 529 public String getDisplayName(Locale locale) {
549 - try {
550 - OpenListResourceBundle bundle = LocaleData.getCurrencyNames(locale);
551 - String result = null;
552 - String bundleKey = currencyCode.toLowerCase(Locale.ROOT);
553 -
554 - // Check whether a provider can provide an implementation that's closer
555 - // to the requested locale than what the Java runtime itself can provide.
556 - LocaleServiceProviderPool pool =
557 - LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
558 - if (pool.hasProviders()) {
559 - result = pool.getLocalizedObject(
560 - CurrencyNameGetter.INSTANCE,
561 - locale, bundleKey, bundle, currencyCode, DISPLAYNAME);
562 - }
563 -
564 - if (result == null) {
565 - result = bundle.getString(bundleKey);
566 - }
567 -
568 - if (result != null) {
569 - return result;
570 - }
571 - } catch (MissingResourceException e) {
572 - // fall through
530 + LocaleServiceProviderPool pool =
531 + LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
532 + String result = pool.getLocalizedObject(
533 + CurrencyNameGetter.INSTANCE,
534 + locale, currencyCode, DISPLAYNAME);
535 + if (result != null) {
536 + return result;
573 537 }
574 538
575 539 // use currency code as symbol of last resort
576 540 return currencyCode;
577 541 }
578 542
579 543 /**
580 544 * Returns the ISO 4217 currency code of this currency.
581 545 *
582 546 * @return the ISO 4217 currency code of this currency
583 547 */
548 + @Override
584 549 public String toString() {
585 550 return currencyCode;
586 551 }
587 552
588 553 /**
589 554 * Resolves instances being deserialized to a single instance per currency.
590 555 */
591 556 private Object readResolve() {
592 557 return getInstance(currencyCode);
593 558 }
594 559
595 560 /**
596 561 * Gets the main table entry for the country whose country code consists
597 562 * of char1 and char2.
598 563 */
599 564 private static int getMainTableEntry(char char1, char char2) {
600 565 if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
601 566 throw new IllegalArgumentException();
602 567 }
603 568 return mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')];
604 569 }
605 570
606 571 /**
607 572 * Sets the main table entry for the country whose country code consists
608 573 * of char1 and char2.
609 574 */
610 575 private static void setMainTableEntry(char char1, char char2, int entry) {
611 576 if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
612 577 throw new IllegalArgumentException();
613 578 }
614 579 mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')] = entry;
615 580 }
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
616 581
617 582 /**
618 583 * Obtains a localized currency names from a CurrencyNameProvider
619 584 * implementation.
620 585 */
621 586 private static class CurrencyNameGetter
622 587 implements LocaleServiceProviderPool.LocalizedObjectGetter<CurrencyNameProvider,
623 588 String> {
624 589 private static final CurrencyNameGetter INSTANCE = new CurrencyNameGetter();
625 590
591 + @Override
626 592 public String getObject(CurrencyNameProvider currencyNameProvider,
627 593 Locale locale,
628 594 String key,
629 595 Object... params) {
630 596 assert params.length == 1;
631 597 int type = (Integer)params[0];
632 598
633 599 switch(type) {
634 600 case SYMBOL:
635 601 return currencyNameProvider.getSymbol(key, locale);
636 602 case DISPLAYNAME:
637 603 return currencyNameProvider.getDisplayName(key, locale);
638 604 default:
639 605 assert false; // shouldn't happen
640 606 }
641 607
642 608 return null;
643 609 }
644 610 }
645 611
646 612 private static int[] readIntArray(DataInputStream dis, int count) throws IOException {
647 613 int[] ret = new int[count];
648 614 for (int i = 0; i < count; i++) {
649 615 ret[i] = dis.readInt();
650 616 }
651 617
652 618 return ret;
653 619 }
654 620
655 621 private static long[] readLongArray(DataInputStream dis, int count) throws IOException {
656 622 long[] ret = new long[count];
657 623 for (int i = 0; i < count; i++) {
658 624 ret[i] = dis.readLong();
659 625 }
660 626
661 627 return ret;
662 628 }
663 629
664 630 private static String[] readStringArray(DataInputStream dis, int count) throws IOException {
665 631 String[] ret = new String[count];
666 632 for (int i = 0; i < count; i++) {
667 633 ret[i] = dis.readUTF();
668 634 }
669 635
670 636 return ret;
671 637 }
672 638
673 639 /**
674 640 * Replaces currency data found in the currencydata.properties file
675 641 *
676 642 * @param pattern regex pattern for the properties
677 643 * @param ctry country code
678 644 * @param data currency data. This is a comma separated string that
679 645 * consists of "three-letter alphabet code", "three-digit numeric code",
680 646 * and "one-digit (0,1,2, or 3) default fraction digit".
681 647 * For example, "JPZ,392,0".
682 648 * @throws
683 649 */
684 650 private static void replaceCurrencyData(Pattern pattern, String ctry, String curdata) {
685 651
686 652 if (ctry.length() != 2) {
687 653 // ignore invalid country code
688 654 String message = new StringBuilder()
689 655 .append("The entry in currency.properties for ")
690 656 .append(ctry).append(" is ignored because of the invalid country code.")
691 657 .toString();
692 658 info(message, null);
693 659 return;
694 660 }
695 661
696 662 Matcher m = pattern.matcher(curdata);
697 663 if (!m.find()) {
698 664 // format is not recognized. ignore the data
699 665 String message = new StringBuilder()
700 666 .append("The entry in currency.properties for ")
701 667 .append(ctry)
702 668 .append(" is ignored because the value format is not recognized.")
703 669 .toString();
704 670 info(message, null);
705 671 return;
706 672 }
707 673
708 674 String code = m.group(1);
709 675 int numeric = Integer.parseInt(m.group(2));
710 676 int fraction = Integer.parseInt(m.group(3));
711 677 int entry = numeric << NUMERIC_CODE_SHIFT;
712 678
713 679 int index;
714 680 for (index = 0; index < scOldCurrencies.length; index++) {
715 681 if (scOldCurrencies[index].equals(code)) {
716 682 break;
717 683 }
718 684 }
719 685
720 686 if (index == scOldCurrencies.length) {
721 687 // simple case
722 688 entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT) |
723 689 (code.charAt(2) - 'A');
724 690 } else {
725 691 // special case
726 692 entry |= SPECIAL_CASE_COUNTRY_MASK |
727 693 (index + SPECIAL_CASE_COUNTRY_INDEX_DELTA);
728 694 }
729 695 setMainTableEntry(ctry.charAt(0), ctry.charAt(1), entry);
730 696 }
731 697
732 698 private static void info(String message, Throwable t) {
733 699 PlatformLogger logger = PlatformLogger.getLogger("java.util.Currency");
734 700 if (logger.isLoggable(PlatformLogger.INFO)) {
735 701 if (t != null) {
736 702 logger.info(message, t);
737 703 } else {
738 704 logger.info(message);
739 705 }
740 706 }
741 707 }
742 708 }
↓ open down ↓ |
107 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX