commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r448015 - /jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
Date Tue, 19 Sep 2006 23:45:15 GMT
Author: bayard
Date: Tue Sep 19 16:45:14 2006
New Revision: 448015

URL: http://svn.apache.org/viewvc?view=rev&rev=448015
Log:
Applied the optimisations to the join(Iterator, x) methods that Stepan Koltsov included in
his patch for #LANG-266. 

Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?view=diff&rev=448015&r1=448014&r2=448015
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java Tue
Sep 19 16:45:14 2006
@@ -2635,19 +2635,33 @@
      * @since 2.0
      */
     public static String join(Iterator iterator, char separator) {
+
+        // handle null, zero and one elements before building a buffer
         if (iterator == null) {
             return null;
         }
+        if (!iterator.hasNext()) {
+            return EMPTY;
+        }
+        Object first = iterator.next();
+        if (!iterator.hasNext()) {
+            return ObjectUtils.toString(first);
+        }
+
+        // two or more elements
         StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
+        if (first != null) {
+            buf.append(first);
+        }
+
         while (iterator.hasNext()) {
+            buf.append(separator);
             Object obj = iterator.next();
             if (obj != null) {
                 buf.append(obj);
             }
-            if (iterator.hasNext()) {
-                buf.append(separator);
-            }
         }
+
         return buf.toString();
     }
 
@@ -2665,17 +2679,32 @@
      * @return the joined String, <code>null</code> if null iterator input
      */
     public static String join(Iterator iterator, String separator) {
+
+        // handle null, zero and one elements before building a buffer
         if (iterator == null) {
             return null;
         }
+        if (!iterator.hasNext()) {
+            return EMPTY;
+        }
+        Object first = iterator.next();
+        if (!iterator.hasNext()) {
+            return ObjectUtils.toString(first);
+        }
+
+        // two or more elements
         StringBuffer buf = new StringBuffer(256); // Java default is 16, probably too small
+        if (first != null) {
+            buf.append(first);
+        }
+
         while (iterator.hasNext()) {
+            if (separator != null) {
+                buf.append(separator);
+            }
             Object obj = iterator.next();
             if (obj != null) {
                 buf.append(obj);
-            }
-            if ((separator != null) && iterator.hasNext()) {
-                buf.append(separator);
             }
         }
         return buf.toString();



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message