Replaced String concatenation in loop

Replaced String concatenation in loop with StringBuilder
Unit test added

Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
This commit is contained in:
Laszlo Hornyak 2014-02-09 21:27:28 +01:00
parent 55b6b6d50b
commit 8d801bffab
2 changed files with 15 additions and 4 deletions

View File

@ -95,17 +95,17 @@ public class StringUtils {
*/
public static String listToCsvTags(List<String> tagsList) {
String tags = "";
StringBuilder tags = new StringBuilder();
if (tagsList.size() > 0) {
for (int i = 0; i < tagsList.size(); i++) {
tags += tagsList.get(i);
tags.append(tagsList.get(i));
if (i != tagsList.size() - 1) {
tags += ",";
tags.append(',');
}
}
}
return tags;
return tags.toString();
}
public static String getExceptionStackInfo(Throwable e) {

View File

@ -18,6 +18,11 @@ package com.cloud.utils;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import junit.framework.Assert;
import org.junit.Test;
public class StringUtilsTest {
@ -216,4 +221,10 @@ public class StringUtilsTest {
String result = StringUtils.cleanString(input);
assertEquals(result, expected);
}
@Test
public void listToCsvTags() {
Assert.assertEquals("a,b,c", StringUtils.listToCsvTags(Arrays.asList("a","b", "c")));
Assert.assertEquals("", StringUtils.listToCsvTags(new ArrayList<String>()));
}
}