svn commit: r1859921 - /ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1859921 - /ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java

mthl
Author: mthl
Date: Sat May 25 09:12:05 2019
New Revision: 1859921

URL: http://svn.apache.org/viewvc?rev=1859921&view=rev
Log:
Improved: Rewrite ‘StringUtil#join’ method
(OFBIZ-11014)

Use streams instead of low-level string builder.

Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java?rev=1859921&r1=1859920&r2=1859921&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/StringUtil.java Sat May 25 09:12:05 2019
@@ -33,6 +33,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 import org.apache.commons.codec.DecoderException;
 import org.apache.commons.codec.binary.Hex;
@@ -100,25 +101,16 @@ public class StringUtil {
     }
 
     /**
-     * Creates a single string from a Collection of strings seperated by a delimiter.
-     * @param col a collection of strings to join
-     * @param delim the delimiter character(s) to use. (null value will join with no delimiter)
-     * @return a String of all values in the collection seperated by the delimiter
+     * Creates a single string from a Collection of strings separated by a delimiter.
+     *
+     * @param col  a collection of strings to join
+     * @param delim  the delimiter character(s) to use. (null value will join with no delimiter)
+     * @return a String of all values in the collection separated by the delimiter
      */
-    public static String join(Collection<?> col, String delim) {
-        if (UtilValidate.isEmpty(col)) {
-            return null;
-        }
-        StringBuilder buf = new StringBuilder();
-        Iterator<?> i = col.iterator();
-
-        while (i.hasNext()) {
-            buf.append(i.next());
-            if (i.hasNext()) {
-                buf.append(delim);
-            }
-        }
-        return buf.toString();
+    public static String join(Collection<?> col, CharSequence delim) {
+        return UtilValidate.isEmpty(col)
+                ? null
+                : col.stream().map(Object::toString).collect(Collectors.joining(delim));
     }
 
     /**