LEFT | RIGHT |
(no file at all) | |
| 1 // © 2017 and later: Unicode, Inc. and others. |
| 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
| 3 package com.ibm.icu.impl.number; |
| 4 |
| 5 public class PropertiesAffixPatternProvider implements AffixPatternProvider { |
| 6 private final String posPrefix; |
| 7 private final String posSuffix; |
| 8 private final String negPrefix; |
| 9 private final String negSuffix; |
| 10 |
| 11 public PropertiesAffixPatternProvider(DecimalFormatProperties properties) { |
| 12 // There are two ways to set affixes in DecimalFormat: via the pattern s
tring (applyPattern), and via the |
| 13 // explicit setters (setPositivePrefix and friends). The way to resolve
the settings is as follows: |
| 14 // |
| 15 // 1) If the explicit setting is present for the field, use it. |
| 16 // 2) Otherwise, follows UTS 35 rules based on the pattern string. |
| 17 // |
| 18 // Importantly, the explicit setters affect only the one field they over
ride. If you set the positive |
| 19 // prefix, that should not affect the negative prefix. Since it is impo
ssible for the user of this class |
| 20 // to know whether the origin for a string was the override or the patte
rn, we have to say that we always |
| 21 // have a negative subpattern and perform all resolution logic here. |
| 22 |
| 23 // Convenience: Extract the properties into local variables. |
| 24 // Variables are named with three chars: [p/n][p/s][o/p] |
| 25 // [p/n] => p for positive, n for negative |
| 26 // [p/s] => p for prefix, s for suffix |
| 27 // [o/p] => o for escaped custom override string, p for pattern string |
| 28 String ppo = AffixUtils.escape(properties.getPositivePrefix()); |
| 29 String pso = AffixUtils.escape(properties.getPositiveSuffix()); |
| 30 String npo = AffixUtils.escape(properties.getNegativePrefix()); |
| 31 String nso = AffixUtils.escape(properties.getNegativeSuffix()); |
| 32 String ppp = properties.getPositivePrefixPattern(); |
| 33 String psp = properties.getPositiveSuffixPattern(); |
| 34 String npp = properties.getNegativePrefixPattern(); |
| 35 String nsp = properties.getNegativeSuffixPattern(); |
| 36 |
| 37 if (ppo != null) { |
| 38 posPrefix = ppo; |
| 39 } else if (ppp != null) { |
| 40 posPrefix = ppp; |
| 41 } else { |
| 42 // UTS 35: Default positive prefix is empty string. |
| 43 posPrefix = ""; |
| 44 } |
| 45 |
| 46 if (pso != null) { |
| 47 posSuffix = pso; |
| 48 } else if (psp != null) { |
| 49 posSuffix = psp; |
| 50 } else { |
| 51 // UTS 35: Default positive suffix is empty string. |
| 52 posSuffix = ""; |
| 53 } |
| 54 |
| 55 if (npo != null) { |
| 56 negPrefix = npo; |
| 57 } else if (npp != null) { |
| 58 negPrefix = npp; |
| 59 } else { |
| 60 // UTS 35: Default negative prefix is "-" with positive prefix. |
| 61 // Important: We prepend the "-" to the pattern, not the override! |
| 62 negPrefix = ppp == null ? "-" : "-" + ppp; |
| 63 } |
| 64 |
| 65 if (nso != null) { |
| 66 negSuffix = nso; |
| 67 } else if (nsp != null) { |
| 68 negSuffix = nsp; |
| 69 } else { |
| 70 // UTS 35: Default negative prefix is the positive prefix. |
| 71 negSuffix = psp == null ? "" : psp; |
| 72 } |
| 73 } |
| 74 |
| 75 @Override |
| 76 public char charAt(int flags, int i) { |
| 77 return getString(flags).charAt(i); |
| 78 } |
| 79 |
| 80 @Override |
| 81 public int length(int flags) { |
| 82 return getString(flags).length(); |
| 83 } |
| 84 |
| 85 @Override |
| 86 public String getString(int flags) { |
| 87 boolean prefix = (flags & Flags.PREFIX) != 0; |
| 88 boolean negative = (flags & Flags.NEGATIVE_SUBPATTERN) != 0; |
| 89 if (prefix && negative) { |
| 90 return negPrefix; |
| 91 } else if (prefix) { |
| 92 return posPrefix; |
| 93 } else if (negative) { |
| 94 return negSuffix; |
| 95 } else { |
| 96 return posSuffix; |
| 97 } |
| 98 } |
| 99 |
| 100 @Override |
| 101 public boolean positiveHasPlusSign() { |
| 102 return AffixUtils.containsType(posPrefix, AffixUtils.TYPE_PLUS_SIGN) |
| 103 || AffixUtils.containsType(posSuffix, AffixUtils.TYPE_PLUS_SIGN)
; |
| 104 } |
| 105 |
| 106 @Override |
| 107 public boolean hasNegativeSubpattern() { |
| 108 // See comments in the constructor for more information on why this is a
lways true. |
| 109 return true; |
| 110 } |
| 111 |
| 112 @Override |
| 113 public boolean negativeHasMinusSign() { |
| 114 return AffixUtils.containsType(negPrefix, AffixUtils.TYPE_MINUS_SIGN) |
| 115 || AffixUtils.containsType(negSuffix, AffixUtils.TYPE_MINUS_SIGN
); |
| 116 } |
| 117 |
| 118 @Override |
| 119 public boolean hasCurrencySign() { |
| 120 return AffixUtils.hasCurrencySymbols(posPrefix) || AffixUtils.hasCurrenc
ySymbols(posSuffix) |
| 121 || AffixUtils.hasCurrencySymbols(negPrefix) || AffixUtils.hasCur
rencySymbols(negSuffix); |
| 122 } |
| 123 |
| 124 @Override |
| 125 public boolean containsSymbolType(int type) { |
| 126 return AffixUtils.containsType(posPrefix, type) || AffixUtils.containsTy
pe(posSuffix, type) |
| 127 || AffixUtils.containsType(negPrefix, type) || AffixUtils.contai
nsType(negSuffix, type); |
| 128 } |
| 129 } |
LEFT | RIGHT |