Bug 1299201 - StringUtils: Add helper method for joining strings with a separator. r?grisha draft
authorSebastian Kaspari <s.kaspari@gmail.com>
Tue, 11 Oct 2016 11:52:43 +0200
changeset 423606 fec309b0703e0ab8e0642c7365d0425dde43d211
parent 423605 c6c82bb3dcf0205ab18883933936158b99ba969c
child 423607 ab1f20a575ceeee1ace19f0b61bc64e1d63d9d8d
push id31945
push users.kaspari@gmail.com
push dateTue, 11 Oct 2016 12:27:19 +0000
reviewersgrisha
bugs1299201
milestone52.0a1
Bug 1299201 - StringUtils: Add helper method for joining strings with a separator. r?grisha MozReview-Commit-ID: Fzrap4wkeWk
mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
--- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
+++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/StringUtils.java
@@ -8,16 +8,17 @@ package org.mozilla.gecko.util;
 import android.net.Uri;
 import android.support.annotation.NonNull;
 import android.text.TextUtils;
 
 import org.mozilla.gecko.AppConstants.Versions;
 
 import java.util.Collections;
 import java.util.LinkedHashSet;
+import java.util.List;
 import java.util.Set;
 
 public class StringUtils {
     private static final String LOGTAG = "GeckoStringUtils";
 
     private static final String FILTER_URL_PREFIX = "filter://";
     private static final String USER_ENTERED_URL_PREFIX = "user-entered:";
 
@@ -266,9 +267,25 @@ public class StringUtils {
      */
     public static String forceLTR(String text) {
         if (!isRTL(text)) {
             return text;
         }
 
         return "\u200E" + text;
     }
+
+    /**
+     * Joining together a sequence of strings with a separator.
+     */
+    public static String join(@NonNull String separator, @NonNull List<String> parts) {
+        final StringBuilder builder = new StringBuilder();
+
+        for (int i = 0; i < parts.size(); i++) {
+            if (i > 0) {
+                builder.append(separator);
+            }
+            builder.append(parts.get(i));
+        }
+
+        return builder.toString();
+    }
 }
--- a/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestStringUtils.java
@@ -4,16 +4,19 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.util;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mozilla.gecko.background.testhelpers.TestRunner;
 
+import java.util.Arrays;
+import java.util.Collections;
+
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
 @RunWith(TestRunner.class)
 public class TestStringUtils {
     @Test
     public void testIsHttpOrHttps() {
@@ -79,9 +82,26 @@ public class TestStringUtils {
         // We add the LTR mark only once
         final String someRtlString = "عربي";
         assertEquals(4, someRtlString.length());
         final String forcedLtrString = StringUtils.forceLTR(someRtlString);
         assertEquals(5, forcedLtrString.length());
         final String forcedAgainLtrString = StringUtils.forceLTR(forcedLtrString);
         assertEquals(5, forcedAgainLtrString.length());
     }
+
+    @Test
+    public void testJoin() {
+        assertEquals("", StringUtils.join("", Collections.<String>emptyList()));
+        assertEquals("", StringUtils.join("-", Collections.<String>emptyList()));
+        assertEquals("", StringUtils.join("", Collections.singletonList("")));
+        assertEquals("", StringUtils.join(".", Collections.singletonList("")));
+
+        assertEquals("192.168.0.1", StringUtils.join(".", Arrays.asList("192", "168", "0", "1")));
+        assertEquals("www.mozilla.org", StringUtils.join(".", Arrays.asList("www", "mozilla", "org")));
+
+        assertEquals("hello", StringUtils.join("", Collections.singletonList("hello")));
+        assertEquals("helloworld", StringUtils.join("", Arrays.asList("hello", "world")));
+        assertEquals("hello world", StringUtils.join(" ", Arrays.asList("hello", "world")));
+
+        assertEquals("m::o::z::i::l::l::a", StringUtils.join("::", Arrays.asList("m", "o", "z", "i", "l", "l", "a")));
+    }
 }