joining one element collection optimized
authorSergey Simonchik <sergey.simonchik@jetbrains.com>
Fri, 27 Feb 2015 14:10:01 +0000 (17:10 +0300)
committerSergey Simonchik <sergey.simonchik@jetbrains.com>
Fri, 27 Feb 2015 14:10:01 +0000 (17:10 +0300)
platform/util/src/com/intellij/openapi/util/text/StringUtil.java
platform/util/testSrc/com/intellij/util/text/StringUtilTest.java

index 155d18059f71c1513a5aa3110626b669e2767e43..56d811273557a7240e2b20a81bebcd8c7de8f58c 100644 (file)
@@ -1456,6 +1456,13 @@ public class StringUtil extends StringUtilRt {
   @NotNull
   @Contract(pure = true)
   public static String join(@NotNull Collection<? extends String> strings, @NotNull String separator) {
+    int size = strings.size();
+    if (size == 0) {
+      return "";
+    }
+    if (size == 1) {
+      return String.valueOf(strings.iterator().next());
+    }
     StringBuilder result = new StringBuilder();
     join(strings, separator, result);
     return result.toString();
index bd5365df0e0687a551d03e506cd55660f32ec88f..2e825f504e9d9d41e055b51e397afbdf10468827 100644 (file)
@@ -219,6 +219,9 @@ public class StringUtilTest extends TestCase {
   }
 
   public void testJoin() {
+    assertEquals("", StringUtil.join(Collections.emptyList(), ","));
+    assertEquals("qqq", StringUtil.join(Collections.singletonList("qqq"), ","));
+    assertEquals("null", StringUtil.join(Collections.singletonList(null), ","));
     assertEquals("foo,,bar", StringUtil.join(Arrays.asList("foo", "", "bar"), ","));
     assertEquals("foo,,bar", StringUtil.join(new String[]{"foo", "", "bar"}, ","));
   }