• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import java.text.Format.Field;
7 
8 import ohos.global.icu.impl.FormattedStringBuilder;
9 
10 /**
11  * The canonical implementation of {@link Modifier}, containing a prefix and suffix string.
12  * @hide exposed on OHOS
13  */
14 public class ConstantAffixModifier implements Modifier {
15 
16     // TODO: Avoid making a new instance by default if prefix and suffix are empty
17     public static final ConstantAffixModifier EMPTY = new ConstantAffixModifier();
18 
19     private final String prefix;
20     private final String suffix;
21     private final Field field;
22     private final boolean strong;
23 
24     /**
25      * Constructs an instance with the given strings.
26      *
27      * <p>
28      * The arguments need to be Strings, not CharSequences, because Strings are immutable but
29      * CharSequences are not.
30      *
31      * @param prefix
32      *            The prefix string.
33      * @param suffix
34      *            The suffix string.
35      * @param field
36      *            The field type to be associated with this modifier. Can be null.
37      * @param strong
38      *            Whether this modifier should be strongly applied.
39      * @see Field
40      */
ConstantAffixModifier(String prefix, String suffix, Field field, boolean strong)41     public ConstantAffixModifier(String prefix, String suffix, Field field, boolean strong) {
42         // Use an empty string instead of null if we are given null
43         // TODO: Consider returning a null modifier if both prefix and suffix are empty.
44         this.prefix = (prefix == null ? "" : prefix);
45         this.suffix = (suffix == null ? "" : suffix);
46         this.field = field;
47         this.strong = strong;
48     }
49 
50     /** Constructs a new instance with an empty prefix, suffix, and field. */
ConstantAffixModifier()51     public ConstantAffixModifier() {
52         prefix = "";
53         suffix = "";
54         field = null;
55         strong = false;
56     }
57 
58     @Override
apply(FormattedStringBuilder output, int leftIndex, int rightIndex)59     public int apply(FormattedStringBuilder output, int leftIndex, int rightIndex) {
60         // Insert the suffix first since inserting the prefix will change the rightIndex
61         int length = output.insert(rightIndex, suffix, field);
62         length += output.insert(leftIndex, prefix, field);
63         return length;
64     }
65 
66     @Override
getPrefixLength()67     public int getPrefixLength() {
68         return prefix.length();
69     }
70 
71     @Override
getCodePointCount()72     public int getCodePointCount() {
73         return prefix.codePointCount(0, prefix.length()) + suffix.codePointCount(0, suffix.length());
74     }
75 
76     @Override
isStrong()77     public boolean isStrong() {
78         return strong;
79     }
80 
81     @Override
containsField(Field field)82     public boolean containsField(Field field) {
83         // This method is not currently used.
84         assert false;
85         return false;
86     }
87 
88     @Override
getParameters()89     public Parameters getParameters() {
90         return null;
91     }
92 
93     @Override
semanticallyEquivalent(Modifier other)94     public boolean semanticallyEquivalent(Modifier other) {
95         if (!(other instanceof ConstantAffixModifier)) {
96             return false;
97         }
98         ConstantAffixModifier _other = (ConstantAffixModifier) other;
99         return prefix.equals(_other.prefix) && suffix.equals(_other.suffix) && field == _other.field
100                 && strong == _other.strong;
101     }
102 
103     @Override
toString()104     public String toString() {
105         return String.format("<ConstantAffixModifier prefix:'%s' suffix:'%s'>", prefix, suffix);
106     }
107 }
108