From 734695c80e0f1de30834b1e3c5f21ac076771555 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 14 Jul 2015 13:40:23 +0200 Subject: [PATCH] Improve StringUtils.collectionToString() Use generics, and iterator and append(char) instead of append(String). --- .../jivesoftware/smack/util/StringUtils.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java index d8318adee..596b03163 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java @@ -19,6 +19,7 @@ package org.jivesoftware.smack.util; import java.io.UnsupportedEncodingException; import java.util.Collection; +import java.util.Iterator; import java.util.Random; /** @@ -221,16 +222,22 @@ public class StringUtils { return cs.length() == 0; } - public static String collectionToString(Collection collection) { - StringBuilder sb = new StringBuilder(); - for (String s : collection) { - sb.append(s); - sb.append(" "); + /** + * Transform a collection of CharSequences to a whitespace delimited String. + * + * @param collection the collection to transform. + * @return a String with all the elements of the collection. + */ + public static String collectionToString(Collection collection) { + StringBuilder sb = new StringBuilder(collection.size() * 20); + for (Iterator it = collection.iterator(); it.hasNext();) { + CharSequence cs = it.next(); + sb.append(cs); + if (it.hasNext()) { + sb.append(' '); + } } - String res = sb.toString(); - // Remove the trailing whitespace - res = res.substring(0, res.length() - 1); - return res; + return sb.toString(); } public static String returnIfNotEmptyTrimmed(String string) {