/** * Obtains an instance of {@code CurrencyUnit} for the specified ISO-4217 numeric currency code. * <p> * The numeric code is an alternative to the three letter code. * * @param numericCurrencyCode the numeric currency code, not null * @return the singleton instance, never null * @throws IllegalCurrencyException if the currency is unknown */ public static CurrencyUnit ofNumericCode(int numericCurrencyCode) { CurrencyUnit currency = currenciesByNumericCode.get(numericCurrencyCode); if (currency == null) { throw new IllegalCurrencyException("Unknown currency '" + numericCurrencyCode + '\''); } return currency; }
/** * Obtains an instance of {@code CurrencyUnit} for the specified locale. * <p> * Only the country is used from the locale. * * @param locale the locale, not null * @return the singleton instance, never null * @throws IllegalCurrencyException if the currency is unknown */ public static CurrencyUnit of(Locale locale) { MoneyUtils.checkNotNull(locale, "Locale must not be null"); CurrencyUnit currency = currenciesByCountry.get(locale.getCountry()); if (currency == null) { throw new IllegalCurrencyException("Unknown currency for locale '" + locale + '\''); } return currency; }
/** * Obtains an instance of {@code CurrencyUnit} for the specified ISO-3166 country code. * <p> * Country codes should generally be in upper case. * This method is case sensitive. * * @param countryCode the country code, typically ISO-3166, not null * @return the singleton instance, never null * @throws IllegalCurrencyException if the currency is unknown */ public static CurrencyUnit ofCountry(String countryCode) { MoneyUtils.checkNotNull(countryCode, "Country code must not be null"); CurrencyUnit currency = currenciesByCountry.get(countryCode); if (currency == null) { throw new IllegalCurrencyException("Unknown currency for country '" + countryCode + '\''); } return currency; }
/** * Obtains an instance of {@code CurrencyUnit} for the specified three letter currency code. * <p> * A currency is uniquely identified by a three letter code, based on ISO-4217. * Valid currency codes are three upper-case ASCII letters. * * @param currencyCode the three-letter currency code, not null * @return the singleton instance, never null * @throws IllegalCurrencyException if the currency is unknown */ @FromString public static CurrencyUnit of(String currencyCode) { MoneyUtils.checkNotNull(currencyCode, "Currency code must not be null"); CurrencyUnit currency = currenciesByCode.get(currencyCode); if (currency == null) { throw new IllegalCurrencyException("Unknown currency '" + currencyCode + '\''); } return currency; }
/** * Obtains an instance of {@code CurrencyUnit} for the specified ISO-4217 numeric currency code. * <p> * The numeric code is an alternative to the three letter code. * This method is lenient and does not require the string to be left padded with zeroes. * * @param numericCurrencyCode the currency code, not null * @return the singleton instance, never null * @throws IllegalCurrencyException if the currency is unknown */ public static CurrencyUnit ofNumericCode(String numericCurrencyCode) { MoneyUtils.checkNotNull(numericCurrencyCode, "Currency code must not be null"); switch (numericCurrencyCode.length()) { case 1: return ofNumericCode(numericCurrencyCode.charAt(0) - '0'); case 2: return ofNumericCode((numericCurrencyCode.charAt(0) - '0') * 10 + numericCurrencyCode.charAt(1) - '0'); case 3: return ofNumericCode((numericCurrencyCode.charAt(0) - '0') * 100 + (numericCurrencyCode.charAt(1) - '0') * 10 + numericCurrencyCode.charAt(2) - '0'); default: throw new IllegalCurrencyException("Unknown currency '" + numericCurrencyCode + '\''); } }