LEFT | RIGHT |
1 // © 2017 and later: Unicode, Inc. and others. | 1 // © 2017 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 package com.ibm.icu.impl.number.modifiers; | 3 package com.ibm.icu.impl.number.modifiers; |
4 | 4 |
5 import com.ibm.icu.impl.number.DoubleSidedStringBuilder; | |
6 import com.ibm.icu.impl.number.Modifier; | 5 import com.ibm.icu.impl.number.Modifier; |
| 6 import com.ibm.icu.impl.number.Modifier.AffixModifier; |
| 7 import com.ibm.icu.impl.number.NumberStringBuilder; |
7 import com.ibm.icu.impl.number.Properties; | 8 import com.ibm.icu.impl.number.Properties; |
| 9 import com.ibm.icu.text.NumberFormat.Field; |
8 | 10 |
9 /** The canonical implementation of {@link Modifier}, containing a prefix and su
ffix string. */ | 11 /** The canonical implementation of {@link Modifier}, containing a prefix and su
ffix string. */ |
10 public class ConstantAffixModifier extends Modifier.BaseModifier { | 12 public class ConstantAffixModifier extends Modifier.BaseModifier implements Affi
xModifier { |
11 // TODO: Avoid making a new ConstantAffixModifier by default if prefix and suf
fix are empty | |
12 public static final ConstantAffixModifier EMPTY = new ConstantAffixModifier(""
, ""); | |
13 | 13 |
14 // Although these strings are public, they are read-only because they are fina
l. | 14 // TODO: Avoid making a new instance by default if prefix and suffix are empty |
15 public final String prefix; | 15 public static final AffixModifier EMPTY = new ConstantAffixModifier(); |
16 public final String suffix; | 16 |
| 17 private final String prefix; |
| 18 private final String suffix; |
| 19 private final Field field; |
| 20 private final boolean strong; |
17 | 21 |
18 /** | 22 /** |
19 * Constructs an instance with the given strings. | 23 * Constructs an instance with the given strings. |
20 * | 24 * |
21 * <p>The arguments need to be Strings, not CharSequences, because Strings are
immutable but | 25 * <p>The arguments need to be Strings, not CharSequences, because Strings are
immutable but |
22 * CharSequences are not. | 26 * CharSequences are not. |
23 * | 27 * |
24 * @param prefix The prefix string. | 28 * @param prefix The prefix string. |
25 * @param suffix The suffix string. | 29 * @param suffix The suffix string. |
| 30 * @param field The field type to be associated with this modifier. Can be nul
l. |
| 31 * @param strong Whether this modifier should be strongly applied. |
| 32 * @see Field |
26 */ | 33 */ |
27 public ConstantAffixModifier(String prefix, String suffix) { | 34 public ConstantAffixModifier(String prefix, String suffix, Field field, boolea
n strong) { |
28 // Use an empty string instead of null if we are given null | 35 // Use an empty string instead of null if we are given null |
| 36 // TODO: Consider returning a null modifier if both prefix and suffix are em
pty. |
29 this.prefix = (prefix == null ? "" : prefix); | 37 this.prefix = (prefix == null ? "" : prefix); |
30 this.suffix = (suffix == null ? "" : suffix); | 38 this.suffix = (suffix == null ? "" : suffix); |
| 39 this.field = field; |
| 40 this.strong = strong; |
| 41 } |
| 42 |
| 43 /** |
| 44 * Constructs a new instance with an empty prefix, suffix, and field. |
| 45 */ |
| 46 public ConstantAffixModifier() { |
| 47 prefix = ""; |
| 48 suffix = ""; |
| 49 field = null; |
| 50 strong = false; |
31 } | 51 } |
32 | 52 |
33 @Override | 53 @Override |
34 public int apply(DoubleSidedStringBuilder output, int leftIndex, int rightInde
x) { | 54 public int apply(NumberStringBuilder output, int leftIndex, int rightIndex) { |
35 // Insert the suffix first since inserting the prefix will change the rightI
ndex | 55 // Insert the suffix first since inserting the prefix will change the rightI
ndex |
36 output.insert(rightIndex, suffix); | 56 int length = output.insert(rightIndex, suffix, field); |
37 output.insert(leftIndex, prefix); | 57 length += output.insert(leftIndex, prefix, field); |
38 return prefix.length() + suffix.length(); | 58 return length; |
39 } | 59 } |
40 | 60 |
41 @Override | 61 @Override |
42 public int length() { | 62 public int length() { |
43 return prefix.length() + suffix.length(); | 63 return prefix.length() + suffix.length(); |
| 64 } |
| 65 |
| 66 @Override |
| 67 public boolean isStrong() { |
| 68 return strong; |
| 69 } |
| 70 |
| 71 @Override |
| 72 public String getPrefix() { |
| 73 return prefix; |
| 74 } |
| 75 |
| 76 @Override |
| 77 public String getSuffix() { |
| 78 return suffix; |
| 79 } |
| 80 |
| 81 public boolean contentEquals(CharSequence _prefix, CharSequence _suffix) { |
| 82 if (_prefix == null && !prefix.isEmpty()) return false; |
| 83 if (_suffix == null && !suffix.isEmpty()) return false; |
| 84 if (prefix.length() != _prefix.length()) return false; |
| 85 if (suffix.length() != _suffix.length()) return false; |
| 86 for (int i = 0; i < prefix.length(); i++) { |
| 87 if (prefix.charAt(i) != _prefix.charAt(i)) return false; |
| 88 } |
| 89 for (int i = 0; i < suffix.length(); i++) { |
| 90 if (suffix.charAt(i) != _suffix.charAt(i)) return false; |
| 91 } |
| 92 return true; |
44 } | 93 } |
45 | 94 |
46 @Override | 95 @Override |
47 public String toString() { | 96 public String toString() { |
48 return String.format( | 97 return String.format( |
49 "<ConstantAffixModifier(%d) prefix:'%s' suffix:'%s'>", length(), prefix,
suffix); | 98 "<ConstantAffixModifier(%d) prefix:'%s' suffix:'%s'>", length(), prefix,
suffix); |
50 } | 99 } |
51 | 100 |
52 @Override | 101 @Override |
53 public void export(Properties properties) { | 102 public void export(Properties properties) { |
54 properties.setPositivePrefix(prefix); | 103 throw new UnsupportedOperationException(); |
55 properties.setPositiveSuffix(suffix); | |
56 } | 104 } |
57 } | 105 } |
LEFT | RIGHT |