• 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 import ohos.global.icu.impl.SimpleFormatterImpl;
10 import ohos.global.icu.impl.number.range.PrefixInfixSuffixLengthHelper;
11 import ohos.global.icu.util.ICUException;
12 
13 /**
14  * The second primary implementation of {@link Modifier}, this one consuming a
15  * {@link ohos.global.icu.text.SimpleFormatter} pattern.
16  * @hide exposed on OHOS
17  */
18 public class SimpleModifier implements Modifier {
19     private final String compiledPattern;
20     private final Field field;
21     private final boolean strong;
22 
23     // Parameters: used for number range formatting
24     private final Parameters parameters;
25 
26     /** TODO: This is copied from SimpleFormatterImpl. */
27     private static final int ARG_NUM_LIMIT = 0x100;
28 
29     /** Creates a modifier that uses the SimpleFormatter string formats. */
SimpleModifier(String compiledPattern, Field field, boolean strong)30     public SimpleModifier(String compiledPattern, Field field, boolean strong) {
31         this(compiledPattern, field, strong, null);
32     }
33 
34     /** Creates a modifier that uses the SimpleFormatter string formats. */
SimpleModifier(String compiledPattern, Field field, boolean strong, Parameters parameters)35     public SimpleModifier(String compiledPattern, Field field, boolean strong, Parameters parameters) {
36         assert compiledPattern != null;
37         this.compiledPattern = compiledPattern;
38         this.field = field;
39         this.strong = strong;
40         this.parameters = parameters;
41     }
42 
43     @Override
apply(FormattedStringBuilder output, int leftIndex, int rightIndex)44     public int apply(FormattedStringBuilder output, int leftIndex, int rightIndex) {
45         return SimpleFormatterImpl.formatPrefixSuffix(compiledPattern, field, leftIndex, rightIndex, output);
46     }
47 
48     @Override
getPrefixLength()49     public int getPrefixLength() {
50         return SimpleFormatterImpl.getPrefixLength(compiledPattern);
51     }
52 
53     @Override
getCodePointCount()54     public int getCodePointCount() {
55         return SimpleFormatterImpl.getLength(compiledPattern, true);
56     }
57 
58     @Override
isStrong()59     public boolean isStrong() {
60         return strong;
61     }
62 
63     @Override
containsField(Field field)64     public boolean containsField(Field field) {
65         // This method is not currently used.
66         assert false;
67         return false;
68     }
69 
70     @Override
getParameters()71     public Parameters getParameters() {
72         return parameters;
73     }
74 
75     @Override
semanticallyEquivalent(Modifier other)76     public boolean semanticallyEquivalent(Modifier other) {
77         if (!(other instanceof SimpleModifier)) {
78             return false;
79         }
80         SimpleModifier _other = (SimpleModifier) other;
81         if (parameters != null && _other.parameters != null && parameters.obj == _other.parameters.obj) {
82             return true;
83         }
84         return compiledPattern.equals(_other.compiledPattern) && field == _other.field && strong == _other.strong;
85     }
86 
87     /**
88      * TODO: Like above, this belongs with the rest of the SimpleFormatterImpl code.
89      * I put it here so that the SimpleFormatter uses in FormattedStringBuilder are near each other.
90      *
91      * <p>
92      * Applies the compiled two-argument pattern to the FormattedStringBuilder.
93      *
94      * <p>
95      * This method is optimized for the case where the prefix and suffix are often empty, such as
96      * in the range pattern like "{0}-{1}".
97      */
formatTwoArgPattern(String compiledPattern, FormattedStringBuilder result, int index, PrefixInfixSuffixLengthHelper h, Field field)98     public static void formatTwoArgPattern(String compiledPattern, FormattedStringBuilder result, int index, PrefixInfixSuffixLengthHelper h,
99             Field field) {
100         int argLimit = SimpleFormatterImpl.getArgumentLimit(compiledPattern);
101         if (argLimit != 2) {
102             throw new ICUException();
103         }
104         int offset = 1; // offset into compiledPattern
105         int length = 0; // chars added to result
106 
107         int prefixLength = compiledPattern.charAt(offset);
108         offset++;
109         if (prefixLength < ARG_NUM_LIMIT) {
110             // No prefix
111             prefixLength = 0;
112         } else {
113             prefixLength -= ARG_NUM_LIMIT;
114             result.insert(index + length, compiledPattern, offset, offset + prefixLength, field);
115             offset += prefixLength;
116             length += prefixLength;
117             offset++;
118         }
119 
120         int infixLength = compiledPattern.charAt(offset);
121         offset++;
122         if (infixLength < ARG_NUM_LIMIT) {
123             // No infix
124             infixLength = 0;
125         } else {
126             infixLength -= ARG_NUM_LIMIT;
127             result.insert(index + length, compiledPattern, offset, offset + infixLength, field);
128             offset += infixLength;
129             length += infixLength;
130             offset++;
131         }
132 
133         int suffixLength;
134         if (offset == compiledPattern.length()) {
135             // No suffix
136             suffixLength = 0;
137         } else {
138             suffixLength = compiledPattern.charAt(offset) -  ARG_NUM_LIMIT;
139             offset++;
140             result.insert(index + length, compiledPattern, offset, offset + suffixLength, field);
141             length += suffixLength;
142         }
143 
144         h.lengthPrefix = prefixLength;
145         h.lengthInfix = infixLength;
146         h.lengthSuffix = suffixLength;
147     }
148 }
149