1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /** 4 ******************************************************************************* 5 * Copyright (C) 2001-2012, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.util; 10 11 import java.io.Serializable; 12 import java.util.Locale; 13 14 import com.ibm.icu.util.ULocale.Category; 15 16 /** 17 * A class encapsulating a currency, as defined by ISO 4217. A 18 * <tt>Currency</tt> object can be created given a <tt>Locale</tt> or 19 * given an ISO 4217 code. Once created, the <tt>Currency</tt> object 20 * can return various data necessary to its proper display: 21 * 22 * <ul><li>A display symbol, for a specific locale 23 * <li>The number of fraction digits to display 24 * <li>A rounding increment 25 * </ul> 26 * 27 * The <tt>DecimalFormat</tt> class uses these data to display 28 * currencies. 29 * 30 * <p>Note: This class deliberately resembles 31 * <tt>java.util.Currency</tt> but it has a completely independent 32 * implementation, and adds features not present in the JDK. 33 * @author Alan Liu 34 * @stable ICU 2.2 35 */ 36 public class Currency implements Serializable { 37 private static final long serialVersionUID = 1L; 38 39 /** 40 * @internal 41 */ 42 public final java.util.Currency currency; 43 44 /** 45 * @internal 46 * @param delegate the NumberFormat to which to delegate 47 */ 48 public Currency(java.util.Currency delegate) { 49 this.currency = delegate; 50 } 51 52 /** 53 * Selector for getName() indicating a symbolic name for a 54 * currency, such as "$" for USD. 55 * @stable ICU 2.6 56 */ 57 public static final int SYMBOL_NAME = 0; 58 59 /** 60 * Selector for ucurr_getName indicating the long name for a 61 * currency, such as "US Dollar" for USD. 62 * @stable ICU 2.6 63 */ 64 public static final int LONG_NAME = 1; 65 66 /** 67 * Selector for getName() indicating the plural long name for a 68 * currency, such as "US dollar" for USD in "1 US dollar", 69 * and "US dollars" for USD in "2 US dollars". 70 * @stable ICU 4.2 71 */ 72 public static final int PLURAL_LONG_NAME = 2; 73 74 /** 75 * Returns a currency object for the default currency in the given 76 * locale. 77 * @param locale the locale 78 * @return the currency object for this locale 79 * @stable ICU 2.2 80 */ 81 public static Currency getInstance(Locale locale) { 82 return new Currency(java.util.Currency.getInstance(locale)); 83 } 84 85 /** 86 * Returns a currency object for the default currency in the given 87 * locale. 88 * @stable ICU 3.2 89 */ 90 public static Currency getInstance(ULocale locale) { 91 return new Currency(java.util.Currency.getInstance(locale.toLocale())); 92 } 93 94 // /** 95 // * Returns an array of Strings which contain the currency 96 // * identifiers that are valid for the given locale on the 97 // * given date. If there are no such identifiers, returns null. 98 // * Returned identifiers are in preference order. 99 // * @param loc the locale for which to retrieve currency codes. 100 // * @param d the date for which to retrieve currency codes for the given locale. 101 // * @return The array of ISO currency codes. 102 // * @stable ICU 4.0 103 // */ 104 // public static String[] getAvailableCurrencyCodes(ULocale loc, Date d) { 105 // throw new UnsupportedOperationException("Method not supproted by com.ibm.icu.base"); 106 // } 107 108 // /** 109 // * Returns the set of available currencies. The returned set of currencies contains all of the 110 // * available currencies, including obsolete ones. The result set can be modified without 111 // * affecting the available currencies in the runtime. 112 // * 113 // * @return The set of available currencies. The returned set could be empty if there is no 114 // * currency data available. 115 // * 116 // * @stable ICU 49 117 // */ 118 // public static Set<Currency> getAvailableCurrencies() { 119 // throw new UnsupportedOperationException("Method not supproted by com.ibm.icu.base"); 120 // } 121 122 /** 123 * Returns a currency object given an ISO 4217 3-letter code. 124 * @param theISOCode the iso code 125 * @return the currency for this iso code 126 * @throws NullPointerException if <code>theISOCode</code> is null. 127 * @throws IllegalArgumentException if <code>theISOCode</code> is not a 128 * 3-letter alpha code. 129 * @stable ICU 2.2 130 */ 131 public static Currency getInstance(String theISOCode) { 132 return new Currency(java.util.Currency.getInstance(theISOCode)); 133 } 134 135 // /** 136 // * Registers a new currency for the provided locale. The returned object 137 // * is a key that can be used to unregister this currency object. 138 // * @param currency the currency to register 139 // * @param locale the ulocale under which to register the currency 140 // * @return a registry key that can be used to unregister this currency 141 // * @see #unregister 142 // * @stable ICU 3.2 143 // */ 144 // public static Object registerInstance(Currency currency, ULocale locale) { 145 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 146 // } 147 148 // /** 149 // * Unregister the currency associated with this key (obtained from 150 // * registerInstance). 151 // * @param registryKey the registry key returned from registerInstance 152 // * @see #registerInstance 153 // * @stable ICU 2.6 154 // */ 155 // public static boolean unregister(Object registryKey) { 156 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 157 // } 158 159 // /** 160 // * Return an array of the locales for which a currency 161 // * is defined. 162 // * @return an array of the available locales 163 // * @stable ICU 2.2 164 // */ 165 // public static Locale[] getAvailableLocales() { 166 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 167 // } 168 169 // /** 170 // * Return an array of the ulocales for which a currency 171 // * is defined. 172 // * @return an array of the available ulocales 173 // * @stable ICU 3.2 174 // */ 175 // public static ULocale[] getAvailableULocales() { 176 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 177 // } 178 179 // /** 180 // * Given a key and a locale, returns an array of values for the key for which data 181 // * exists. If commonlyUsed is true, these are the values that typically are used 182 // * with this locale, otherwise these are all values for which data exists. 183 // * This is a common service API. 184 // * <p> 185 // * The only supported key is "currency", other values return an empty array. 186 // * <p> 187 // * Currency information is based on the region of the locale. If the locale does not 188 // * indicate a region, {@link ULocale#addLikelySubtags(ULocale)} is used to infer a region, 189 // * except for the 'und' locale. 190 // * <p> 191 // * If commonlyUsed is true, only the currencies known to be in use as of the current date 192 // * are returned. When there are more than one, these are returned in preference order 193 // * (typically, this occurs when a country is transitioning to a new currency, and the 194 // * newer currency is preferred), see 195 // * <a href="http://unicode.org/reports/tr35/#Supplemental_Currency_Data">Unicode TR#35 Sec. C1</a>. 196 // * If commonlyUsed is false, all currencies ever used in any locale are returned, in no 197 // * particular order. 198 // * 199 // * @param key key whose values to look up. the only recognized key is "currency" 200 // * @param locale the locale 201 // * @param commonlyUsed if true, return only values that are currently used in the locale. 202 // * Otherwise returns all values. 203 // * @return an array of values for the given key and the locale. If there is no data, the 204 // * array will be empty. 205 // * @stable ICU 4.2 206 // */ 207 // public static final String[] getKeywordValuesForLocale(String key, ULocale locale, 208 // boolean commonlyUsed) { 209 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 210 // } 211 212 /** 213 * Return a hashcode for this currency. 214 * @stable ICU 2.2 215 */ 216 public int hashCode() { 217 return currency.hashCode(); 218 } 219 220 /** 221 * Return true if rhs is a Currency instance, 222 * is non-null, and has the same currency code. 223 * @stable ICU 2.2 224 */ 225 public boolean equals(Object rhs) { 226 try { 227 return currency.equals(((Currency)rhs).currency); 228 } 229 catch (Exception e) { 230 return false; 231 } 232 } 233 234 /** 235 * Returns the ISO 4217 3-letter code for this currency object. 236 * @stable ICU 2.2 237 */ 238 public String getCurrencyCode() { 239 return currency.getCurrencyCode(); 240 } 241 242 // /** 243 // * Returns the ISO 4217 numeric code for this currency object. 244 // * <p>Note: If the ISO 4217 numeric code is not assigned for the currency or 245 // * the currency is unknown, this method returns 0.</p> 246 // * @return The ISO 4217 numeric code of this currency. 247 // * @stable ICU 49 248 // */ 249 // public int getNumericCode() { 250 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 251 // } 252 253 /** 254 * Convenience and compatibility override of getName that 255 * requests the symbol name. 256 * @see #getName 257 * @stable ICU 3.4 258 */ 259 public String getSymbol() { 260 return currency.getSymbol(ULocale.getDefault(Category.DISPLAY).toLocale()); 261 } 262 263 /** 264 * Convenience and compatibility override of getName that 265 * requests the symbol name. 266 * @param loc the Locale for the symbol 267 * @see #getName 268 * @stable ICU 3.4 269 */ 270 public String getSymbol(Locale loc) { 271 return currency.getSymbol(loc); 272 } 273 274 /** 275 * Convenience and compatibility override of getName that 276 * requests the symbol name. 277 * @param uloc the ULocale for the symbol 278 * @see #getName 279 * @stable ICU 3.4 280 */ 281 public String getSymbol(ULocale uloc) { 282 return currency.getSymbol(uloc.toLocale()); 283 } 284 285 // /** 286 // * Returns the display name for the given currency in the 287 // * given locale. 288 // * This is a convenient method for 289 // * getName(ULocale, int, boolean[]); 290 // * @stable ICU 3.2 291 // */ 292 // public String getName(Locale locale, 293 // int nameStyle, 294 // boolean[] isChoiceFormat) { 295 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 296 // } 297 298 // /** 299 // * Returns the display name for the given currency in the 300 // * given locale. For example, the display name for the USD 301 // * currency object in the en_US locale is "$". 302 // * @param locale locale in which to display currency 303 // * @param nameStyle selector for which kind of name to return. 304 // * The nameStyle should be either SYMBOL_NAME or 305 // * LONG_NAME. Otherwise, throw IllegalArgumentException. 306 // * @param isChoiceFormat fill-in; isChoiceFormat[0] is set to true 307 // * if the returned value is a ChoiceFormat pattern; otherwise it 308 // * is set to false 309 // * @return display string for this currency. If the resource data 310 // * contains no entry for this currency, then the ISO 4217 code is 311 // * returned. If isChoiceFormat[0] is true, then the result is a 312 // * ChoiceFormat pattern. Otherwise it is a static string. <b>Note:</b> 313 // * as of ICU 4.4, choice formats are not used, and the value returned 314 // * in isChoiceFormat is always false. 315 // * <p> 316 // * @throws IllegalArgumentException if the nameStyle is not SYMBOL_NAME 317 // * or LONG_NAME. 318 // * @see #getName(ULocale, int, String, boolean[]) 319 // * @stable ICU 3.2 320 // */ 321 // public String getName(ULocale locale, int nameStyle, boolean[] isChoiceFormat) { 322 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 323 // } 324 325 // /** 326 // * Returns the display name for the given currency in the given locale. 327 // * This is a convenience overload of getName(ULocale, int, String, boolean[]); 328 // * @stable ICU 4.2 329 // */ 330 // public String getName(Locale locale, int nameStyle, String pluralCount, 331 // boolean[] isChoiceFormat) { 332 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 333 // } 334 335 // /** 336 // * Returns the display name for this currency in the default locale. 337 // * If the resource data for the default locale contains no entry for this currency, 338 // * then the ISO 4217 code is returned. 339 // * <p> 340 // * Note: This method was added for JDK compatibility support and equivalent to 341 // * <code>getName(Locale.getDefault(), LONG_NAME, null)</code>. 342 // * 343 // * @return The display name of this currency 344 // * @see #getDisplayName(Locale) 345 // * @see #getName(Locale, int, boolean[]) 346 // * @stable ICU 49 347 // */ 348 // public String getDisplayName() { 349 // //return getName(Locale.getDefault(), LONG_NAME, null); 350 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 351 // } 352 353 // /** 354 // * Returns the display name for this currency in the given locale. 355 // * If the resource data for the given locale contains no entry for this currency, 356 // * then the ISO 4217 code is returned. 357 // * <p> 358 // * Note: This method was added for JDK compatibility support and equivalent to 359 // * <code>getName(locale, LONG_NAME, null)</code>. 360 // * 361 // * @param locale locale in which to display currency 362 // * @return The display name of this currency for the specified locale 363 // * @see #getDisplayName(Locale) 364 // * @see #getName(Locale, int, boolean[]) 365 // * @stable ICU 49 366 // */ 367 // public String getDisplayName(Locale locale) { 368 // //return getName(locale, LONG_NAME, null); 369 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 370 // } 371 372 // /** 373 // * Returns the display name for the given currency in the 374 // * given locale. For example, the SYMBOL_NAME for the USD 375 // * currency object in the en_US locale is "$". 376 // * The PLURAL_LONG_NAME for the USD currency object when the currency 377 // * amount is plural is "US dollars", such as in "3.00 US dollars"; 378 // * while the PLURAL_LONG_NAME for the USD currency object when the currency 379 // * amount is singular is "US dollar", such as in "1.00 US dollar". 380 // * @param locale locale in which to display currency 381 // * @param nameStyle selector for which kind of name to return 382 // * @param pluralCount plural count string for this locale 383 // * @param isChoiceFormat fill-in; isChoiceFormat[0] is set to true 384 // * if the returned value is a ChoiceFormat pattern; otherwise it 385 // * is set to false 386 // * @return display string for this currency. If the resource data 387 // * contains no entry for this currency, then the ISO 4217 code is 388 // * returned. If isChoiceFormat[0] is true, then the result is a 389 // * ChoiceFormat pattern. Otherwise it is a static string. <b>Note:</b> 390 // * as of ICU 4.4, choice formats are not used, and the value returned 391 // * in isChoiceFormat is always false. 392 // * @throws IllegalArgumentException if the nameStyle is not SYMBOL_NAME, 393 // * LONG_NAME, or PLURAL_LONG_NAME. 394 // * @stable ICU 4.2 395 // */ 396 // public String getName(ULocale locale, int nameStyle, String pluralCount, 397 // boolean[] isChoiceFormat) { 398 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 399 // } 400 401 // /** 402 // * Attempt to parse the given string as a currency, either as a 403 // * display name in the given locale, or as a 3-letter ISO 4217 404 // * code. If multiple display names match, then the longest one is 405 // * selected. If both a display name and a 3-letter ISO code 406 // * match, then the display name is preferred, unless it's length 407 // * is less than 3. 408 // * 409 // * @param locale the locale of the display names to match 410 // * @param text the text to parse 411 // * @param type parse against currency type: LONG_NAME only or not 412 // * @param pos input-output position; on input, the position within 413 // * text to match; must have 0 <= pos.getIndex() < text.length(); 414 // * on output, the position after the last matched character. If 415 // * the parse fails, the position in unchanged upon output. 416 // * @return the ISO 4217 code, as a string, of the best match, or 417 // * null if there is no match 418 // * 419 // * @internal 420 // * @deprecated This API is ICU internal only. 421 // */ 422 // public static String parse(ULocale locale, String text, int type, ParsePosition pos) { 423 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 424 // } 425 426 /** 427 * Returns the number of the number of fraction digits that should 428 * be displayed for this currency. 429 * @return a non-negative number of fraction digits to be 430 * displayed 431 * @stable ICU 2.2 432 */ 433 public int getDefaultFractionDigits() { 434 return currency.getDefaultFractionDigits(); 435 } 436 437 // /** 438 // * Returns the rounding increment for this currency, or 0.0 if no 439 // * rounding is done by this currency. 440 // * @return the non-negative rounding increment, or 0.0 if none 441 // * @stable ICU 2.2 442 // */ 443 // public double getRoundingIncrement() { 444 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 445 // } 446 447 /** 448 * Returns the ISO 4217 code for this currency. 449 * @stable ICU 2.2 450 */ 451 public String toString() { 452 return currency.toString(); 453 } 454 455 // /** 456 // * Return the locale that was used to create this object, or null. 457 // * This may may differ from the locale requested at the time of 458 // * this object's creation. For example, if an object is created 459 // * for locale <tt>en_US_CALIFORNIA</tt>, the actual data may be 460 // * drawn from <tt>en</tt> (the <i>actual</i> locale), and 461 // * <tt>en_US</tt> may be the most specific locale that exists (the 462 // * <i>valid</i> locale). 463 // * 464 // * <p>Note: This method will be obsoleted. The implementation is 465 // * no longer locale-specific and so there is no longer a valid or 466 // * actual locale associated with the Currency object. Until 467 // * it is removed, this method will return the root locale. 468 // * @param type type of information requested, either {@link 469 // * com.ibm.icu.util.ULocale#VALID_LOCALE} or {@link 470 // * com.ibm.icu.util.ULocale#ACTUAL_LOCALE}. 471 // * @return the information specified by <i>type</i>, or null if 472 // * this object was not constructed from locale data. 473 // * @see com.ibm.icu.util.ULocale 474 // * @see com.ibm.icu.util.ULocale#VALID_LOCALE 475 // * @see com.ibm.icu.util.ULocale#ACTUAL_LOCALE 476 // * @obsolete ICU 3.2 to be removed 477 // * @deprecated This API is obsolete. 478 // */ 479 // public final ULocale getLocale(ULocale.Type type) { 480 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 481 // } 482 483 // /** 484 // * Queries if the given ISO 4217 3-letter code is available on the specified date range. 485 // * <p> 486 // * Note: For checking availability of a currency on a specific date, specify the date on both <code>from</code> and 487 // * <code>to</code>. When both <code>from</code> and <code>to</code> are null, this method checks if the specified 488 // * currency is available all time. 489 // * 490 // * @param code 491 // * The ISO 4217 3-letter code. 492 // * @param from 493 // * The lower bound of the date range, inclusive. When <code>from</code> is null, check the availability 494 // * of the currency any date before <code>to</code> 495 // * @param to 496 // * The upper bound of the date range, inclusive. When <code>to</code> is null, check the availability of 497 // * the currency any date after <code>from</code> 498 // * @return true if the given ISO 4217 3-letter code is supported on the specified date range. 499 // * @throws IllegalArgumentException when <code>to</code> is before <code>from</code>. 500 // * 501 // * @draft ICU 4.6 502 // * @provisional This API might change or be removed in a future release. 503 // */ 504 // public static boolean isAvailable(String code, Date from, Date to) { 505 // throw new UnsupportedOperationException("Method not supported by com.ibm.icu.base"); 506 // } 507 508 } 509 510 //eof 511