zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddiede...@apache.org
Subject [zookeeper] branch master updated: ZOOKEEPER-3691: Use JDK String Join Method in ZK StringUtils
Date Tue, 17 Nov 2020 17:25:46 GMT
This is an automated email from the ASF dual-hosted git repository.

ddiederen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 6dfdf14  ZOOKEEPER-3691: Use JDK String Join Method in ZK StringUtils
6dfdf14 is described below

commit 6dfdf14a637d853bd77236dfbdd347912808074f
Author: David Mollitor <dmollitor@apache.org>
AuthorDate: Tue Nov 17 17:24:51 2020 +0000

    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
---
 .../org/apache/zookeeper/common/StringUtils.java   | 29 ++++++++-------
 .../org/apache/zookeeper/test/StringUtilTest.java  | 41 ++++++++++++++++++----
 2 files changed, 50 insertions(+), 20 deletions(-)

diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/common/StringUtils.java b/zookeeper-server/src/main/java/org/apache/zookeeper/common/StringUtils.java
index 36ee2d7..391aff1 100644
--- a/zookeeper-server/src/main/java/org/apache/zookeeper/common/StringUtils.java
+++ b/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.Collections;
 import java.util.List;
+import java.util.Objects;
 
 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) {
-        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);
     }
 
 }
diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/test/StringUtilTest.java
b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StringUtilTest.java
index 48c1bc9..22d804e 100644
--- a/zookeeper-server/src/test/java/org/apache/zookeeper/test/StringUtilTest.java
+++ b/zookeeper-server/src/test/java/org/apache/zookeeper/test/StringUtilTest.java
@@ -19,24 +19,51 @@
 package org.apache.zookeeper.test;
 
 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.common.StringUtils;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 public class StringUtilTest extends ZKTestCase {
 
     @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"), ","));
     }
 
 }


Mime
View raw message