开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 0 Fork 0

gwdcode/JDK11.0.2-lib.src.java.base.java

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
Currency.java 48.77 KB
一键复制 编辑 原始数据 按行查看 历史
gwdcode 提交于 2020年10月30日 22:13 +08:00 . 常用普通java包(jdk11.0.2)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.util;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.IOException;
import java.io.Serializable;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.spi.CurrencyNameProvider;
import java.util.stream.Collectors;
import jdk.internal.util.StaticProperty;
import sun.util.locale.provider.CalendarDataUtility;
import sun.util.locale.provider.LocaleServiceProviderPool;
import sun.util.logging.PlatformLogger;
/**
* Represents a currency. Currencies are identified by their ISO 4217 currency
* codes. Visit the <a href="http://www.iso.org/iso/home/standards/currency_codes.htm">
* ISO web site</a> for more information.
* <p>
* The class is designed so that there's never more than one
* <code>Currency</code> instance for any given currency. Therefore, there's
* no public constructor. You obtain a <code>Currency</code> instance using
* the <code>getInstance</code> methods.
* <p>
* Users can supersede the Java runtime currency data by means of the system
* property {@code java.util.currency.data}. If this system property is
* defined then its value is the location of a properties file, the contents of
* which are key/value pairs of the ISO 3166 country codes and the ISO 4217
* currency data respectively. The value part consists of three ISO 4217 values
* of a currency, i.e., an alphabetic code, a numeric code, and a minor unit.
* Those three ISO 4217 values are separated by commas.
* The lines which start with '#'s are considered comment lines. An optional UTC
* timestamp may be specified per currency entry if users need to specify a
* cutover date indicating when the new data comes into effect. The timestamp is
* appended to the end of the currency properties and uses a comma as a separator.
* If a UTC datestamp is present and valid, the JRE will only use the new currency
* properties if the current UTC date is later than the date specified at class
* loading time. The format of the timestamp must be of ISO 8601 format :
* {@code 'yyyy-MM-dd'T'HH:mm:ss'}. For example,
* <p>
* <code>
* #Sample currency properties<br>
* JP=JPZ,999,0
* </code>
* <p>
* will supersede the currency data for Japan. If JPZ is one of the existing
* ISO 4217 currency code referred by other countries, the existing
* JPZ currency data is updated with the given numeric code and minor
* unit value.
*
* <p>
* <code>
* #Sample currency properties with cutover date<br>
* JP=JPZ,999,0,2014年01月01日T00:00:00
* </code>
* <p>
* will supersede the currency data for Japan if {@code Currency} class is loaded after
* 1st January 2014 00:00:00 GMT.
* <p>
* Where syntactically malformed entries are encountered, the entry is ignored
* and the remainder of entries in file are processed. For instances where duplicate
* country code entries exist, the behavior of the Currency information for that
* {@code Currency} is undefined and the remainder of entries in file are processed.
* <p>
* If multiple property entries with same currency code but different numeric code
* and/or minor unit are encountered, those entries are ignored and the remainder
* of entries in file are processed.
*
* <p>
* It is recommended to use {@link java.math.BigDecimal} class while dealing
* with {@code Currency} or monetary values as it provides better handling of floating
* point numbers and their operations.
*
* @see java.math.BigDecimal
* @since 1.4
*/
public final class Currency implements Serializable {
private static final long serialVersionUID = -158308464356906721L;
/**
* ISO 4217 currency code for this currency.
*
* @serial
*/
private final String currencyCode;
/**
* Default fraction digits for this currency.
* Set from currency data tables.
*/
private final transient int defaultFractionDigits;
/**
* ISO 4217 numeric code for this currency.
* Set from currency data tables.
*/
private final transient int numericCode;
// class data: instance map
private static ConcurrentMap<String, Currency> instances = new ConcurrentHashMap<>(7);
private static HashSet<Currency> available;
// Class data: currency data obtained from currency.data file.
// Purpose:
// - determine valid country codes
// - determine valid currency codes
// - map country codes to currency codes
// - obtain default fraction digits for currency codes
//
// sc = special case; dfd = default fraction digits
// Simple countries are those where the country code is a prefix of the
// currency code, and there are no known plans to change the currency.
//
// table formats:
// - mainTable:
// - maps country code to 32-bit int
// - 26*26 entries, corresponding to [A-Z]*[A-Z]
// - \u007F -> not valid country
// - bits 20-31: unused
// - bits 10-19: numeric code (0 to 1023)
// - bit 9: 1 - special case, bits 0-4 indicate which one
// 0 - simple country, bits 0-4 indicate final char of currency code
// - bits 5-8: fraction digits for simple countries, 0 for special cases
// - bits 0-4: final char for currency code for simple country, or ID of special case
// - special case IDs:
// - 0: country has no currency
// - other: index into specialCasesList
static int formatVersion;
static int dataVersion;
static int[] mainTable;
static List<SpecialCaseEntry> specialCasesList;
static List<OtherCurrencyEntry> otherCurrenciesList;
// handy constants - must match definitions in GenerateCurrencyData
// magic number
private static final int MAGIC_NUMBER = 0x43757244;
// number of characters from A to Z
private static final int A_TO_Z = ('Z' - 'A') + 1;
// entry for invalid country codes
private static final int INVALID_COUNTRY_ENTRY = 0x0000007F;
// entry for countries without currency
private static final int COUNTRY_WITHOUT_CURRENCY_ENTRY = 0x00000200;
// mask for simple case country entries
private static final int SIMPLE_CASE_COUNTRY_MASK = 0x00000000;
// mask for simple case country entry final character
private static final int SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK = 0x0000001F;
// mask for simple case country entry default currency digits
private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK = 0x000001E0;
// shift count for simple case country entry default currency digits
private static final int SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT = 5;
// maximum number for simple case country entry default currency digits
private static final int SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS = 9;
// mask for special case country entries
private static final int SPECIAL_CASE_COUNTRY_MASK = 0x00000200;
// mask for special case country index
private static final int SPECIAL_CASE_COUNTRY_INDEX_MASK = 0x0000001F;
// delta from entry index component in main table to index into special case tables
private static final int SPECIAL_CASE_COUNTRY_INDEX_DELTA = 1;
// mask for distinguishing simple and special case countries
private static final int COUNTRY_TYPE_MASK = SIMPLE_CASE_COUNTRY_MASK | SPECIAL_CASE_COUNTRY_MASK;
// mask for the numeric code of the currency
private static final int NUMERIC_CODE_MASK = 0x000FFC00;
// shift count for the numeric code of the currency
private static final int NUMERIC_CODE_SHIFT = 10;
// Currency data format version
private static final int VALID_FORMAT_VERSION = 3;
static {
AccessController.doPrivileged(new PrivilegedAction<>() {
@Override
public Void run() {
try {
try (InputStream in = getClass().getResourceAsStream("/java/util/currency.data")) {
if (in == null) {
throw new InternalError("Currency data not found");
}
DataInputStream dis = new DataInputStream(new BufferedInputStream(in));
if (dis.readInt() != MAGIC_NUMBER) {
throw new InternalError("Currency data is possibly corrupted");
}
formatVersion = dis.readInt();
if (formatVersion != VALID_FORMAT_VERSION) {
throw new InternalError("Currency data format is incorrect");
}
dataVersion = dis.readInt();
mainTable = readIntArray(dis, A_TO_Z * A_TO_Z);
int scCount = dis.readInt();
specialCasesList = readSpecialCases(dis, scCount);
int ocCount = dis.readInt();
otherCurrenciesList = readOtherCurrencies(dis, ocCount);
}
} catch (IOException e) {
throw new InternalError(e);
}
// look for the properties file for overrides
String propsFile = System.getProperty("java.util.currency.data");
if (propsFile == null) {
propsFile = StaticProperty.javaHome() + File.separator + "lib" +
File.separator + "currency.properties";
}
try {
File propFile = new File(propsFile);
if (propFile.exists()) {
Properties props = new Properties();
try (FileReader fr = new FileReader(propFile)) {
props.load(fr);
}
Pattern propertiesPattern =
Pattern.compile("([A-Z]{3})\\s*,\\s*(\\d{3})\\s*,\\s*" +
"(\\d+)\\s*,?\\s*(\\d{4}-\\d{2}-\\d{2}T\\d{2}:" +
"\\d{2}:\\d{2})?");
List<CurrencyProperty> currencyEntries
= getValidCurrencyData(props, propertiesPattern);
currencyEntries.forEach(Currency::replaceCurrencyData);
}
} catch (IOException e) {
CurrencyProperty.info("currency.properties is ignored"
+ " because of an IOException", e);
}
return null;
}
});
}
/**
* Constants for retrieving localized names from the name providers.
*/
private static final int SYMBOL = 0;
private static final int DISPLAYNAME = 1;
/**
* Constructs a <code>Currency</code> instance. The constructor is private
* so that we can insure that there's never more than one instance for a
* given currency.
*/
private Currency(String currencyCode, int defaultFractionDigits, int numericCode) {
this.currencyCode = currencyCode;
this.defaultFractionDigits = defaultFractionDigits;
this.numericCode = numericCode;
}
/**
* Returns the <code>Currency</code> instance for the given currency code.
*
* @param currencyCode the ISO 4217 code of the currency
* @return the <code>Currency</code> instance for the given currency code
* @exception NullPointerException if <code>currencyCode</code> is null
* @exception IllegalArgumentException if <code>currencyCode</code> is not
* a supported ISO 4217 code.
*/
public static Currency getInstance(String currencyCode) {
return getInstance(currencyCode, Integer.MIN_VALUE, 0);
}
private static Currency getInstance(String currencyCode, int defaultFractionDigits,
int numericCode) {
// Try to look up the currency code in the instances table.
// This does the null pointer check as a side effect.
// Also, if there already is an entry, the currencyCode must be valid.
Currency instance = instances.get(currencyCode);
if (instance != null) {
return instance;
}
if (defaultFractionDigits == Integer.MIN_VALUE) {
// Currency code not internally generated, need to verify first
// A currency code must have 3 characters and exist in the main table
// or in the list of other currencies.
boolean found = false;
if (currencyCode.length() != 3) {
throw new IllegalArgumentException();
}
char char1 = currencyCode.charAt(0);
char char2 = currencyCode.charAt(1);
int tableEntry = getMainTableEntry(char1, char2);
if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
&& tableEntry != INVALID_COUNTRY_ENTRY
&& currencyCode.charAt(2) - 'A' == (tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) {
defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
found = true;
} else { //special case
int[] fractionAndNumericCode = SpecialCaseEntry.findEntry(currencyCode);
if (fractionAndNumericCode != null) {
defaultFractionDigits = fractionAndNumericCode[0];
numericCode = fractionAndNumericCode[1];
found = true;
}
}
if (!found) {
OtherCurrencyEntry ocEntry = OtherCurrencyEntry.findEntry(currencyCode);
if (ocEntry == null) {
throw new IllegalArgumentException();
}
defaultFractionDigits = ocEntry.fraction;
numericCode = ocEntry.numericCode;
}
}
Currency currencyVal =
new Currency(currencyCode, defaultFractionDigits, numericCode);
instance = instances.putIfAbsent(currencyCode, currencyVal);
return (instance != null ? instance : currencyVal);
}
/**
* Returns the <code>Currency</code> instance for the country of the
* given locale. The language and variant components of the locale
* are ignored. The result may vary over time, as countries change their
* currencies. For example, for the original member countries of the
* European Monetary Union, the method returns the old national currencies
* until December 31, 2001, and the Euro from January 1, 2002, local time
* of the respective countries.
* <p>
* If the specified {@code locale} contains "cu" and/or "rg"
* <a href="./Locale.html#def_locale_extension">Unicode extensions</a>,
* the instance returned from this method reflects
* the values specified with those extensions. If both "cu" and "rg" are
* specified, the currency from the "cu" extension supersedes the implicit one
* from the "rg" extension.
* <p>
* The method returns <code>null</code> for territories that don't
* have a currency, such as Antarctica.
*
* @param locale the locale for whose country a <code>Currency</code>
* instance is needed
* @return the <code>Currency</code> instance for the country of the given
* locale, or {@code null}
* @exception NullPointerException if <code>locale</code>
* is {@code null}
* @exception IllegalArgumentException if the country of the given {@code locale}
* is not a supported ISO 3166 country code.
*/
public static Currency getInstance(Locale locale) {
// check for locale overrides
String override = locale.getUnicodeLocaleType("cu");
if (override != null) {
try {
return getInstance(override.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException iae) {
// override currency is invalid. Fall through.
}
}
String country = CalendarDataUtility.findRegionOverride(locale).getCountry();
if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
throw new IllegalArgumentException();
}
char char1 = country.charAt(0);
char char2 = country.charAt(1);
int tableEntry = getMainTableEntry(char1, char2);
if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
&& tableEntry != INVALID_COUNTRY_ENTRY) {
char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');
int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
StringBuilder sb = new StringBuilder(country);
sb.append(finalChar);
return getInstance(sb.toString(), defaultFractionDigits, numericCode);
} else {
// special cases
if (tableEntry == INVALID_COUNTRY_ENTRY) {
throw new IllegalArgumentException();
}
if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) {
return null;
} else {
int index = SpecialCaseEntry.toIndex(tableEntry);
SpecialCaseEntry scEntry = specialCasesList.get(index);
if (scEntry.cutOverTime == Long.MAX_VALUE
|| System.currentTimeMillis() < scEntry.cutOverTime) {
return getInstance(scEntry.oldCurrency,
scEntry.oldCurrencyFraction,
scEntry.oldCurrencyNumericCode);
} else {
return getInstance(scEntry.newCurrency,
scEntry.newCurrencyFraction,
scEntry.newCurrencyNumericCode);
}
}
}
}
/**
* Gets the set of available currencies. The returned set of currencies
* contains all of the available currencies, which may include currencies
* that represent obsolete ISO 4217 codes. The set can be modified
* without affecting the available currencies in the runtime.
*
* @return the set of available currencies. If there is no currency
* available in the runtime, the returned set is empty.
* @since 1.7
*/
public static Set<Currency> getAvailableCurrencies() {
synchronized(Currency.class) {
if (available == null) {
available = new HashSet<>(256);
// Add simple currencies first
for (char c1 = 'A'; c1 <= 'Z'; c1 ++) {
for (char c2 = 'A'; c2 <= 'Z'; c2 ++) {
int tableEntry = getMainTableEntry(c1, c2);
if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
&& tableEntry != INVALID_COUNTRY_ENTRY) {
char finalChar = (char) ((tableEntry & SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK) + 'A');
int defaultFractionDigits = (tableEntry & SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK) >> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
int numericCode = (tableEntry & NUMERIC_CODE_MASK) >> NUMERIC_CODE_SHIFT;
StringBuilder sb = new StringBuilder();
sb.append(c1);
sb.append(c2);
sb.append(finalChar);
available.add(getInstance(sb.toString(), defaultFractionDigits, numericCode));
} else if ((tableEntry & COUNTRY_TYPE_MASK) == SPECIAL_CASE_COUNTRY_MASK
&& tableEntry != INVALID_COUNTRY_ENTRY
&& tableEntry != COUNTRY_WITHOUT_CURRENCY_ENTRY) {
int index = SpecialCaseEntry.toIndex(tableEntry);
SpecialCaseEntry scEntry = specialCasesList.get(index);
if (scEntry.cutOverTime == Long.MAX_VALUE
|| System.currentTimeMillis() < scEntry.cutOverTime) {
available.add(getInstance(scEntry.oldCurrency,
scEntry.oldCurrencyFraction,
scEntry.oldCurrencyNumericCode));
} else {
available.add(getInstance(scEntry.newCurrency,
scEntry.newCurrencyFraction,
scEntry.newCurrencyNumericCode));
}
}
}
}
// Now add other currencies
for (OtherCurrencyEntry entry : otherCurrenciesList) {
available.add(getInstance(entry.currencyCode));
}
}
}
@SuppressWarnings("unchecked")
Set<Currency> result = (Set<Currency>) available.clone();
return result;
}
/**
* Gets the ISO 4217 currency code of this currency.
*
* @return the ISO 4217 currency code of this currency.
*/
public String getCurrencyCode() {
return currencyCode;
}
/**
* Gets the symbol of this currency for the default
* {@link Locale.Category#DISPLAY DISPLAY} locale.
* For example, for the US Dollar, the symbol is "$" if the default
* locale is the US, while for other locales it may be "US$". If no
* symbol can be determined, the ISO 4217 currency code is returned.
* <p>
* If the default {@link Locale.Category#DISPLAY DISPLAY} locale
* contains "rg" (region override)
* <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
* the symbol returned from this method reflects
* the value specified with that extension.
* <p>
* This is equivalent to calling
* {@link #getSymbol(Locale)
* getSymbol(Locale.getDefault(Locale.Category.DISPLAY))}.
*
* @return the symbol of this currency for the default
* {@link Locale.Category#DISPLAY DISPLAY} locale
*/
public String getSymbol() {
return getSymbol(Locale.getDefault(Locale.Category.DISPLAY));
}
/**
* Gets the symbol of this currency for the specified locale.
* For example, for the US Dollar, the symbol is "$" if the specified
* locale is the US, while for other locales it may be "US$". If no
* symbol can be determined, the ISO 4217 currency code is returned.
* <p>
* If the specified {@code locale} contains "rg" (region override)
* <a href="./Locale.html#def_locale_extension">Unicode extension</a>,
* the symbol returned from this method reflects
* the value specified with that extension.
*
* @param locale the locale for which a display name for this currency is
* needed
* @return the symbol of this currency for the specified locale
* @exception NullPointerException if <code>locale</code> is null
*/
public String getSymbol(Locale locale) {
LocaleServiceProviderPool pool =
LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
locale = CalendarDataUtility.findRegionOverride(locale);
String symbol = pool.getLocalizedObject(
CurrencyNameGetter.INSTANCE,
locale, currencyCode, SYMBOL);
if (symbol != null) {
return symbol;
}
// use currency code as symbol of last resort
return currencyCode;
}
/**
* Gets the default number of fraction digits used with this currency.
* Note that the number of fraction digits is the same as ISO 4217's
* minor unit for the currency.
* For example, the default number of fraction digits for the Euro is 2,
* while for the Japanese Yen it's 0.
* In the case of pseudo-currencies, such as IMF Special Drawing Rights,
* -1 is returned.
*
* @return the default number of fraction digits used with this currency
*/
public int getDefaultFractionDigits() {
return defaultFractionDigits;
}
/**
* Returns the ISO 4217 numeric code of this currency.
*
* @return the ISO 4217 numeric code of this currency
* @since 1.7
*/
public int getNumericCode() {
return numericCode;
}
/**
* Returns the 3 digit ISO 4217 numeric code of this currency as a {@code String}.
* Unlike {@link #getNumericCode()}, which returns the numeric code as {@code int},
* this method always returns the numeric code as a 3 digit string.
* e.g. a numeric value of 32 would be returned as "032",
* and a numeric value of 6 would be returned as "006".
*
* @return the 3 digit ISO 4217 numeric code of this currency as a {@code String}
* @since 9
*/
public String getNumericCodeAsString() {
/* numeric code could be returned as a 3 digit string simply by using
String.format("%03d",numericCode); which uses regex to parse the format,
"%03d" in this case. Parsing a regex gives an extra performance overhead,
so String.format() approach is avoided in this scenario.
*/
if (numericCode < 100) {
StringBuilder sb = new StringBuilder();
sb.append('0');
if (numericCode < 10) {
sb.append('0');
}
return sb.append(numericCode).toString();
}
return String.valueOf(numericCode);
}
/**
* Gets the name that is suitable for displaying this currency for
* the default {@link Locale.Category#DISPLAY DISPLAY} locale.
* If there is no suitable display name found
* for the default locale, the ISO 4217 currency code is returned.
* <p>
* This is equivalent to calling
* {@link #getDisplayName(Locale)
* getDisplayName(Locale.getDefault(Locale.Category.DISPLAY))}.
*
* @return the display name of this currency for the default
* {@link Locale.Category#DISPLAY DISPLAY} locale
* @since 1.7
*/
public String getDisplayName() {
return getDisplayName(Locale.getDefault(Locale.Category.DISPLAY));
}
/**
* Gets the name that is suitable for displaying this currency for
* the specified locale. If there is no suitable display name found
* for the specified locale, the ISO 4217 currency code is returned.
*
* @param locale the locale for which a display name for this currency is
* needed
* @return the display name of this currency for the specified locale
* @exception NullPointerException if <code>locale</code> is null
* @since 1.7
*/
public String getDisplayName(Locale locale) {
LocaleServiceProviderPool pool =
LocaleServiceProviderPool.getPool(CurrencyNameProvider.class);
String result = pool.getLocalizedObject(
CurrencyNameGetter.INSTANCE,
locale, currencyCode, DISPLAYNAME);
if (result != null) {
return result;
}
// use currency code as symbol of last resort
return currencyCode;
}
/**
* Returns the ISO 4217 currency code of this currency.
*
* @return the ISO 4217 currency code of this currency
*/
@Override
public String toString() {
return currencyCode;
}
/**
* Resolves instances being deserialized to a single instance per currency.
*/
private Object readResolve() {
return getInstance(currencyCode);
}
/**
* Gets the main table entry for the country whose country code consists
* of char1 and char2.
*/
private static int getMainTableEntry(char char1, char char2) {
if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
throw new IllegalArgumentException();
}
return mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')];
}
/**
* Sets the main table entry for the country whose country code consists
* of char1 and char2.
*/
private static void setMainTableEntry(char char1, char char2, int entry) {
if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
throw new IllegalArgumentException();
}
mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')] = entry;
}
/**
* Obtains a localized currency names from a CurrencyNameProvider
* implementation.
*/
private static class CurrencyNameGetter
implements LocaleServiceProviderPool.LocalizedObjectGetter<CurrencyNameProvider,
String> {
private static final CurrencyNameGetter INSTANCE = new CurrencyNameGetter();
@Override
public String getObject(CurrencyNameProvider currencyNameProvider,
Locale locale,
String key,
Object... params) {
assert params.length == 1;
int type = (Integer)params[0];
switch(type) {
case SYMBOL:
return currencyNameProvider.getSymbol(key, locale);
case DISPLAYNAME:
return currencyNameProvider.getDisplayName(key, locale);
default:
assert false; // shouldn't happen
}
return null;
}
}
private static int[] readIntArray(DataInputStream dis, int count) throws IOException {
int[] ret = new int[count];
for (int i = 0; i < count; i++) {
ret[i] = dis.readInt();
}
return ret;
}
private static List<SpecialCaseEntry> readSpecialCases(DataInputStream dis,
int count)
throws IOException {
List<SpecialCaseEntry> list = new ArrayList<>(count);
long cutOverTime;
String oldCurrency;
String newCurrency;
int oldCurrencyFraction;
int newCurrencyFraction;
int oldCurrencyNumericCode;
int newCurrencyNumericCode;
for (int i = 0; i < count; i++) {
cutOverTime = dis.readLong();
oldCurrency = dis.readUTF();
newCurrency = dis.readUTF();
oldCurrencyFraction = dis.readInt();
newCurrencyFraction = dis.readInt();
oldCurrencyNumericCode = dis.readInt();
newCurrencyNumericCode = dis.readInt();
SpecialCaseEntry sc = new SpecialCaseEntry(cutOverTime,
oldCurrency, newCurrency,
oldCurrencyFraction, newCurrencyFraction,
oldCurrencyNumericCode, newCurrencyNumericCode);
list.add(sc);
}
return list;
}
private static List<OtherCurrencyEntry> readOtherCurrencies(DataInputStream dis,
int count)
throws IOException {
List<OtherCurrencyEntry> list = new ArrayList<>(count);
String currencyCode;
int fraction;
int numericCode;
for (int i = 0; i < count; i++) {
currencyCode = dis.readUTF();
fraction = dis.readInt();
numericCode = dis.readInt();
OtherCurrencyEntry oc = new OtherCurrencyEntry(currencyCode,
fraction,
numericCode);
list.add(oc);
}
return list;
}
/**
* Parse currency data found in the properties file (that
* java.util.currency.data designates) to a List of CurrencyProperty
* instances. Also, remove invalid entries and the multiple currency
* code inconsistencies.
*
* @param props properties containing currency data
* @param pattern regex pattern for the properties entry
* @return list of parsed property entries
*/
private static List<CurrencyProperty> getValidCurrencyData(Properties props,
Pattern pattern) {
Set<String> keys = props.stringPropertyNames();
List<CurrencyProperty> propertyEntries = new ArrayList<>();
// remove all invalid entries and parse all valid currency properties
// entries to a group of CurrencyProperty, classified by currency code
Map<String, List<CurrencyProperty>> currencyCodeGroup = keys.stream()
.map(k -> CurrencyProperty
.getValidEntry(k.toUpperCase(Locale.ROOT),
props.getProperty(k).toUpperCase(Locale.ROOT),
pattern)).flatMap(o -> o.stream())
.collect(Collectors.groupingBy(entry -> entry.currencyCode));
// check each group for inconsistencies
currencyCodeGroup.forEach((curCode, list) -> {
boolean inconsistent = CurrencyProperty
.containsInconsistentInstances(list);
if (inconsistent) {
list.forEach(prop -> CurrencyProperty.info("The property"
+ " entry for " + prop.country + " is inconsistent."
+ " Ignored.", null));
} else {
propertyEntries.addAll(list);
}
});
return propertyEntries;
}
/**
* Replaces currency data found in the properties file that
* java.util.currency.data designates. This method is invoked for
* each valid currency entry.
*
* @param prop CurrencyProperty instance of the valid property entry
*/
private static void replaceCurrencyData(CurrencyProperty prop) {
String ctry = prop.country;
String code = prop.currencyCode;
int numeric = prop.numericCode;
int fraction = prop.fraction;
int entry = numeric << NUMERIC_CODE_SHIFT;
int index = SpecialCaseEntry.indexOf(code, fraction, numeric);
// If a new entry changes the numeric code/dfd of an existing
// currency code, update it in the sc list at the respective
// index and also change it in the other currencies list and
// main table (if that currency code is also used as a
// simple case).
// If all three components do not match with the new entry,
// but the currency code exists in the special case list
// update the sc entry with the new entry
int scCurrencyCodeIndex = -1;
if (index == -1) {
scCurrencyCodeIndex = SpecialCaseEntry.currencyCodeIndex(code);
if (scCurrencyCodeIndex != -1) {
//currency code exists in sc list, then update the old entry
specialCasesList.set(scCurrencyCodeIndex,
new SpecialCaseEntry(code, fraction, numeric));
// also update the entry in other currencies list
OtherCurrencyEntry oe = OtherCurrencyEntry.findEntry(code);
if (oe != null) {
int oIndex = otherCurrenciesList.indexOf(oe);
otherCurrenciesList.set(oIndex, new OtherCurrencyEntry(
code, fraction, numeric));
}
}
}
/* If a country switches from simple case to special case or
* one special case to other special case which is not present
* in the sc arrays then insert the new entry in special case arrays.
* If an entry with given currency code exists, update with the new
* entry.
*/
if (index == -1 && (ctry.charAt(0) != code.charAt(0)
|| ctry.charAt(1) != code.charAt(1))) {
if(scCurrencyCodeIndex == -1) {
specialCasesList.add(new SpecialCaseEntry(code, fraction,
numeric));
index = specialCasesList.size() - 1;
} else {
index = scCurrencyCodeIndex;
}
// update the entry in main table if it exists as a simple case
updateMainTableEntry(code, fraction, numeric);
}
if (index == -1) {
// simple case
entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT)
| (code.charAt(2) - 'A');
} else {
// special case
entry = SPECIAL_CASE_COUNTRY_MASK
| (index + SPECIAL_CASE_COUNTRY_INDEX_DELTA);
}
setMainTableEntry(ctry.charAt(0), ctry.charAt(1), entry);
}
// update the entry in maintable for any simple case found, if a new
// entry as a special case updates the entry in sc list with
// existing currency code
private static void updateMainTableEntry(String code, int fraction,
int numeric) {
// checking the existence of currency code in mainTable
int tableEntry = getMainTableEntry(code.charAt(0), code.charAt(1));
int entry = numeric << NUMERIC_CODE_SHIFT;
if ((tableEntry & COUNTRY_TYPE_MASK) == SIMPLE_CASE_COUNTRY_MASK
&& tableEntry != INVALID_COUNTRY_ENTRY
&& code.charAt(2) - 'A' == (tableEntry
& SIMPLE_CASE_COUNTRY_FINAL_CHAR_MASK)) {
int numericCode = (tableEntry & NUMERIC_CODE_MASK)
>> NUMERIC_CODE_SHIFT;
int defaultFractionDigits = (tableEntry
& SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_MASK)
>> SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT;
if (numeric != numericCode || fraction != defaultFractionDigits) {
// update the entry in main table
entry |= (fraction << SIMPLE_CASE_COUNTRY_DEFAULT_DIGITS_SHIFT)
| (code.charAt(2) - 'A');
setMainTableEntry(code.charAt(0), code.charAt(1), entry);
}
}
}
/* Used to represent a special case currency entry
* - cutOverTime: cut-over time in millis as returned by
* System.currentTimeMillis for special case countries that are changing
* currencies; Long.MAX_VALUE for countries that are not changing currencies
* - oldCurrency: old currencies for special case countries
* - newCurrency: new currencies for special case countries that are
* changing currencies; null for others
* - oldCurrencyFraction: default fraction digits for old currencies
* - newCurrencyFraction: default fraction digits for new currencies, 0 for
* countries that are not changing currencies
* - oldCurrencyNumericCode: numeric code for old currencies
* - newCurrencyNumericCode: numeric code for new currencies, 0 for countries
* that are not changing currencies
*/
private static class SpecialCaseEntry {
final private long cutOverTime;
final private String oldCurrency;
final private String newCurrency;
final private int oldCurrencyFraction;
final private int newCurrencyFraction;
final private int oldCurrencyNumericCode;
final private int newCurrencyNumericCode;
private SpecialCaseEntry(long cutOverTime, String oldCurrency, String newCurrency,
int oldCurrencyFraction, int newCurrencyFraction,
int oldCurrencyNumericCode, int newCurrencyNumericCode) {
this.cutOverTime = cutOverTime;
this.oldCurrency = oldCurrency;
this.newCurrency = newCurrency;
this.oldCurrencyFraction = oldCurrencyFraction;
this.newCurrencyFraction = newCurrencyFraction;
this.oldCurrencyNumericCode = oldCurrencyNumericCode;
this.newCurrencyNumericCode = newCurrencyNumericCode;
}
private SpecialCaseEntry(String currencyCode, int fraction,
int numericCode) {
this(Long.MAX_VALUE, currencyCode, "", fraction, 0, numericCode, 0);
}
//get the index of the special case entry
private static int indexOf(String code, int fraction, int numeric) {
int size = specialCasesList.size();
for (int index = 0; index < size; index++) {
SpecialCaseEntry scEntry = specialCasesList.get(index);
if (scEntry.oldCurrency.equals(code)
&& scEntry.oldCurrencyFraction == fraction
&& scEntry.oldCurrencyNumericCode == numeric
&& scEntry.cutOverTime == Long.MAX_VALUE) {
return index;
}
}
return -1;
}
// get the fraction and numericCode of the sc currencycode
private static int[] findEntry(String code) {
int[] fractionAndNumericCode = null;
int size = specialCasesList.size();
for (int index = 0; index < size; index++) {
SpecialCaseEntry scEntry = specialCasesList.get(index);
if (scEntry.oldCurrency.equals(code) && (scEntry.cutOverTime == Long.MAX_VALUE
|| System.currentTimeMillis() < scEntry.cutOverTime)) {
//consider only when there is no new currency or cutover time is not passed
fractionAndNumericCode = new int[2];
fractionAndNumericCode[0] = scEntry.oldCurrencyFraction;
fractionAndNumericCode[1] = scEntry.oldCurrencyNumericCode;
break;
} else if (scEntry.newCurrency.equals(code)
&& System.currentTimeMillis() >= scEntry.cutOverTime) {
//consider only if the cutover time is passed
fractionAndNumericCode = new int[2];
fractionAndNumericCode[0] = scEntry.newCurrencyFraction;
fractionAndNumericCode[1] = scEntry.newCurrencyNumericCode;
break;
}
}
return fractionAndNumericCode;
}
// get the index based on currency code
private static int currencyCodeIndex(String code) {
int size = specialCasesList.size();
for (int index = 0; index < size; index++) {
SpecialCaseEntry scEntry = specialCasesList.get(index);
if (scEntry.oldCurrency.equals(code) && (scEntry.cutOverTime == Long.MAX_VALUE
|| System.currentTimeMillis() < scEntry.cutOverTime)) {
//consider only when there is no new currency or cutover time is not passed
return index;
} else if (scEntry.newCurrency.equals(code)
&& System.currentTimeMillis() >= scEntry.cutOverTime) {
//consider only if the cutover time is passed
return index;
}
}
return -1;
}
// convert the special case entry to sc arrays index
private static int toIndex(int tableEntry) {
return (tableEntry & SPECIAL_CASE_COUNTRY_INDEX_MASK) - SPECIAL_CASE_COUNTRY_INDEX_DELTA;
}
}
/* Used to represent Other currencies
* - currencyCode: currency codes that are not the main currency
* of a simple country
* - otherCurrenciesDFD: decimal format digits for other currencies
* - otherCurrenciesNumericCode: numeric code for other currencies
*/
private static class OtherCurrencyEntry {
final private String currencyCode;
final private int fraction;
final private int numericCode;
private OtherCurrencyEntry(String currencyCode, int fraction,
int numericCode) {
this.currencyCode = currencyCode;
this.fraction = fraction;
this.numericCode = numericCode;
}
//get the instance of the other currency code
private static OtherCurrencyEntry findEntry(String code) {
int size = otherCurrenciesList.size();
for (int index = 0; index < size; index++) {
OtherCurrencyEntry ocEntry = otherCurrenciesList.get(index);
if (ocEntry.currencyCode.equalsIgnoreCase(code)) {
return ocEntry;
}
}
return null;
}
}
/*
* Used to represent an entry of the properties file that
* java.util.currency.data designates
*
* - country: country representing the currency entry
* - currencyCode: currency code
* - fraction: default fraction digit
* - numericCode: numeric code
* - date: cutover date
*/
private static class CurrencyProperty {
final private String country;
final private String currencyCode;
final private int fraction;
final private int numericCode;
final private String date;
private CurrencyProperty(String country, String currencyCode,
int fraction, int numericCode, String date) {
this.country = country;
this.currencyCode = currencyCode;
this.fraction = fraction;
this.numericCode = numericCode;
this.date = date;
}
/**
* Check the valid currency data and create/return an Optional instance
* of CurrencyProperty
*
* @param ctry country representing the currency data
* @param curData currency data of the given {@code ctry}
* @param pattern regex pattern for the properties entry
* @return Optional containing CurrencyProperty instance, If valid;
* empty otherwise
*/
private static Optional<CurrencyProperty> getValidEntry(String ctry,
String curData,
Pattern pattern) {
CurrencyProperty prop = null;
if (ctry.length() != 2) {
// Invalid country code. Ignore the entry.
} else {
prop = parseProperty(ctry, curData, pattern);
// if the property entry failed any of the below checked
// criteria it is ignored
if (prop == null
|| (prop.date == null && curData.chars()
.map(c -> c == ',' ? 1 : 0).sum() >= 3)) {
// format is not recognized. ignore the data if date
// string is null and we've 4 values, bad date value
prop = null;
} else if (prop.fraction
> SIMPLE_CASE_COUNTRY_MAX_DEFAULT_DIGITS) {
prop = null;
} else {
try {
if (prop.date != null
&& !isPastCutoverDate(prop.date)) {
prop = null;
}
} catch (ParseException ex) {
prop = null;
}
}
}
if (prop == null) {
info("The property entry for " + ctry + " is invalid."
+ " Ignored.", null);
}
return Optional.ofNullable(prop);
}
/*
* Parse properties entry and return CurrencyProperty instance
*/
private static CurrencyProperty parseProperty(String ctry,
String curData, Pattern pattern) {
Matcher m = pattern.matcher(curData);
if (!m.find()) {
return null;
} else {
return new CurrencyProperty(ctry, m.group(1),
Integer.parseInt(m.group(3)),
Integer.parseInt(m.group(2)), m.group(4));
}
}
/**
* Checks if the given list contains multiple inconsistent currency instances
*/
private static boolean containsInconsistentInstances(
List<CurrencyProperty> list) {
int numCode = list.get(0).numericCode;
int fractionDigit = list.get(0).fraction;
return list.stream().anyMatch(prop -> prop.numericCode != numCode
|| prop.fraction != fractionDigit);
}
private static boolean isPastCutoverDate(String s)
throws ParseException {
SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT);
format.setTimeZone(TimeZone.getTimeZone("UTC"));
format.setLenient(false);
long time = format.parse(s.trim()).getTime();
return System.currentTimeMillis() > time;
}
private static void info(String message, Throwable t) {
PlatformLogger logger = PlatformLogger
.getLogger("java.util.Currency");
if (logger.isLoggable(PlatformLogger.Level.INFO)) {
if (t != null) {
logger.info(message, t);
} else {
logger.info(message);
}
}
}
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gwdcode/jdk11.0.2lib.src.java.base.java.git
git@gitee.com:gwdcode/jdk11.0.2lib.src.java.base.java.git
gwdcode
jdk11.0.2lib.src.java.base.java
JDK11.0.2-lib.src.java.base.java
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /