aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/Utils.java
diff options
context:
space:
mode:
authorJean Chalard <jchalard@google.com>2011-08-19 19:48:48 +0900
committerJean Chalard <jchalard@google.com>2011-08-19 19:49:59 +0900
commit6da8b74582b1c70cae02558c605c5a224329cf7a (patch)
treefa92d1f3c478a213f2effa8da9253e627208f984 /java/src/com/android/inputmethod/latin/Utils.java
parent6d78302155d8a6437ab6541d93ddb42bf21e0a61 (diff)
downloadlatinime-6da8b74582b1c70cae02558c605c5a224329cf7a.tar.gz
latinime-6da8b74582b1c70cae02558c605c5a224329cf7a.tar.xz
latinime-6da8b74582b1c70cae02558c605c5a224329cf7a.zip
Move the dupes-removing method to the Utils class
This is preparation for bug: 5175740 Change-Id: I18b2042317f740cb1e021d3dfbf90ecfbb1a1d37
Diffstat (limited to 'java/src/com/android/inputmethod/latin/Utils.java')
-rw-r--r--java/src/com/android/inputmethod/latin/Utils.java34
1 files changed, 34 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java
index 1a6260a4e..36fbfd951 100644
--- a/java/src/com/android/inputmethod/latin/Utils.java
+++ b/java/src/com/android/inputmethod/latin/Utils.java
@@ -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);
+ }
+ }
}