1 7 package com.ibm.icu.util; 8 9 import java.io.Serializable ; 10 import java.text.ChoiceFormat ; 11 import java.text.ParsePosition ; 12 import java.util.Locale ; 13 import java.util.MissingResourceException ; 14 15 import com.ibm.icu.impl.ICUDebug; 16 import com.ibm.icu.impl.ICUResourceBundle; 17 import com.ibm.icu.impl.LocaleUtility; 18 19 39 public class Currency extends MeasureUnit implements Serializable { 40 private static final long serialVersionUID = -5839973855554750484L; 42 private static final boolean DEBUG = ICUDebug.enabled("currency"); 43 46 private String isoCode; 47 48 53 public static final int SYMBOL_NAME = 0; 54 55 60 public static final int LONG_NAME = 1; 61 62 64 static abstract class ServiceShim { 66 abstract ULocale[] getAvailableULocales(); 67 abstract Locale [] getAvailableLocales(); 68 abstract Currency createInstance(ULocale l); 69 abstract Object registerInstance(Currency c, ULocale l); 70 abstract boolean unregister(Object f); 71 } 72 73 private static ServiceShim shim; 74 private static ServiceShim getShim() { 75 if (shim == null) { 80 try { 81 Class cls = Class.forName("com.ibm.icu.util.CurrencyServiceShim"); 82 shim = (ServiceShim)cls.newInstance(); 83 } 84 catch (Exception e) { 85 if(DEBUG){ 86 e.printStackTrace(); 87 } 88 throw new RuntimeException (e.getMessage()); 89 } 90 } 91 return shim; 92 } 93 94 101 public static Currency getInstance(Locale locale) { 102 return getInstance(ULocale.forLocale(locale)); 103 } 104 105 111 public static Currency getInstance(ULocale locale) { 112 String currency = locale.getKeywordValue("currency"); 113 if (currency != null) { 114 return getInstance(currency); 115 } 116 117 if (shim == null) { 118 return createCurrency(locale); 119 } 120 121 return shim.createInstance(locale); 122 } 123 124 127 static Currency createCurrency(ULocale loc) { 128 String country = loc.getCountry(); 130 String variant = loc.getVariant(); 131 if (variant.equals("PREEURO") || variant.equals("EURO")) { 132 country = country + '_' + variant; 133 } 134 ICUResourceBundle bundle = (ICUResourceBundle) ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"CurrencyData", ICUResourceBundle.ICU_DATA_CLASS_LOADER); 135 if(bundle==null){ 136 } 138 ICUResourceBundle cm = bundle.get("CurrencyMap"); 139 140 String curriso = null; 142 try { 143 curriso = cm.getString(country); 144 if (curriso != null) { 145 return new Currency(curriso); 146 } 147 } catch (MissingResourceException ex) { 148 try{ 149 String rep = ULocale.getCurrentCountryID(country); 152 if(DEBUG) System.out.println("DEBUG: oldID: "+country +" newID:" +rep); 153 if(rep != country){ 156 curriso = cm.getString(rep); 157 if (curriso != null) { 158 return new Currency(curriso); 159 } 160 } 161 }catch(MissingResourceException e){ 162 } 164 } 165 return null; 166 185 } 186 187 193 public static Currency getInstance(String theISOCode) { 194 return new Currency(theISOCode); 195 } 196 197 207 public static Object registerInstance(Currency currency, ULocale locale) { 208 return getShim().registerInstance(currency, locale); 209 } 210 211 218 public static boolean unregister(Object registryKey) { 219 if (registryKey == null) { 220 throw new IllegalArgumentException ("registryKey must not be null"); 221 } 222 if (shim == null) { 223 return false; 224 } 225 return shim.unregister(registryKey); 226 } 227 228 234 public static Locale [] getAvailableLocales() { 235 if (shim == null) { 236 return ICUResourceBundle.getAvailableLocales(ICUResourceBundle.ICU_BASE_NAME); 237 } else { 238 return shim.getAvailableLocales(); 239 } 240 } 241 242 248 public static ULocale[] getAvailableULocales() { 249 if (shim == null) { 250 return ICUResourceBundle.getAvailableULocales(ICUResourceBundle.ICU_BASE_NAME); 251 } else { 252 return shim.getAvailableULocales(); 253 } 254 } 255 256 258 262 public int hashCode() { 263 return isoCode.hashCode(); 264 } 265 266 271 public boolean equals(Object rhs) { 272 if (rhs == null) return false; 273 if (rhs == this) return true; 274 try { 275 Currency c = (Currency) rhs; 276 return isoCode.equals(c.isoCode); 277 } 278 catch (ClassCastException e) { 279 return false; 280 } 281 } 282 283 287 public String getCurrencyCode() { 288 return isoCode; 289 } 290 291 298 public String getSymbol() { 299 return getSymbol(ULocale.getDefault()); 300 } 301 302 310 public String getSymbol(Locale loc) { 311 return getSymbol(ULocale.forLocale(loc)); 312 } 313 314 322 public String getSymbol(ULocale uloc) { 323 return getName(uloc, SYMBOL_NAME, new boolean[1]); 324 } 325 326 342 public String getName(Locale locale, 343 int nameStyle, 344 boolean[] isChoiceFormat) { 345 return getName(ULocale.forLocale(locale), nameStyle, isChoiceFormat); 346 } 347 348 364 public String getName(ULocale locale, 365 int nameStyle, 366 boolean[] isChoiceFormat) { 367 368 379 if (nameStyle < 0 || nameStyle > 1) { 380 throw new IllegalArgumentException (); 381 } 382 383 String s = null; 384 385 try { 386 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locale); 387 ICUResourceBundle currencies = rb.get("Currencies"); 388 389 s = currencies.getWithFallback(isoCode).getString(nameStyle); 391 }catch (MissingResourceException e) { 392 } 394 395 isChoiceFormat[0] = false; 401 if (s != null) { 402 int i=0; 403 while (i < s.length() && s.charAt(i) == '=' && i < 2) { 404 ++i; 405 } 406 isChoiceFormat[0]= (i == 1); 407 if (i != 0) { 408 s = s.substring(1); 410 } 411 return s; 412 } 413 414 return isoCode; 416 } 417 418 438 public static String parse(ULocale locale, String text, ParsePosition pos) { 439 440 448 int start = pos.getIndex(); 449 String fragment = text.substring(start); 450 451 String iso = null; 452 int max = 0; 453 454 465 471 474 476 while (locale != null) { 477 ICUResourceBundle rb = (ICUResourceBundle)UResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,locale); 478 480 try { 481 ICUResourceBundle currencies = rb.get("Currencies"); 482 for (int i=0; i<currencies.getSize(); ++i) { 484 ICUResourceBundle item = currencies.get(i); 486 String name = item.getString(0); 487 if (name.length() < 1) { 488 continue; 491 } else if (name.charAt(0) == '=') { 492 name = name.substring(1); 493 if (name.length() > 0 && name.charAt(0) != '=') { 494 ChoiceFormat choice = new ChoiceFormat (name); 495 choice.parse(text, pos); 497 int len = pos.getIndex() - start; 498 if (len > max) { 499 iso = item.getKey(); 500 max = len; 501 } 502 pos.setIndex(start); 503 continue; 504 } 505 } 506 if (name.length() > max && fragment.startsWith(name)) { 507 iso = item.getKey(); 508 max = name.length(); 509 } 510 } 511 } 512 catch (MissingResourceException e) {} 513 514 locale = locale.getFallback(); 515 } 516 517 529 if (max < 3 && (text.length() - start) >= 3) { 535 boolean valid = true; 536 for (int k=0; k<3; ++k) { 537 char ch = text.charAt(start + k); if (ch < 'A' || ch > 'Z') { 539 valid = false; 540 break; 541 } 542 } 543 if (valid) { 544 iso = text.substring(start, start+3); 545 max = 3; 546 } 547 } 548 549 pos.setIndex(start + max); 550 return iso; 551 } 552 553 560 public int getDefaultFractionDigits() { 561 return (findData())[0]; 562 } 563 564 570 public double getRoundingIncrement() { 571 int[] data = findData(); 572 573 int data1 = data[1]; 575 if (data1 == 0) { 578 return 0.0; 579 } 580 581 int data0 = data[0]; 583 if (data0 < 0 || data0 >= POW10.length) { 585 return 0.0; 586 } 587 588 return (double) data1 / POW10[data0]; 591 } 592 593 597 public String toString() { 598 return isoCode; 599 } 600 601 609 protected Currency(String theISOCode) { 610 isoCode = theISOCode; 611 } 612 613 619 private int[] findData() { 620 621 try { 622 ICUResourceBundle root = (ICUResourceBundle)ICUResourceBundle.getBundleInstance(ICUResourceBundle.ICU_BASE_NAME,"CurrencyData", ICUResourceBundle.ICU_DATA_CLASS_LOADER); 626 ICUResourceBundle currencyMeta = root.get("CurrencyMeta"); 627 628 int[] i = currencyMeta.get(isoCode).getIntVector(); 631 632 651 if (i == null) { 652 i = currencyMeta.get("DEFAULT").getIntVector(); 653 } 654 655 if (i != null && i.length >= 2) { 656 return i; 657 } 658 } 659 catch (MissingResourceException e) {} 660 661 return LAST_RESORT_DATA; 663 } 664 665 private static final int[] LAST_RESORT_DATA = new int[] { 2, 0 }; 670 671 private static final int[] POW10 = { 1, 10, 100, 1000, 10000, 100000, 673 1000000, 10000000, 100000000, 1000000000 }; 674 675 677 701 public final ULocale getLocale(ULocale.Type type) { 702 return ULocale.ROOT; 703 } 704 705 723 final void setLocale(ULocale valid, ULocale actual) { 724 if ((valid == null) != (actual == null)) { 726 throw new IllegalArgumentException (); 728 } 730 this.validLocale = valid; 733 this.actualLocale = actual; 734 } 735 736 742 private ULocale validLocale; 743 744 751 private ULocale actualLocale; 752 753 } 755 756 | Popular Tags |