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 java.io.IOException; 7 import java.math.BigDecimal; 8 import java.text.AttributedCharacterIterator; 9 import java.util.Arrays; 10 11 import ohos.global.icu.impl.FormattedStringBuilder; 12 import ohos.global.icu.impl.FormattedValueStringBuilderImpl; 13 import ohos.global.icu.impl.number.DecimalQuantity; 14 import ohos.global.icu.number.NumberRangeFormatter.RangeIdentityResult; 15 import ohos.global.icu.text.ConstrainedFieldPosition; 16 import ohos.global.icu.text.FormattedValue; 17 import ohos.global.icu.util.ICUUncheckedIOException; 18 19 /** 20 * The result of a number range formatting operation. This class allows the result to be exported in several data types, 21 * including a String, an AttributedCharacterIterator, and a BigDecimal. 22 * 23 * Instances of this class are immutable and thread-safe. 24 * 25 * @author sffc 26 * @see NumberRangeFormatter 27 * @hide exposed on OHOS 28 */ 29 public class FormattedNumberRange implements FormattedValue { 30 final FormattedStringBuilder string; 31 final DecimalQuantity quantity1; 32 final DecimalQuantity quantity2; 33 final RangeIdentityResult identityResult; 34 FormattedNumberRange(FormattedStringBuilder string, DecimalQuantity quantity1, DecimalQuantity quantity2, RangeIdentityResult identityResult)35 FormattedNumberRange(FormattedStringBuilder string, DecimalQuantity quantity1, DecimalQuantity quantity2, 36 RangeIdentityResult identityResult) { 37 this.string = string; 38 this.quantity1 = quantity1; 39 this.quantity2 = quantity2; 40 this.identityResult = identityResult; 41 } 42 43 /** 44 * {@inheritDoc} 45 */ 46 @Override toString()47 public String toString() { 48 return string.toString(); 49 } 50 51 /** 52 * {@inheritDoc} 53 */ 54 @Override appendTo(A appendable)55 public <A extends Appendable> A appendTo(A appendable) { 56 try { 57 appendable.append(string); 58 } catch (IOException e) { 59 // Throw as an unchecked exception to avoid users needing try/catch 60 throw new ICUUncheckedIOException(e); 61 } 62 return appendable; 63 } 64 65 /** 66 * {@inheritDoc} 67 */ 68 @Override charAt(int index)69 public char charAt(int index) { 70 return string.charAt(index); 71 } 72 73 /** 74 * {@inheritDoc} 75 */ 76 @Override length()77 public int length() { 78 return string.length(); 79 } 80 81 /** 82 * {@inheritDoc} 83 */ 84 @Override subSequence(int start, int end)85 public CharSequence subSequence(int start, int end) { 86 return string.subString(start, end); 87 } 88 89 /** 90 * {@inheritDoc} 91 */ 92 @Override nextPosition(ConstrainedFieldPosition cfpos)93 public boolean nextPosition(ConstrainedFieldPosition cfpos) { 94 return FormattedValueStringBuilderImpl.nextPosition(string, cfpos, null); 95 } 96 97 /** 98 * {@inheritDoc} 99 */ 100 @Override toCharacterIterator()101 public AttributedCharacterIterator toCharacterIterator() { 102 return FormattedValueStringBuilderImpl.toCharacterIterator(string, null); 103 } 104 105 /** 106 * Export the first formatted number as a BigDecimal. This endpoint is useful for obtaining the exact number being 107 * printed after scaling and rounding have been applied by the number range formatting pipeline. 108 * 109 * @return A BigDecimal representation of the first formatted number. 110 * @see NumberRangeFormatter 111 * @see #getSecondBigDecimal 112 */ getFirstBigDecimal()113 public BigDecimal getFirstBigDecimal() { 114 return quantity1.toBigDecimal(); 115 } 116 117 /** 118 * Export the second formatted number as a BigDecimal. This endpoint is useful for obtaining the exact number being 119 * printed after scaling and rounding have been applied by the number range formatting pipeline. 120 * 121 * @return A BigDecimal representation of the second formatted number. 122 * @see NumberRangeFormatter 123 * @see #getFirstBigDecimal 124 */ getSecondBigDecimal()125 public BigDecimal getSecondBigDecimal() { 126 return quantity2.toBigDecimal(); 127 } 128 129 /** 130 * Returns whether the pair of numbers was successfully formatted as a range or whether an identity fallback was 131 * used. For example, if the first and second number were the same either before or after rounding occurred, an 132 * identity fallback was used. 133 * 134 * @return A RangeIdentityType indicating the resulting identity situation in the formatted number range. 135 * @see NumberRangeFormatter 136 * @see NumberRangeFormatter.RangeIdentityFallback 137 */ getIdentityResult()138 public RangeIdentityResult getIdentityResult() { 139 return identityResult; 140 } 141 142 /** 143 * {@inheritDoc} 144 */ 145 @Override hashCode()146 public int hashCode() { 147 // FormattedStringBuilder and BigDecimal are mutable, so we can't call 148 // #equals() or #hashCode() on them directly. 149 return Arrays.hashCode(string.toCharArray()) ^ Arrays.hashCode(string.toFieldArray()) 150 ^ quantity1.toBigDecimal().hashCode() ^ quantity2.toBigDecimal().hashCode(); 151 } 152 153 /** 154 * {@inheritDoc} 155 */ 156 @Override equals(Object other)157 public boolean equals(Object other) { 158 if (this == other) 159 return true; 160 if (other == null) 161 return false; 162 if (!(other instanceof FormattedNumberRange)) 163 return false; 164 // FormattedStringBuilder and BigDecimal are mutable, so we can't call 165 // #equals() or #hashCode() on them directly. 166 FormattedNumberRange _other = (FormattedNumberRange) other; 167 return Arrays.equals(string.toCharArray(), _other.string.toCharArray()) 168 && Arrays.equals(string.toFieldArray(), _other.string.toFieldArray()) 169 && quantity1.toBigDecimal().equals(_other.quantity1.toBigDecimal()) 170 && quantity2.toBigDecimal().equals(_other.quantity2.toBigDecimal()); 171 } 172 } 173