Bug 1299201 - StringUtils: Add helper method for joining strings with a separator. r?grisha
MozReview-Commit-ID: Fzrap4wkeWk
--- 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,28 @@ 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) {
+ if (parts.size() == 0) {
+ return "";
+ }
+
+ final StringBuilder builder = new StringBuilder();
+ builder.append(parts.get(0));
+
+ for (int i = 1; i < parts.size(); i++) {
+ 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")));
+ }
}