1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2017 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 /** 7 * @hide exposed on OHOS 8 */ 9 public class PropertiesAffixPatternProvider implements AffixPatternProvider { 10 private final String posPrefix; 11 private final String posSuffix; 12 private final String negPrefix; 13 private final String negSuffix; 14 private final boolean isCurrencyPattern; 15 forProperties(DecimalFormatProperties properties)16 public static AffixPatternProvider forProperties(DecimalFormatProperties properties) { 17 if (properties.getCurrencyPluralInfo() == null) { 18 return new PropertiesAffixPatternProvider(properties); 19 } else { 20 return new CurrencyPluralInfoAffixProvider(properties.getCurrencyPluralInfo(), properties); 21 } 22 } 23 PropertiesAffixPatternProvider(DecimalFormatProperties properties)24 PropertiesAffixPatternProvider(DecimalFormatProperties properties) { 25 // There are two ways to set affixes in DecimalFormat: via the pattern string (applyPattern), and via the 26 // explicit setters (setPositivePrefix and friends). The way to resolve the settings is as follows: 27 // 28 // 1) If the explicit setting is present for the field, use it. 29 // 2) Otherwise, follows UTS 35 rules based on the pattern string. 30 // 31 // Importantly, the explicit setters affect only the one field they override. If you set the positive 32 // prefix, that should not affect the negative prefix. 33 34 // Convenience: Extract the properties into local variables. 35 // Variables are named with three chars: [p/n][p/s][o/p] 36 // [p/n] => p for positive, n for negative 37 // [p/s] => p for prefix, s for suffix 38 // [o/p] => o for escaped custom override string, p for pattern string 39 String ppo = AffixUtils.escape(properties.getPositivePrefix()); 40 String pso = AffixUtils.escape(properties.getPositiveSuffix()); 41 String npo = AffixUtils.escape(properties.getNegativePrefix()); 42 String nso = AffixUtils.escape(properties.getNegativeSuffix()); 43 String ppp = properties.getPositivePrefixPattern(); 44 String psp = properties.getPositiveSuffixPattern(); 45 String npp = properties.getNegativePrefixPattern(); 46 String nsp = properties.getNegativeSuffixPattern(); 47 48 if (ppo != null) { 49 posPrefix = ppo; 50 } else if (ppp != null) { 51 posPrefix = ppp; 52 } else { 53 // UTS 35: Default positive prefix is empty string. 54 posPrefix = ""; 55 } 56 57 if (pso != null) { 58 posSuffix = pso; 59 } else if (psp != null) { 60 posSuffix = psp; 61 } else { 62 // UTS 35: Default positive suffix is empty string. 63 posSuffix = ""; 64 } 65 66 if (npo != null) { 67 negPrefix = npo; 68 } else if (npp != null) { 69 negPrefix = npp; 70 } else { 71 // UTS 35: Default negative prefix is "-" with positive prefix. 72 // Important: We prepend the "-" to the pattern, not the override! 73 negPrefix = ppp == null ? "-" : "-" + ppp; 74 } 75 76 if (nso != null) { 77 negSuffix = nso; 78 } else if (nsp != null) { 79 negSuffix = nsp; 80 } else { 81 // UTS 35: Default negative prefix is the positive prefix. 82 negSuffix = psp == null ? "" : psp; 83 } 84 85 // For declaring if this is a currency pattern, we need to look at the 86 // original pattern, not at any user-specified overrides. 87 isCurrencyPattern = ( 88 AffixUtils.hasCurrencySymbols(ppp) || 89 AffixUtils.hasCurrencySymbols(psp) || 90 AffixUtils.hasCurrencySymbols(npp) || 91 AffixUtils.hasCurrencySymbols(nsp)); 92 } 93 94 @Override charAt(int flags, int i)95 public char charAt(int flags, int i) { 96 return getString(flags).charAt(i); 97 } 98 99 @Override length(int flags)100 public int length(int flags) { 101 return getString(flags).length(); 102 } 103 104 @Override getString(int flags)105 public String getString(int flags) { 106 boolean prefix = (flags & Flags.PREFIX) != 0; 107 boolean negative = (flags & Flags.NEGATIVE_SUBPATTERN) != 0; 108 if (prefix && negative) { 109 return negPrefix; 110 } else if (prefix) { 111 return posPrefix; 112 } else if (negative) { 113 return negSuffix; 114 } else { 115 return posSuffix; 116 } 117 } 118 119 @Override positiveHasPlusSign()120 public boolean positiveHasPlusSign() { 121 return AffixUtils.containsType(posPrefix, AffixUtils.TYPE_PLUS_SIGN) 122 || AffixUtils.containsType(posSuffix, AffixUtils.TYPE_PLUS_SIGN); 123 } 124 125 @Override hasNegativeSubpattern()126 public boolean hasNegativeSubpattern() { 127 return ( 128 negSuffix != posSuffix || 129 negPrefix.length() != posPrefix.length() + 1 || 130 !negPrefix.regionMatches(1, posPrefix, 0, posPrefix.length()) || 131 negPrefix.charAt(0) != '-' 132 ); 133 } 134 135 @Override negativeHasMinusSign()136 public boolean negativeHasMinusSign() { 137 return AffixUtils.containsType(negPrefix, AffixUtils.TYPE_MINUS_SIGN) 138 || AffixUtils.containsType(negSuffix, AffixUtils.TYPE_MINUS_SIGN); 139 } 140 141 @Override hasCurrencySign()142 public boolean hasCurrencySign() { 143 return isCurrencyPattern; 144 } 145 146 @Override containsSymbolType(int type)147 public boolean containsSymbolType(int type) { 148 return AffixUtils.containsType(posPrefix, type) || AffixUtils.containsType(posSuffix, type) 149 || AffixUtils.containsType(negPrefix, type) || AffixUtils.containsType(negSuffix, type); 150 } 151 152 @Override hasBody()153 public boolean hasBody() { 154 return true; 155 } 156 157 @Override toString()158 public String toString() { 159 return super.toString() 160 + " {" 161 + posPrefix 162 + "#" 163 + posSuffix 164 + ";" 165 + negPrefix 166 + "#" 167 + negSuffix 168 + "}"; 169 } 170 }