Add StringUtils.joinCommaSplittableText

Change-Id: I80ad9d8b5aee53f1403486a56ffae8d09cbd4e28
diff --git a/java/src/com/android/inputmethod/latin/utils/StringUtils.java b/java/src/com/android/inputmethod/latin/utils/StringUtils.java
index a365483..8c386e8 100644
--- a/java/src/com/android/inputmethod/latin/utils/StringUtils.java
+++ b/java/src/com/android/inputmethod/latin/utils/StringUtils.java
@@ -39,6 +39,8 @@
     public static final int CAPITALIZE_FIRST = 1; // First only
     public static final int CAPITALIZE_ALL = 2;   // All caps
 
+    private static final String EMPTY_STRING = "";
+
     private StringUtils() {
         // This utility class is not publicly instantiable.
     }
@@ -80,6 +82,22 @@
         return containsInArray(text, extraValues.split(SEPARATOR_FOR_COMMA_SPLITTABLE_TEXT));
     }
 
+    // Remove @UsedForTesting annotation once this method is used in the production code.
+    @UsedForTesting
+    public static String joinCommaSplittableText(final String head, final String tail) {
+        if (TextUtils.isEmpty(head) && TextUtils.isEmpty(tail)) {
+            return EMPTY_STRING;
+        }
+        // Here either head or tail is not null.
+        if (TextUtils.isEmpty(head)) {
+            return tail;
+        }
+        if (TextUtils.isEmpty(tail)) {
+            return head;
+        }
+        return head + SEPARATOR_FOR_COMMA_SPLITTABLE_TEXT + tail;
+    }
+
     public static String appendToCommaSplittableTextIfNotExists(final String text,
             final String extraValues) {
         if (TextUtils.isEmpty(extraValues)) {
@@ -94,7 +112,7 @@
     public static String removeFromCommaSplittableTextIfExists(final String text,
             final String extraValues) {
         if (TextUtils.isEmpty(extraValues)) {
-            return "";
+            return EMPTY_STRING;
         }
         final String[] elements = extraValues.split(SEPARATOR_FOR_COMMA_SPLITTABLE_TEXT);
         if (!containsInArray(text, elements)) {
@@ -380,7 +398,7 @@
     @UsedForTesting
     public static String byteArrayToHexString(byte[] bytes) {
         if (bytes == null || bytes.length == 0) {
-            return "";
+            return EMPTY_STRING;
         }
         final StringBuilder sb = new StringBuilder();
         for (byte b : bytes) {
@@ -444,7 +462,7 @@
 
     public static String listToJsonStr(List<Object> list) {
         if (list == null || list.isEmpty()) {
-            return "";
+            return EMPTY_STRING;
         }
         final StringWriter sw = new StringWriter();
         final JsonWriter writer = new JsonWriter(sw);
@@ -470,6 +488,6 @@
             } catch (IOException e) {
             }
         }
-        return "";
+        return EMPTY_STRING;
     }
 }
diff --git a/tests/src/com/android/inputmethod/latin/utils/StringUtilsTests.java b/tests/src/com/android/inputmethod/latin/utils/StringUtilsTests.java
index 4e396a1..21fcf11 100644
--- a/tests/src/com/android/inputmethod/latin/utils/StringUtilsTests.java
+++ b/tests/src/com/android/inputmethod/latin/utils/StringUtilsTests.java
@@ -44,7 +44,7 @@
         }));
     }
 
-    public void testContainsInExtraValues() {
+    public void testContainsInCommaSplittableText() {
         assertFalse("null", StringUtils.containsInCommaSplittableText("key", null));
         assertFalse("empty", StringUtils.containsInCommaSplittableText("key", ""));
         assertFalse("not in 1 element",
@@ -56,7 +56,28 @@
         assertTrue("in 2 elements", StringUtils.containsInCommaSplittableText("key", "key1,key"));
     }
 
-    public void testAppendToExtraValuesIfNotExists() {
+    public void testJoinCommaSplittableText() {
+        assertEquals("2 nulls", "",
+                StringUtils.joinCommaSplittableText(null, null));
+        assertEquals("null and empty", "",
+                StringUtils.joinCommaSplittableText(null, ""));
+        assertEquals("empty and null", "",
+                StringUtils.joinCommaSplittableText("", null));
+        assertEquals("2 empties", "",
+                StringUtils.joinCommaSplittableText("", ""));
+        assertEquals("text and null", "text",
+                StringUtils.joinCommaSplittableText("text", null));
+        assertEquals("text and empty", "text",
+                StringUtils.joinCommaSplittableText("text", ""));
+        assertEquals("null and text", "text",
+                StringUtils.joinCommaSplittableText(null, "text"));
+        assertEquals("empty and text", "text",
+                StringUtils.joinCommaSplittableText("", "text"));
+        assertEquals("2 texts", "text1,text2",
+                StringUtils.joinCommaSplittableText("text1", "text2"));
+    }
+
+    public void testAppendToCommaSplittableTextIfNotExists() {
         assertEquals("null", "key",
                 StringUtils.appendToCommaSplittableTextIfNotExists("key", null));
         assertEquals("empty", "key",
@@ -77,7 +98,7 @@
                 StringUtils.appendToCommaSplittableTextIfNotExists("key", "key1,key,key3"));
     }
 
-    public void testRemoveFromExtraValuesIfExists() {
+    public void testRemoveFromCommaSplittableTextIfExists() {
         assertEquals("null", "", StringUtils.removeFromCommaSplittableTextIfExists("key", null));
         assertEquals("empty", "", StringUtils.removeFromCommaSplittableTextIfExists("key", ""));