Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 144edb4878d01dfdabe7a4750e1db883cb6caa5e 1 parent 5d84027
@mikewied mikewied authored
View
11 src/main/java/net/spy/memcached/util/StringUtils.java
@@ -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();
}
View
27 src/test/java/net/spy/memcached/util/UtilTest.java
@@ -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;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.