• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2018 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.parse;
5 
6 import ohos.global.icu.impl.number.AffixUtils;
7 
8 /**
9  * A specialized version of {@link SeriesMatcher} that matches EITHER a prefix OR a suffix.
10  * {@link AffixMatcher} combines two of these in order to match both the prefix and suffix.
11  *
12  * @author sffc
13  * @hide exposed on OHOS
14  */
15 public class AffixPatternMatcher extends SeriesMatcher implements AffixUtils.TokenConsumer {
16 
17     private final String affixPattern;
18 
19     // Used during construction only:
20     private AffixTokenMatcherFactory factory;
21     private IgnorablesMatcher ignorables;
22     private int lastTypeOrCp;
23 
AffixPatternMatcher(String affixPattern)24     private AffixPatternMatcher(String affixPattern) {
25         this.affixPattern = affixPattern;
26     }
27 
28     /**
29      * Creates an AffixPatternMatcher (based on SeriesMatcher) from the given affix pattern. Returns null
30      * if the affix pattern is empty.
31      */
fromAffixPattern( String affixPattern, AffixTokenMatcherFactory factory, int parseFlags)32     public static AffixPatternMatcher fromAffixPattern(
33             String affixPattern,
34             AffixTokenMatcherFactory factory,
35             int parseFlags) {
36         if (affixPattern.isEmpty()) {
37             return null;
38         }
39 
40         AffixPatternMatcher series = new AffixPatternMatcher(affixPattern);
41         series.factory = factory;
42         series.ignorables = (0 != (parseFlags & ParsingUtils.PARSE_FLAG_EXACT_AFFIX)) ? null
43                 : factory.ignorables();
44         series.lastTypeOrCp = 0;
45         AffixUtils.iterateWithConsumer(affixPattern, series);
46 
47         // De-reference the memory
48         series.factory = null;
49         series.ignorables = null;
50         series.lastTypeOrCp = 0;
51 
52         series.freeze();
53         return series;
54     }
55 
56     /**
57      * This method is NOT intended to be called directly. It is here for the AffixUtils.TokenConsumer
58      * interface only.
59      */
60     @Override
consumeToken(int typeOrCp)61     public void consumeToken(int typeOrCp) {
62         // This is called by AffixUtils.iterateWithConsumer() for each token.
63 
64         // Add an ignorables matcher between tokens except between two literals, and don't put two
65         // ignorables matchers in a row.
66         if (ignorables != null
67                 && length() > 0
68                 && (lastTypeOrCp < 0 || !ignorables.getSet().contains(lastTypeOrCp))) {
69             addMatcher(ignorables);
70         }
71 
72         if (typeOrCp < 0) {
73             // Case 1: the token is a symbol.
74             switch (typeOrCp) {
75             case AffixUtils.TYPE_MINUS_SIGN:
76                 addMatcher(factory.minusSign());
77                 break;
78             case AffixUtils.TYPE_PLUS_SIGN:
79                 addMatcher(factory.plusSign());
80                 break;
81             case AffixUtils.TYPE_PERCENT:
82                 addMatcher(factory.percent());
83                 break;
84             case AffixUtils.TYPE_PERMILLE:
85                 addMatcher(factory.permille());
86                 break;
87             case AffixUtils.TYPE_CURRENCY_SINGLE:
88             case AffixUtils.TYPE_CURRENCY_DOUBLE:
89             case AffixUtils.TYPE_CURRENCY_TRIPLE:
90             case AffixUtils.TYPE_CURRENCY_QUAD:
91             case AffixUtils.TYPE_CURRENCY_QUINT:
92                 // All currency symbols use the same matcher
93                 addMatcher(factory.currency());
94                 break;
95             default:
96                 throw new AssertionError();
97             }
98 
99         } else if (ignorables != null && ignorables.getSet().contains(typeOrCp)) {
100             // Case 2: the token is an ignorable literal.
101             // No action necessary: the ignorables matcher has already been added.
102 
103         } else {
104             // Case 3: the token is a non-ignorable literal.
105             addMatcher(CodePointMatcher.getInstance(typeOrCp));
106         }
107         lastTypeOrCp = typeOrCp;
108     }
109 
getPattern()110     public String getPattern() {
111         return affixPattern;
112     }
113 
114     @Override
equals(Object other)115     public boolean equals(Object other) {
116         if (this == other)
117             return true;
118         if (!(other instanceof AffixPatternMatcher))
119             return false;
120         return affixPattern.equals(((AffixPatternMatcher) other).affixPattern);
121     }
122 
123     @Override
hashCode()124     public int hashCode() {
125         return affixPattern.hashCode();
126     }
127 
128     @Override
toString()129     public String toString() {
130         return affixPattern;
131     }
132 }
133