Permalink
Browse files

JCBC-52: Fix StringUtils.join()

This function was implemented incorrectly. It was returning
a,b,c,

when it should have been returning
a,b,c

This change fixes this issue.

Change-Id: I822bd3accc2dd2b122b1b44dfe79d8ed36250cf2
Reviewed-on: http://review.couchbase.org/17027
Reviewed-by: Matt Ingenthron <matt@couchbase.com>
Tested-by: Michael Wiederhold <mike@couchbase.com>
  • Loading branch information...
1 parent 5d84027 commit 144edb4878d01dfdabe7a4750e1db883cb6caa5e @mikewied mikewied committed with mikewied Jun 11, 2012
@@ -23,6 +23,7 @@
package net.spy.memcached.util;
import java.util.Collection;
+import java.util.Iterator;
import net.spy.memcached.KeyUtil;
import net.spy.memcached.MemcachedClientIF;
@@ -38,9 +39,13 @@ private StringUtils() {
public static String join(Collection<String> keys, String delimiter) {
StringBuilder sb = new StringBuilder();
- for (String key : keys) {
- sb.append(key);
- sb.append(delimiter);
+ if (!keys.isEmpty()) {
+ Iterator<String> itr = keys.iterator();
+ sb.append(itr.next());
+ while (itr.hasNext()) {
+ sb.append(delimiter);
+ sb.append(itr.next());
+ }
}
return sb.toString();
}
@@ -0,0 +1,27 @@
+package net.spy.memcached.util;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+import org.junit.Test;
+
+import junit.framework.TestCase;
+
+public class UtilTest extends TestCase {
+ public void setup() {
+ // Empty
+ }
+
+ public void teardown() {
+ // Empty
+ }
+
+ @Test
+ public void testJoin() {
+ Collection<String> keys = new LinkedList<String>();
+ keys.add("key1");
+ keys.add("key1");
+ keys.add("key1");
+ assert StringUtils.join(keys, ",").compareTo("key1,key2,key3") == 0;
+ }
+}

0 comments on commit 144edb4

Please sign in to comment.