136 // - map country codes to currency codes 137 // - obtain default fraction digits for currency codes 138 // 139 // sc = special case; dfd = default fraction digits 140 // Simple countries are those where the country code is a prefix of the 141 // currency code, and there are no known plans to change the currency. 142 // 143 // table formats: 144 // - mainTable: 145 // - maps country code to 32-bit int 146 // - 26*26 entries, corresponding to [A-Z]*[A-Z] 147 // - \u007F -> not valid country 148 // - bits 20-31: unused 149 // - bits 10-19: numeric code (0 to 1023) 150 // - bit 9: 1 - special case, bits 0-4 indicate which one 151 // 0 - simple country, bits 0-4 indicate final char of currency code 152 // - bits 5-8: fraction digits for simple countries, 0 for special cases 153 // - bits 0-4: final char for currency code for simple country, or ID of special case 154 // - special case IDs: 155 // - 0: country has no currency 156 // - other: index into sc* arrays + 1 157 // - scCutOverTimes: cut-over time in millis as returned by 158 // System.currentTimeMillis for special case countries that are changing 159 // currencies; Long.MAX_VALUE for countries that are not changing currencies 160 // - scOldCurrencies: old currencies for special case countries 161 // - scNewCurrencies: new currencies for special case countries that are 162 // changing currencies; null for others 163 // - scOldCurrenciesDFD: default fraction digits for old currencies 164 // - scNewCurrenciesDFD: default fraction digits for new currencies, 0 for 165 // countries that are not changing currencies 166 // - otherCurrencies: concatenation of all currency codes that are not the 167 // main currency of a simple country, separated by "-" 168 // - otherCurrenciesDFD: decimal format digits for currencies in otherCurrencies, same order 169 170 static int formatVersion; 171 static int dataVersion; 172 static int[] mainTable; 173 static long[] scCutOverTimes; 174 static String[] scOldCurrencies; 175 static String[] scNewCurrencies; 176 static int[] scOldCurrenciesDFD; 177 static int[] scNewCurrenciesDFD; 178 static int[] scOldCurrenciesNumericCode; 179 static int[] scNewCurrenciesNumericCode; 180 static String otherCurrencies; 181 static int[] otherCurrenciesDFD; 182 static int[] otherCurrenciesNumericCode; 183 184 // handy constants - must match definitions in GenerateCurrencyData 185 // magic number 186 private static final int MAGIC_NUMBER = 0x43757244; 187 // number of characters from A to Z 188 private static final int A_TO_Z = ('Z' - 'A') + 1; 189 // entry for invalid country codes 190 private static final int INVALID_COUNTRY_ENTRY = 0x0000007F; 191 // entry for countries without currency 192 private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x00000200; 193 // mask for simple case country entries 194 private static final int SIMPLE_CASE_COUNTRY_MASK = 0x00000000; 195 // mask for simple case country entry final character 196 private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x0000001F; 197 // mask for simple case country entry default currency digits 198 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x000001E0; 199 // shift count for simple case country entry default currency digits 200 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT = 5; 201 // maximum number for simple case country entry default currency digits 202 private static final int SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS = 9; 203 // mask for special case country entries 204 private static final int SPECIAL_CASE_COUNTRY_MASK = 0x00000200; 205 // mask for special case country index 206 private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x0000001F; 207 // delta from entry index component in main table to index into special case tables 208 private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1; 209 // mask for distinguishing simple and special case countries 210 private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK; 211 // mask for the numeric code of the currency 212 private static final int NUMERIC_CODE_MASK = 0x000FFC00; 213 // shift count for the numeric code of the currency 214 private static final int NUMERIC_CODE_SHIFT = 10; 215 216 // Currency data format version 217 private static final int VALID_FORMAT_VERSION = 2; 218 219 static { 220 AccessController.doPrivileged(new PrivilegedAction<>() { 221 @Override 222 public Void run() { 223 try { 224 try (InputStream in = getClass().getResourceAsStream("/java/util/currency.data")) { 225 if (in == null) { 226 throw new InternalError("Currency data not found"); 227 } 228 DataInputStream dis = new DataInputStream(new BufferedInputStream(in)); 229 if (dis.readInt() != MAGIC_NUMBER) { 230 throw new InternalError("Currency data is possibly corrupted"); 231 } 232 formatVersion = dis.readInt(); 233 if (formatVersion != VALID_FORMAT_VERSION) { 234 throw new InternalError("Currency data format is incorrect"); 235 } 236 dataVersion = dis.readInt(); 237 mainTable = readIntArray(dis, A_TO_Z * A_TO_Z); 238 int scCount = dis.readInt(); 239 scCutOverTimes = readLongArray(dis, scCount); 240 scOldCurrencies = readStringArray(dis, scCount); 241 scNewCurrencies = readStringArray(dis, scCount); 242 scOldCurrenciesDFD = readIntArray(dis, scCount); 243 scNewCurrenciesDFD = readIntArray(dis, scCount); 244 scOldCurrenciesNumericCode = readIntArray(dis, scCount); 245 scNewCurrenciesNumericCode = readIntArray(dis, scCount); 246 int ocCount = dis.readInt(); 247 otherCurrencies = dis.readUTF(); 248 otherCurrenciesDFD = readIntArray(dis, ocCount); 249 otherCurrenciesNumericCode = readIntArray(dis, ocCount); 250 } 251 } catch (IOException e) { 252 throw new InternalError(e); 253 } 254 255 // look for the properties file for overrides 256 String propsFile = System.getProperty("java.util.currency.data"); 257 if (propsFile == null) { 258 propsFile = System.getProperty("java.home") + File.separator + "lib" + 259 File.separator + "currency.properties"; 260 } 261 try { 262 File propFile = new File(propsFile); 263 if (propFile.exists()) { 264 Properties props = new Properties(); 265 try (FileReader fr = new FileReader(propFile)) { 266 props.load(fr); 267 } 268 Set<String> keys = props.stringPropertyNames(); 269 Pattern propertiesPattern = 312 * a supported ISO 4217 code. 313 */ 314 public static Currency getInstance(String currencyCode) { 315 return getInstance(currencyCode, Integer.MIN_VALUE, 0); 316 } 317 318 private static Currency getInstance(String currencyCode, int defaultFractionDigits, 319 int numericCode) { 320 // Try to look up the currency code in the instances table. 321 // This does the null pointer check as a side effect. 322 // Also, if there already is an entry, the currencyCode must be valid. 323 Currency instance = instances.get(currencyCode); 324 if (instance != null) { 325 return instance; 326 } 327 328 if (defaultFractionDigits == Integer.MIN_VALUE) { 329 // Currency code not internally generated, need to verify first 330 // A currency code must have 3 characters and exist in the main table 331 // or in the list of other currencies. 332 if (currencyCode.length() != 3) { 333 throw new IllegalArgumentException(); 334 } 335 char char1 = currencyCode.charAt(0); 336 char char2 = currencyCode.charAt(1); 337 int tableEntry = getMainTableEntry(char1, char2); 338 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 339 && tableEntry != INVALID_COUNTRY_ENTRY 340 && currencyCode.charAt(2) - 'A' == (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) { 341 defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 342 numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 343 } else { 344 // Check for '-' separately so we don't get false hits in the table. 345 if (currencyCode.charAt(2) == '-') { 346 throw new IllegalArgumentException(); 347 } 348 int index = otherCurrencies.indexOf(currencyCode); 349 if (index == -1) { 350 throw new IllegalArgumentException(); 351 } 352 defaultFractionDigits = otherCurrenciesDFD[index / 4]; 353 numericCode = otherCurrenciesNumericCode[index / 4]; 354 } 355 } 356 357 Currency currencyVal = 358 new Currency(currencyCode, defaultFractionDigits, numericCode); 359 instance = instances.putIfAbsent(currencyCode, currencyVal); 360 return (instance != null ? instance : currencyVal); 361 } 362 363 /** 364 * Returns the <code>Currency</code> instance for the country of the 365 * given locale. The language and variant components of the locale 366 * are ignored. The result may vary over time, as countries change their 367 * currencies. For example, for the original member countries of the 368 * European Monetary Union, the method returns the old national currencies 369 * until December 31, 2001, and the Euro from January 1, 2002, local time 370 * of the respective countries. 371 * <p> 372 * The method returns <code>null</code> for territories that don't 373 * have a currency, such as Antarctica. 393 394 char char1 = country.charAt(0); 395 char char2 = country.charAt(1); 396 int tableEntry = getMainTableEntry(char1, char2); 397 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 398 && tableEntry != INVALID_COUNTRY_ENTRY) { 399 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); 400 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 401 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 402 StringBuilder sb = new StringBuilder(country); 403 sb.append(finalChar); 404 return getInstance(sb.toString(), defaultFractionDigits, numericCode); 405 } else { 406 // special cases 407 if (tableEntry == INVALID_COUNTRY_ENTRY) { 408 throw new IllegalArgumentException(); 409 } 410 if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) { 411 return null; 412 } else { 413 int index = (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA; 414 if (scCutOverTimes[index] == Long.MAX_VALUE || System.currentTimeMillis() < scCutOverTimes[index]) { 415 return getInstance(scOldCurrencies[index], scOldCurrenciesDFD[index], 416 scOldCurrenciesNumericCode[index]); 417 } else { 418 return getInstance(scNewCurrencies[index], scNewCurrenciesDFD[index], 419 scNewCurrenciesNumericCode[index]); 420 } 421 } 422 } 423 } 424 425 /** 426 * Gets the set of available currencies. The returned set of currencies 427 * contains all of the available currencies, which may include currencies 428 * that represent obsolete ISO 4217 codes. The set can be modified 429 * without affecting the available currencies in the runtime. 430 * 431 * @return the set of available currencies. If there is no currency 432 * available in the runtime, the returned set is empty. 433 * @since 1.7 434 */ 435 public static Set<Currency> getAvailableCurrencies() { 436 synchronized(Currency.class) { 437 if (available == null) { 438 available = new HashSet<>(256); 439 440 // Add simple currencies first 441 for (char c1 = 'A'; c1 <= 'Z'; c1 ++) { 442 for (char c2 = 'A'; c2 <= 'Z'; c2 ++) { 443 int tableEntry = getMainTableEntry(c1, c2); 444 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 445 && tableEntry != INVALID_COUNTRY_ENTRY) { 446 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); 447 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 448 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 449 StringBuilder sb = new StringBuilder(); 450 sb.append(c1); 451 sb.append(c2); 452 sb.append(finalChar); 453 available.add(getInstance(sb.toString(), defaultFractionDigits, numericCode)); 454 } 455 } 456 } 457 458 // Now add other currencies 459 StringTokenizer st = new StringTokenizer(otherCurrencies, "-"); 460 while (st.hasMoreElements()) { 461 available.add(getInstance((String)st.nextElement())); 462 } 463 } 464 } 465 466 @SuppressWarnings("unchecked") 467 Set<Currency> result = (Set<Currency>) available.clone(); 468 return result; 469 } 470 471 /** 472 * Gets the ISO 4217 currency code of this currency. 473 * 474 * @return the ISO 4217 currency code of this currency. 475 */ 476 public String getCurrencyCode() { 477 return currencyCode; 478 } 479 480 /** 481 * Gets the symbol of this currency for the default 676 return currencyNameProvider.getSymbol(key, locale); 677 case DISPLAYNAME: 678 return currencyNameProvider.getDisplayName(key, locale); 679 default: 680 assert false; // shouldn't happen 681 } 682 683 return null; 684 } 685 } 686 687 private static int[] readIntArray(DataInputStream dis, int count) throws IOException { 688 int[] ret = new int[count]; 689 for (int i = 0; i < count; i++) { 690 ret[i] = dis.readInt(); 691 } 692 693 return ret; 694 } 695 696 private static long[] readLongArray(DataInputStream dis, int count) throws IOException { 697 long[] ret = new long[count]; 698 for (int i = 0; i < count; i++) { 699 ret[i] = dis.readLong(); 700 } 701 702 return ret; 703 } 704 705 private static String[] readStringArray(DataInputStream dis, int count) throws IOException { 706 String[] ret = new String[count]; 707 for (int i = 0; i < count; i++) { 708 ret[i] = dis.readUTF(); 709 } 710 711 return ret; 712 } 713 714 /** 715 * Replaces currency data found in the currencydata.properties file 716 * 717 * @param pattern regex pattern for the properties 718 * @param ctry country code 719 * @param curdata currency data. This is a comma separated string that 720 * consists of "three-letter alphabet code", "three-digit numeric code", 721 * and "one-digit (0-9) default fraction digit". 722 * For example, "JPZ,392,0". 723 * An optional UTC date can be appended to the string (comma separated) 724 * to allow a currency change take effect after date specified. 725 * For example, "JP=JPZ,999,0,2014-01-01T00:00:00" has no effect unless 726 * UTC time is past 1st January 2014 00:00:00 GMT. 727 */ 728 private static void replaceCurrencyData(Pattern pattern, String ctry, String curdata) { 729 730 if (ctry.length() != 2) { 731 // ignore invalid country code 749 " ignored since cutover date has not passed :" + curdata, null); 750 return; 751 } 752 } catch (ParseException ex) { 753 info("currency.properties entry for " + ctry + 754 " ignored since exception encountered :" + ex.getMessage(), null); 755 return; 756 } 757 758 String code = m.group(1); 759 int numeric = Integer.parseInt(m.group(2)); 760 int entry = numeric << NUMERIC_CODE_SHIFT; 761 int fraction = Integer.parseInt(m.group(3)); 762 if (fraction > SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS) { 763 info("currency.properties entry for " + ctry + 764 " ignored since the fraction is more than " + 765 SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS + ":" + curdata, null); 766 return; 767 } 768 769 int index; 770 for (index = 0; index < scOldCurrencies.length; index++) { 771 if (scOldCurrencies[index].equals(code)) { 772 break; 773 } 774 } 775 776 if (index == scOldCurrencies.length) { 777 // simple case 778 entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT) | 779 (code.charAt(2) - 'A'); 780 } else { 781 // special case 782 entry |= SPECIAL_CASE_COUNTRY_MASK | 783 (index + SPECIAL_CASE_COUNTRY_INDEX_DELTA); 784 } 785 setMainTableEntry(ctry.charAt(0), ctry.charAt(1), entry); 786 } 787 788 private static boolean isPastCutoverDate(String s) throws ParseException { 789 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT); 790 format.setTimeZone(TimeZone.getTimeZone("UTC")); 791 format.setLenient(false); 792 long time = format.parse(s.trim()).getTime(); 793 return System.currentTimeMillis() > time; 794 795 } 796 797 private static int countOccurrences(String value, char match) { 798 int count = 0; 799 for (char c : value.toCharArray()) { 800 if (c == match) { 801 ++count; 802 } 803 } 804 return count; 805 } 806 807 private static void info(String message, Throwable t) { 808 PlatformLogger logger = PlatformLogger.getLogger("java.util.Currency"); 809 if (logger.isLoggable(PlatformLogger.Level.INFO)) { 810 if (t != null) { 811 logger.info(message, t); 812 } else { 813 logger.info(message); 814 } 815 } 816 } 817 } 818 | 136 // - map country codes to currency codes 137 // - obtain default fraction digits for currency codes 138 // 139 // sc = special case; dfd = default fraction digits 140 // Simple countries are those where the country code is a prefix of the 141 // currency code, and there are no known plans to change the currency. 142 // 143 // table formats: 144 // - mainTable: 145 // - maps country code to 32-bit int 146 // - 26*26 entries, corresponding to [A-Z]*[A-Z] 147 // - \u007F -> not valid country 148 // - bits 20-31: unused 149 // - bits 10-19: numeric code (0 to 1023) 150 // - bit 9: 1 - special case, bits 0-4 indicate which one 151 // 0 - simple country, bits 0-4 indicate final char of currency code 152 // - bits 5-8: fraction digits for simple countries, 0 for special cases 153 // - bits 0-4: final char for currency code for simple country, or ID of special case 154 // - special case IDs: 155 // - 0: country has no currency 156 // - other: index into specialCasesList 157 158 static int formatVersion; 159 static int dataVersion; 160 static int[] mainTable; 161 static List<SpecialCaseEntry> specialCasesList; 162 static List<OtherCurrencyEntry> otherCurrenciesList; 163 164 // handy constants - must match definitions in GenerateCurrencyData 165 // magic number 166 private static final int MAGIC_NUMBER = 0x43757244; 167 // number of characters from A to Z 168 private static final int A_TO_Z = ('Z' - 'A') + 1; 169 // entry for invalid country codes 170 private static final int INVALID_COUNTRY_ENTRY = 0x0000007F; 171 // entry for countries without currency 172 private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x00000200; 173 // mask for simple case country entries 174 private static final int SIMPLE_CASE_COUNTRY_MASK = 0x00000000; 175 // mask for simple case country entry final character 176 private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x0000001F; 177 // mask for simple case country entry default currency digits 178 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x000001E0; 179 // shift count for simple case country entry default currency digits 180 private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT = 5; 181 // maximum number for simple case country entry default currency digits 182 private static final int SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS = 9; 183 // mask for special case country entries 184 private static final int SPECIAL_CASE_COUNTRY_MASK = 0x00000200; 185 // mask for special case country index 186 private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x0000001F; 187 // delta from entry index component in main table to index into special case tables 188 private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1; 189 // mask for distinguishing simple and special case countries 190 private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK; 191 // mask for the numeric code of the currency 192 private static final int NUMERIC_CODE_MASK = 0x000FFC00; 193 // shift count for the numeric code of the currency 194 private static final int NUMERIC_CODE_SHIFT = 10; 195 196 // Currency data format version 197 private static final int VALID_FORMAT_VERSION = 3; 198 199 static { 200 AccessController.doPrivileged(new PrivilegedAction<>() { 201 @Override 202 public Void run() { 203 try { 204 try (InputStream in = getClass().getResourceAsStream("/java/util/currency.data")) { 205 if (in == null) { 206 throw new InternalError("Currency data not found"); 207 } 208 DataInputStream dis = new DataInputStream(new BufferedInputStream(in)); 209 if (dis.readInt() != MAGIC_NUMBER) { 210 throw new InternalError("Currency data is possibly corrupted"); 211 } 212 formatVersion = dis.readInt(); 213 if (formatVersion != VALID_FORMAT_VERSION) { 214 throw new InternalError("Currency data format is incorrect"); 215 } 216 dataVersion = dis.readInt(); 217 mainTable = readIntArray(dis, A_TO_Z * A_TO_Z); 218 int scCount = dis.readInt(); 219 specialCasesList = readSpecialCases(dis, scCount); 220 int ocCount = dis.readInt(); 221 otherCurrenciesList = readOtherCurrencies(dis, ocCount); 222 } 223 } catch (IOException e) { 224 throw new InternalError(e); 225 } 226 227 // look for the properties file for overrides 228 String propsFile = System.getProperty("java.util.currency.data"); 229 if (propsFile == null) { 230 propsFile = System.getProperty("java.home") + File.separator + "lib" + 231 File.separator + "currency.properties"; 232 } 233 try { 234 File propFile = new File(propsFile); 235 if (propFile.exists()) { 236 Properties props = new Properties(); 237 try (FileReader fr = new FileReader(propFile)) { 238 props.load(fr); 239 } 240 Set<String> keys = props.stringPropertyNames(); 241 Pattern propertiesPattern = 284 * a supported ISO 4217 code. 285 */ 286 public static Currency getInstance(String currencyCode) { 287 return getInstance(currencyCode, Integer.MIN_VALUE, 0); 288 } 289 290 private static Currency getInstance(String currencyCode, int defaultFractionDigits, 291 int numericCode) { 292 // Try to look up the currency code in the instances table. 293 // This does the null pointer check as a side effect. 294 // Also, if there already is an entry, the currencyCode must be valid. 295 Currency instance = instances.get(currencyCode); 296 if (instance != null) { 297 return instance; 298 } 299 300 if (defaultFractionDigits == Integer.MIN_VALUE) { 301 // Currency code not internally generated, need to verify first 302 // A currency code must have 3 characters and exist in the main table 303 // or in the list of other currencies. 304 boolean found = false; 305 if (currencyCode.length() != 3) { 306 throw new IllegalArgumentException(); 307 } 308 char char1 = currencyCode.charAt(0); 309 char char2 = currencyCode.charAt(1); 310 int tableEntry = getMainTableEntry(char1, char2); 311 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 312 && tableEntry != INVALID_COUNTRY_ENTRY 313 && currencyCode.charAt(2) - 'A' == (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) { 314 defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 315 numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 316 found = true; 317 } else { //special case 318 int[] fractionAndNumericCode = SpecialCaseEntry.findEntry(currencyCode); 319 if (fractionAndNumericCode != null) { 320 defaultFractionDigits = fractionAndNumericCode[0]; 321 numericCode = fractionAndNumericCode[1]; 322 found = true; 323 } 324 } 325 326 if (!found) { 327 OtherCurrencyEntry ocEntry = OtherCurrencyEntry.findEntry(currencyCode); 328 if (ocEntry == null) { 329 throw new IllegalArgumentException(); 330 } 331 defaultFractionDigits = ocEntry.fraction; 332 numericCode = ocEntry.numericCode; 333 } 334 } 335 336 Currency currencyVal = 337 new Currency(currencyCode, defaultFractionDigits, numericCode); 338 instance = instances.putIfAbsent(currencyCode, currencyVal); 339 return (instance != null ? instance : currencyVal); 340 } 341 342 /** 343 * Returns the <code>Currency</code> instance for the country of the 344 * given locale. The language and variant components of the locale 345 * are ignored. The result may vary over time, as countries change their 346 * currencies. For example, for the original member countries of the 347 * European Monetary Union, the method returns the old national currencies 348 * until December 31, 2001, and the Euro from January 1, 2002, local time 349 * of the respective countries. 350 * <p> 351 * The method returns <code>null</code> for territories that don't 352 * have a currency, such as Antarctica. 372 373 char char1 = country.charAt(0); 374 char char2 = country.charAt(1); 375 int tableEntry = getMainTableEntry(char1, char2); 376 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 377 && tableEntry != INVALID_COUNTRY_ENTRY) { 378 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); 379 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 380 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 381 StringBuilder sb = new StringBuilder(country); 382 sb.append(finalChar); 383 return getInstance(sb.toString(), defaultFractionDigits, numericCode); 384 } else { 385 // special cases 386 if (tableEntry == INVALID_COUNTRY_ENTRY) { 387 throw new IllegalArgumentException(); 388 } 389 if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) { 390 return null; 391 } else { 392 int index = SpecialCaseEntry.toIndex(tableEntry); 393 SpecialCaseEntry scEntry = specialCasesList.get(index); 394 if (scEntry.cutOverTime == Long.MAX_VALUE 395 || System.currentTimeMillis() < scEntry.cutOverTime) { 396 return getInstance(scEntry.oldCurrency, 397 scEntry.oldCurrencyFraction, 398 scEntry.oldCurrencyNumericCode); 399 } else { 400 return getInstance(scEntry.newCurrency, 401 scEntry.newCurrencyFraction, 402 scEntry.newCurrencyNumericCode); 403 } 404 } 405 } 406 } 407 408 /** 409 * Gets the set of available currencies. The returned set of currencies 410 * contains all of the available currencies, which may include currencies 411 * that represent obsolete ISO 4217 codes. The set can be modified 412 * without affecting the available currencies in the runtime. 413 * 414 * @return the set of available currencies. If there is no currency 415 * available in the runtime, the returned set is empty. 416 * @since 1.7 417 */ 418 public static Set<Currency> getAvailableCurrencies() { 419 synchronized(Currency.class) { 420 if (available == null) { 421 available = new HashSet<>(256); 422 423 // Add simple currencies first 424 for (char c1 = 'A'; c1 <= 'Z'; c1 ++) { 425 for (char c2 = 'A'; c2 <= 'Z'; c2 ++) { 426 int tableEntry = getMainTableEntry(c1, c2); 427 if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK 428 && tableEntry != INVALID_COUNTRY_ENTRY) { 429 char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A'); 430 int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT; 431 int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT; 432 StringBuilder sb = new StringBuilder(); 433 sb.append(c1); 434 sb.append(c2); 435 sb.append(finalChar); 436 available.add(getInstance(sb.toString(), defaultFractionDigits, numericCode)); 437 } else if ((tableEntry & COUNTRY_TYPE_MASK) == SPECIAL_CASE_COUNTRY_MASK 438 && tableEntry != INVALID_COUNTRY_ENTRY 439 && tableEntry != COUNTRY_WITHOUT_CURRENCY_ENTRY) { 440 int index = SpecialCaseEntry.toIndex(tableEntry); 441 SpecialCaseEntry scEntry = specialCasesList.get(index); 442 443 if (scEntry.cutOverTime == Long.MAX_VALUE 444 || System.currentTimeMillis() < scEntry.cutOverTime) { 445 available.add(getInstance(scEntry.oldCurrency, 446 scEntry.oldCurrencyFraction, 447 scEntry.oldCurrencyNumericCode)); 448 } else { 449 available.add(getInstance(scEntry.newCurrency, 450 scEntry.newCurrencyFraction, 451 scEntry.newCurrencyNumericCode)); 452 } 453 } 454 } 455 } 456 457 // Now add other currencies 458 for (OtherCurrencyEntry entry : otherCurrenciesList) { 459 available.add(getInstance(entry.currencyCode)); 460 } 461 } 462 } 463 464 @SuppressWarnings("unchecked") 465 Set<Currency> result = (Set<Currency>) available.clone(); 466 return result; 467 } 468 469 /** 470 * Gets the ISO 4217 currency code of this currency. 471 * 472 * @return the ISO 4217 currency code of this currency. 473 */ 474 public String getCurrencyCode() { 475 return currencyCode; 476 } 477 478 /** 479 * Gets the symbol of this currency for the default 674 return currencyNameProvider.getSymbol(key, locale); 675 case DISPLAYNAME: 676 return currencyNameProvider.getDisplayName(key, locale); 677 default: 678 assert false; // shouldn't happen 679 } 680 681 return null; 682 } 683 } 684 685 private static int[] readIntArray(DataInputStream dis, int count) throws IOException { 686 int[] ret = new int[count]; 687 for (int i = 0; i < count; i++) { 688 ret[i] = dis.readInt(); 689 } 690 691 return ret; 692 } 693 694 private static List<SpecialCaseEntry> readSpecialCases(DataInputStream dis, 695 int count) 696 throws IOException { 697 698 List<SpecialCaseEntry> list = new ArrayList<>(count); 699 long cutOverTime; 700 String oldCurrency; 701 String newCurrency; 702 int oldCurrencyFraction; 703 int newCurrencyFraction; 704 int oldCurrencyNumericCode; 705 int newCurrencyNumericCode; 706 707 for (int i = 0; i < count; i++) { 708 cutOverTime = dis.readLong(); 709 oldCurrency = dis.readUTF(); 710 newCurrency = dis.readUTF(); 711 oldCurrencyFraction = dis.readInt(); 712 newCurrencyFraction = dis.readInt(); 713 oldCurrencyNumericCode = dis.readInt(); 714 newCurrencyNumericCode = dis.readInt(); 715 SpecialCaseEntry sc = new SpecialCaseEntry(cutOverTime, 716 oldCurrency, newCurrency, 717 oldCurrencyFraction, newCurrencyFraction, 718 oldCurrencyNumericCode, newCurrencyNumericCode); 719 list.add(sc); 720 } 721 return list; 722 } 723 724 private static List<OtherCurrencyEntry> readOtherCurrencies(DataInputStream dis, 725 int count) 726 throws IOException { 727 728 List<OtherCurrencyEntry> list = new ArrayList<>(count); 729 String currencyCode; 730 int fraction; 731 int numericCode; 732 733 for (int i = 0; i < count; i++) { 734 currencyCode = dis.readUTF(); 735 fraction = dis.readInt(); 736 numericCode = dis.readInt(); 737 OtherCurrencyEntry oc = new OtherCurrencyEntry(currencyCode, 738 fraction, 739 numericCode); 740 list.add(oc); 741 } 742 return list; 743 } 744 745 /** 746 * Replaces currency data found in the currencydata.properties file 747 * 748 * @param pattern regex pattern for the properties 749 * @param ctry country code 750 * @param curdata currency data. This is a comma separated string that 751 * consists of "three-letter alphabet code", "three-digit numeric code", 752 * and "one-digit (0-9) default fraction digit". 753 * For example, "JPZ,392,0". 754 * An optional UTC date can be appended to the string (comma separated) 755 * to allow a currency change take effect after date specified. 756 * For example, "JP=JPZ,999,0,2014-01-01T00:00:00" has no effect unless 757 * UTC time is past 1st January 2014 00:00:00 GMT. 758 */ 759 private static void replaceCurrencyData(Pattern pattern, String ctry, String curdata) { 760 761 if (ctry.length() != 2) { 762 // ignore invalid country code 780 " ignored since cutover date has not passed :" + curdata, null); 781 return; 782 } 783 } catch (ParseException ex) { 784 info("currency.properties entry for " + ctry + 785 " ignored since exception encountered :" + ex.getMessage(), null); 786 return; 787 } 788 789 String code = m.group(1); 790 int numeric = Integer.parseInt(m.group(2)); 791 int entry = numeric << NUMERIC_CODE_SHIFT; 792 int fraction = Integer.parseInt(m.group(3)); 793 if (fraction > SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS) { 794 info("currency.properties entry for " + ctry + 795 " ignored since the fraction is more than " + 796 SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS + ":" + curdata, null); 797 return; 798 } 799 800 int index = SpecialCaseEntry.indexOf(code, fraction, numeric); 801 802 /* if a country switches from simple case to special case or 803 * one special case to other special case which is not present 804 * in the sc arrays then insert the new entry in special case arrays 805 */ 806 if (index == -1 && (ctry.charAt(0) != code.charAt(0) 807 || ctry.charAt(1) != code.charAt(1))) { 808 809 specialCasesList.add(new SpecialCaseEntry(code, fraction, numeric)); 810 index = specialCasesList.size() - 1; 811 } 812 813 if (index == -1) { 814 // simple case 815 entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT) 816 | (code.charAt(2) - 'A'); 817 } else { 818 // special case 819 entry = SPECIAL_CASE_COUNTRY_MASK 820 | (index + SPECIAL_CASE_COUNTRY_INDEX_DELTA); 821 } 822 setMainTableEntry(ctry.charAt(0), ctry.charAt(1), entry); 823 } 824 825 private static boolean isPastCutoverDate(String s) throws ParseException { 826 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT); 827 format.setTimeZone(TimeZone.getTimeZone("UTC")); 828 format.setLenient(false); 829 long time = format.parse(s.trim()).getTime(); 830 return System.currentTimeMillis() > time; 831 832 } 833 834 private static int countOccurrences(String value, char match) { 835 int count = 0; 836 for (char c : value.toCharArray()) { 837 if (c == match) { 838 ++count; 839 } 840 } 841 return count; 842 } 843 844 private static void info(String message, Throwable t) { 845 PlatformLogger logger = PlatformLogger.getLogger("java.util.Currency"); 846 if (logger.isLoggable(PlatformLogger.Level.INFO)) { 847 if (t != null) { 848 logger.info(message, t); 849 } else { 850 logger.info(message); 851 } 852 } 853 } 854 855 /* Used to represent a special case currency entry 856 * - cutOverTime: cut-over time in millis as returned by 857 * System.currentTimeMillis for special case countries that are changing 858 * currencies; Long.MAX_VALUE for countries that are not changing currencies 859 * - oldCurrency: old currencies for special case countries 860 * - newCurrency: new currencies for special case countries that are 861 * changing currencies; null for others 862 * - oldCurrencyFraction: default fraction digits for old currencies 863 * - newCurrencyFraction: default fraction digits for new currencies, 0 for 864 * countries that are not changing currencies 865 * - oldCurrencyNumericCode: numeric code for old currencies 866 * - newCurrencyNumericCode: numeric code for new currencies, 0 for countries 867 * that are not changing currencies 868 */ 869 private static class SpecialCaseEntry { 870 871 final private long cutOverTime; 872 final private String oldCurrency; 873 final private String newCurrency; 874 final private int oldCurrencyFraction; 875 final private int newCurrencyFraction; 876 final private int oldCurrencyNumericCode; 877 final private int newCurrencyNumericCode; 878 879 private SpecialCaseEntry(long cutOverTime, String oldCurrency, String newCurrency, 880 int oldCurrencyFraction, int newCurrencyFraction, 881 int oldCurrencyNumericCode, int newCurrencyNumericCode) { 882 this.cutOverTime = cutOverTime; 883 this.oldCurrency = oldCurrency; 884 this.newCurrency = newCurrency; 885 this.oldCurrencyFraction = oldCurrencyFraction; 886 this.newCurrencyFraction = newCurrencyFraction; 887 this.oldCurrencyNumericCode = oldCurrencyNumericCode; 888 this.newCurrencyNumericCode = newCurrencyNumericCode; 889 } 890 891 private SpecialCaseEntry(String currencyCode, int fraction, 892 int numericCode) { 893 this(Long.MAX_VALUE, currencyCode, "", fraction, 0, numericCode, 0); 894 } 895 896 //get the index of the special case entry 897 private static int indexOf(String code, int fraction, int numeric) { 898 int size = specialCasesList.size(); 899 for (int index = 0; index < size; index++) { 900 SpecialCaseEntry scEntry = specialCasesList.get(index); 901 if (scEntry.oldCurrency.equals(code) 902 && scEntry.oldCurrencyFraction == fraction 903 && scEntry.oldCurrencyNumericCode == numeric 904 && scEntry.cutOverTime == Long.MAX_VALUE) { 905 return index; 906 } 907 } 908 return -1; 909 } 910 911 // get the fraction and numericCode of the sc currencycode 912 private static int[] findEntry(String code) { 913 int[] fractionAndNumericCode = null; 914 int size = specialCasesList.size(); 915 for (int index = 0; index < size; index++) { 916 SpecialCaseEntry scEntry = specialCasesList.get(index); 917 if (scEntry.oldCurrency.equals(code) && (scEntry.cutOverTime == Long.MAX_VALUE 918 || System.currentTimeMillis() < scEntry.cutOverTime)) { 919 //consider only when there is no new currency or cutover time is not passed 920 fractionAndNumericCode = new int[2]; 921 fractionAndNumericCode[0] = scEntry.oldCurrencyFraction; 922 fractionAndNumericCode[1] = scEntry.oldCurrencyNumericCode; 923 break; 924 } else if (scEntry.newCurrency.equals(code) 925 && System.currentTimeMillis() >= scEntry.cutOverTime) { 926 //consider only if the cutover time is passed 927 fractionAndNumericCode = new int[2]; 928 fractionAndNumericCode[0] = scEntry.newCurrencyFraction; 929 fractionAndNumericCode[1] = scEntry.newCurrencyNumericCode; 930 break; 931 } 932 } 933 return fractionAndNumericCode; 934 } 935 936 // convert the special case entry to sc arrays index 937 private static int toIndex(int tableEntry) { 938 return (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA; 939 } 940 941 } 942 943 /* Used to represent Other currencies 944 * - currencyCode: currency codes that are not the main currency 945 * of a simple country 946 * - otherCurrenciesDFD: decimal format digits for other currencies 947 * - otherCurrenciesNumericCode: numeric code for other currencies 948 */ 949 private static class OtherCurrencyEntry { 950 951 final private String currencyCode; 952 final private int fraction; 953 final private int numericCode; 954 955 private OtherCurrencyEntry(String currencyCode, int fraction, 956 int numericCode) { 957 this.currencyCode = currencyCode; 958 this.fraction = fraction; 959 this.numericCode = numericCode; 960 } 961 962 //get the instance of the other currency code 963 private static OtherCurrencyEntry findEntry(String code) { 964 int size = otherCurrenciesList.size(); 965 for (int index = 0; index < size; index++) { 966 OtherCurrencyEntry ocEntry = otherCurrenciesList.get(index); 967 if (ocEntry.currencyCode.equalsIgnoreCase(code)) { 968 return ocEntry; 969 } 970 } 971 return null; 972 } 973 974 } 975 976 } 977 978 |