272 private static final int SYMBOL = 0;
273 private static final int DISPLAYNAME = 1;
274
275
276 /**
277 * Constructs a <code>Currency</code> instance. The constructor is private
278 * so that we can insure that there's never more than one instance for a
279 * given currency.
280 */
281 private Currency(String currencyCode, int defaultFractionDigits, int numericCode) {
282 this.currencyCode = currencyCode;
283 this.defaultFractionDigits = defaultFractionDigits;
284 this.numericCode = numericCode;
285 }
286
287 /**
288 * Returns the <code>Currency</code> instance for the given currency code.
289 *
290 * @param currencyCode the ISO 4217 code of the currency
291 * @return the <code>Currency</code> instance for the given currency code
292 * @exception NullPointerException if <code>currencyCode</code> is null
293 * @exception IllegalArgumentException if <code>currencyCode</code> is not
294 * a supported ISO 4217 code.
295 */
296 public static Currency getInstance(String currencyCode) {
297 return getInstance(currencyCode, Integer.MIN_VALUE, 0);
298 }
299
300 private static Currency getInstance(String currencyCode, int defaultFractionDigits,
301 int numericCode) {
302 // Try to look up the currency code in the instances table.
303 // This does the null pointer check as a side effect.
304 // Also, if there already is an entry, the currencyCode must be valid.
305 Currency instance = instances.get(currencyCode);
306 if (instance != null) {
307 return instance;
308 }
309
310 if (defaultFractionDigits == Integer.MIN_VALUE) {
311 // Currency code not internally generated, need to verify first
312 // A currency code must have 3 characters and exist in the main table
313 // or in the list of other currencies.
355 * are ignored. The result may vary over time, as countries change their
356 * currencies. For example, for the original member countries of the
357 * European Monetary Union, the method returns the old national currencies
358 * until December 31, 2001, and the Euro from January 1, 2002, local time
359 * of the respective countries.
360 * <p>
361 * If the specified {@code locale} contains "cu" and/or "rg"
362 * <a href="./Locale.html#def_locale_extension">Unicode extensions</a>,
363 * the instance returned from this method reflects
364 * the values specified with those extensions. If both "cu" and "rg" are
365 * specified, the currency from the "cu" extension supersedes the implicit one
366 * from the "rg" extension.
367 * <p>
368 * The method returns <code>null</code> for territories that don't
369 * have a currency, such as Antarctica.
370 *
371 * @param locale the locale for whose country a <code>Currency</code>
372 * instance is needed
373 * @return the <code>Currency</code> instance for the country of the given
374 * locale, or {@code null}
375 * @exception NullPointerException if <code>locale</code>
376 * is {@code null}
377 * @exception IllegalArgumentException if the country of the given {@code locale}
378 * is not a supported ISO 3166 country code.
379 */
380 public static Currency getInstance(Locale locale) {
381 // check for locale overrides
382 String override = locale.getUnicodeLocaleType("cu");
383 if (override != null) {
384 try {
385 return getInstance(override.toUpperCase(Locale.ROOT));
386 } catch (IllegalArgumentException iae) {
387 // override currency is invalid. Fall through.
388 }
389 }
390
391 String country = CalendarDataUtility.findRegionOverride(locale).getCountry();
392
393 if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
394 throw new IllegalArgumentException();
395 }
396
397 char char1 = country.charAt(0);
520 * {@link Locale.Category#DISPLAY DISPLAY} locale
521 */
522 public String getSymbol() {
523 return getSymbol(Locale.getDefault(Locale.Category.DISPLAY));
524 }
525
526 /**
527 * Gets the symbol of this currency for the specified locale.
528 * For example, for the US Dollar, the symbol is "$" if the specified
529 * locale is the US, while for other locales it may be "US$". If no
530 * symbol can be determined, the ISO 4217 currency code is returned.
531 * <p>
532 * If the specified {@code locale} contains "rg" (region override)
533 * <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
534 * the symbol returned from this method reflects
535 * the value specified with that extension.
536 *
537 * @param locale the locale for which a display name for this currency is
538 * needed
539 * @return the symbol of this currency for the specified locale
540 * @exception NullPointerException if <code>locale</code> is null
541 */
542 public String getSymbol(Locale locale) {
543 LocaleServiceProviderPool pool =
544 LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
545 locale = CalendarDataUtility.findRegionOverride(locale);
546 String symbol = pool.getLocalizedObject(
547 CurrencyNameGetter.INSTANCE,
548 locale, currencyCode, SYMBOL);
549 if (symbol != null) {
550 return symbol;
551 }
552
553 // use currency code as symbol of last resort
554 return currencyCode;
555 }
556
557 /**
558 * Gets the default number of fraction digits used with this currency.
559 * Note that the number of fraction digits is the same as ISO 4217's
560 * minor unit for the currency.
615 * This is equivalent to calling
616 * {@link #getDisplayName(Locale)
617 * getDisplayName(Locale.getDefault(Locale.Category.DISPLAY))}.
618 *
619 * @return the display name of this currency for the default
620 * {@link Locale.Category#DISPLAY DISPLAY} locale
621 * @since 1.7
622 */
623 public String getDisplayName() {
624 return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY));
625 }
626
627 /**
628 * Gets the name that is suitable for displaying this currency for
629 * the specified locale. If there is no suitable display name found
630 * for the specified locale, the ISO 4217 currency code is returned.
631 *
632 * @param locale the locale for which a display name for this currency is
633 * needed
634 * @return the display name of this currency for the specified locale
635 * @exception NullPointerException if <code>locale</code> is null
636 * @since 1.7
637 */
638 public String getDisplayName(Locale locale) {
639 LocaleServiceProviderPool pool =
640 LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
641 String result = pool.getLocalizedObject(
642 CurrencyNameGetter.INSTANCE,
643 locale, currencyCode, DISPLAYNAME);
644 if (result != null) {
645 return result;
646 }
647
648 // use currency code as symbol of last resort
649 return currencyCode;
650 }
651
652 /**
653 * Returns the ISO 4217 currency code of this currency.
654 *
655 * @return the ISO 4217 currency code of this currency
|
272 private static final int SYMBOL = 0;
273 private static final int DISPLAYNAME = 1;
274
275
276 /**
277 * Constructs a <code>Currency</code> instance. The constructor is private
278 * so that we can insure that there's never more than one instance for a
279 * given currency.
280 */
281 private Currency(String currencyCode, int defaultFractionDigits, int numericCode) {
282 this.currencyCode = currencyCode;
283 this.defaultFractionDigits = defaultFractionDigits;
284 this.numericCode = numericCode;
285 }
286
287 /**
288 * Returns the <code>Currency</code> instance for the given currency code.
289 *
290 * @param currencyCode the ISO 4217 code of the currency
291 * @return the <code>Currency</code> instance for the given currency code
292 * @throws NullPointerException if <code>currencyCode</code> is null
293 * @throws IllegalArgumentException if <code>currencyCode</code> is not
294 * a supported ISO 4217 code.
295 */
296 public static Currency getInstance(String currencyCode) {
297 return getInstance(currencyCode, Integer.MIN_VALUE, 0);
298 }
299
300 private static Currency getInstance(String currencyCode, int defaultFractionDigits,
301 int numericCode) {
302 // Try to look up the currency code in the instances table.
303 // This does the null pointer check as a side effect.
304 // Also, if there already is an entry, the currencyCode must be valid.
305 Currency instance = instances.get(currencyCode);
306 if (instance != null) {
307 return instance;
308 }
309
310 if (defaultFractionDigits == Integer.MIN_VALUE) {
311 // Currency code not internally generated, need to verify first
312 // A currency code must have 3 characters and exist in the main table
313 // or in the list of other currencies.
355 * are ignored. The result may vary over time, as countries change their
356 * currencies. For example, for the original member countries of the
357 * European Monetary Union, the method returns the old national currencies
358 * until December 31, 2001, and the Euro from January 1, 2002, local time
359 * of the respective countries.
360 * <p>
361 * If the specified {@code locale} contains "cu" and/or "rg"
362 * <a href="./Locale.html#def_locale_extension">Unicode extensions</a>,
363 * the instance returned from this method reflects
364 * the values specified with those extensions. If both "cu" and "rg" are
365 * specified, the currency from the "cu" extension supersedes the implicit one
366 * from the "rg" extension.
367 * <p>
368 * The method returns <code>null</code> for territories that don't
369 * have a currency, such as Antarctica.
370 *
371 * @param locale the locale for whose country a <code>Currency</code>
372 * instance is needed
373 * @return the <code>Currency</code> instance for the country of the given
374 * locale, or {@code null}
375 * @throws NullPointerException if <code>locale</code>
376 * is {@code null}
377 * @throws IllegalArgumentException if the country of the given {@code locale}
378 * is not a supported ISO 3166 country code.
379 */
380 public static Currency getInstance(Locale locale) {
381 // check for locale overrides
382 String override = locale.getUnicodeLocaleType("cu");
383 if (override != null) {
384 try {
385 return getInstance(override.toUpperCase(Locale.ROOT));
386 } catch (IllegalArgumentException iae) {
387 // override currency is invalid. Fall through.
388 }
389 }
390
391 String country = CalendarDataUtility.findRegionOverride(locale).getCountry();
392
393 if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
394 throw new IllegalArgumentException();
395 }
396
397 char char1 = country.charAt(0);
520 * {@link Locale.Category#DISPLAY DISPLAY} locale
521 */
522 public String getSymbol() {
523 return getSymbol(Locale.getDefault(Locale.Category.DISPLAY));
524 }
525
526 /**
527 * Gets the symbol of this currency for the specified locale.
528 * For example, for the US Dollar, the symbol is "$" if the specified
529 * locale is the US, while for other locales it may be "US$". If no
530 * symbol can be determined, the ISO 4217 currency code is returned.
531 * <p>
532 * If the specified {@code locale} contains "rg" (region override)
533 * <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
534 * the symbol returned from this method reflects
535 * the value specified with that extension.
536 *
537 * @param locale the locale for which a display name for this currency is
538 * needed
539 * @return the symbol of this currency for the specified locale
540 * @throws NullPointerException if <code>locale</code> is null
541 */
542 public String getSymbol(Locale locale) {
543 LocaleServiceProviderPool pool =
544 LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
545 locale = CalendarDataUtility.findRegionOverride(locale);
546 String symbol = pool.getLocalizedObject(
547 CurrencyNameGetter.INSTANCE,
548 locale, currencyCode, SYMBOL);
549 if (symbol != null) {
550 return symbol;
551 }
552
553 // use currency code as symbol of last resort
554 return currencyCode;
555 }
556
557 /**
558 * Gets the default number of fraction digits used with this currency.
559 * Note that the number of fraction digits is the same as ISO 4217's
560 * minor unit for the currency.
615 * This is equivalent to calling
616 * {@link #getDisplayName(Locale)
617 * getDisplayName(Locale.getDefault(Locale.Category.DISPLAY))}.
618 *
619 * @return the display name of this currency for the default
620 * {@link Locale.Category#DISPLAY DISPLAY} locale
621 * @since 1.7
622 */
623 public String getDisplayName() {
624 return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY));
625 }
626
627 /**
628 * Gets the name that is suitable for displaying this currency for
629 * the specified locale. If there is no suitable display name found
630 * for the specified locale, the ISO 4217 currency code is returned.
631 *
632 * @param locale the locale for which a display name for this currency is
633 * needed
634 * @return the display name of this currency for the specified locale
635 * @throws NullPointerException if <code>locale</code> is null
636 * @since 1.7
637 */
638 public String getDisplayName(Locale locale) {
639 LocaleServiceProviderPool pool =
640 LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
641 String result = pool.getLocalizedObject(
642 CurrencyNameGetter.INSTANCE,
643 locale, currencyCode, DISPLAYNAME);
644 if (result != null) {
645 return result;
646 }
647
648 // use currency code as symbol of last resort
649 return currencyCode;
650 }
651
652 /**
653 * Returns the ISO 4217 currency code of this currency.
654 *
655 * @return the ISO 4217 currency code of this currency
|