1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2018 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 package ohos.global.icu.number; 5 6 import ohos.global.icu.impl.number.DecimalQuantity; 7 import ohos.global.icu.impl.number.DecimalQuantity_DualStorageBCD; 8 9 /** 10 * A NumberRangeFormatter that has a locale associated with it; this means .formatRange() methods are available. 11 * 12 * Instances of this class are immutable and thread-safe. 13 * 14 * @author sffc 15 * @see NumberRangeFormatter 16 * @hide exposed on OHOS 17 */ 18 public class LocalizedNumberRangeFormatter extends NumberRangeFormatterSettings<LocalizedNumberRangeFormatter> { 19 20 private volatile NumberRangeFormatterImpl fImpl; 21 LocalizedNumberRangeFormatter(NumberRangeFormatterSettings<?> parent, int key, Object value)22 LocalizedNumberRangeFormatter(NumberRangeFormatterSettings<?> parent, int key, Object value) { 23 super(parent, key, value); 24 } 25 26 /** 27 * Format the given integers to a string using the settings specified in the NumberRangeFormatter fluent setting 28 * chain. 29 * 30 * @param first 31 * The first number in the range, usually to the left in LTR locales. 32 * @param second 33 * The second number in the range, usually to the right in LTR locales. 34 * @return A FormattedNumberRange object; call .toString() to get the string. 35 * @see NumberRangeFormatter 36 */ formatRange(int first, int second)37 public FormattedNumberRange formatRange(int first, int second) { 38 DecimalQuantity dq1 = new DecimalQuantity_DualStorageBCD(first); 39 DecimalQuantity dq2 = new DecimalQuantity_DualStorageBCD(second); 40 return formatImpl(dq1, dq2, first == second); 41 } 42 43 /** 44 * Format the given doubles to a string using the settings specified in the NumberRangeFormatter fluent setting 45 * chain. 46 * 47 * @param first 48 * The first number in the range, usually to the left in LTR locales. 49 * @param second 50 * The second number in the range, usually to the right in LTR locales. 51 * @return A FormattedNumberRange object; call .toString() to get the string. 52 * @see NumberRangeFormatter 53 */ formatRange(double first, double second)54 public FormattedNumberRange formatRange(double first, double second) { 55 DecimalQuantity dq1 = new DecimalQuantity_DualStorageBCD(first); 56 DecimalQuantity dq2 = new DecimalQuantity_DualStorageBCD(second); 57 // Note: double equality could be changed to epsilon equality later if there is demand. 58 // The epsilon should be set via an API method. 59 return formatImpl(dq1, dq2, first == second); 60 } 61 62 /** 63 * Format the given Numbers to a string using the settings specified in the NumberRangeFormatter fluent setting 64 * chain. 65 * 66 * @param first 67 * The first number in the range, usually to the left in LTR locales. 68 * @param second 69 * The second number in the range, usually to the right in LTR locales. 70 * @return A FormattedNumberRange object; call .toString() to get the string. 71 * @throws IllegalArgumentException if first or second is null 72 * @see NumberRangeFormatter 73 */ formatRange(Number first, Number second)74 public FormattedNumberRange formatRange(Number first, Number second) { 75 if (first == null || second == null) { 76 throw new IllegalArgumentException("Cannot format null values in range"); 77 } 78 DecimalQuantity dq1 = new DecimalQuantity_DualStorageBCD(first); 79 DecimalQuantity dq2 = new DecimalQuantity_DualStorageBCD(second); 80 return formatImpl(dq1, dq2, first.equals(second)); 81 } 82 formatImpl(DecimalQuantity first, DecimalQuantity second, boolean equalBeforeRounding)83 FormattedNumberRange formatImpl(DecimalQuantity first, DecimalQuantity second, boolean equalBeforeRounding) { 84 if (fImpl == null) { 85 fImpl = new NumberRangeFormatterImpl(resolve()); 86 } 87 return fImpl.format(first, second, equalBeforeRounding); 88 } 89 90 @Override create(int key, Object value)91 LocalizedNumberRangeFormatter create(int key, Object value) { 92 return new LocalizedNumberRangeFormatter(this, key, value); 93 } 94 95 } 96