Browse Source

ZOOKEEPER-3691: Use JDK String Join Method in ZK StringUtils

Author: David Mollitor <dmollitor@apache.org>
Author: belugabehr <12578579+belugabehr@users.noreply.github.com>

Reviewers: maoling <maoling199210191@sina.com>, Enrico Olivelli <eolivelli@apache.org>, Damien Diederen <ddiederen@apache.org>

Closes #1217 from belugabehr/ZOOKEEPER-3691
David Mollitor 4 years ago
parent
commit
6dfdf14a63

+ 16 - 13
zookeeper-server/src/main/java/org/apache/zookeeper/common/StringUtils.java

@@ -20,6 +20,7 @@ package org.apache.zookeeper.common;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Collections;
 import java.util.List;
 import java.util.List;
+import java.util.Objects;
 
 
 public class StringUtils {
 public class StringUtils {
 
 
@@ -44,22 +45,24 @@ public class StringUtils {
     }
     }
 
 
     /**
     /**
-     * This method takes a List&lt;String&gt; and a delimiter and joins the strings
-     * into a single string, where the original strings are separated using
-     * the given delimiter.
+     * This method takes a List&lt;String&gt; and a delimiter and joins the
+     * strings into a single string, where the original strings are separated
+     * using the given delimiter. This method is a null-safe version of
+     * {@link String#join(CharSequence, Iterable)}
      *
      *
+     * <p>
+     * Note that if an individual element is null, then "null" is added.
+     * </p>
+     * @param list a {@code List} that will have its elements joined together
+     * @param delim a sequence of characters that is used to separate each of the
+     *          elements in the resulting String
+     * @return a new String that is composed from the elements argument or
+     *         {@code null} if list is {@code null}
+     * @throws NullPointerException if delim is {@code null}
      */
      */
     public static String joinStrings(List<String> list, String delim) {
     public static String joinStrings(List<String> list, String delim) {
-        if (list == null) {
-            return null;
-        }
-
-        StringBuilder builder = new StringBuilder(list.get(0));
-        for (String s : list.subList(1, list.size())) {
-            builder.append(delim).append(s);
-        }
-
-        return builder.toString();
+        Objects.requireNonNull(delim);
+        return list == null ? null : String.join(delim, list);
     }
     }
 
 
 }
 }

+ 34 - 7
zookeeper-server/src/test/java/org/apache/zookeeper/test/StringUtilTest.java

@@ -19,24 +19,51 @@
 package org.apache.zookeeper.test;
 package org.apache.zookeeper.test;
 
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import java.util.Arrays;
+import java.util.Collections;
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.ZKTestCase;
 import org.apache.zookeeper.common.StringUtils;
 import org.apache.zookeeper.common.StringUtils;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.Test;
 
 
 public class StringUtilTest extends ZKTestCase {
 public class StringUtilTest extends ZKTestCase {
 
 
     @Test
     @Test
-    public void testStrings() {
+    public void testStringSplit() {
+        final String s1 = "   a  ,   b  , ";
+        assertEquals(Arrays.asList("a", "b"), StringUtils.split(s1, ","));
 
 
-        String s1 = "   a  ,   b  , ";
-        assertEquals("[a, b]", StringUtils.split(s1, ",").toString());
+        assertEquals(Collections.emptyList(), StringUtils.split("", ","));
 
 
-        String s2 = "";
-        assertEquals(0, StringUtils.split(s2, ",").size());
+        final String s3 = "1, , 2";
+        assertEquals(Arrays.asList("1", "2"), StringUtils.split(s3, ","));
+    }
+
+    @Test
+    public void testStringJoinNullDelim() {
+        Assertions.assertThrows(NullPointerException.class, () -> {
+            StringUtils.joinStrings(Collections.emptyList(), null);
+        });
+    }
+
+    @Test
+    public void testStringJoinNullListNullDelim() {
+        Assertions.assertThrows(NullPointerException.class, () -> {
+            StringUtils.joinStrings(null, null);
+        });
+    }
 
 
-        String s3 = "1, , 2";
-        assertEquals("[1, 2]", StringUtils.split(s3, ",").toString());
+    @Test
+    public void testStringJoinNullList() {
+        assertNull(StringUtils.joinStrings(null, ","));
+    }
 
 
+    @Test
+    public void testStringJoin() {
+        final String expected = "a,B,null,d";
+        assertEquals(expected,
+            StringUtils.joinStrings(Arrays.asList("a", "B", null, "d"), ","));
     }
     }
 
 
 }
 }