@@ -106,9 +106,8 @@ public void append(final CharSequence str) {
106106 * @param maxPhonemes the maximum number of phonemes to build up
107107 */
108108 public void apply (final Rule .PhonemeExpr phonemeExpr , final int maxPhonemes ) {
109- final Set <Rule .Phoneme > newPhonemes = new LinkedHashSet <>(maxPhonemes );
110-
111- EXPR : for (final Rule .Phoneme left : this .phonemes ) {
109+ final Set <Rule .Phoneme > newPhonemes = new LinkedHashSet <>(Math .min (phonemes .size () * phonemeExpr .size (), maxPhonemes ));
110+ EXPR : for (final Rule .Phoneme left : phonemes ) {
112111 for (final Rule .Phoneme right : phonemeExpr .getPhonemes ()) {
113112 final LanguageSet languages = left .getLanguages ().restrictTo (right .getLanguages ());
114113 if (!languages .isEmpty ()) {
@@ -122,9 +121,8 @@ public void apply(final Rule.PhonemeExpr phonemeExpr, final int maxPhonemes) {
122121 }
123122 }
124123 }
125-
126- this .phonemes .clear ();
127- this .phonemes .addAll (newPhonemes );
124+ phonemes .clear ();
125+ phonemes .addAll (newPhonemes );
128126 }
129127
130128 /**
@@ -133,7 +131,7 @@ public void apply(final Rule.PhonemeExpr phonemeExpr, final int maxPhonemes) {
133131 * @return the phoneme set
134132 */
135133 public Set <Rule .Phoneme > getPhonemes () {
136- return this . phonemes ;
134+ return phonemes ;
137135 }
138136
139137 /**
@@ -155,22 +153,24 @@ public String makeString() {
155153 * processed already), and {@code found} indicates if a matching rule was found or not. In the case where a
156154 * matching rule was found, {@code phonemeBuilder} is replaced with a new builder containing the phonemes
157155 * updated by the matching rule.
158- *
156+ * <p>
159157 * Although this class is not thread-safe (it has mutable unprotected fields), it is not shared between threads
160158 * as it is constructed as needed by the calling methods.
159+ * </p>
160+ *
161161 * @since 1.6
162162 */
163163 private static final class RulesApplication {
164+
164165 private final Map <String , List <Rule >> finalRules ;
165166 private final CharSequence input ;
166-
167167 private final PhonemeBuilder phonemeBuilder ;
168168 private int i ;
169169 private final int maxPhonemes ;
170170 private boolean found ;
171171
172- public RulesApplication (final Map <String , List <Rule >> finalRules , final CharSequence input ,
173- final PhonemeBuilder phonemeBuilder , final int i , final int maxPhonemes ) {
172+ public RulesApplication (final Map <String , List <Rule >> finalRules , final CharSequence input , final PhonemeBuilder phonemeBuilder , final int i ,
173+ final int maxPhonemes ) {
174174 Objects .requireNonNull (finalRules , "finalRules" );
175175 this .finalRules = finalRules ;
176176 this .phonemeBuilder = phonemeBuilder ;
@@ -180,11 +180,11 @@ public RulesApplication(final Map<String, List<Rule>> finalRules, final CharSequ
180180 }
181181
182182 public int getI () {
183- return this . i ;
183+ return i ;
184184 }
185185
186186 public PhonemeBuilder getPhonemeBuilder () {
187- return this . phonemeBuilder ;
187+ return phonemeBuilder ;
188188 }
189189
190190 /**
@@ -195,31 +195,31 @@ public PhonemeBuilder getPhonemeBuilder() {
195195 * @return {@code this}
196196 */
197197 public RulesApplication invoke () {
198- this . found = false ;
198+ found = false ;
199199 int patternLength = 1 ;
200- final List <Rule > rules = this . finalRules .get (input .subSequence (i , i + patternLength ));
200+ final List <Rule > rules = finalRules .get (input .subSequence (i , i + patternLength ));
201201 if (rules != null ) {
202202 for (final Rule rule : rules ) {
203203 final String pattern = rule .getPattern ();
204204 patternLength = pattern .length ();
205- if (rule .patternAndContextMatches (this . input , this . i )) {
206- this . phonemeBuilder .apply (rule .getPhoneme (), maxPhonemes );
207- this . found = true ;
205+ if (rule .patternAndContextMatches (input , i )) {
206+ phonemeBuilder .apply (rule .getPhoneme (), maxPhonemes );
207+ found = true ;
208208 break ;
209209 }
210210 }
211211 }
212212
213- if (!this . found ) {
213+ if (!found ) {
214214 patternLength = 1 ;
215215 }
216216
217- this . i += patternLength ;
217+ i += patternLength ;
218218 return this ;
219219 }
220220
221221 public boolean isFound () {
222- return this . found ;
222+ return found ;
223223 }
224224 }
225225
@@ -269,11 +269,11 @@ private static String join(final List<String> strings, final String sep) {
269269 * the type of names it will use
270270 * @param ruleType
271271 * the type of rules it will apply
272- * @param concat
272+ * @param concatenate
273273 * if it will concatenate multiple encodings
274274 */
275- public PhoneticEngine (final NameType nameType , final RuleType ruleType , final boolean concat ) {
276- this (nameType , ruleType , concat , DEFAULT_MAX_PHONEMES );
275+ public PhoneticEngine (final NameType nameType , final RuleType ruleType , final boolean concatenate ) {
276+ this (nameType , ruleType , concatenate , DEFAULT_MAX_PHONEMES );
277277 }
278278
279279 /**
@@ -283,20 +283,19 @@ public PhoneticEngine(final NameType nameType, final RuleType ruleType, final bo
283283 * the type of names it will use
284284 * @param ruleType
285285 * the type of rules it will apply
286- * @param concat
286+ * @param concatenate
287287 * if it will concatenate multiple encodings
288288 * @param maxPhonemes
289289 * the maximum number of phonemes that will be handled
290290 * @since 1.7
291291 */
292- public PhoneticEngine (final NameType nameType , final RuleType ruleType , final boolean concat ,
293- final int maxPhonemes ) {
292+ public PhoneticEngine (final NameType nameType , final RuleType ruleType , final boolean concatenate , final int maxPhonemes ) {
294293 if (ruleType == RuleType .RULES ) {
295294 throw new IllegalArgumentException ("ruleType must not be " + RuleType .RULES );
296295 }
297296 this .nameType = nameType ;
298297 this .ruleType = ruleType ;
299- this .concat = concat ;
298+ this .concat = concatenate ;
300299 this .lang = Lang .instance (nameType );
301300 this .maxPhonemes = maxPhonemes ;
302301 }
0 commit comments