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.impl.number; 5 6 import java.io.Externalizable; 7 import java.io.IOException; 8 import java.io.ObjectInput; 9 import java.io.ObjectOutput; 10 import java.io.ObjectStreamException; 11 import java.text.AttributedCharacterIterator; 12 import java.text.FieldPosition; 13 import java.text.Format; 14 import java.text.ParsePosition; 15 16 import ohos.global.icu.impl.FormattedStringBuilder; 17 import ohos.global.icu.impl.FormattedValueStringBuilderImpl; 18 import ohos.global.icu.impl.Utility; 19 import ohos.global.icu.number.LocalizedNumberFormatter; 20 import ohos.global.icu.number.NumberFormatter; 21 import ohos.global.icu.util.ULocale; 22 23 /** 24 * A wrapper around LocalizedNumberFormatter implementing the Format interface, enabling improved 25 * compatibility with other APIs. This class is serializable. 26 * @hide exposed on OHOS 27 */ 28 public class LocalizedNumberFormatterAsFormat extends Format { 29 30 private static final long serialVersionUID = 1L; 31 32 private final transient LocalizedNumberFormatter formatter; 33 34 // Even though the locale is inside the LocalizedNumberFormatter, we have to keep it here, too, because 35 // LocalizedNumberFormatter doesn't have a getLocale() method, and ICU-TC didn't want to add one. 36 private final transient ULocale locale; 37 LocalizedNumberFormatterAsFormat(LocalizedNumberFormatter formatter, ULocale locale)38 public LocalizedNumberFormatterAsFormat(LocalizedNumberFormatter formatter, ULocale locale) { 39 this.formatter = formatter; 40 this.locale = locale; 41 } 42 43 /** 44 * Formats a Number using the wrapped LocalizedNumberFormatter. The provided object must be a Number. 45 * 46 * {@inheritDoc} 47 */ 48 @Override format(Object obj, StringBuffer toAppendTo, FieldPosition pos)49 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { 50 if (!(obj instanceof Number)) { 51 throw new IllegalArgumentException(); 52 } 53 DecimalQuantity dq = new DecimalQuantity_DualStorageBCD((Number) obj); 54 FormattedStringBuilder string = new FormattedStringBuilder(); 55 formatter.formatImpl(dq, string); 56 // always return first occurrence: 57 pos.setBeginIndex(0); 58 pos.setEndIndex(0); 59 boolean found = FormattedValueStringBuilderImpl.nextFieldPosition(string, pos); 60 if (found && toAppendTo.length() != 0) { 61 pos.setBeginIndex(pos.getBeginIndex() + toAppendTo.length()); 62 pos.setEndIndex(pos.getEndIndex() + toAppendTo.length()); 63 } 64 Utility.appendTo(string, toAppendTo); 65 return toAppendTo; 66 } 67 68 /** 69 * Formats a Number using the wrapped LocalizedNumberFormatter. The provided object must be a Number. 70 * 71 * {@inheritDoc} 72 */ 73 @Override formatToCharacterIterator(Object obj)74 public AttributedCharacterIterator formatToCharacterIterator(Object obj) { 75 if (!(obj instanceof Number)) { 76 throw new IllegalArgumentException(); 77 } 78 return formatter.format((Number) obj).toCharacterIterator(); 79 } 80 81 /** 82 * Not supported. This method will throw UnsupportedOperationException. 83 */ 84 @Override parseObject(String source, ParsePosition pos)85 public Object parseObject(String source, ParsePosition pos) { 86 throw new UnsupportedOperationException(); 87 } 88 89 /** 90 * Gets the LocalizedNumberFormatter that this wrapper class uses to format numbers. 91 * 92 * @return The unwrapped LocalizedNumberFormatter. 93 */ getNumberFormatter()94 public LocalizedNumberFormatter getNumberFormatter() { 95 return formatter; 96 } 97 98 @Override hashCode()99 public int hashCode() { 100 return formatter.hashCode(); 101 } 102 103 @Override equals(Object other)104 public boolean equals(Object other) { 105 if (this == other) { 106 return true; 107 } 108 if (other == null) { 109 return false; 110 } 111 if (!(other instanceof LocalizedNumberFormatterAsFormat)) { 112 return false; 113 } 114 return formatter.equals(((LocalizedNumberFormatterAsFormat) other).getNumberFormatter()); 115 } 116 writeReplace()117 private Object writeReplace() throws ObjectStreamException { 118 Proxy proxy = new Proxy(); 119 proxy.languageTag = locale.toLanguageTag(); 120 proxy.skeleton = formatter.toSkeleton(); 121 return proxy; 122 } 123 124 static class Proxy implements Externalizable { 125 private static final long serialVersionUID = 1L; 126 127 String languageTag; 128 String skeleton; 129 130 // Must have public constructor, to enable Externalizable Proxy()131 public Proxy() { 132 } 133 134 @Override writeExternal(ObjectOutput out)135 public void writeExternal(ObjectOutput out) throws IOException { 136 out.writeByte(0); // version 137 out.writeUTF(languageTag); 138 out.writeUTF(skeleton); 139 } 140 141 @Override readExternal(ObjectInput in)142 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 143 in.readByte(); // version 144 languageTag = in.readUTF(); 145 skeleton = in.readUTF(); 146 } 147 readResolve()148 private Object readResolve() throws ObjectStreamException { 149 return NumberFormatter.forSkeleton(skeleton) 150 .locale(ULocale.forLanguageTag(languageTag)) 151 .toFormat(); 152 } 153 } 154 } 155