Skip to content

Commit 8d801bf

Browse files
committed
Replaced String concatenation in loop
Replaced String concatenation in loop with StringBuilder Unit test added Signed-off-by: Laszlo Hornyak <laszlo.hornyak@gmail.com>
1 parent 55b6b6d commit 8d801bf

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

utils/src/com/cloud/utils/StringUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,17 @@ public static List<String> csvTagsToList(String tags) {
9595
*/
9696

9797
public static String listToCsvTags(List<String> tagsList) {
98-
String tags = "";
98+
StringBuilder tags = new StringBuilder();
9999
if (tagsList.size() > 0) {
100100
for (int i = 0; i < tagsList.size(); i++) {
101-
tags += tagsList.get(i);
101+
tags.append(tagsList.get(i));
102102
if (i != tagsList.size() - 1) {
103-
tags += ",";
103+
tags.append(',');
104104
}
105105
}
106106
}
107107

108-
return tags;
108+
return tags.toString();
109109
}
110110

111111
public static String getExceptionStackInfo(Throwable e) {

utils/test/com/cloud/utils/StringUtilsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818

1919
import static org.junit.Assert.assertEquals;
2020

21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
24+
import junit.framework.Assert;
25+
2126
import org.junit.Test;
2227

2328
public class StringUtilsTest {
@@ -216,4 +221,10 @@ public void testCleanSecretkeyFromRequestString() {
216221
String result = StringUtils.cleanString(input);
217222
assertEquals(result, expected);
218223
}
224+
225+
@Test
226+
public void listToCsvTags() {
227+
Assert.assertEquals("a,b,c", StringUtils.listToCsvTags(Arrays.asList("a","b", "c")));
228+
Assert.assertEquals("", StringUtils.listToCsvTags(new ArrayList<String>()));
229+
}
219230
}

0 commit comments

Comments
 (0)