1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2009-2015, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package ohos.global.icu.text; 11 12 import java.lang.reflect.Field; 13 import java.util.Collections; 14 import java.util.Date; 15 import java.util.List; 16 17 import ohos.global.icu.impl.Grego; 18 import ohos.global.icu.impl.Utility; 19 import ohos.global.icu.util.Currency.CurrencyUsage; 20 21 /** 22 * Provides information about currencies that is not specific to a locale. 23 * 24 * A note about currency dates. The CLDR data provides data to the day, 25 * inclusive. The date information used by CurrencyInfo and CurrencyFilter 26 * is represented by milliseconds, which is overly precise. These times are 27 * in GMT, so queries involving dates should use GMT times, but more generally 28 * you should avoid relying on time of day in queries. 29 * 30 * This class is not intended for public subclassing. 31 * 32 * @hide exposed on OHOS 33 */ 34 public class CurrencyMetaInfo { 35 private static final CurrencyMetaInfo impl; 36 private static final boolean hasData; 37 38 /** 39 * Returns the unique instance of the currency meta info. 40 * @return the meta info 41 */ getInstance()42 public static CurrencyMetaInfo getInstance() { 43 return impl; 44 } 45 46 /** 47 * Returns the unique instance of the currency meta info, or null if 48 * noSubstitute is true and there is no data to support this API. 49 * @param noSubstitute true if no substitute data should be used 50 * @return the meta info, or null 51 */ getInstance(boolean noSubstitute)52 public static CurrencyMetaInfo getInstance(boolean noSubstitute) { 53 return hasData ? impl : null; 54 } 55 56 /** 57 * Returns true if there is data for the currency meta info. 58 * @return true if there is actual data 59 * @deprecated This API is ICU internal only. 60 * @hide draft / provisional / internal are hidden on OHOS 61 */ 62 @Deprecated hasData()63 public static boolean hasData() { 64 return hasData; 65 } 66 67 /** 68 * Subclass constructor. 69 * @deprecated This API is ICU internal only. 70 * @hide draft / provisional / internal are hidden on OHOS 71 */ 72 @Deprecated CurrencyMetaInfo()73 protected CurrencyMetaInfo() { 74 } 75 76 /** 77 * A filter used to select which currency info is returned. 78 * @hide exposed on OHOS 79 */ 80 public static final class CurrencyFilter { 81 /** 82 * The region to filter on. If null, accepts any region. 83 */ 84 public final String region; 85 86 /** 87 * The currency to filter on. If null, accepts any currency. 88 */ 89 public final String currency; 90 91 /** 92 * The from date to filter on (as milliseconds). Accepts any currency on or after this date. 93 */ 94 public final long from; 95 96 /** 97 * The to date to filter on (as milliseconds). Accepts any currency on or before this date. 98 */ 99 public final long to; 100 101 /** 102 * true if we are filtering only for currencies used as legal tender. 103 * @deprecated This API is ICU internal only. 104 * @hide draft / provisional / internal are hidden on OHOS 105 */ 106 @Deprecated 107 public final boolean tenderOnly; 108 CurrencyFilter(String region, String currency, long from, long to, boolean tenderOnly)109 private CurrencyFilter(String region, String currency, long from, long to, boolean tenderOnly) { 110 this.region = region; 111 this.currency = currency; 112 this.from = from; 113 this.to = to; 114 this.tenderOnly = tenderOnly; 115 116 } 117 118 private static final CurrencyFilter ALL = new CurrencyFilter( 119 null, null, Long.MIN_VALUE, Long.MAX_VALUE, false); 120 121 /** 122 * Returns a filter that accepts all currency data. 123 * @return a filter 124 */ all()125 public static CurrencyFilter all() { 126 return ALL; 127 } 128 129 /** 130 * Returns a filter that accepts all currencies in use as of the current date. 131 * @return a filter 132 * @see #withDate(Date) 133 */ now()134 public static CurrencyFilter now() { 135 return ALL.withDate(new Date()); 136 } 137 138 /** 139 * Returns a filter that accepts all currencies ever used in the given region. 140 * @param region the region code 141 * @return a filter 142 * @see #withRegion(String) 143 */ onRegion(String region)144 public static CurrencyFilter onRegion(String region) { 145 return ALL.withRegion(region); 146 } 147 148 /** 149 * Returns a filter that accepts the given currency. 150 * @param currency the currency code 151 * @return a filter 152 * @see #withCurrency(String) 153 */ onCurrency(String currency)154 public static CurrencyFilter onCurrency(String currency) { 155 return ALL.withCurrency(currency); 156 } 157 158 /** 159 * Returns a filter that accepts all currencies in use on the given date. 160 * @param date the date 161 * @return a filter 162 * @see #withDate(Date) 163 */ onDate(Date date)164 public static CurrencyFilter onDate(Date date) { 165 return ALL.withDate(date); 166 } 167 168 /** 169 * Returns a filter that accepts all currencies that were in use at some point between 170 * the given dates, or if dates are equal, currencies in use on that date. 171 * @param from date on or after a currency must have been in use 172 * @param to date on or before which a currency must have been in use, 173 * or if equal to from, the date on which a currency must have been in use 174 * @return a filter 175 * @see #withDateRange(Date, Date) 176 */ onDateRange(Date from, Date to)177 public static CurrencyFilter onDateRange(Date from, Date to) { 178 return ALL.withDateRange(from, to); 179 } 180 181 /** 182 * Returns a filter that accepts all currencies in use on the given date. 183 * @param date the date as milliseconds after Jan 1, 1970 184 */ onDate(long date)185 public static CurrencyFilter onDate(long date) { 186 return ALL.withDate(date); 187 } 188 189 /** 190 * Returns a filter that accepts all currencies that were in use at some 191 * point between the given dates, or if dates are equal, currencies in 192 * use on that date. 193 * @param from The date on or after a currency must have been in use. 194 * Measured in milliseconds since Jan 1, 1970 GMT. 195 * @param to The date on or before which a currency must have been in use. 196 * Measured in milliseconds since Jan 1, 1970 GMT. 197 */ onDateRange(long from, long to)198 public static CurrencyFilter onDateRange(long from, long to) { 199 return ALL.withDateRange(from, to); 200 } 201 202 /** 203 * Returns a CurrencyFilter for finding currencies that were either once used, 204 * are used, or will be used as tender. 205 */ onTender()206 public static CurrencyFilter onTender() { 207 return ALL.withTender(); 208 } 209 210 /** 211 * Returns a copy of this filter, with the specified region. Region can be null to 212 * indicate no filter on region. 213 * @param region the region code 214 * @return the filter 215 * @see #onRegion(String) 216 */ withRegion(String region)217 public CurrencyFilter withRegion(String region) { 218 return new CurrencyFilter(region, this.currency, this.from, this.to, this.tenderOnly); 219 } 220 221 /** 222 * Returns a copy of this filter, with the specified currency. Currency can be null to 223 * indicate no filter on currency. 224 * @param currency the currency code 225 * @return the filter 226 * @see #onCurrency(String) 227 */ withCurrency(String currency)228 public CurrencyFilter withCurrency(String currency) { 229 return new CurrencyFilter(this.region, currency, this.from, this.to, this.tenderOnly); 230 } 231 232 /** 233 * Returns a copy of this filter, with from and to set to the given date. 234 * @param date the date on which the currency must have been in use 235 * @return the filter 236 * @see #onDate(Date) 237 */ withDate(Date date)238 public CurrencyFilter withDate(Date date) { 239 return new CurrencyFilter(this.region, this.currency, date.getTime(), date.getTime(), this.tenderOnly); 240 } 241 242 /** 243 * Returns a copy of this filter, with from and to set to the given dates. 244 * @param from date on or after which the currency must have been in use 245 * @param to date on or before which the currency must have been in use 246 * @return the filter 247 * @see #onDateRange(Date, Date) 248 */ withDateRange(Date from, Date to)249 public CurrencyFilter withDateRange(Date from, Date to) { 250 long fromLong = from == null ? Long.MIN_VALUE : from.getTime(); 251 long toLong = to == null ? Long.MAX_VALUE : to.getTime(); 252 return new CurrencyFilter(this.region, this.currency, fromLong, toLong, this.tenderOnly); 253 } 254 255 /** 256 * Returns a copy of this filter that accepts all currencies in use on 257 * the given date. 258 * @param date the date as milliseconds after Jan 1, 1970 259 */ withDate(long date)260 public CurrencyFilter withDate(long date) { 261 return new CurrencyFilter(this.region, this.currency, date, date, this.tenderOnly); 262 } 263 264 /** 265 * Returns a copy of this filter that accepts all currencies that were 266 * in use at some point between the given dates, or if dates are equal, 267 * currencies in use on that date. 268 * @param from The date on or after a currency must have been in use. 269 * Measured in milliseconds since Jan 1, 1970 GMT. 270 * @param to The date on or before which a currency must have been in use. 271 * Measured in milliseconds since Jan 1, 1970 GMT. 272 */ withDateRange(long from, long to)273 public CurrencyFilter withDateRange(long from, long to) { 274 return new CurrencyFilter(this.region, this.currency, from, to, this.tenderOnly); 275 } 276 277 /** 278 * Returns a copy of this filter that filters for currencies that were 279 * either once used, are used, or will be used as tender. 280 */ withTender()281 public CurrencyFilter withTender() { 282 return new CurrencyFilter(this.region, this.currency, this.from, this.to, true); 283 } 284 285 /** 286 * {@inheritDoc} 287 */ 288 @Override equals(Object rhs)289 public boolean equals(Object rhs) { 290 return rhs instanceof CurrencyFilter && 291 equals((CurrencyFilter) rhs); 292 } 293 294 /** 295 * Type-safe override of {@link #equals(Object)}. 296 * @param rhs the currency filter to compare to 297 * @return true if the filters are equal 298 */ equals(CurrencyFilter rhs)299 public boolean equals(CurrencyFilter rhs) { 300 return Utility.sameObjects(this, rhs) || (rhs != null && 301 equals(this.region, rhs.region) && 302 equals(this.currency, rhs.currency) && 303 this.from == rhs.from && 304 this.to == rhs.to && 305 this.tenderOnly == rhs.tenderOnly); 306 } 307 308 /** 309 * {@inheritDoc} 310 */ 311 @Override hashCode()312 public int hashCode() { 313 int hc = 0; 314 if (region != null) { 315 hc = region.hashCode(); 316 } 317 if (currency != null) { 318 hc = hc * 31 + currency.hashCode(); 319 } 320 hc = hc * 31 + (int) from; 321 hc = hc * 31 + (int) (from >>> 32); 322 hc = hc * 31 + (int) to; 323 hc = hc * 31 + (int) (to >>> 32); 324 hc = hc * 31 + (tenderOnly ? 1 : 0); 325 return hc; 326 } 327 328 /** 329 * Returns a string representing the filter, for debugging. 330 * @return A string representing the filter. 331 */ 332 @Override toString()333 public String toString() { 334 return debugString(this); 335 } 336 equals(String lhs, String rhs)337 private static boolean equals(String lhs, String rhs) { 338 return (Utility.sameObjects(lhs, rhs) || 339 (lhs != null && lhs.equals(rhs))); 340 } 341 } 342 343 /** 344 * Represents the raw information about fraction digits and rounding increment. 345 * @hide exposed on OHOS 346 */ 347 public static final class CurrencyDigits { 348 /** 349 * Number of fraction digits used to display this currency. 350 */ 351 public final int fractionDigits; 352 /** 353 * Rounding increment used when displaying this currency. 354 */ 355 public final int roundingIncrement; 356 357 /** 358 * Constructor for CurrencyDigits. 359 * @param fractionDigits the fraction digits 360 * @param roundingIncrement the rounding increment 361 */ CurrencyDigits(int fractionDigits, int roundingIncrement)362 public CurrencyDigits(int fractionDigits, int roundingIncrement) { 363 this.fractionDigits = fractionDigits; 364 this.roundingIncrement = roundingIncrement; 365 } 366 367 /** 368 * Returns a string representing the currency digits, for debugging. 369 * @return A string representing the currency digits. 370 */ 371 @Override toString()372 public String toString() { 373 return debugString(this); 374 } 375 } 376 377 /** 378 * Represents a complete currency info record listing the region, currency, from and to dates, 379 * and priority. 380 * Use {@link CurrencyMetaInfo#currencyInfo(CurrencyFilter)} 381 * for a list of info objects matching the filter. 382 * @hide exposed on OHOS 383 */ 384 public static final class CurrencyInfo { 385 /** 386 * Region code where currency is used. 387 */ 388 public final String region; 389 390 /** 391 * The three-letter ISO currency code. 392 */ 393 public final String code; 394 395 /** 396 * Date on which the currency was first officially used in the region. 397 * This is midnight at the start of the first day on which the currency was used, GMT. 398 * If there is no date, this is Long.MIN_VALUE; 399 */ 400 public final long from; 401 402 /** 403 * Date at which the currency stopped being officially used in the region. 404 * This is one millisecond before midnight at the end of the last day on which the currency was used, GMT. 405 * If there is no date, this is Long.MAX_VALUE. 406 */ 407 public final long to; 408 409 /** 410 * Preference order of currencies being used at the same time in the region. Lower 411 * values are preferred (generally, this is a transition from an older to a newer 412 * currency). Priorities within a single country are unique. 413 */ 414 public final int priority; 415 416 417 private final boolean tender; 418 419 /** 420 * @deprecated ICU 51 Use {@link CurrencyMetaInfo#currencyInfo(CurrencyFilter)} instead. 421 */ 422 @Deprecated CurrencyInfo(String region, String code, long from, long to, int priority)423 public CurrencyInfo(String region, String code, long from, long to, int priority) { 424 this(region, code, from, to, priority, true); 425 } 426 427 /** 428 * Constructs a currency info. 429 * 430 * @deprecated This API is ICU internal only. 431 * @hide draft / provisional / internal are hidden on OHOS 432 */ 433 @Deprecated CurrencyInfo(String region, String code, long from, long to, int priority, boolean tender)434 public CurrencyInfo(String region, String code, long from, long to, int priority, boolean tender) { 435 this.region = region; 436 this.code = code; 437 this.from = from; 438 this.to = to; 439 this.priority = priority; 440 this.tender = tender; 441 } 442 443 /** 444 * Returns a string representation of this object, useful for debugging. 445 * @return A string representation of this object. 446 */ 447 @Override toString()448 public String toString() { 449 return debugString(this); 450 } 451 452 /** 453 * Determine whether or not this currency was once used, is used, 454 * or will be used as tender in this region. 455 */ isTender()456 public boolean isTender() { 457 return tender; 458 } 459 } 460 461 ///CLOVER:OFF 462 /** 463 * Returns the list of CurrencyInfos matching the provided filter. Results 464 * are ordered by country code, then by highest to lowest priority (0 is highest). 465 * The returned list is unmodifiable. 466 * @param filter the filter to control which currency info to return 467 * @return the matching information 468 */ currencyInfo(CurrencyFilter filter)469 public List<CurrencyInfo> currencyInfo(CurrencyFilter filter) { 470 return Collections.emptyList(); 471 } 472 473 /** 474 * Returns the list of currency codes matching the provided filter. 475 * Results are ordered as in {@link #currencyInfo(CurrencyFilter)}. 476 * The returned list is unmodifiable. 477 * @param filter the filter to control which currencies to return. If filter is null, 478 * returns all currencies for which information is available. 479 * @return the matching currency codes 480 */ currencies(CurrencyFilter filter)481 public List<String> currencies(CurrencyFilter filter) { 482 return Collections.emptyList(); 483 } 484 485 /** 486 * Returns the list of region codes matching the provided filter. 487 * Results are ordered as in {@link #currencyInfo(CurrencyFilter)}. 488 * The returned list is unmodifiable. 489 * @param filter the filter to control which regions to return. If filter is null, 490 * returns all regions for which information is available. 491 * @return the matching region codes 492 */ regions(CurrencyFilter filter)493 public List<String> regions(CurrencyFilter filter) { 494 return Collections.emptyList(); 495 } 496 ///CLOVER:ON 497 498 /** 499 * Returns the CurrencyDigits for the currency code. 500 * This is equivalent to currencyDigits(isoCode, CurrencyUsage.STANDARD); 501 * @param isoCode the currency code 502 * @return the CurrencyDigits 503 */ currencyDigits(String isoCode)504 public CurrencyDigits currencyDigits(String isoCode) { 505 return currencyDigits(isoCode, CurrencyUsage.STANDARD); 506 } 507 508 /** 509 * Returns the CurrencyDigits for the currency code with Context Usage. 510 * @param isoCode the currency code 511 * @param currencyUsage the currency usage 512 * @return the CurrencyDigits 513 */ currencyDigits(String isoCode, CurrencyUsage currencyUsage)514 public CurrencyDigits currencyDigits(String isoCode, CurrencyUsage currencyUsage) { 515 return defaultDigits; 516 } 517 518 /** 519 * @deprecated This API is ICU internal only. 520 * @hide draft / provisional / internal are hidden on OHOS 521 */ 522 @Deprecated 523 protected static final CurrencyDigits defaultDigits = new CurrencyDigits(2, 0); 524 525 static { 526 CurrencyMetaInfo temp = null; 527 boolean tempHasData = false; 528 try { 529 Class<?> clzz = Class.forName("ohos.global.icu.impl.ICUCurrencyMetaInfo"); 530 temp = (CurrencyMetaInfo) clzz.newInstance(); 531 tempHasData = true; 532 } catch (Throwable t) { 533 temp = new CurrencyMetaInfo(); 534 } 535 impl = temp; 536 hasData = tempHasData; 537 } 538 dateString(long date)539 private static String dateString(long date) { 540 if (date == Long.MAX_VALUE || date == Long.MIN_VALUE) { 541 return null; 542 } 543 return Grego.timeToString(date); 544 } 545 debugString(Object o)546 private static String debugString(Object o) { 547 StringBuilder sb = new StringBuilder(); 548 try { 549 for (Field f : o.getClass().getFields()) { 550 Object v = f.get(o); 551 if (v != null) { 552 String s; 553 if (v instanceof Date) { 554 s = dateString(((Date)v).getTime()); 555 } else if (v instanceof Long) { 556 s = dateString(((Long)v).longValue()); 557 } else { 558 s = String.valueOf(v); 559 } 560 if (s == null) { 561 continue; 562 } 563 if (sb.length() > 0) { 564 sb.append(","); 565 } 566 sb.append(f.getName()) 567 .append("='") 568 .append(s) 569 .append("'"); 570 } 571 } 572 } catch (Throwable t) { 573 } 574 sb.insert(0, o.getClass().getSimpleName() + "("); 575 sb.append(")"); 576 return sb.toString(); 577 } 578 } 579