Skip to content

Commit c170f6b

Browse files
committed
[CODEC-174] Improve PhonemeBuilder.apply by updating the internal state instead of creating a new PhonemeBuilder each time.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1542831 13f79535-47bb-0310-9956-ffa450edef68
1 parent f986994 commit c170f6b

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

src/main/java/org/apache/commons/codec/language/bm/PhoneticEngine.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,34 @@ public void append(final CharSequence str) {
9595
}
9696

9797
/**
98-
* Creates a new phoneme builder containing the application of the expression to all phonemes in this builder.
98+
* Applies the given phoneme expression to all phonemes in this phoneme builder.
9999
* <p>
100100
* This will lengthen phonemes that have compatible language sets to the expression, and drop those that are
101101
* incompatible.
102102
*
103103
* @param phonemeExpr the expression to apply
104104
* @param maxPhonemes the maximum number of phonemes to build up
105-
* @return a new phoneme builder containing the results of <code>phonemeExpr</code> applied to each phoneme
106-
* in turn
107105
*/
108-
public PhonemeBuilder apply(final Rule.PhonemeExpr phonemeExpr, final int maxPhonemes) {
109-
final Set<Rule.Phoneme> newPhonemes = new LinkedHashSet<Rule.Phoneme>();
106+
public void apply(final Rule.PhonemeExpr phonemeExpr, final int maxPhonemes) {
107+
final List<Rule.Phoneme> newPhonemes = new ArrayList<Rule.Phoneme>(maxPhonemes);
110108

111109
EXPR: for (final Rule.Phoneme left : this.phonemes) {
112110
for (final Rule.Phoneme right : phonemeExpr.getPhonemes()) {
113-
LanguageSet languages = left.getLanguages().restrictTo(right.getLanguages());
111+
final LanguageSet languages = left.getLanguages().restrictTo(right.getLanguages());
114112
if (!languages.isEmpty()) {
115113
final Rule.Phoneme join = new Phoneme(left, right, languages);
116114
if (newPhonemes.size() < maxPhonemes) {
117115
newPhonemes.add(join);
118-
} else {
119-
break EXPR;
116+
if (newPhonemes.size() >= maxPhonemes) {
117+
break EXPR;
118+
}
120119
}
121120
}
122121
}
123122
}
124123

125-
return new PhonemeBuilder(newPhonemes);
124+
this.phonemes.clear();
125+
this.phonemes.addAll(newPhonemes);
126126
}
127127

128128
/**
@@ -212,7 +212,7 @@ public RulesApplication invoke() {
212212
final String pattern = rule.getPattern();
213213
patternLength = pattern.length();
214214
if (rule.patternAndContextMatches(this.input, this.i)) {
215-
this.phonemeBuilder = this.phonemeBuilder.apply(rule.getPhoneme(), maxPhonemes);
215+
this.phonemeBuilder.apply(rule.getPhoneme(), maxPhonemes);
216216
this.found = true;
217217
break;
218218
}

0 commit comments

Comments
 (0)