Move the dupes-removing method to the Utils class

This is preparation for bug: 5175740

Change-Id: I18b2042317f740cb1e021d3dfbf90ecfbb1a1d37
This commit is contained in:
Jean Chalard 2011-08-19 19:48:48 +09:00
parent 6d78302155
commit 6da8b74582
2 changed files with 35 additions and 28 deletions

View file

@ -404,7 +404,7 @@ public class Suggest implements Dictionary.WordCallback {
if (typedWord != null) {
mSuggestions.add(0, typedWordString);
}
removeDupes(mSuggestions);
Utils.removeDupes(mSuggestions);
if (DBG) {
double normalizedScore = mAutoCorrection.getNormalizedScore();
@ -431,33 +431,6 @@ public class Suggest implements Dictionary.WordCallback {
return new SuggestedWords.Builder().addWords(mSuggestions, null);
}
private static void removeDupes(final ArrayList<CharSequence> suggestions) {
if (suggestions.size() < 2) return;
int i = 1;
// Don't cache suggestions.size(), since we may be removing items
while (i < suggestions.size()) {
final CharSequence cur = suggestions.get(i);
// Compare each candidate with each previous candidate
for (int j = 0; j < i; j++) {
CharSequence previous = suggestions.get(j);
if (TextUtils.equals(cur, previous)) {
removeFromSuggestions(suggestions, i);
i--;
break;
}
}
i++;
}
}
private static void removeFromSuggestions(final ArrayList<CharSequence> suggestions,
final int index) {
final CharSequence garbage = suggestions.remove(index);
if (garbage instanceof StringBuilder) {
StringBuilderPool.recycle((StringBuilder)garbage);
}
}
public boolean hasAutoCorrection() {
return mAutoCorrection.hasAutoCorrection();
}

View file

@ -32,6 +32,7 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;
import android.text.InputType;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
@ -735,4 +736,37 @@ public class Utils {
return retval;
}
}
/**
* Remove duplicates from an array of strings.
*
* This method will always keep the first occurence of all strings at their position
* in the array, removing the subsequent ones.
*/
public static void removeDupes(final ArrayList<CharSequence> suggestions) {
if (suggestions.size() < 2) return;
int i = 1;
// Don't cache suggestions.size(), since we may be removing items
while (i < suggestions.size()) {
final CharSequence cur = suggestions.get(i);
// Compare each candidate with each previous candidate
for (int j = 0; j < i; j++) {
CharSequence previous = suggestions.get(j);
if (TextUtils.equals(cur, previous)) {
removeFromSuggestions(suggestions, i);
i--;
break;
}
}
i++;
}
}
private static void removeFromSuggestions(final ArrayList<CharSequence> suggestions,
final int index) {
final CharSequence garbage = suggestions.remove(index);
if (garbage instanceof StringBuilder) {
StringBuilderPool.recycle((StringBuilder)garbage);
}
}
}